summaryrefslogtreecommitdiff
path: root/tests/Unit/ApplicationTest.php
blob: e466ecd7e2b67749d8c82c12369a59401b62b582 (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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
<?php

namespace Engelsystem\Test\Unit;

use Engelsystem\Application;
use Engelsystem\Config\Config;
use Engelsystem\Container\Container;
use Engelsystem\Container\ServiceProvider;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use Psr\Http\Server\MiddlewareInterface;
use ReflectionClass;

class ApplicationTest extends TestCase
{
    /**
     * @covers \Engelsystem\Application::__construct
     * @covers \Engelsystem\Application::registerBaseBindings
     */
    public function testConstructor()
    {
        $app = new Application('.');

        $this->assertInstanceOf(Container::class, $app);
        $this->assertInstanceOf(ContainerInterface::class, $app);
        $this->assertSame($app, $app->get('app'));
        $this->assertSame($app, $app->get('container'));
        $this->assertSame($app, $app->get(Container::class));
        $this->assertSame($app, $app->get(Application::class));
        $this->assertSame($app, $app->get(ContainerInterface::class));
        $this->assertSame($app, Application::getInstance());
        $this->assertSame($app, Container::getInstance());
    }

    /**
     * @covers \Engelsystem\Application::path
     * @covers \Engelsystem\Application::registerPaths
     * @covers \Engelsystem\Application::setAppPath
     */
    public function testAppPath()
    {
        $app = new Application();

        $this->assertFalse($app->has('path'));

        $app->setAppPath('.');
        $this->assertTrue($app->has('path'));
        $this->assertTrue($app->has('path.assets'));
        $this->assertTrue($app->has('path.config'));
        $this->assertTrue($app->has('path.lang'));
        $this->assertTrue($app->has('path.resources'));
        $this->assertTrue($app->has('path.views'));
        $this->assertTrue($app->has('path.storage'));
        $this->assertTrue($app->has('path.cache'));
        $this->assertTrue($app->has('path.cache.routes'));
        $this->assertTrue($app->has('path.cache.views'));

        $this->assertEquals(realpath('.'), $app->path());
        $this->assertEquals(realpath('.') . '/config', $app->get('path.config'));

        $app->setAppPath('./../');
        $this->assertEquals(realpath('../') . '/config', $app->get('path.config'));
    }

    /**
     * @covers \Engelsystem\Application::register
     */
    public function testRegister()
    {
        $app = new Application();

        $serviceProvider = $this->mockServiceProvider($app, ['register']);
        $serviceProvider->expects($this->once())
            ->method('register');

        $app->register($serviceProvider);

        $anotherServiceProvider = $this->mockServiceProvider($app, ['register', 'boot']);
        $anotherServiceProvider->expects($this->once())
            ->method('register');
        $anotherServiceProvider->expects($this->once())
            ->method('boot');

        $app->bootstrap();
        $app->register($anotherServiceProvider);
    }

    /**
     * @covers \Engelsystem\Application::register
     */
    public function testRegisterBoot()
    {
        $app = new Application();
        $app->bootstrap();

        $serviceProvider = $this->mockServiceProvider($app, ['register', 'boot']);
        $serviceProvider->expects($this->once())
            ->method('register');
        $serviceProvider->expects($this->once())
            ->method('boot');

        $app->register($serviceProvider);
    }

    /**
     * @covers \Engelsystem\Application::register
     */
    public function testRegisterClassName()
    {
        $app = new Application();

        $mockClassName = $this->getMockClass(ServiceProvider::class);
        $serviceProvider = $this->getMockBuilder($mockClassName)
            ->setConstructorArgs([$app])
            ->setMethods(['register'])
            ->getMock();

        $serviceProvider->expects($this->once())
            ->method('register');

        $app->instance($mockClassName, $serviceProvider);
        $app->register($mockClassName);
    }

    /**
     * @covers \Engelsystem\Application::bootstrap
     * @covers \Engelsystem\Application::getMiddleware
     * @covers \Engelsystem\Application::isBooted
     */
    public function testBootstrap()
    {
        /** @var Application|MockObject $app */
        $app = $this->getMockBuilder(Application::class)
            ->setMethods(['register'])
            ->getMock();

        $serviceProvider = $this->mockServiceProvider($app, ['boot']);
        $serviceProvider->expects($this->once())
            ->method('boot');

        $app->expects($this->once())
            ->method('register')
            ->with($serviceProvider);

        /** @var Config|MockObject $config */
        $config = $this->getMockBuilder(Config::class)
            ->getMock();

        $middleware = [MiddlewareInterface::class];
        $config->expects($this->exactly(2))
            ->method('get')
            ->withConsecutive(['providers'], ['middleware'])
            ->willReturnOnConsecutiveCalls([$serviceProvider], $middleware);

        $property = (new ReflectionClass($app))->getProperty('serviceProviders');
        $property->setAccessible(true);
        $property->setValue($app, [$serviceProvider]);

        $app->bootstrap($config);

        $this->assertTrue($app->isBooted());
        $this->assertEquals($middleware, $app->getMiddleware());

        // Run bootstrap another time to ensure that providers are registered only once
        $app->bootstrap($config);
    }

    /**
     * @param Application $app
     * @param array       $methods
     * @return ServiceProvider|MockObject
     */
    protected function mockServiceProvider(Application $app, $methods = [])
    {
        $serviceProvider = $this->getMockBuilder(ServiceProvider::class)
            ->setConstructorArgs([$app])
            ->setMethods($methods)
            ->getMockForAbstractClass();

        return $serviceProvider;
    }
}