Skip to content

Commit b24e2b6

Browse files
TavoNiievezNaktibalda
authored andcommitted
Added amOnAction function
1 parent 1504e65 commit b24e2b6

File tree

2 files changed

+58
-1
lines changed

2 files changed

+58
-1
lines changed

documentation.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,21 @@ Authenticates user for HTTP_AUTH
220220
* `param` $password
221221

222222

223+
### amOnAction
224+
225+
Opens web page by action name
226+
227+
``` php
228+
<?php
229+
$I->amOnAction('PostController::index');
230+
$I->amOnAction('HomeController');
231+
$I->amOnAction('ArticleController', ['slug' => 'lorem-ipsum']);
232+
```
233+
234+
* `param string` $action
235+
* `param array` $params
236+
237+
223238
### amOnPage
224239

225240
Opens the page for the given relative URI.

src/Codeception/Module/Symfony.php

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
use Symfony\Component\Finder\Finder;
1414
use Symfony\Component\DependencyInjection\ContainerInterface;
1515
use Symfony\Component\Finder\SplFileInfo;
16+
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
1617
use Symfony\Component\VarDumper\Cloner\Data;
1718
use Symfony\Bundle\FrameworkBundle\Console\Application;
1819
use Symfony\Component\Console\Input\ArrayInput;
@@ -871,12 +872,53 @@ public function seeInSession($attrib, $value = null)
871872

872873
$session = $this->grabService('session');
873874

874-
if (! $session->has($attrib)) {
875+
if (!$session->has($attrib)) {
875876
$this->fail("No session attribute with name '$attrib'");
876877
}
877878

878879
if (null !== $value) {
879880
$this->assertEquals($value, $session->get($attrib));
880881
}
881882
}
883+
884+
/**
885+
* Opens web page by action name
886+
*
887+
* ``` php
888+
* <?php
889+
* $I->amOnAction('PostController::index');
890+
* $I->amOnAction('HomeController');
891+
* $I->amOnAction('ArticleController', ['slug' => 'lorem-ipsum']);
892+
* ```
893+
*
894+
* @param string $action
895+
* @param array $params
896+
*/
897+
public function amOnAction($action, $params = [])
898+
{
899+
$container = $this->_getContainer();
900+
901+
if (!$container->has('router')) {
902+
$this->fail("Symfony container doesn't have 'router' service");
903+
return;
904+
}
905+
906+
$router = $this->grabService('router');
907+
908+
$routes = $router->getRouteCollection()->getIterator();
909+
910+
foreach ($routes as $route) {
911+
$controller = basename($route->getDefault('_controller'));
912+
if ($controller === $action) {
913+
$resource = $router->match($route->getPath());
914+
$url = $router->generate(
915+
$resource['_route'],
916+
$params,
917+
UrlGeneratorInterface::ABSOLUTE_PATH
918+
);
919+
$this->amOnPage($url);
920+
return;
921+
}
922+
}
923+
}
882924
}

0 commit comments

Comments
 (0)