From a3014b6344fe750d89acf73e2936de1665d7bdc6 Mon Sep 17 00:00:00 2001 From: Tavo Nieves J Date: Sat, 12 Sep 2020 10:20:36 -0500 Subject: [PATCH] Added seeCurrentActionIs function --- documentation.md | 13 +++++++++++ src/Codeception/Module/Symfony.php | 37 ++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+) diff --git a/documentation.md b/documentation.md index a7cbf282..121da4ad 100644 --- a/documentation.md +++ b/documentation.md @@ -896,6 +896,19 @@ $I->seeCookie('PHPSESSID'); * `param array` $params +### seeCurrentActionIs + +Checks that current page matches action + +``` php +seeCurrentActionIs('PostController::index'); +$I->seeCurrentActionIs('HomeController'); +``` + + * `param string` $action + + ### seeCurrentRouteIs Checks that current url matches route. diff --git a/src/Codeception/Module/Symfony.php b/src/Codeception/Module/Symfony.php index 39756533..cd047dc6 100644 --- a/src/Codeception/Module/Symfony.php +++ b/src/Codeception/Module/Symfony.php @@ -770,4 +770,41 @@ private function getPossibleKernelClasses() return [$this->config['kernel_class']]; } + + /** + * Checks that current page matches action + * + * ``` php + * seeCurrentActionIs('PostController::index'); + * $I->seeCurrentActionIs('HomeController'); + * ``` + * + * @param string $action + */ + public function seeCurrentActionIs($action) + { + $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) { + $request = $this->client->getRequest(); + $currentAction = basename($request->attributes->get('_controller')); + + $this->assertEquals($currentAction, $action, "Current action is '$currentAction'."); + return; + } + } + $this->fail("Action '$action' does not exist"); + } }