quadmenu
/
vendor
/
franmastromarino
/
wp-orm
/
tests
/
Collection
/
CollectionRepositoryBuilderTestPrimaryKeyString.php
CollectionRepositoryBuilderTestPrimaryKeyString.php in QuadMenu – Mega Menu 3.3.8, at vendor/franmastromarino/wp-orm/tests/Collection/CollectionRepositoryBuilderTestPrimaryKeyString.php
| 1 | <?php |
| 2 | |
| 3 | namespace QuadLayers\WP_Orm\Tests\Collection; |
| 4 | |
| 5 | use PHPUnit\Framework\TestCase; |
| 6 | use Brain\Monkey\Functions; |
| 7 | use QuadLayers\WP_Orm\Builder\CollectionRepositoryBuilder; |
| 8 | use QuadLayers\WP_Orm\Repository\CollectionRepository; |
| 9 | |
| 10 | class CollectionRepositoryBuilderTestPrimaryKeyString extends TestCase |
| 11 | { |
| 12 | private string $table = 'test_table'; |
| 13 | private CollectionRepository $repository; |
| 14 | |
| 15 | protected function setUp(): void |
| 16 | { |
| 17 | |
| 18 | $builder = (new CollectionRepositoryBuilder()) |
| 19 | ->setTable($this->table) |
| 20 | ->setEntity('\QuadLayers\WP_Orm\Tests\Collection\CollectionEntityTestPrimaryKeyString') |
| 21 | ->setAutoIncrement(false); |
| 22 | |
| 23 | $this->repository = $builder->getRepository(); |
| 24 | |
| 25 | |
| 26 | |
| 27 | // When get_option is called, return testInput |
| 28 | Functions\when('get_option')->justReturn([]); |
| 29 | } |
| 30 | |
| 31 | public function testFindAll() |
| 32 | { |
| 33 | |
| 34 | $test = [ |
| 35 | [ |
| 36 | 'test_id' => 'test1', |
| 37 | 'key1' => 'test', |
| 38 | ], |
| 39 | [ |
| 40 | 'test_id' => 'test2', |
| 41 | 'key1' => 'test2', |
| 42 | ], |
| 43 | [ |
| 44 | 'test_id' => 'test9', |
| 45 | 'key1' => 'test3', |
| 46 | ], |
| 47 | ]; |
| 48 | |
| 49 | // Initialize a variable to keep track of the update_option call count |
| 50 | $callCount = 0; |
| 51 | |
| 52 | Functions\when('update_option')->alias( |
| 53 | function ($option, $value) use (&$callCount, $test) { |
| 54 | // Check if the table is correct |
| 55 | if ($this->table !== $option) { |
| 56 | return false; |
| 57 | } |
| 58 | |
| 59 | // // Increase the call count |
| 60 | $callCount++; |
| 61 | |
| 62 | // // Check if the value matches the test values up to the current call count |
| 63 | if (serialize(array_slice($test, 0, $callCount + 1)) !== serialize($value)) { |
| 64 | return false; |
| 65 | } |
| 66 | |
| 67 | return true; |
| 68 | } |
| 69 | ); |
| 70 | |
| 71 | foreach ($test as $index => $data) { |
| 72 | $this->repository->create($data); |
| 73 | } |
| 74 | |
| 75 | $results = $this->repository->findAll(); |
| 76 | |
| 77 | foreach ($results as $index => $entity) { |
| 78 | $testEntity = array_merge( |
| 79 | $test[$index], |
| 80 | ['test_id' => $test[$index]['test_id']] |
| 81 | ); |
| 82 | $this->assertEquals($entity->getModifiedProperties(), $testEntity); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | public function testDeleteAll() |
| 87 | { |
| 88 | Functions\when('delete_option')->justReturn(true); |
| 89 | $this->repository->deleteAll(); |
| 90 | $this->assertEquals($this->repository->findAll(), null); |
| 91 | } |
| 92 | |
| 93 | public function testCreate() |
| 94 | { |
| 95 | |
| 96 | Functions\when('update_option')->justReturn(true); |
| 97 | |
| 98 | $entity = $this->repository->create(['test_id' => 'test1','key1' => 'value1_2_updated']); |
| 99 | |
| 100 | $result = $entity->getModifiedProperties(); |
| 101 | |
| 102 | $this->assertEquals($result, ['test_id' => 'test1','key1' => 'value1_2_updated']); |
| 103 | } |
| 104 | |
| 105 | public function testUpdate() |
| 106 | { |
| 107 | |
| 108 | Functions\when('update_option')->justReturn(true); |
| 109 | |
| 110 | $entity0 = $this->repository->create(['test_id' => 'test1','key1' => 'value1_2_updated']); |
| 111 | $entity0 = $this->repository->update('test1', ['key1' => 'value1_2_updated']); |
| 112 | |
| 113 | $result = $entity0->getModifiedProperties(); |
| 114 | |
| 115 | $this->assertEquals($result, ['test_id' => 'test1','key1' => 'value1_2_updated']); |
| 116 | } |
| 117 | |
| 118 | public function testDelete() |
| 119 | { |
| 120 | |
| 121 | Functions\when('update_option')->justReturn(true); |
| 122 | |
| 123 | $entity0 = $this->repository->create(['test_id' => 'test1']); |
| 124 | $entity1 = $this->repository->create(['test_id' => 'test2']); |
| 125 | $entity2 = $this->repository->create(['test_id' => 'test3']); |
| 126 | |
| 127 | $result = $this->repository->delete('test1'); |
| 128 | |
| 129 | $this->assertTrue($result); |
| 130 | $this->assertEquals(null, $this->repository->find('test1')); |
| 131 | $this->assertEquals($entity1, $this->repository->find('test2')); |
| 132 | $this->assertEquals($entity2, $this->repository->find('test3')); |
| 133 | } |
| 134 | |
| 135 | public function testDefaults() |
| 136 | { |
| 137 | $entity0 = $this->repository->create(['test_id' => 'test1']); |
| 138 | $entity1 = $this->repository->create(['test_id' => 'test2']); |
| 139 | $entity2 = $this->repository->create(['test_id' => 'test3']); |
| 140 | |
| 141 | $results = $this->repository->findAll(); |
| 142 | |
| 143 | foreach ($results as $index => $entity) { |
| 144 | $defaults = $entity->getDefaults(); |
| 145 | $this->assertEquals($defaults, [ |
| 146 | 'test_id' => '', |
| 147 | 'key1' => 'default_value_1', |
| 148 | 'key2' => 'default_value_2', |
| 149 | 'key3' => [ |
| 150 | 'key_3_1' => 'default_value_3', |
| 151 | 'key_3_2' => 'default_value_4', |
| 152 | ] |
| 153 | ]); |
| 154 | } |
| 155 | } |
| 156 | } |
| 157 |