Passed
Push — 6.0 ( af2f12...47ce62 )
by liu
05:13
created

AppTest::testInitialize()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 14
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 24
rs 9.7998
1
<?php
2
0 ignored issues
show
Coding Style introduced by
Missing file doc comment
Loading history...
3
namespace think\tests;
4
5
use Mockery as m;
6
use org\bovigo\vfs\vfsStream;
7
use org\bovigo\vfs\vfsStreamDirectory;
8
use PHPUnit\Framework\TestCase;
9
use stdClass;
10
use think\App;
11
use think\Env;
12
use think\Event;
13
use think\exception\ClassNotFoundException;
14
use think\Service;
15
16
class SomeService extends Service
17
{
18
    public $bind = [
19
        'some' => 'class',
20
    ];
21
22
    public function register()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function register()
Loading history...
23
    {
24
25
    }
26
27
    public function boot()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function boot()
Loading history...
28
    {
29
30
    }
31
}
32
33
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
34
 * @property array initializers
35
 */
36
class AppTest extends TestCase
37
{
38
    /** @var App */
0 ignored issues
show
Coding Style introduced by
The open comment tag must be the only content on the line
Loading history...
Coding Style introduced by
Missing short description in doc comment
Loading history...
Coding Style introduced by
The close comment tag must be the only content on the line
Loading history...
39
    protected $app;
40
41
    protected function setUp()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function setUp()
Loading history...
42
    {
43
        $this->app = new App();
44
    }
45
46
    protected function tearDown(): void
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function tearDown()
Loading history...
47
    {
48
        m::close();
49
    }
50
51
    public function testService()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function testService()
Loading history...
52
    {
53
        $this->app->register(stdClass::class);
54
55
        $this->assertInstanceOf(stdClass::class, $this->app->getService(stdClass::class));
56
57
        $service = m::mock(SomeService::class);
58
59
        $service->shouldReceive('register')->once();
60
61
        $this->app->register($service);
0 ignored issues
show
Bug introduced by
$service of type Mockery\MockInterface is incompatible with the type string|think\Service expected by parameter $service of think\App::register(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

61
        $this->app->register(/** @scrutinizer ignore-type */ $service);
Loading history...
62
63
        $this->assertEquals($service, $this->app->getService(SomeService::class));
64
65
        $service2 = m::mock(SomeService::class);
66
67
        $service2->shouldReceive('register')->once();
68
69
        $this->app->register($service2);
70
71
        $this->assertEquals($service, $this->app->getService(SomeService::class));
72
73
        $this->app->register($service2, true);
74
75
        $this->assertEquals($service2, $this->app->getService(SomeService::class));
76
77
        $service->shouldReceive('boot')->once();
78
        $service2->shouldReceive('boot')->once();
79
80
        $this->app->boot();
81
    }
82
83
    public function testDebug()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function testDebug()
Loading history...
84
    {
85
        $this->app->debug(false);
86
87
        $this->assertFalse($this->app->isDebug());
88
89
        $this->app->debug(true);
90
91
        $this->assertTrue($this->app->isDebug());
92
    }
93
94
    public function testNamespace()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function testNamespace()
Loading history...
95
    {
96
        $namespace = 'test';
97
98
        $this->app->setNamespace($namespace);
99
100
        $this->assertEquals($namespace, $this->app->getNamespace());
101
    }
102
103
    public function testVersion()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function testVersion()
Loading history...
104
    {
105
        $this->assertEquals(App::VERSION, $this->app->version());
106
    }
107
108
    public function testPath()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function testPath()
Loading history...
109
    {
110
        $rootPath = __DIR__ . DIRECTORY_SEPARATOR;
111
112
        $app = new App($rootPath);
113
114
        $this->assertEquals($rootPath, $app->getRootPath());
115
116
        $this->assertEquals(dirname(__DIR__) . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR, $app->getThinkPath());
117
118
        $this->assertEquals($rootPath . 'app' . DIRECTORY_SEPARATOR, $app->getAppPath());
119
120
        $appPath = $rootPath . 'app' . DIRECTORY_SEPARATOR . 'admin' . DIRECTORY_SEPARATOR;
121
        $app->setAppPath($appPath);
122
        $this->assertEquals($appPath, $app->getAppPath());
123
124
        $this->assertEquals($rootPath . 'app' . DIRECTORY_SEPARATOR, $app->getBasePath());
125
126
        $this->assertEquals($rootPath . 'config' . DIRECTORY_SEPARATOR, $app->getConfigPath());
127
128
        $this->assertEquals($rootPath . 'runtime' . DIRECTORY_SEPARATOR, $app->getRuntimePath());
129
130
        $runtimePath = $rootPath . 'runtime' . DIRECTORY_SEPARATOR . 'admin' . DIRECTORY_SEPARATOR;
131
        $app->setRuntimePath($runtimePath);
132
        $this->assertEquals($runtimePath, $app->getRuntimePath());
133
    }
134
135
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
136
     * @param vfsStreamDirectory $root
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
137
     * @param bool               $debug
0 ignored issues
show
Coding Style introduced by
Missing parameter comment
Loading history...
138
     * @return App
139
     */
140
    protected function prepareAppForInitialize(vfsStreamDirectory $root, $debug = true)
141
    {
142
        $rootPath = $root->url() . DIRECTORY_SEPARATOR;
143
144
        $app = new App($rootPath);
145
146
        $initializer = m::mock();
147
        $initializer->shouldReceive('init')->once()->with($app);
148
149
        $app->instance($initializer->mockery_getName(), $initializer);
150
151
        (function () use ($initializer) {
152
            $this->initializers = [$initializer->mockery_getName()];
153
        })->call($app);
154
155
        $env = m::mock(Env::class);
156
        $env->shouldReceive('load')->once()->with($rootPath . '.env');
157
        $env->shouldReceive('get')->once()->with('config_ext', '.php')->andReturn('.php');
158
        $env->shouldReceive('get')->once()->with('app_debug')->andReturn($debug);
159
160
        $event = m::mock(Event::class);
161
        $event->shouldReceive('trigger')->once()->with('AppInit');
162
        $event->shouldReceive('bind')->once()->with([]);
163
        $event->shouldReceive('listenEvents')->once()->with([]);
164
        $event->shouldReceive('subscribe')->once()->with([]);
165
166
        $app->instance('env', $env);
167
        $app->instance('event', $event);
168
169
        return $app;
170
    }
171
172
    public function testInitialize()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function testInitialize()
Loading history...
173
    {
174
        $root = vfsStream::setup('rootDir', null, [
0 ignored issues
show
Coding Style introduced by
The opening parenthesis of a multi-line function call should be the last content on the line.
Loading history...
175
            '.env'   => '',
176
            'app'    => [
177
                'common.php'   => '',
178
                'event.php'    => '<?php return ["bind"=>[],"listen"=>[],"subscribe"=>[]];',
179
                'provider.php' => '<?php return [];',
180
            ],
181
            'config' => [
182
                'app.php' => '<?php return [];',
183
            ],
184
        ]);
0 ignored issues
show
Coding Style introduced by
For multi-line function calls, the closing parenthesis should be on a new line.

If a function call spawns multiple lines, the coding standard suggests to move the closing parenthesis to a new line:

someFunctionCall(
    $firstArgument,
    $secondArgument,
    $thirdArgument
); // Closing parenthesis on a new line.
Loading history...
185
186
        $app = $this->prepareAppForInitialize($root, true);
187
188
        $app->debug(false);
189
190
        $app->initialize();
191
192
        $this->assertIsInt($app->getBeginMem());
193
        $this->assertIsFloat($app->getBeginTime());
194
195
        $this->assertTrue($app->initialized());
196
    }
197
198
    public function testFactory()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function testFactory()
Loading history...
199
    {
200
        $this->assertInstanceOf(stdClass::class, App::factory(stdClass::class));
0 ignored issues
show
Deprecated Code introduced by
The function think\Container::factory() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

200
        $this->assertInstanceOf(stdClass::class, /** @scrutinizer ignore-deprecated */ App::factory(stdClass::class));
Loading history...
201
202
        $this->expectException(ClassNotFoundException::class);
203
204
        App::factory('SomeClass');
0 ignored issues
show
Deprecated Code introduced by
The function think\Container::factory() has been deprecated. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

204
        /** @scrutinizer ignore-deprecated */ App::factory('SomeClass');
Loading history...
205
    }
206
207
    public function testParseClass()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function testParseClass()
Loading history...
208
    {
209
        $this->assertEquals('app\\controller\\SomeClass', $this->app->parseClass('controller', 'some_class'));
210
        $this->app->setNamespace('app2');
211
        $this->assertEquals('app2\\controller\\SomeClass', $this->app->parseClass('controller', 'some_class'));
212
    }
213
214
}
215