quadmenu
/
vendor
/
franmastromarino
/
wp-orm
/
tests
/
Collection
/
CollectionRepositoryBuilderTestPrimaryKey.php
CollectionRepositoryBuilderTestPrimaryKey.php in QuadMenu – Mega Menu 3.3.8, at vendor/franmastromarino/wp-orm/tests/Collection/CollectionRepositoryBuilderTestPrimaryKey.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 CollectionRepositoryBuilderTestPrimaryKey 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\CollectionEntityTestPrimaryKey') |
| 21 | ->setAutoIncrement(true); |
| 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 | 'key1' => 'test', |
| 37 | ], |
| 38 | [ |
| 39 | 'key1' => 'test2', |
| 40 | ], |
| 41 | [ |
| 42 | 'key1' => 'test3', |
| 43 | ], |
| 44 | ]; |
| 45 | |
| 46 | // Initialize a variable to keep track of the update_option call count |
| 47 | $callCount = 0; |
| 48 | |
| 49 | Functions\when('update_option')->alias( |
| 50 | function ($option, $value) use (&$callCount, $test) { |
| 51 | // Check if the table is correct |
| 52 | if ($this->table !== $option) { |
| 53 | return false; |
| 54 | } |
| 55 | |
| 56 | // // Increase the call count |
| 57 | $callCount++; |
| 58 | |
| 59 | // // Check if the value matches the test values up to the current call count |
| 60 | if (serialize(array_slice($test, 0, $callCount + 1)) !== serialize($value)) { |
| 61 | return false; |
| 62 | } |
| 63 | |
| 64 | return true; |
| 65 | } |
| 66 | ); |
| 67 | |
| 68 | foreach ($test as $index => $data) { |
| 69 | $this->repository->create($data); |
| 70 | } |
| 71 | |
| 72 | $results = $this->repository->findAll(); |
| 73 | |
| 74 | foreach ($results as $index => $entity) { |
| 75 | $testEntity = array_merge( |
| 76 | $test[$index], |
| 77 | ['test' => $index] |
| 78 | ); |
| 79 | $this->assertEquals($entity->getModifiedProperties(), $testEntity); |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | public function testDeleteAll() |
| 84 | { |
| 85 | Functions\when('delete_option')->justReturn(true); |
| 86 | $this->repository->deleteAll(); |
| 87 | $this->assertEquals($this->repository->findAll(), null); |
| 88 | } |
| 89 | |
| 90 | public function testCreate() |
| 91 | { |
| 92 | |
| 93 | Functions\when('update_option')->justReturn(true); |
| 94 | |
| 95 | $entity = $this->repository->create(['key1' => 'value1_2_updated']); |
| 96 | |
| 97 | $result = $entity->getModifiedProperties(); |
| 98 | |
| 99 | $this->assertEquals($result, ['test' => 0,'key1' => 'value1_2_updated']); |
| 100 | } |
| 101 | |
| 102 | public function testUpdate() |
| 103 | { |
| 104 | |
| 105 | Functions\when('update_option')->justReturn(true); |
| 106 | |
| 107 | $entity0 = $this->repository->create([]); |
| 108 | $entity0 = $this->repository->update(0, ['key1' => 'value1_2_updated']); |
| 109 | |
| 110 | $result = $entity0->getModifiedProperties(); |
| 111 | |
| 112 | $this->assertEquals($result, ['test' => 0,'key1' => 'value1_2_updated']); |
| 113 | } |
| 114 | |
| 115 | public function testDelete() |
| 116 | { |
| 117 | |
| 118 | Functions\when('update_option')->justReturn(true); |
| 119 | |
| 120 | $entity0 = $this->repository->create([]); |
| 121 | $entity1 = $this->repository->create([]); |
| 122 | $entity2 = $this->repository->create([]); |
| 123 | |
| 124 | $result = $this->repository->delete(0); |
| 125 | |
| 126 | $this->assertTrue($result); |
| 127 | $this->assertEquals(null, $this->repository->find(0)); |
| 128 | $this->assertEquals($entity1, $this->repository->find(1)); |
| 129 | $this->assertEquals($entity2, $this->repository->find(2)); |
| 130 | } |
| 131 | |
| 132 | public function testDefaults() |
| 133 | { |
| 134 | $entity0 = $this->repository->create([]); |
| 135 | $entity1 = $this->repository->create([]); |
| 136 | $entity2 = $this->repository->create([]); |
| 137 | |
| 138 | $results = $this->repository->findAll(); |
| 139 | |
| 140 | foreach ($results as $index => $entity) { |
| 141 | $defaults = $entity->getDefaults(); |
| 142 | $this->assertEquals($defaults, [ |
| 143 | 'test' => 0, |
| 144 | 'key1' => 'default_value_1', |
| 145 | 'key2' => 'default_value_2', |
| 146 | 'key3' => [ |
| 147 | 'key_3_1' => 'default_value_3', |
| 148 | 'key_3_2' => 'default_value_4', |
| 149 | ] |
| 150 | ]); |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 |