Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
67 / 67 |
|
100.00% |
15 / 15 |
CRAP | |
100.00% |
1 / 1 |
TestCase | |
100.00% |
67 / 67 |
|
100.00% |
15 / 15 |
15 | |
100.00% |
1 / 1 |
setUp | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
prepareDefaults | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
assertResponseStatus | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
assertResponseStatusCode | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
assertResponseStatusReason | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
assertResponseBodyContains | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
assertResponseBodyNotContains | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
assertResponseHeader | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
assertResponseContainsHeader | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
assertResponseNotContainsHeader | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
assertMatchedRouteName | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
assertStderrContains | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
assertStderrNotContains | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
assertStdoutContains | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
assertStdoutNotContains | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 |
1 | <?php declare(strict_types=1); |
2 | /* |
3 | * This file is part of Aplus Framework Testing Library. |
4 | * |
5 | * (c) Natan Felles <natanfelles@gmail.com> |
6 | * |
7 | * For the full copyright and license information, please view the LICENSE |
8 | * file that was distributed with this source code. |
9 | */ |
10 | namespace Framework\Testing; |
11 | |
12 | use Framework\CLI\Streams\Stderr; |
13 | use Framework\CLI\Streams\Stdout; |
14 | use Framework\Config\Config; |
15 | use Framework\MVC\App; |
16 | use Framework\Testing\Constraints\MatchedRouteName; |
17 | use Framework\Testing\Constraints\ResponseBodyContains; |
18 | use Framework\Testing\Constraints\ResponseBodyNotContains; |
19 | use Framework\Testing\Constraints\ResponseContainsHeader; |
20 | use Framework\Testing\Constraints\ResponseHeader; |
21 | use Framework\Testing\Constraints\ResponseNotContainsHeader; |
22 | use Framework\Testing\Constraints\ResponseStatus; |
23 | use Framework\Testing\Constraints\ResponseStatusCode; |
24 | use Framework\Testing\Constraints\ResponseStatusReason; |
25 | use Framework\Testing\Constraints\StderrContains; |
26 | use Framework\Testing\Constraints\StderrNotContains; |
27 | use Framework\Testing\Constraints\StdoutContains; |
28 | use Framework\Testing\Constraints\StdoutNotContains; |
29 | use PHPUnit\Framework\TestCase as PHPUnitTestCase; |
30 | |
31 | /** |
32 | * Class TestCase. |
33 | * |
34 | * @package testing |
35 | */ |
36 | abstract class TestCase extends PHPUnitTestCase |
37 | { |
38 | /** |
39 | * @var array<string,mixed>|string|null |
40 | */ |
41 | protected array | string | null $configs = null; |
42 | protected Config $config; |
43 | protected AppTesting $app; |
44 | |
45 | protected function setUp() : void |
46 | { |
47 | $this->prepareDefaults(); |
48 | } |
49 | |
50 | protected function prepareDefaults() : void |
51 | { |
52 | $this->app = new AppTesting($this->config ?? new Config($this->configs)); |
53 | } |
54 | |
55 | public static function assertResponseStatus(string $status, string $message = '') : void |
56 | { |
57 | self::assertThat( |
58 | $status, |
59 | new ResponseStatus(App::response()->getStatus()), |
60 | $message |
61 | ); |
62 | } |
63 | |
64 | public static function assertResponseStatusCode(int $code, string $message = '') : void |
65 | { |
66 | self::assertThat( |
67 | $code, |
68 | new ResponseStatusCode(App::response()->getStatusCode()), |
69 | $message |
70 | ); |
71 | } |
72 | |
73 | public static function assertResponseStatusReason(string $reason, string $message = '') : void |
74 | { |
75 | self::assertThat( |
76 | $reason, |
77 | new ResponseStatusReason(App::response()->getStatusReason()), |
78 | $message |
79 | ); |
80 | } |
81 | |
82 | public static function assertResponseBodyContains(string $string, string $message = '') : void |
83 | { |
84 | self::assertThat( |
85 | App::response()->getBody(), |
86 | new ResponseBodyContains($string), |
87 | $message |
88 | ); |
89 | } |
90 | |
91 | public static function assertResponseBodyNotContains(string $string, string $message = '') : void |
92 | { |
93 | self::assertThat( |
94 | App::response()->getBody(), |
95 | new ResponseBodyNotContains($string), |
96 | $message |
97 | ); |
98 | } |
99 | |
100 | public static function assertResponseHeader(string $name, string $value, string $message = '') : void |
101 | { |
102 | self::assertThat( |
103 | $value, |
104 | new ResponseHeader(App::response()->getHeader($name), $name), |
105 | $message |
106 | ); |
107 | } |
108 | |
109 | public static function assertResponseContainsHeader(string $name, string $message = '') : void |
110 | { |
111 | self::assertThat( |
112 | App::response()->getHeader($name), |
113 | new ResponseContainsHeader($name), |
114 | $message |
115 | ); |
116 | } |
117 | |
118 | public static function assertResponseNotContainsHeader(string $name, string $message = '') : void |
119 | { |
120 | self::assertThat( |
121 | App::response()->getHeader($name), |
122 | new ResponseNotContainsHeader($name), |
123 | $message |
124 | ); |
125 | } |
126 | |
127 | public static function assertMatchedRouteName(?string $name, string $message = '') : void |
128 | { |
129 | self::assertThat( |
130 | $name, |
131 | new MatchedRouteName(App::router()->getMatchedRoute()?->getName()), |
132 | $message |
133 | ); |
134 | } |
135 | |
136 | public static function assertStderrContains(string $string, string $message = '') : void |
137 | { |
138 | self::assertThat( |
139 | Stderr::getContents(), |
140 | new StderrContains($string), |
141 | $message |
142 | ); |
143 | } |
144 | |
145 | public static function assertStderrNotContains(string $string, string $message = '') : void |
146 | { |
147 | self::assertThat( |
148 | Stderr::getContents(), |
149 | new StderrNotContains($string), |
150 | $message |
151 | ); |
152 | } |
153 | |
154 | public static function assertStdoutContains(string $string, string $message = '') : void |
155 | { |
156 | self::assertThat( |
157 | Stdout::getContents(), |
158 | new StdoutContains($string), |
159 | $message |
160 | ); |
161 | } |
162 | |
163 | public static function assertStdoutNotContains(string $string, string $message = '') : void |
164 | { |
165 | self::assertThat( |
166 | Stdout::getContents(), |
167 | new StdoutNotContains($string), |
168 | $message |
169 | ); |
170 | } |
171 | } |