88
99trait AdditionalAssertions
1010{
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-
2911 public function assertActionUsesFormRequest (string $ controller , string $ method , string $ form_request ): void
3012 {
3113 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
7557 }
7658 }
7759
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+
78102 public function assertMiddlewareGroupUsesMiddleware (string $ middlewareGroup , array $ middlewares ): void
79103 {
80104 $ router = resolve (\Illuminate \Routing \Router::class);
@@ -88,6 +112,24 @@ public function assertMiddlewareGroupUsesMiddleware(string $middlewareGroup, arr
88112 PHPUnitAssert::assertTrue (count ($ missingMiddlware ) === 0 , "Middlware Group ` $ middlewareGroup` does not use expected ` " .implode (', ' , $ missingMiddlware ).'` middleware(s) ' );
89113 }
90114
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+
91133 public function assertRouteUsesMiddleware (string $ routeName , array $ middlewares , bool $ exact = false ): void
92134 {
93135 $ router = resolve (\Illuminate \Routing \Router::class);
@@ -122,21 +164,6 @@ public function assertRouteUsesMiddleware(string $routeName, array $middlewares,
122164 }
123165 }
124166
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-
140167 public function assertValidationRuleContains ($ rule , string $ class ): void
141168 {
142169 if (is_object ($ rule )) {
@@ -154,50 +181,23 @@ public function assertValidationRuleContains($rule, string $class): void
154181 }
155182 }
156183
157- public static function assertArrayStructure (array $ structure , array $ actual )
184+ public function assertValidationRules (array $ expected , array $ actual ): void
158185 {
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 ));
192187 }
193188
194- private function normalizeRules ( array $ rules )
189+ public function createFormRequest ( string $ form_request , array $ data = [] )
195190 {
196- return array_map ([ $ this , 'expandRules ' ] , $ rules );
191+ return $ form_request :: createFromBase (SymfonyRequest:: create ( '' , 'POST ' , $ data ) );
197192 }
198193
199194 private function expandRules ($ rule )
200195 {
201196 return is_string ($ rule ) ? explode ('| ' , $ rule ) : $ rule ;
202197 }
198+
199+ private function normalizeRules (array $ rules )
200+ {
201+ return array_map ([$ this , 'expandRules ' ], $ rules );
202+ }
203203}
0 commit comments