diff --git a/documentation.md b/documentation.md index a7cbf282..61aabf5b 100644 --- a/documentation.md +++ b/documentation.md @@ -220,6 +220,21 @@ Authenticates user for HTTP_AUTH * `param` $password +### amOnAction + +Opens web page by action name + +``` php +amOnAction('PostController::index'); +$I->amOnAction('HomeController'); +$I->amOnAction('ArticleController', ['slug' => 'lorem-ipsum']); +``` + + * `param string` $action + * `param array` $params + + ### amOnPage Opens the page for the given relative URI. diff --git a/src/Codeception/Module/Symfony.php b/src/Codeception/Module/Symfony.php index 39756533..dfefc031 100644 --- a/src/Codeception/Module/Symfony.php +++ b/src/Codeception/Module/Symfony.php @@ -13,6 +13,7 @@ use Symfony\Component\Finder\Finder; use Symfony\Component\DependencyInjection\ContainerInterface; use Symfony\Component\Finder\SplFileInfo; +use Symfony\Component\Routing\Generator\UrlGeneratorInterface; use Symfony\Component\VarDumper\Cloner\Data; use Symfony\Bundle\FrameworkBundle\Console\Application; use Symfony\Component\Console\Input\ArrayInput; @@ -770,4 +771,45 @@ private function getPossibleKernelClasses() return [$this->config['kernel_class']]; } + + /** + * Opens web page by action name + * + * ``` php + * amOnAction('PostController::index'); + * $I->amOnAction('HomeController'); + * $I->amOnAction('ArticleController', ['slug' => 'lorem-ipsum']); + * ``` + * + * @param string $action + * @param array $params + */ + public function amOnAction($action, $params = []) + { + $container = $this->_getContainer(); + + if (!$container->has('router')) { + $this->fail("Symfony container doesn't have 'router' service"); + return; + } + + $router = $this->grabService('router'); + + $routes = $router->getRouteCollection()->getIterator(); + + foreach ($routes as $route) { + $controller = basename($route->getDefault('_controller')); + if ($controller === $action) { + $resource = $router->match($route->getPath()); + $url = $router->generate( + $resource['_route'], + $params, + UrlGeneratorInterface::ABSOLUTE_PATH + ); + $this->amOnPage($url); + return; + } + } + } }