summaryrefslogtreecommitdiff
path: root/tests/Unit/GroupTest.php
blob: 5bb82bc9856904cbcb59b0d931ebe6b3e8cfd132 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
<?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) => new Group(new Router(), $parent);

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);
});

it('gets the path with no prefix or parents')
    ->expect(fn () => $make()->getFullPrefix())
    ->toBe('/');

it('gets the path without parents')
    ->expect(fn () => $make()->prefix('/t1/t2')->getFullPrefix())
    ->toBe('/t1/t2');

it('gets the path with parent')
    ->expect(fn () => $make(
        $make()->prefix('hello/world')
    )->prefix('/t1/t2')->getFullPrefix())
    ->toBe('/hello/world/t1/t2');

it('gets the path with two ancestors')
    ->expect(fn () => $make(
        $make(
            $make()->prefix('test')
        )->prefix('hello/world')
    )->prefix('/t1/t2')->getFullPrefix())
    ->toBe('/test/hello/world/t1/t2');

it('gets the path with three ancestors, one without prefix')
    ->expect(fn () => $make(
        $make(
            $make($make()->prefix('test'))
        )->prefix('hello/world')
    )->prefix('/t1/t2')->getFullPrefix())
    ->toBe('/test/hello/world/t1/t2');

it('gets the name with no prefix or parents')
    ->expect(fn () => $make()->getFullName())
    ->toBeNull();

it('gets the name without parents')
    ->expect(fn () => $make()->name('test-test')->getFullName())
    ->toBe('test-test');

it('gets the name with parent')
    ->expect(fn () => $make(
        $make()->name('hello-world.')
    )->name('test')->getFullName())
    ->toBe('hello-world.test');

it('gets the name with two ancestors')
    ->expect(fn () => $make(
        $make(
            $make()->name('test.')
        )->name('test1')
    )->name('hello')->getFullName())
    ->toBe('test.test1hello');

it('gets the name with three ancestors, one without name')
    ->expect(fn () => $make(
        $make(
            $make($make()->name('test1.'))
        )->name('test2.')
    )->name('test3.')->getFullName())
    ->toBe('test1.test2.test3.');