8
8
9
9
trait AdditionalAssertions
10
10
{
11
- public function assertRouteUsesFormRequest (string $ routeName , string $ formRequest ): void
12
- {
13
- $ controllerAction = collect (Route::getRoutes ())->filter (function (\Illuminate \Routing \Route $ route ) use ($ routeName ) {
14
- return $ route ->getName () == $ routeName ;
15
- })->pluck ('action.controller ' );
16
-
17
- PHPUnitAssert::assertNotEmpty ($ controllerAction , 'Route " ' .$ routeName .'" is not defined. ' );
18
- PHPUnitAssert::assertCount (1 , $ controllerAction , 'Route " ' .$ routeName .'" is defined multiple times, route names should be unique. ' );
19
-
20
- $ controller = $ controllerAction ->first ();
21
- $ method = '__invoke ' ;
22
- if (strstr ($ controllerAction ->first (), '@ ' )) {
23
- [$ controller , $ method ] = explode ('@ ' , $ controllerAction ->first ());
24
- }
25
-
26
- $ this ->assertActionUsesFormRequest ($ controller , $ method , $ formRequest );
27
- }
28
-
29
11
public function assertActionUsesFormRequest (string $ controller , string $ method , string $ form_request ): void
30
12
{
31
13
PHPUnitAssert::assertTrue (is_subclass_of ($ form_request , 'Illuminate \\Foundation \\Http \\FormRequest ' ), $ form_request .' is not a type of Form Request ' );
@@ -75,6 +57,48 @@ public function assertActionUsesMiddleware($controller, $method, $middleware = n
75
57
}
76
58
}
77
59
60
+ public static function assertArrayStructure (array $ structure , array $ actual )
61
+ {
62
+ foreach ($ structure as $ key => $ type ) {
63
+ if (is_array ($ type ) && $ key === '* ' ) {
64
+ PHPUnitAssert::assertIsArray ($ actual );
65
+
66
+ foreach ($ actual as $ data ) {
67
+ static ::assertArrayStructure ($ structure ['* ' ], $ data );
68
+ }
69
+ } elseif (is_array ($ type ) && array_key_exists ($ key , $ structure )) {
70
+ if (is_array ($ structure [$ key ])) {
71
+ static ::assertArrayStructure ($ structure [$ key ], $ actual [$ key ]);
72
+ }
73
+ } else {
74
+ switch ($ type ) {
75
+ case 'string ' :
76
+ PHPUnitAssert::assertIsString ($ actual [$ key ]);
77
+ break ;
78
+ case 'integer ' :
79
+ PHPUnitAssert::assertIsInt ($ actual [$ key ]);
80
+ break ;
81
+ case 'number ' :
82
+ PHPUnitAssert::assertIsNumeric ($ actual [$ key ]);
83
+ break ;
84
+ case 'boolean ' :
85
+ PHPUnitAssert::assertIsBool ($ actual [$ key ]);
86
+ break ;
87
+ case 'array ' :
88
+ PHPUnitAssert::assertIsArray ($ actual [$ key ]);
89
+ break ;
90
+ default :
91
+ PHPUnitAssert::fail ('unexpected type: ' .$ type );
92
+ }
93
+ }
94
+ }
95
+ }
96
+
97
+ public function assertExactValidationRules (array $ expected , array $ actual ): void
98
+ {
99
+ PHPUnitAssert::assertEquals ($ this ->normalizeRules ($ expected ), $ this ->normalizeRules ($ actual ));
100
+ }
101
+
78
102
public function assertMiddlewareGroupUsesMiddleware (string $ middlewareGroup , array $ middlewares ): void
79
103
{
80
104
$ router = resolve (\Illuminate \Routing \Router::class);
@@ -88,6 +112,24 @@ public function assertMiddlewareGroupUsesMiddleware(string $middlewareGroup, arr
88
112
PHPUnitAssert::assertTrue (count ($ missingMiddlware ) === 0 , "Middlware Group ` $ middlewareGroup` does not use expected ` " .implode (', ' , $ missingMiddlware ).'` middleware(s) ' );
89
113
}
90
114
115
+ public function assertRouteUsesFormRequest (string $ routeName , string $ formRequest ): void
116
+ {
117
+ $ controllerAction = collect (Route::getRoutes ())->filter (function (\Illuminate \Routing \Route $ route ) use ($ routeName ) {
118
+ return $ route ->getName () == $ routeName ;
119
+ })->pluck ('action.controller ' );
120
+
121
+ PHPUnitAssert::assertNotEmpty ($ controllerAction , 'Route " ' .$ routeName .'" is not defined. ' );
122
+ PHPUnitAssert::assertCount (1 , $ controllerAction , 'Route " ' .$ routeName .'" is defined multiple times, route names should be unique. ' );
123
+
124
+ $ controller = $ controllerAction ->first ();
125
+ $ method = '__invoke ' ;
126
+ if (strstr ($ controllerAction ->first (), '@ ' )) {
127
+ [$ controller , $ method ] = explode ('@ ' , $ controllerAction ->first ());
128
+ }
129
+
130
+ $ this ->assertActionUsesFormRequest ($ controller , $ method , $ formRequest );
131
+ }
132
+
91
133
public function assertRouteUsesMiddleware (string $ routeName , array $ middlewares , bool $ exact = false ): void
92
134
{
93
135
$ router = resolve (\Illuminate \Routing \Router::class);
@@ -122,21 +164,6 @@ public function assertRouteUsesMiddleware(string $routeName, array $middlewares,
122
164
}
123
165
}
124
166
125
- public function createFormRequest (string $ form_request , array $ data = [])
126
- {
127
- return $ form_request ::createFromBase (SymfonyRequest::create ('' , 'POST ' , $ data ));
128
- }
129
-
130
- public function assertValidationRules (array $ expected , array $ actual ): void
131
- {
132
- \Illuminate \Testing \Assert::assertArraySubset ($ this ->normalizeRules ($ expected ), $ this ->normalizeRules ($ actual ));
133
- }
134
-
135
- public function assertExactValidationRules (array $ expected , array $ actual ): void
136
- {
137
- PHPUnitAssert::assertEquals ($ this ->normalizeRules ($ expected ), $ this ->normalizeRules ($ actual ));
138
- }
139
-
140
167
public function assertValidationRuleContains ($ rule , string $ class ): void
141
168
{
142
169
if (is_object ($ rule )) {
@@ -154,50 +181,23 @@ public function assertValidationRuleContains($rule, string $class): void
154
181
}
155
182
}
156
183
157
- public static function assertArrayStructure (array $ structure , array $ actual )
184
+ public function assertValidationRules (array $ expected , array $ actual ): void
158
185
{
159
- foreach ($ structure as $ key => $ type ) {
160
- if (is_array ($ type ) && $ key === '* ' ) {
161
- PHPUnitAssert::assertIsArray ($ actual );
162
-
163
- foreach ($ actual as $ data ) {
164
- static ::assertArrayStructure ($ structure ['* ' ], $ data );
165
- }
166
- } elseif (is_array ($ type ) && array_key_exists ($ key , $ structure )) {
167
- if (is_array ($ structure [$ key ])) {
168
- static ::assertArrayStructure ($ structure [$ key ], $ actual [$ key ]);
169
- }
170
- } else {
171
- switch ($ type ) {
172
- case 'string ' :
173
- PHPUnitAssert::assertIsString ($ actual [$ key ]);
174
- break ;
175
- case 'integer ' :
176
- PHPUnitAssert::assertIsInt ($ actual [$ key ]);
177
- break ;
178
- case 'number ' :
179
- PHPUnitAssert::assertIsNumeric ($ actual [$ key ]);
180
- break ;
181
- case 'boolean ' :
182
- PHPUnitAssert::assertIsBool ($ actual [$ key ]);
183
- break ;
184
- case 'array ' :
185
- PHPUnitAssert::assertIsArray ($ actual [$ key ]);
186
- break ;
187
- default :
188
- PHPUnitAssert::fail ('unexpected type: ' .$ type );
189
- }
190
- }
191
- }
186
+ \Illuminate \Testing \Assert::assertArraySubset ($ this ->normalizeRules ($ expected ), $ this ->normalizeRules ($ actual ));
192
187
}
193
188
194
- private function normalizeRules ( array $ rules )
189
+ public function createFormRequest ( string $ form_request , array $ data = [] )
195
190
{
196
- return array_map ([ $ this , 'expandRules ' ] , $ rules );
191
+ return $ form_request :: createFromBase (SymfonyRequest:: create ( '' , 'POST ' , $ data ) );
197
192
}
198
193
199
194
private function expandRules ($ rule )
200
195
{
201
196
return is_string ($ rule ) ? explode ('| ' , $ rule ) : $ rule ;
202
197
}
198
+
199
+ private function normalizeRules (array $ rules )
200
+ {
201
+ return array_map ([$ this , 'expandRules ' ], $ rules );
202
+ }
203
203
}
0 commit comments