summaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/Unit/GroupDefinitionTest.php15
-rw-r--r--tests/Unit/GroupTest.php81
2 files changed, 95 insertions, 1 deletions
diff --git a/tests/Unit/GroupDefinitionTest.php b/tests/Unit/GroupDefinitionTest.php
index 3a064a1..59a8d3d 100644
--- a/tests/Unit/GroupDefinitionTest.php
+++ b/tests/Unit/GroupDefinitionTest.php
@@ -2,9 +2,22 @@
declare(strict_types=1);
+use Lightscale\Router\Group;
use Lightscale\Router\GroupDefinition;
use Lightscale\Router\Router;
+$make = fn () => new GroupDefinition(new Group(new Router(), null));
+
it('initializes')
- ->expect(fn () => new GroupDefinition(new Router()))
+ ->expect(fn () => $make())
->toBeInstanceOf(GroupDefinition::class);
+
+it('creates group with prefix')
+ ->expect(fn () => $make()->prefix('/test'))
+ ->toBeInstanceOf(Group::class)
+ ->getPrefix()->toBe('/test');
+
+it('creates group with name')
+ ->expect(fn () => $make()->name('name'))
+ ->toBeInstanceOf(Group::class)
+ ->getName()->toBe('name');
diff --git a/tests/Unit/GroupTest.php b/tests/Unit/GroupTest.php
new file mode 100644
index 0000000..3b17f19
--- /dev/null
+++ b/tests/Unit/GroupTest.php
@@ -0,0 +1,81 @@
+<?php
+
+declare(strict_types=1);
+
+use Lightscale\Router\Group;
+use Lightscale\Router\GroupDefinition;
+use Lightscale\Router\Router;
+use Lightscale\Router\Test\Utils\TestCallable;
+
+$make = fn(?Group $parent = null, ...$args) => new Group(new Router, $parent, ...$args);
+
+it('initializes with null defaults')
+ ->expect(fn() => $make())
+ ->toBeInstanceOf(Group::class)
+ ->getName()->toBeNull()
+ ->getPrefix()->toBeNull();
+
+it('initializes with values')
+ ->expect(new Group(new Router, null, '/test-path', 'test-name'))
+ ->toBeInstanceOf(Group::class)
+ ->getName()->toBe('test-name')
+ ->getPrefix()->toBe('/test-path');
+
+it('can set/get prefix')
+ ->expect(fn() => $make()->prefix('/test'))
+ ->toBeInstanceOf(Group::class)
+ ->getPrefix()->toBe('/test');
+
+it('can set/get name')
+ ->expect(fn() => $make()->name('test'))
+ ->toBeInstanceOf(Group::class)
+ ->getName()->toBe('test');
+
+it('calls group call back with definition', function () use ($make) {
+ $cb = TestCallable::make(fn() => null);
+ $make()->group($cb);
+
+ $cb->assertCalled();
+ $call = $cb->getLastCall();
+
+ expect($call?->args)->{0}->toBeInstanceOf(GroupDefinition::class);
+});
+
+it('can have a parent')
+ ->expect(fn () => $make($make())->getParent())
+ ->toBeInstanceOf(Group::class);
+
+it('can have a null parent')
+ ->expect(fn () => $make(null)->getParent())
+ ->toBeNull();
+
+it('can get all ancestors')
+ ->expect(fn () => $make($make($make()))->getAncestors())
+ ->toBeArray()
+ ->toContainOnlyInstancesOf(Group::class)
+ ->toHaveCount(2);
+
+it('order all ancestors root first', function () use($make) {
+ $g3 = $make($g2 = $make($g1 = $make()));
+
+ expect($g3->getAncestors())
+ ->toHaveCount(2)
+ ->{0}->toBe($g1)
+ ->{1}->toBe($g2);
+});
+
+it('can get all ancestors and self')
+ ->expect(fn () => $make($make($make()))->getAncestorsAndSelf())
+ ->toBeArray()
+ ->toContainOnlyInstancesOf(Group::class)
+ ->toHaveCount(3);
+
+it('order all ancestors and self root first', function () use($make) {
+ $g3 = $make($g2 = $make($g1 = $make()));
+
+ expect($g3->getAncestorsAndSelf())
+ ->toHaveCount(3)
+ ->{0}->toBe($g1)
+ ->{1}->toBe($g2)
+ ->{2}->toBe($g3);
+});