|
2 | 2 |
|
3 | 3 | namespace JMac\Testing\Traits;
|
4 | 4 |
|
| 5 | +use Illuminate\Foundation\Testing\Assert as LaravelAssert; |
5 | 6 | use Illuminate\Support\Facades\Route;
|
6 |
| -use PHPUnit\Framework\Assert; |
| 7 | +use PHPUnit\Framework\Assert as PHPUnitAssert; |
| 8 | +use Symfony\Component\HttpFoundation\Request as SymfonyRequest; |
7 | 9 |
|
8 | 10 | trait HttpTestAssertions
|
9 | 11 | {
|
10 |
| - public function assertRouteUsesFormRequest(string $routeName, string $formRequest) { |
11 |
| - $controllerAction = collect(Route::getRoutes())->filter(function (\Illuminate\Routing\Route $route) use ($routeName) { |
12 |
| - return $route->getName() == $routeName; |
13 |
| - })->pluck('action.controller'); |
| 12 | + public function assertRouteUsesFormRequest(string $routeName, string $formRequest) |
| 13 | + { |
| 14 | + $controllerAction = collect(Route::getRoutes())->filter(function (\Illuminate\Routing\Route $route) use ($routeName) { |
| 15 | + return $route->getName() == $routeName; |
| 16 | + })->pluck('action.controller'); |
14 | 17 |
|
15 |
| - Assert::assertNotEmpty($controllerAction, 'Route "' . $routeName . '" is not defined.'); |
16 |
| - Assert::assertCount(1, $controllerAction, 'Route "' . $routeName . '" is defined multiple times, route names should be unique.'); |
| 18 | + PHPUnitAssert::assertNotEmpty($controllerAction, 'Route "' . $routeName . '" is not defined.'); |
| 19 | + PHPUnitAssert::assertCount(1, $controllerAction, 'Route "' . $routeName . '" is defined multiple times, route names should be unique.'); |
17 | 20 |
|
18 |
| - [$controller, $method] = explode('@', $controllerAction->first()); |
| 21 | + [$controller, $method] = explode('@', $controllerAction->first()); |
19 | 22 |
|
20 |
| - $this->assertActionUsesFormRequest($controller, $method, $formRequest); |
21 |
| - } |
| 23 | + $this->assertActionUsesFormRequest($controller, $method, $formRequest); |
| 24 | + } |
22 | 25 |
|
23 | 26 | public function assertActionUsesFormRequest(string $controller, string $method, string $form_request)
|
24 | 27 | {
|
25 |
| - Assert::assertTrue(is_subclass_of($form_request, 'Illuminate\\Foundation\\Http\\FormRequest'), $form_request . ' is not a type of Form Request'); |
| 28 | + PHPUnitAssert::assertTrue(is_subclass_of($form_request, 'Illuminate\\Foundation\\Http\\FormRequest'), $form_request . ' is not a type of Form Request'); |
26 | 29 |
|
27 | 30 | try {
|
28 | 31 | $reflector = new \ReflectionClass($controller);
|
29 | 32 | $action = $reflector->getMethod($method);
|
30 | 33 | } catch (\ReflectionException $exception) {
|
31 |
| - Assert::fail('Controller action could not be found: ' . $controller . '@' . $method); |
| 34 | + PHPUnitAssert::fail('Controller action could not be found: ' . $controller . '@' . $method); |
32 | 35 | }
|
33 | 36 |
|
34 |
| - Assert::assertTrue($action->isPublic(), 'Action "' . $method . '" is not public, controller actions must be public.'); |
| 37 | + PHPUnitAssert::assertTrue($action->isPublic(), 'Action "' . $method . '" is not public, controller actions must be public.'); |
35 | 38 |
|
36 | 39 | $actual = collect($action->getParameters())->contains(function ($parameter) use ($form_request) {
|
37 | 40 | return $parameter->getType() instanceof \ReflectionNamedType && $parameter->getType()->getName() === $form_request;
|
38 | 41 | });
|
39 | 42 |
|
40 |
| - Assert::assertTrue($actual, 'Action "' . $method . '" does not have validation using the "' . $form_request . '" Form Request.'); |
| 43 | + PHPUnitAssert::assertTrue($actual, 'Action "' . $method . '" does not have validation using the "' . $form_request . '" Form Request.'); |
41 | 44 | }
|
42 | 45 |
|
43 | 46 | public function assertActionUsesMiddleware($controller, $method, $middleware)
|
44 | 47 | {
|
45 | 48 | $router = resolve(\Illuminate\Routing\Router::class);
|
46 | 49 | $route = $router->getRoutes()->getByAction($controller . '@' . $method);
|
47 | 50 |
|
48 |
| - Assert::assertNotNull($route, 'Unable to find route for controller action (' . $controller . '@' . $method . ')'); |
| 51 | + PHPUnitAssert::assertNotNull($route, 'Unable to find route for controller action (' . $controller . '@' . $method . ')'); |
49 | 52 |
|
50 | 53 | if (is_array($middleware)) {
|
51 |
| - Assert::assertSame([], array_diff($middleware, $route->gatherMiddleware()), 'Controller action does not use middleware (' . implode(', ', $middleware) . ')'); |
| 54 | + PHPUnitAssert::assertSame([], array_diff($middleware, $route->gatherMiddleware()), 'Controller action does not use middleware (' . implode(', ', $middleware) . ')'); |
52 | 55 | } else {
|
53 |
| - Assert::assertTrue(in_array($middleware, $route->gatherMiddleware()), 'Controller action does not use middleware (' . $middleware . ')'); |
| 56 | + PHPUnitAssert::assertTrue(in_array($middleware, $route->gatherMiddleware()), 'Controller action does not use middleware (' . $middleware . ')'); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + public function createFormRequest(string $form_request, array $data = []) |
| 61 | + { |
| 62 | + return $form_request::createFromBase(SymfonyRequest::create(null, 'POST', $data)); |
| 63 | + } |
| 64 | + |
| 65 | + public function assertValidationRules(array $expected, array $actual) |
| 66 | + { |
| 67 | + LaravelAssert::assertArraySubset($this->normalizeRules($expected), $this->normalizeRules($actual)); |
| 68 | + } |
| 69 | + |
| 70 | + public function assertExactValidationRules(array $expected, array $actual) |
| 71 | + { |
| 72 | + PHPUnitAssert::assertEquals($this->normalizeRules($expected), $this->normalizeRules($actual)); |
| 73 | + } |
| 74 | + |
| 75 | + public function assertValidationRuleContains($rule, string $class) |
| 76 | + { |
| 77 | + if (is_object($rule)) { |
| 78 | + PHPUnitAssert::assertInstanceOf($rule, $class); |
| 79 | + |
| 80 | + return; |
| 81 | + } |
| 82 | + |
| 83 | + $matches = array_filter($this->expandRules($rule), function ($rule) use ($class) { |
| 84 | + return $rule instanceof $class; |
| 85 | + }); |
| 86 | + |
| 87 | + if (empty($matches)) { |
| 88 | + PHPUnitAssert::fail('Failed asserting rule contains ' . $class); |
54 | 89 | }
|
55 | 90 | }
|
| 91 | + |
| 92 | + private function normalizeRules(array $rules) |
| 93 | + { |
| 94 | + return array_map([$this, 'expandRules'], $rules); |
| 95 | + } |
| 96 | + |
| 97 | + private function expandRules($rule) |
| 98 | + { |
| 99 | + return is_string($rule) ? explode('|', $rule) : $rule; |
| 100 | + } |
56 | 101 | }
|
0 commit comments