Skip to content

Commit 52df3be

Browse files
committed
Created tests for cypher map
1 parent fa0d5f3 commit 52df3be

File tree

1 file changed

+194
-58
lines changed

1 file changed

+194
-58
lines changed

tests/Unit/CypherMapTest.php

Lines changed: 194 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,12 @@
1313

1414
use ArrayIterator;
1515
use BadMethodCallException;
16-
use function hexdec;
16+
use Generator;
17+
use InvalidArgumentException;
18+
use IteratorAggregate;
19+
use function json_encode;
20+
use const JSON_THROW_ON_ERROR;
21+
use Laudis\Neo4j\Databags\Pair;
1722
use Laudis\Neo4j\Types\CypherList;
1823
use Laudis\Neo4j\Types\CypherMap;
1924
use OutOfBoundsException;
@@ -144,180 +149,311 @@ public function testMap(): void
144149
{
145150
$filter = $this->map->map(static fn (string $i, string $x) => $i.':'.$x);
146151

147-
self::assertEquals(new CypherList(['A:x', 'B:y', 'C:z']), $filter);
152+
self::assertEquals(new CypherMap(['A' => 'A:x', 'B' => 'B:y', 'C' => 'C:z']), $filter);
148153
}
149154

150155
public function testReduce(): void
151156
{
152-
$count = $this->map->reduce(static function (?int $initial, int $key, string $value) {
153-
return ($initial ?? 0) + $key * hexdec($value);
157+
$count = $this->map->reduce(static function (?int $initial, string $key, string $value) {
158+
return ($initial ?? 0) + ord($value) + ord($key);
154159
}, 5);
155160

156-
self::assertEquals(5 + hexdec('B') + 2 * hexdec('C'), $count);
161+
self::assertEquals(5 + ord('A') + ord('x') + ord('B') + ord('y') + ord('C') + ord('z'), $count);
157162
}
158163

159164
public function testFind(): void
160165
{
166+
self::assertFalse($this->map->find('A'));
161167
self::assertFalse($this->map->find('X'));
162-
self::assertEquals(0, $this->map->find('A'));
163-
self::assertEquals(1, $this->map->find('B'));
164-
self::assertEquals(2, $this->map->find('C'));
168+
self::assertEquals('C', $this->map->find('z'));
169+
self::assertEquals('B', $this->map->find('y'));
170+
self::assertEquals('A', $this->map->find('x'));
165171
}
166172

167173
public function testReversed(): void
168174
{
169-
self::assertEquals(new CypherList(['C', 'B', 'A']), $this->map->reversed());
170-
self::assertEquals(new CypherList(['A', 'B', 'C']), $this->map);
171-
self::assertEquals(new CypherList(['A', 'B', 'C']), $this->map->reversed()->reversed());
175+
self::assertEquals(new CypherMap(['C' => 'z', 'B' => 'y', 'A' => 'x']), $this->map->reversed());
176+
self::assertEquals(new CypherMap(['A' => 'x', 'B' => 'y', 'C' => 'z']), $this->map);
177+
self::assertEquals(new CypherMap(['A' => 'x', 'B' => 'y', 'C' => 'z']), $this->map->reversed()->reversed());
172178
}
173179

174180
public function testSliceSingle(): void
175181
{
176182
$sliced = $this->map->slice(1, 1);
177-
self::assertEquals(new CypherList(['B']), $sliced);
183+
self::assertEquals(new CypherMap(['B' => 'y']), $sliced);
178184
}
179185

180186
public function testSliceDouble(): void
181187
{
182188
$sliced = $this->map->slice(1, 2);
183-
self::assertEquals(new CypherList(['B', 'C']), $sliced);
189+
self::assertEquals(new CypherMap(['B' => 'y', 'C' => 'z']), $sliced);
184190
}
185191

186192
public function testSliceAll(): void
187193
{
188194
$sliced = $this->map->slice(0, 3);
189-
self::assertEquals(new CypherList(['A', 'B', 'C']), $sliced);
195+
self::assertEquals($this->map, $sliced);
190196
}
191197

192198
public function testSliceTooMuch(): void
193199
{
194200
$sliced = $this->map->slice(0, 5);
195-
self::assertEquals(new CypherList(['A', 'B', 'C']), $sliced);
201+
self::assertEquals($this->map, $sliced);
196202
}
197203

198204
public function testSliceEmpty(): void
199205
{
200206
$sliced = $this->map->slice(0, 0);
201-
self::assertEquals(new CypherList(), $sliced);
207+
self::assertEquals(new CypherMap(), $sliced);
202208
}
203209

204210
public function testGetValid(): void
205211
{
206-
self::assertEquals('A', $this->map->get(0));
207-
self::assertEquals('B', $this->map->get(1));
208-
self::assertEquals('C', $this->map->get(2));
212+
self::assertEquals('x', $this->map->get('A'));
213+
self::assertEquals('y', $this->map->get('B'));
214+
self::assertEquals('z', $this->map->get('C'));
215+
}
216+
217+
public function testGetDefault(): void
218+
{
219+
self::assertEquals('x', $this->map->get('A', null));
220+
self::assertNull($this->map->get('x', null));
221+
self::assertEquals(new stdClass(), $this->map->get('Cd', new stdClass()));
209222
}
210223

211224
public function testFirst(): void
212225
{
213-
self::assertEquals('A', $this->map->first());
226+
self::assertEquals(new Pair('A', 'x'), $this->map->first());
214227
}
215228

216229
public function testFirstInvalid(): void
217230
{
218231
$this->expectException(OutOfBoundsException::class);
219-
$this->expectExceptionMessage('Cannot grab first element of an empty list');
220-
(new CypherList())->first();
232+
$this->expectExceptionMessage('Cannot grab first element of an empty map');
233+
(new CypherMap())->first();
221234
}
222235

223236
public function testLast(): void
224237
{
225-
self::assertEquals('C', $this->map->last());
238+
self::assertEquals(new Pair('C', 'z'), $this->map->last());
226239
}
227240

228241
public function testLastInvalid(): void
229242
{
230243
$this->expectException(OutOfBoundsException::class);
231-
$this->expectExceptionMessage('Cannot grab last element of an empty list');
232-
(new CypherList())->last();
244+
$this->expectExceptionMessage('Cannot grab last element of an empty map');
245+
(new CypherMap())->last();
233246
}
234247

235248
public function testGetInvalid(): void
236249
{
237250
$this->expectException(OutOfBoundsException::class);
238-
$this->expectExceptionMessage('Cannot get item in sequence at position: 3');
239-
$this->map->get(3);
240-
}
241-
242-
public function testGetNegative(): void
243-
{
244-
$this->expectException(OutOfBoundsException::class);
245-
$this->expectExceptionMessage('Cannot get item in sequence at position: -1');
246-
$this->map->get(-1);
251+
$this->expectExceptionMessage('Cannot get item in sequence with key: a');
252+
$this->map->get('a');
247253
}
248254

249255
public function testIteration(): void
250256
{
251257
$counter = 0;
252258
foreach ($this->map as $key => $item) {
253259
++$counter;
254-
self::assertEquals(['A', 'B', 'C'][$key], $item);
260+
self::assertEquals(['A' => 'x', 'B' => 'y', 'C' => 'z'][$key], $item);
255261
}
256262
self::assertEquals(3, $counter);
257263
}
258264

259265
public function testIterationEmpty(): void
260266
{
261267
$counter = 0;
262-
foreach ((new CypherList()) as $key => $item) {
268+
foreach ((new CypherMap()) as $key => $item) {
263269
++$counter;
264-
self::assertEquals(['A', 'B', 'C'][$key], $item);
270+
self::assertEquals(['A' => 'x'][$key], $item);
265271
}
266272
self::assertEquals(0, $counter);
267273
}
268274

269275
public function testOffsetSet(): void
270276
{
271277
$this->expectException(BadMethodCallException::class);
272-
$this->expectExceptionMessage('Laudis\Neo4j\Types\CypherList is immutable');
278+
$this->expectExceptionMessage('Laudis\Neo4j\Types\CypherMap is immutable');
273279

274-
$this->map[0] = 'a';
280+
$this->map['A'] = 'a';
275281
}
276282

277283
public function testOffsetUnset(): void
278284
{
279285
$this->expectException(BadMethodCallException::class);
280-
$this->expectExceptionMessage('Laudis\Neo4j\Types\CypherList is immutable');
286+
$this->expectExceptionMessage('Laudis\Neo4j\Types\CypherMap is immutable');
281287

282-
unset($this->map[0]);
288+
unset($this->map['A']);
283289
}
284290

285291
public function testOffsetGetValid(): void
286292
{
287-
self::assertEquals('A', $this->map[0]);
288-
self::assertEquals('B', $this->map[1]);
289-
self::assertEquals('C', $this->map[2]);
293+
self::assertEquals('x', $this->map['A']);
294+
self::assertEquals('y', $this->map['B']);
295+
self::assertEquals('z', $this->map['C']);
290296
}
291297

292298
public function testOffsetGetInvalid(): void
293299
{
294300
$this->expectException(OutOfBoundsException::class);
295-
$this->expectExceptionMessage('Offset: "3" does not exists in object of instance: Laudis\Neo4j\Types\CypherList');
296-
$this->map[3];
301+
$this->expectExceptionMessage('Offset: "AA" does not exists in object of instance: Laudis\Neo4j\Types\CypherMap');
302+
$this->map['AA'];
303+
}
304+
305+
public function testIssetValid(): void
306+
{
307+
self::assertTrue(isset($this->map['A']));
308+
self::assertTrue(isset($this->map['B']));
309+
self::assertTrue(isset($this->map['C']));
310+
}
311+
312+
public function testIssetInValid(): void
313+
{
314+
self::assertFalse(isset($this->map['a']));
315+
}
316+
317+
public function testIssetValidNull(): void
318+
{
319+
self::assertTrue(isset((new CypherMap(['a' => null]))['a']));
320+
}
321+
322+
public function testJsonSerialize(): void
323+
{
324+
self::assertEquals('{"A":"x","B":"y","C":"z"}', json_encode($this->map, JSON_THROW_ON_ERROR));
325+
}
326+
327+
public function testJsonSerializeEmpty(): void
328+
{
329+
self::assertEquals('{}', json_encode(new CypherMap(), JSON_THROW_ON_ERROR));
330+
}
331+
332+
public function testJoin(): void
333+
{
334+
self::assertEquals('x;y;z', $this->map->join(';'));
335+
}
336+
337+
public function testJoinEmpty(): void
338+
{
339+
self::assertEquals('', (new CypherMap())->join('A'));
340+
}
341+
342+
public function testDiff(): void
343+
{
344+
$subtract = new CypherMap(['B' => null, 'Z' => 'z']);
345+
$result = $this->map->diff($subtract);
346+
347+
self::assertEquals(new CypherMap(['A' => 'x', 'C' => 'z']), $result);
348+
self::assertEquals(new CypherMap(['B' => null, 'Z' => 'z']), $subtract);
349+
self::assertEquals(new CypherMap(['A' => 'x', 'B' => 'y', 'C' => 'z']), $this->map);
350+
}
351+
352+
public function testDiffEmpty(): void
353+
{
354+
self::assertEquals(new CypherMap(['A' => 'x', 'B' => 'y', 'C' => 'z']), $this->map->diff([]));
297355
}
298356

299-
public function testOffsetGetNegative(): void
357+
public function testIntersect(): void
358+
{
359+
$intersect = new CypherMap(['B' => null, 'Z' => 'z']);
360+
$result = $this->map->intersect($intersect);
361+
362+
self::assertEquals(new CypherMap(['B' => 'y']), $result);
363+
self::assertEquals(new CypherMap(['B' => null, 'Z' => 'z']), $intersect);
364+
self::assertEquals(new CypherMap(['A' => 'x', 'B' => 'y', 'C' => 'z']), $this->map);
365+
}
366+
367+
public function testUnion(): void
368+
{
369+
$intersect = new CypherMap(['B' => null, 'Z' => 'z']);
370+
$result = $this->map->union($intersect);
371+
372+
self::assertEquals(new CypherMap(['A' => 'x', 'B' => 'y', 'C' => 'z', 'Z' => 'z']), $result);
373+
self::assertEquals(new CypherMap(['B' => null, 'Z' => 'z']), $intersect);
374+
self::assertEquals(new CypherMap(['A' => 'x', 'B' => 'y', 'C' => 'z']), $this->map);
375+
}
376+
377+
public function testXor(): void
378+
{
379+
$intersect = new CypherMap(['B' => null, 'Z' => 'z']);
380+
$result = $this->map->xor($intersect);
381+
382+
self::assertEquals(new CypherMap(['A' => 'x', 'C' => 'z', 'Z' => 'z']), $result);
383+
self::assertEquals(new CypherMap(['B' => null, 'Z' => 'z']), $intersect);
384+
self::assertEquals(new CypherMap(['A' => 'x', 'B' => 'y', 'C' => 'z']), $this->map);
385+
}
386+
387+
public function testValue(): void
388+
{
389+
self::assertEquals(new CypherList(['x', 'y', 'z']), $this->map->values());
390+
}
391+
392+
public function testKeys(): void
393+
{
394+
self::assertEquals(new CypherList(['A', 'B', 'C']), $this->map->keys());
395+
}
396+
397+
public function testPairs(): void
398+
{
399+
$list = new CypherList([new Pair('A', 'x'), new Pair('B', 'y'), new Pair('C', 'z')]);
400+
self::assertEquals($list, $this->map->pairs());
401+
}
402+
403+
public function testSkip(): void
404+
{
405+
self::assertEquals(new Pair('A', 'x'), $this->map->skip(0));
406+
self::assertEquals(new Pair('B', 'y'), $this->map->skip(1));
407+
self::assertEquals(new Pair('C', 'z'), $this->map->skip(2));
408+
}
409+
410+
public function testSkipInvalid(): void
300411
{
301412
$this->expectException(OutOfBoundsException::class);
302-
$this->expectExceptionMessage('Offset: "-1" does not exists in object of instance: Laudis\Neo4j\Types\CypherList');
303-
$this->map[-1];
413+
$this->expectExceptionMessage('Cannot skip to a pair at position: 4');
414+
self::assertEquals(new Pair('A', 'x'), $this->map->skip(4));
304415
}
305416

306-
public function testIssetValid(): void
417+
public function testInvalidConstruct(): void
307418
{
308-
self::assertTrue(isset($this->map[0]));
309-
self::assertTrue(isset($this->map[1]));
310-
self::assertTrue(isset($this->map[2]));
419+
$this->expectException(InvalidArgumentException::class);
420+
$this->expectExceptionMessage('Iterable must have a stringable keys');
421+
422+
new CypherMap(new class() implements IteratorAggregate {
423+
public function getIterator(): Generator
424+
{
425+
yield new stdClass() => 'x';
426+
}
427+
});
311428
}
312429

313-
public function testIssetInValid(): void
430+
public function testSortedDefault(): void
314431
{
315-
self::assertFalse(isset($this->map[-1]));
316-
self::assertFalse(isset($this->map[3]));
432+
self::assertEquals($this->map, $this->map->sorted());
433+
self::assertEquals($this->map, $this->map->reversed()->sorted());
317434
}
318435

319-
public function testIssetValidNull(): void
436+
public function testSortedCustom(): void
320437
{
321-
self::assertTrue(isset((new CypherList([null]))[0]));
438+
$sorted = $this->map->sorted(static fn (string $x, $y) => -1 * ($x <=> $y));
439+
440+
self::assertEquals(new CypherMap(['C' => 'z', 'B' => 'y', 'A' => 'x']), $sorted);
441+
self::assertEquals(new CypherMap(['A' => 'x', 'B' => 'y', 'C' => 'z']), $this->map);
322442
}
443+
444+
public function testKSorted(): void
445+
{
446+
self::assertEquals($this->map, $this->map->ksorted());
447+
self::assertEquals($this->map, $this->map->reversed()->ksorted());
448+
}
449+
450+
public function testKSortedCustom(): void
451+
{
452+
$sorted = $this->map->ksorted(static fn (string $x, $y) => -1 * ($x <=> $y));
453+
454+
self::assertEquals(new CypherMap(['C' => 'z', 'B' => 'y', 'A' => 'x']), $sorted);
455+
self::assertEquals(new CypherMap(['A' => 'x', 'B' => 'y', 'C' => 'z']), $this->map);
456+
}
457+
458+
//test sorted and ksorted
323459
}

0 commit comments

Comments
 (0)