x-systems /
epesi-base
| 1 | <?php |
||
| 2 | |||
| 3 | use Orchestra\Testbench\TestCase; |
||
| 4 | use Epesi\Base\CommonData\Models\CommonData; |
||
| 5 | use Epesi\Base\CommonData\Models\CommonDataNotFound; |
||
| 6 | |||
| 7 | class CommonDataTest extends TestCase |
||
| 8 | { |
||
| 9 | protected function getPackageProviders($app) |
||
|
0 ignored issues
–
show
|
|||
| 10 | { |
||
| 11 | return [ |
||
| 12 | \Kalnoy\Nestedset\NestedSetServiceProvider::class, |
||
| 13 | ]; |
||
| 14 | } |
||
| 15 | |||
| 16 | protected function setUp(): void |
||
| 17 | { |
||
| 18 | parent::setUp(); |
||
| 19 | |||
| 20 | CommonData::migrate(); |
||
| 21 | } |
||
| 22 | |||
| 23 | public function testStoreRetrieve() |
||
| 24 | { |
||
| 25 | // creating - retrieving array |
||
| 26 | $arrayExpected = ['c' => 'cc', 'b' => 'bb', 'a' => 'aa', ]; |
||
| 27 | |||
| 28 | CommonData::newArray('abc/def', $arrayExpected); |
||
| 29 | |||
| 30 | $arrayActual = CommonData::getArray('abc/def'); |
||
| 31 | |||
| 32 | $this->assertEquals($arrayExpected, $arrayActual, 'Problem retrieving commondata sorted by position!'); |
||
| 33 | |||
| 34 | // retrieving sorted array by key |
||
| 35 | $arraySortKey = $arrayExpected; |
||
| 36 | ksort($arraySortKey); |
||
| 37 | |||
| 38 | $this->assertEquals($arraySortKey, CommonData::getArray('abc/def', 'key'), 'Problem retrieving commondata sorted by key!'); |
||
| 39 | |||
| 40 | // retrieving sorted array by value |
||
| 41 | $arraySortValue = $arrayExpected; |
||
| 42 | sort($arraySortValue); |
||
| 43 | |||
| 44 | $this->assertEquals($arraySortKey, CommonData::getArray('abc/def', 'value'), 'Problem retrieving commondata sorted by value!'); |
||
| 45 | |||
| 46 | // retrieving array value |
||
| 47 | $valueActual = CommonData::getValue('abc/def/a'); |
||
| 48 | |||
| 49 | $this->assertEquals($arrayExpected['a'], $valueActual, 'Problem retrieving commondata value!'); |
||
| 50 | |||
| 51 | // setting array values |
||
| 52 | $arrayChanged = ['a' => 'aaa', 'b' => 'bbb', 'c' => 'ccc']; |
||
| 53 | |||
| 54 | foreach ($arrayChanged as $key => $value) { |
||
| 55 | CommonData::setValue('abc/def/' . $key, $value); |
||
| 56 | } |
||
| 57 | |||
| 58 | $arrayActual = CommonData::getArray('abc/def'); |
||
| 59 | |||
| 60 | $this->assertEquals($arrayChanged, $arrayActual, 'Problem setting commondata value!'); |
||
| 61 | |||
| 62 | // deleting array node |
||
| 63 | unset($arrayChanged['a']); |
||
| 64 | |||
| 65 | CommonData::deleteArray('abc/def/a'); |
||
| 66 | |||
| 67 | $arrayActual = CommonData::getArray('abc/def'); |
||
| 68 | |||
| 69 | $this->assertEquals($arrayChanged, $arrayActual, 'Problem deleting commondata node!'); |
||
| 70 | |||
| 71 | // extending array |
||
| 72 | $arrayExtension = ['a' => 'aaa']; |
||
| 73 | |||
| 74 | $arrayChanged = array_merge($arrayChanged, $arrayExtension); |
||
| 75 | |||
| 76 | CommonData::extendArray('abc/def', $arrayExtension); |
||
| 77 | |||
| 78 | $arrayActual = CommonData::getArray('abc/def'); |
||
| 79 | |||
| 80 | $this->assertEquals($arrayChanged, $arrayActual, 'Problem extending commondata array!'); |
||
| 81 | } |
||
| 82 | |||
| 83 | public function testNonExistingArrayException() |
||
| 84 | { |
||
| 85 | $this->expectException(CommonDataNotFound::class); |
||
| 86 | |||
| 87 | CommonData::getArray('abc/d/ef'); |
||
| 88 | } |
||
| 89 | |||
| 90 | public function testNonExistingArraySilent() |
||
| 91 | { |
||
| 92 | $this->assertEquals([], CommonData::getArray('abc/d/ef', 'position', true), 'Problem retrieving non-existent commondata array silently!'); |
||
| 93 | } |
||
| 94 | |||
| 95 | public function testNonExistingValue() |
||
| 96 | { |
||
| 97 | $this->assertFalse(CommonData::getValue('abc/d/ef'), 'Problem retrieving non-existent commondata value!'); |
||
| 98 | } |
||
| 99 | |||
| 100 | // public function testCommonDataField() |
||
| 101 | // { |
||
| 102 | // $values = ['aa' => 'AA', 'bb' => 'BB']; |
||
| 103 | |||
| 104 | // CommonData::newArray('Test/Field', $values); |
||
| 105 | |||
| 106 | // $model = \Epesi\Core\Data\Model::create('test'); |
||
| 107 | |||
| 108 | // $model->addFields([ |
||
| 109 | // 'test' => [\Epesi\Base\CommonData\Fields\CommonData::class, 'path' => 'Test/Field'] |
||
| 110 | // ]); |
||
| 111 | |||
| 112 | // \atk4\schema\Migration::getMigration($model)->migrate(); |
||
| 113 | |||
| 114 | // $model->insert(['test' => 'aa']); |
||
| 115 | |||
| 116 | // $model->loadAny(); |
||
| 117 | |||
| 118 | // $this->assertEquals('aa', $model->get('test')); |
||
| 119 | |||
| 120 | // $this->assertEquals($values, $model->getField('test')->values); |
||
| 121 | // } |
||
| 122 | |||
| 123 | } |
||
| 124 |
This check looks for parameters that have been defined for a function or method, but which are not used in the method body.