Created
March 7, 2018 14:08
-
-
Save jrseliga/b511faae762afeadac2c15aa52ca767a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
abstract class ControllerBaseTest extends TestCase | |
{ | |
use RefreshDatabase; | |
protected $user; | |
/** | |
* Assert route mapped to expected Controller. | |
* | |
* @param \Illuminate\Routing\Route $route | |
* @param string $class | |
*/ | |
protected function assertRouteMappedToExpectedController(Route $route, $class) | |
{ | |
$this->assertSame($class, get_class($route->getController())); | |
} | |
/** | |
* Assert route mapped to expected controller method. | |
* | |
* @param \Illuminate\Routing\Route $route | |
* @param string $action_method | |
*/ | |
protected function assertRouteMappedToExpectedControllerActionMethod(Route $route, $action_method) | |
{ | |
$this->assertSame($action_method, $route->getActionMethod()); | |
} | |
/** | |
* Assert the route was found. | |
* | |
* @param \Illuminate\Routing\Route $route | |
*/ | |
protected function assertRouteFound(Route $route) | |
{ | |
$this->assertInstanceOf(Route::class, $route); | |
} | |
/** | |
* Assert the route was found. | |
* | |
* @param \Illuminate\Routing\Route $route | |
*/ | |
protected function assertRouteUsesMethod(Route $route, $method) | |
{ | |
$this->assertContains($method, $route->methods()); | |
} | |
/** | |
* Assert the route is assigned to the expected middleware | |
* | |
* @param Route $route | |
* @param $middleware | |
*/ | |
protected function assertAssignedToMiddleware(Route $route, $middleware) | |
{ | |
collect($middleware)->each(function ($middleware) use ($route) { | |
$this->assertContains($middleware, $route->middleware()); | |
}); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class VocabulariesControllerTest extends ControllerBaseTest | |
{ | |
/** | |
* Route names the Controller responds to. | |
*/ | |
protected $routes = [ | |
'vocabularies.index' => [ | |
'method' => 'GET', | |
], | |
'vocabularies.show' => [ | |
'method' => 'GET', | |
], | |
]; | |
/** | |
* Ensure routes are registered. | |
*/ | |
public function testRoutesRegistered() | |
{ | |
collect($this->routes)->each(function ($attributes, $route) { | |
$route = app('router')->getRoutes()->getByName($route); | |
$this->assertRouteFound($route); | |
$this->assertRouteUsesMethod($route, $attributes['method']); | |
$this->assertRouteMappedToExpectedController($route, VocabulariesController::class); | |
$this->assertAssignedToMiddleware($route, 'admin'); | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment