Passed
Push — 6.0 ( 167988...10543c )
by
unknown
02:33
created

AppTest::testParseClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
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
1 ignored issue
show
Coding Style introduced by
Missing doc comment for class SomeService
Loading history...
17
{
18
19
    public function register()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function register()
Loading history...
20
    {
21
22
    }
23
24
    public function boot()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function boot()
Loading history...
25
    {
26
27
    }
28
}
29
30
/**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
31
 * @property array initializers
32
 */
5 ignored issues
show
Coding Style introduced by
Missing @category tag in class comment
Loading history...
Coding Style introduced by
Missing @package tag in class comment
Loading history...
Coding Style introduced by
Missing @author tag in class comment
Loading history...
Coding Style introduced by
Missing @license tag in class comment
Loading history...
Coding Style introduced by
Missing @link tag in class comment
Loading history...
33
class AppTest extends TestCase
34
{
35
    /** @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...
36
    protected $app;
37
38
    protected function setUp()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function setUp()
Loading history...
39
    {
40
        $this->app = new App();
41
    }
42
43
    protected function tearDown(): void
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function tearDown()
Loading history...
44
    {
45
        m::close();
46
    }
47
48
    public function testService()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function testService()
Loading history...
49
    {
50
        $this->app->register(stdClass::class);
51
52
        $this->assertInstanceOf(stdClass::class, $this->app->getService(stdClass::class));
53
54
        $service = m::mock(SomeService::class);
55
56
        $service->shouldReceive('register')->once();
57
58
        $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

58
        $this->app->register(/** @scrutinizer ignore-type */ $service);
Loading history...
59
60
        $this->assertEquals($service, $this->app->getService(SomeService::class));
61
62
        $service2 = m::mock(SomeService::class);
63
64
        $service2->shouldReceive('register')->once();
65
66
        $this->app->register($service2);
67
68
        $this->assertEquals($service, $this->app->getService(SomeService::class));
69
70
        $this->app->register($service2, true);
71
72
        $this->assertEquals($service2, $this->app->getService(SomeService::class));
73
74
        $service->shouldReceive('boot')->once();
75
        $service2->shouldReceive('boot')->once();
76
77
        $this->app->boot();
78
    }
79
80
    public function testDebug()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function testDebug()
Loading history...
81
    {
82
        $this->app->debug(false);
83
84
        $this->assertFalse($this->app->isDebug());
85
86
        $this->app->debug(true);
87
88
        $this->assertTrue($this->app->isDebug());
89
    }
90
91
    public function testNamespace()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function testNamespace()
Loading history...
92
    {
93
        $namespace = 'test';
94
95
        $this->app->setNamespace($namespace);
96
97
        $this->assertEquals($namespace, $this->app->getNamespace());
98
    }
99
100
    public function testVersion()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function testVersion()
Loading history...
101
    {
102
        $this->assertEquals(App::VERSION, $this->app->version());
103
    }
104
105
    public function testPath()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function testPath()
Loading history...
106
    {
107
        $rootPath = __DIR__ . DIRECTORY_SEPARATOR;
108
109
        $app = new App($rootPath);
110
111
        $this->assertEquals($rootPath, $app->getRootPath());
112
113
        $this->assertEquals(dirname(__DIR__) . DIRECTORY_SEPARATOR . 'src' . DIRECTORY_SEPARATOR, $app->getThinkPath());
114
115
        $this->assertEquals($rootPath . 'app' . DIRECTORY_SEPARATOR, $app->getAppPath());
116
117
        $appPath = $rootPath . 'app' . DIRECTORY_SEPARATOR . 'admin' . DIRECTORY_SEPARATOR;
118
        $app->setAppPath($appPath);
119
        $this->assertEquals($appPath, $app->getAppPath());
120
121
        $this->assertEquals($rootPath . 'app' . DIRECTORY_SEPARATOR, $app->getBasePath());
122
123
        $this->assertEquals($rootPath . 'config' . DIRECTORY_SEPARATOR, $app->getConfigPath());
124
125
        $this->assertEquals($rootPath . 'runtime' . DIRECTORY_SEPARATOR, $app->getRuntimePath());
126
127
        $runtimePath = $rootPath . 'runtime' . DIRECTORY_SEPARATOR . 'admin' . DIRECTORY_SEPARATOR;
128
        $app->setRuntimePath($runtimePath);
129
        $this->assertEquals($runtimePath, $app->getRuntimePath());
130
    }
131
132
    /**
0 ignored issues
show
Coding Style introduced by
Missing short description in doc comment
Loading history...
133
     * @param vfsStreamDirectory $root
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
134
     * @param bool               $debug
1 ignored issue
show
Coding Style introduced by
Missing parameter comment
Loading history...
Coding Style introduced by
Tag value for @param tag indented incorrectly; expected 2 spaces but found 1
Loading history...
135
     * @return App
136
     */
137
    protected function prepareAppForInitialize(vfsStreamDirectory $root, $debug = true)
138
    {
139
        $rootPath = $root->url() . DIRECTORY_SEPARATOR;
140
141
        $app = new App($rootPath);
142
143
        $initializer = m::mock();
144
        $initializer->shouldReceive('init')->once()->with($app);
145
146
        $app->instance($initializer->mockery_getName(), $initializer);
147
148
        (function () use ($initializer) {
149
            $this->initializers = [$initializer->mockery_getName()];
150
        })->call($app);
151
152
        $env = m::mock(Env::class);
153
        $env->shouldReceive('load')->once()->with($rootPath . '.env');
154
        $env->shouldReceive('get')->once()->with('config_ext', '.php')->andReturn('.php');
155
        $env->shouldReceive('get')->once()->with('app_debug')->andReturn($debug);
156
157
        $event = m::mock(Event::class);
158
        $event->shouldReceive('trigger')->once()->with('AppInit');
159
        $event->shouldReceive('bind')->once()->with([]);
160
        $event->shouldReceive('listenEvents')->once()->with([]);
161
        $event->shouldReceive('subscribe')->once()->with([]);
162
163
        $app->instance('env', $env);
164
        $app->instance('event', $event);
165
166
        return $app;
167
    }
168
169
    public function testInitialize()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function testInitialize()
Loading history...
170
    {
171
        $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...
172
            '.env'   => '',
173
            'app'    => [
174
                'common.php'   => '',
175
                'event.php'    => '<?php return ["bind"=>[],"listen"=>[],"subscribe"=>[]];',
176
                'provider.php' => '<?php return [];',
177
            ],
178
            'config' => [
179
                'app.php' => '<?php return [];',
180
            ],
181
        ]);
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...
182
183
        $app = $this->prepareAppForInitialize($root, true);
184
185
        $app->debug(false);
186
187
        $app->initialize();
188
189
        $this->assertIsInt($app->getBeginMem());
190
        $this->assertIsFloat($app->getBeginTime());
191
192
        $this->assertTrue($app->initialized());
193
    }
194
195
    public function testParseName()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function testParseName()
Loading history...
196
    {
197
        $this->assertEquals('SomeName', App::parseName('some_name', 1));
198
        $this->assertEquals('someName', App::parseName('some_name', 1, false));
199
        $this->assertEquals('some_name', App::parseName('SomeName'));
200
        $this->assertEquals('some_name', App::parseName('someName'));
201
    }
202
203
    public function testClassBaseName()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function testClassBaseName()
Loading history...
204
    {
205
        $this->assertEquals('stdClass', App::classBaseName(stdClass::class));
206
        $this->assertEquals('App', App::classBaseName($this->app));
207
    }
208
209
    public function testFactory()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function testFactory()
Loading history...
210
    {
211
        $this->assertInstanceOf(stdClass::class, App::factory(stdClass::class));
212
213
        $this->expectException(ClassNotFoundException::class);
214
215
        App::factory('SomeClass');
216
    }
217
218
    public function testParseClass()
0 ignored issues
show
Coding Style introduced by
Missing doc comment for function testParseClass()
Loading history...
219
    {
220
        $this->assertEquals('app\\controller\\SomeClass', $this->app->parseClass('controller', 'some_class'));
221
        $this->app->setNamespace('app2');
222
        $this->assertEquals('app2\\controller\\SomeClass', $this->app->parseClass('controller', 'some_class'));
223
    }
224
225
}