1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @author jan huang <[email protected]> |
4
|
|
|
* @copyright 2016 |
5
|
|
|
* |
6
|
|
|
* @see https://www.github.com/janhuang |
7
|
|
|
* @see http://www.fast-d.cn/ |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace FastD; |
11
|
|
|
|
12
|
|
|
use FastD\Http\Response; |
13
|
|
|
use FastD\Testing\WebTestCase; |
14
|
|
|
use PHPUnit_Extensions_Database_DataSet_ArrayDataSet; |
15
|
|
|
use PHPUnit_Extensions_Database_DataSet_CompositeDataSet; |
16
|
|
|
use PHPUnit_Extensions_Database_DataSet_IDataSet; |
17
|
|
|
use PHPUnit_Extensions_Database_DB_IDatabaseConnection; |
18
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Class TestCase. |
22
|
|
|
*/ |
23
|
|
|
class TestCase extends WebTestCase |
24
|
|
|
{ |
25
|
|
|
/** |
26
|
|
|
* @var Application |
27
|
|
|
*/ |
28
|
|
|
protected $app; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Set up unit. |
32
|
|
|
*/ |
33
|
54 |
|
public function setUp() |
34
|
|
|
{ |
35
|
54 |
|
$this->app = $this->createApplication(); |
36
|
54 |
|
parent::setUp(); |
37
|
54 |
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @return Application |
41
|
|
|
*/ |
42
|
|
|
public function createApplication() |
43
|
|
|
{ |
44
|
|
|
return new Application(getcwd()); |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* @param ServerRequestInterface $request |
49
|
|
|
* @param array $params |
50
|
|
|
* @param array $headers |
51
|
|
|
* |
52
|
|
|
* @return Response |
53
|
|
|
*/ |
54
|
|
|
public function handleRequest(ServerRequestInterface $request, array $params = [], array $headers = []) |
55
|
|
|
{ |
56
|
|
|
if (!empty($params)) { |
57
|
|
|
if ('GET' === $request->getMethod()) { |
58
|
|
|
$request->withQueryParams($params); |
59
|
|
|
} elseif ('POST' === $request->getMethod()) { |
60
|
|
|
$request->withParsedBody($params); |
61
|
|
|
} else { |
62
|
|
|
$request->getBody()->write(http_build_query($params)); |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
if (!empty($headers)) { |
67
|
|
|
foreach ($headers as $name => $header) { |
68
|
|
|
$request->withAddedHeader($name, $header); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $this->app->handleRequest($request); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* Returns the test database connection. |
77
|
|
|
* |
78
|
|
|
* @return PHPUnit_Extensions_Database_DB_IDatabaseConnection |
79
|
|
|
*/ |
80
|
54 |
|
protected function getConnection() |
81
|
|
|
{ |
82
|
|
|
try { |
83
|
54 |
|
$connection = env('connection'); |
84
|
54 |
|
if (!$connection) { |
85
|
54 |
|
$connection = 'default'; |
86
|
54 |
|
} |
87
|
|
|
|
88
|
54 |
|
return $this->createDefaultDBConnection(database($connection)->pdo); |
|
|
|
|
89
|
19 |
|
} catch (\Exception $exception) { |
90
|
19 |
|
return null; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Returns the test dataset. |
96
|
|
|
* |
97
|
|
|
* @return PHPUnit_Extensions_Database_DataSet_IDataSet |
98
|
|
|
*/ |
99
|
35 |
|
protected function getDataSet() |
100
|
|
|
{ |
101
|
35 |
|
$path = app()->getPath().'/database/dataset/*'; |
102
|
|
|
|
103
|
35 |
|
$composite = new PHPUnit_Extensions_Database_DataSet_CompositeDataSet(); |
104
|
|
|
|
105
|
35 |
|
foreach (glob($path) as $file) { |
106
|
|
|
$dataSet = load($file); |
107
|
|
|
if (empty($dataSet)) { |
108
|
|
|
$dataSet = []; |
109
|
|
|
} |
110
|
|
|
$tableName = pathinfo($file, PATHINFO_FILENAME); |
111
|
|
|
$composite->addDataSet( |
112
|
|
|
new PHPUnit_Extensions_Database_DataSet_ArrayDataSet( |
113
|
|
|
[ |
114
|
|
|
$tableName => $dataSet, |
115
|
|
|
] |
116
|
|
|
) |
117
|
|
|
); |
118
|
35 |
|
} |
119
|
|
|
|
120
|
35 |
|
return $composite; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.