From d09a588e33f1cbf31e4f3677baa1a2a8c6aa7b54 Mon Sep 17 00:00:00 2001 From: Tavo Nieves J Date: Fri, 11 Sep 2020 15:39:23 -0500 Subject: [PATCH 1/2] Added amOnAction function --- documentation.md | 15 +++++++++++ src/Codeception/Module/Symfony.php | 41 ++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) 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..eaaf87a9 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,44 @@ 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')) { + 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; + } + } + } } From 5b2f3d4d6645a01b3472495d8221765e91c7a08b Mon Sep 17 00:00:00 2001 From: Tavo Nieves J Date: Fri, 11 Sep 2020 21:59:43 -0500 Subject: [PATCH 2/2] fails if you don't have the necessary services --- src/Codeception/Module/Symfony.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Codeception/Module/Symfony.php b/src/Codeception/Module/Symfony.php index eaaf87a9..dfefc031 100644 --- a/src/Codeception/Module/Symfony.php +++ b/src/Codeception/Module/Symfony.php @@ -790,6 +790,7 @@ public function amOnAction($action, $params = []) $container = $this->_getContainer(); if (!$container->has('router')) { + $this->fail("Symfony container doesn't have 'router' service"); return; }