1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author jan huang <[email protected]> |
4
|
|
|
* @copyright 2016 |
5
|
|
|
* |
6
|
|
|
* @see https://www.github.com/janhuang |
7
|
|
|
* @see https://fastdlabs.com |
8
|
|
|
*/ |
9
|
|
|
use FastD\Application; |
10
|
|
|
use FastD\TestCase; |
|
|
|
|
11
|
|
|
|
12
|
|
|
class IndexControllerTest extends TestCase |
|
|
|
|
13
|
|
|
{ |
14
|
|
|
public function createApplication() |
15
|
|
|
{ |
16
|
|
|
return new Application(__DIR__.'/../../default'); |
17
|
|
|
} |
18
|
|
|
|
19
|
|
|
public function testSayHello() |
20
|
|
|
{ |
21
|
|
|
$response = $this->app->handleRequest($this->request('GET', '/')); |
22
|
|
|
$this->equalsJson($response, ['foo' => 'bar']); |
23
|
|
|
$response = $this->app->handleRequest($this->request('GET', '/?foo=var')); |
24
|
|
|
$this->equalsJson($response, ['foo' => 'var']); |
25
|
|
|
$response = $this->app->handleRequest($this->request('GET', '/?foo=bar')); |
26
|
|
|
$this->equalsJson($response, ['foo' => 'bar']); |
27
|
|
|
} |
28
|
|
|
|
29
|
|
|
public function testDb() |
30
|
|
|
{ |
31
|
|
|
$response = $this->app->handleRequest($this->request('GET', '/db')); |
32
|
|
|
|
33
|
|
|
$this->equalsStatus($response, 200); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
View Code Duplication |
public function testHandleDynamicRequest() |
|
|
|
|
37
|
|
|
{ |
38
|
|
|
$response = $this->app->handleRequest($this->request('GET', '/foo/bar')); |
39
|
|
|
$this->equalsJson($response, ['foo' => 'bar']); |
40
|
|
|
$response = $this->app->handleRequest($this->request('GET', '/foo/foobar')); |
41
|
|
|
$this->equalsJson($response, ['foo' => 'foobar']); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
View Code Duplication |
public function testHandleMiddlewareRequest() |
|
|
|
|
45
|
|
|
{ |
46
|
|
|
$response = $this->app->handleRequest($this->request('POST', '/foo/bar')); |
47
|
|
|
$this->equalsJson($response, ['foo' => 'middleware']); |
48
|
|
|
$response = $this->app->handleRequest($this->request('POST', '/foo/not')); |
49
|
|
|
$this->equalsJson($response, ['foo' => 'bar']); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
public function testModel() |
53
|
|
|
{ |
54
|
|
|
$response = $this->app->handleRequest($this->request('GET', '/model')); |
55
|
|
|
$this->assertEquals(200, $response->getStatusCode()); |
56
|
|
|
$this->isSuccessful($response); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function testAuth() |
60
|
|
|
{ |
61
|
|
|
$response = $this->app->handleRequest($this->request('GET', '/auth')); |
62
|
|
|
|
63
|
|
|
$this->assertEquals(401, $response->getStatusCode()); |
64
|
|
|
|
65
|
|
|
$this->equalsJson($response, [ |
66
|
|
|
'msg' => 'not allow access', |
67
|
|
|
'code' => 401, |
68
|
|
|
]); |
69
|
|
|
|
70
|
|
|
$response = $this->app->handleRequest($this->request('GET', 'http://foo:[email protected]/auth', [ |
71
|
|
|
'PHP_AUTH_USER' => 'foo', |
72
|
|
|
'PHP_AUTH_PW' => 'bar', |
73
|
|
|
])); |
74
|
|
|
|
75
|
|
|
$this->assertEquals(200, $response->getStatusCode()); |
76
|
|
|
|
77
|
|
|
$this->equalsJson($response, [ |
78
|
|
|
'foo' => 'bar', |
79
|
|
|
]); |
80
|
|
|
} |
81
|
|
|
} |
82
|
|
|
|
Let’s assume that you have a directory layout like this:
and let’s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: