Passed
Pull Request — master (#653)
by Aleksei
07:43
created

KernelTest.php$1 ➔ testStartingCallbacks()   A

Complexity

Conditions 1

Size

Total Lines 42

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
c 0
b 0
f 0
dl 0
loc 42
rs 9.248
1
<?php
2
3
/**
4
 * Spiral Framework.
5
 *
6
 * @license   MIT
7
 * @author    Anton Titov (Wolfy-J)
8
 */
9
10
declare(strict_types=1);
11
12
namespace Spiral\Tests\Boot;
13
14
use PHPUnit\Framework\TestCase;
15
use Spiral\Boot\DispatcherInterface;
16
use Spiral\Boot\EnvironmentInterface;
17
use Spiral\Boot\Exception\BootException;
18
use Spiral\Tests\Boot\Fixtures\TestCore;
19
use Throwable;
20
21
class KernelTest extends TestCase
22
{
23
    /**
24
     * @throws Throwable
25
     */
26
    public function testKernelException(): void
27
    {
28
        $this->expectException(BootException::class);
29
30
        $kernel = TestCore::init([
31
            'root' => __DIR__,
32
        ]);
33
34
        $kernel->serve();
35
    }
36
37
    /**
38
     * @throws Throwable
39
     */
40
    public function testDispatcher(): void
41
    {
42
        $kernel = TestCore::init([
43
            'root' => __DIR__,
44
        ]);
45
46
        $d = new class() implements DispatcherInterface {
47
            public $fired = false;
48
49
            public function canServe(): bool
50
            {
51
                return true;
52
            }
53
54
            public function serve(): void
55
            {
56
                $this->fired = true;
57
            }
58
        };
59
        $kernel->addDispatcher($d);
60
        $this->assertFalse($d->fired);
61
62
        $kernel->serve();
63
        $this->assertTrue($d->fired);
64
    }
65
66
    /**
67
     * @throws Throwable
68
     */
69
    public function testDispatcherReturnCode(): void
70
    {
71
        $kernel = TestCore::init([
72
            'root' => __DIR__,
73
        ]);
74
75
        $d = new class() implements DispatcherInterface {
76
            public function canServe(): bool
77
            {
78
                return true;
79
            }
80
81
            public function serve(): int
82
            {
83
                return 1;
84
            }
85
        };
86
        $kernel->addDispatcher($d);
87
88
        $result = $kernel->serve();
89
        $this->assertSame(1, $result);
90
    }
91
92
    /**
93
     * @throws Throwable
94
     */
95
    public function testEnv(): void
96
    {
97
        $kernel = TestCore::init([
98
            'root' => __DIR__,
99
        ]);
100
101
        $this->assertSame(
102
            'VALUE',
103
            $kernel->getContainer()->get(EnvironmentInterface::class)->get('INTERNAL')
104
        );
105
    }
106
107
    public function testStartingCallbacks()
108
    {
109
        $kernel = TestCore::create([
110
            'root' => __DIR__,
111
        ]);
112
113
        $kernel->starting(static function (TestCore $core) {
114
            $core->getContainer()->bind('abc', 'foo');
115
        });
116
117
        $kernel->starting(static function (TestCore $core) {
118
            $core->getContainer()->bind('bcd', 'foo');
119
        });
120
121
        $kernel->started( static function (TestCore $core) {
122
            $core->getContainer()->bind('cde', 'foo');
123
        });
124
125
        $kernel->started( static function (TestCore $core) {
126
            $core->getContainer()->bind('def', 'foo');
127
        });
128
129
        $kernel->run();
130
131
        $this->assertTrue($kernel->getContainer()->has('abc'));
132
        $this->assertTrue($kernel->getContainer()->has('bcd'));
133
        $this->assertTrue($kernel->getContainer()->has('cde'));
134
        $this->assertTrue($kernel->getContainer()->has('def'));
135
        $this->assertTrue($kernel->getContainer()->has('efg'));
136
        $this->assertFalse($kernel->getContainer()->has('fgh'));
137
        $this->assertFalse($kernel->getContainer()->has('ghi'));
138
        $this->assertTrue($kernel->getContainer()->has('hij'));
139
        $this->assertTrue($kernel->getContainer()->has('ijk'));
140
        $this->assertTrue($kernel->getContainer()->has('jkl'));
141
        $this->assertFalse($kernel->getContainer()->has('klm'));
142
        $this->assertTrue($kernel->getContainer()->has('lmn'));
143
        $this->assertTrue($kernel->getContainer()->has('mno'));
144
145
146
        $this->assertInstanceOf(
147
            EnvironmentInterface::class,
148
            $kernel->getContainer()->get(EnvironmentInterface::class)
149
        );
150
    }
151
}
152