PluginProbe
QuadMenu – Mega Menu / 3.3.8
QuadMenu – Mega Menu v3.3.8
3.3.8 3.3.7 3.3.6 trunk 1.8.4 1.8.5 1.8.7 1.8.8 1.8.9 1.9.0 1.9.2 1.9.3 1.9.4 1.9.5 1.9.6 1.9.7 1.9.8 1.9.9 2.0.0 2.0.1 2.0.2 2.0.3 2.0.4 2.0.5 2.0.6 All 88 releases
quadmenu / vendor / franmastromarino / wp-orm / tests / Collection / CollectionRepositoryBuilderTestDefaultEntities.php

CollectionRepositoryBuilderTestDefaultEntities.php in QuadMenu – Mega Menu 3.3.8, at vendor/franmastromarino/wp-orm/tests/Collection/CollectionRepositoryBuilderTestDefaultEntities.php

242 lines 8.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 CollectionRepositoryBuilderTestDefaultEntities extends TestCase
11 {
12 private string $table = 'test_table';
13 private CollectionRepository $repository;
14 private array $defaultEntities;
15
16 protected function setUp(): void
17 {
18
19 // Create default entities
20 $this->defaultEntities = [
21 ['id' => 5, 'key1' => 'defaultEntities1', 'allowDelete' => false],
22 ['id' => 6, 'key1' => 'defaultEntities2']
23 ];
24
25 // Set up the builder with default entities
26 $builder = (new CollectionRepositoryBuilder())
27 ->setTable($this->table)
28 ->setEntity('\QuadLayers\WP_Orm\Tests\Collection\CollectionEntityTest')
29 ->setAutoIncrement(true)
30 ->setDefaultEntities($this->defaultEntities); // Set default entities
31
32
33 $this->repository = $builder->getRepository();
34
35 // When get_option is called, return testInput
36 Functions\when('get_option')->justReturn($this->defaultEntities);
37 }
38
39 public function testFindAll()
40 {
41
42 $test = [
43 [
44 'key1' => 'test',
45 ],
46 [
47 'key1' => 'test2',
48 ],
49 [
50 'key1' => 'test3',
51 ],
52 ];
53
54 // Initialize a variable to keep track of the update_option call count
55 $callCount = 0;
56
57 Functions\when('update_option')->alias(
58 function ($option, $value) use (&$callCount, $test) {
59 // Check if the table is correct
60 if ($this->table !== $option) {
61 return false;
62 }
63
64 // // Increase the call count
65 $callCount++;
66
67 // // Check if the value matches the test values up to the current call count
68 if (serialize(array_slice($test, 0, $callCount + 1)) !== serialize($value)) {
69 return false;
70 }
71
72 return true;
73 }
74 );
75
76 foreach ($test as $index => $data) {
77 $this->repository->create($data);
78 }
79
80 $results = $this->repository->findAll();
81
82 $merged = array_merge($this->defaultEntities, $test);
83
84 // Then proceed with count check
85 $this->assertCount(count($merged), $results);
86
87 // Check if the found entities match the default entities properties
88 foreach ($results as $index => $entity) {
89 // Assuming getProperties() returns an associative array of entity properties
90 $properties = $entity->getProperties();
91 $expectedProperties = $merged[$index];
92 $privateProperties = $entity::PRIVATE_PROPERTIES;
93 foreach ($expectedProperties as $propertyName => $value) {
94 if (in_array($propertyName, $privateProperties)) {
95 continue;
96 }
97 $this->assertArrayHasKey($propertyName, $properties, "Property $propertyName should exist in the entity");
98 $this->assertEquals($value, $properties[$propertyName], "Property $propertyName should match the expected value");
99 }
100 }
101 }
102
103 public function testDeleteAll()
104 {
105 Functions\when('delete_option')->justReturn(true);
106
107 $this->repository->deleteAll();
108
109 $results = $this->repository->getCache();
110
111 // Then proceed with count check
112 $this->assertEquals(1, count($results));
113
114 // Check if the found entities match the default entities properties
115 foreach ($results as $index => $entity) {
116 // Assuming getProperties() returns an associative array of entity properties
117 $properties = $entity->getProperties();
118 $expectedProperties = $this->defaultEntities[$index];
119 $privateProperties = $entity::PRIVATE_PROPERTIES;
120 foreach ($expectedProperties as $propertyName => $value) {
121 if (in_array($propertyName, $privateProperties)) {
122 continue;
123 }
124 $this->assertArrayHasKey($propertyName, $properties, "Property $propertyName should exist in the entity");
125 $this->assertEquals($value, $properties[$propertyName], "Property $propertyName should match the expected value");
126 }
127 }
128 }
129
130 public function testCreate()
131 {
132
133 Functions\when('update_option')->justReturn(true);
134
135 $entity = $this->repository->create(['key1' => 'value1_2_updated']);
136
137 $result = $entity->getModifiedProperties();
138
139 $this->assertEquals($result, ['id' => end($this->defaultEntities)['id'] + 1,'key1' => 'value1_2_updated']);
140 }
141
142 public function testUpdate()
143 {
144
145 Functions\when('update_option')->justReturn(true);
146
147 $id = $this->defaultEntities[0]['id'];
148
149 $entity0 = $this->repository->create([]);
150 $entity0 = $this->repository->update($id, ['key1' => 'value1_2_updated']);
151
152 $result = $entity0->getModifiedProperties();
153
154 $this->assertEquals($result, ['id' => $id,'key1' => 'value1_2_updated']);
155 }
156
157 public function testDelete()
158 {
159
160 Functions\when('update_option')->justReturn(true);
161
162 $id = end($this->defaultEntities)['id'];
163
164 $entity0 = $this->repository->create([]);
165 $entity1 = $this->repository->create([]);
166 $entity2 = $this->repository->create([]);
167
168 $result = $this->repository->delete($id);
169
170 $this->assertTrue($result);
171 $this->assertEquals(null, $this->repository->find($id));
172 $this->assertEquals($entity1, $this->repository->find($id + 2));
173 $this->assertEquals($entity2, $this->repository->find($id + 3));
174 }
175
176 public function testAllowDeleteFalse()
177 {
178
179 Functions\when('update_option')->justReturn(true);
180
181 $entity1 = $this->repository->create([]);
182 $entity2 = $this->repository->create([]);
183 $entity3 = $this->repository->create([]);
184
185 // Expecting an exception when attempting to delete entity 0
186 $this->expectException(\InvalidArgumentException::class);
187 $this->expectExceptionMessage('Entity does not allow deletion.');
188
189 $result = $this->repository->delete(5);
190 }
191
192 public function testDefaults()
193 {
194 $entity0 = $this->repository->create([]);
195 $entity1 = $this->repository->create([]);
196 $entity2 = $this->repository->create([]);
197
198 $results = $this->repository->findAll();
199
200 foreach ($results as $index => $entity) {
201 $defaults = $entity->getDefaults();
202 $this->assertEquals($defaults, [
203 'id' => 0,
204 'key1' => 'default_value_1',
205 'key2' => 'default_value_2',
206 'key3' => [
207 'key_3_1' => 'default_value_3',
208 'key_3_2' => 'default_value_4',
209 ]
210 ]);
211 }
212 }
213
214 public function testWithDefaultEntities()
215 {
216
217 // Test if the repository contains the default entities
218 $result = $this->repository->findAll();
219 // Check if $result is iterable
220 $this->assertIsIterable($result, 'The result of findAll() should be iterable');
221
222 // Then proceed with count check
223 $this->assertCount(count($this->defaultEntities), $result);
224
225 // Check if the found entities match the default entities properties
226 foreach ($result as $index => $entity) {
227 // Assuming getProperties() returns an associative array of entity properties
228 $properties = $entity->getProperties();
229 $expectedProperties = $this->defaultEntities[$index];
230 $privateProperties = $entity::PRIVATE_PROPERTIES;
231 foreach ($expectedProperties as $propertyName => $value) {
232 if (in_array($propertyName, $privateProperties)) {
233 continue;
234 }
235 $this->assertArrayHasKey($propertyName, $properties, "Property $propertyName should exist in the entity");
236 $this->assertEquals($value, $properties[$propertyName], "Property $propertyName should match the expected value");
237 }
238 }
239
240 }
241 }
242