Created
May 7, 2013 16:04
-
-
Save monofone/5533792 to your computer and use it in GitHub Desktop.
Flow Routing for behat
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
/** | |
* @Given /^I am on the "([^"]*)" page$/ | |
*/ | |
public function iAmOnThePage($pageName) { | |
$uri = $this->resolvePageUri($pageName); | |
$this->visit($uri); | |
} | |
/** | |
* @Given /^I wait until I am on the "([^"]*)" page$/ | |
*/ | |
public function iWaitUntilIAmOnThePage($pageName) { | |
$uri = $this->resolvePageUri($pageName); | |
for ($elapsed = 0; $elapsed < 10; $elapsed++) { | |
$currentUrlPath = parse_url($this->getSession()->getCurrentUrl(), PHP_URL_PATH); | |
if ($currentUrlPath === $uri) { | |
return; | |
} | |
sleep(1); | |
} | |
$this->assertPageAddress($uri); | |
} | |
/** | |
* @return \TYPO3\Flow\Mvc\Routing\Router | |
*/ | |
protected function getRouter() { | |
if ($this->router === NULL) { | |
$this->router = $this->objectManager->get('\TYPO3\Flow\Mvc\Routing\Router'); | |
$configurationManager = $this->objectManager->get('TYPO3\Flow\Configuration\ConfigurationManager'); | |
$routesConfiguration = $configurationManager->getConfiguration(\TYPO3\Flow\Configuration\ConfigurationManager::CONFIGURATION_TYPE_ROUTES); | |
$this->router->setRoutesConfiguration($routesConfiguration); | |
} | |
return $this->router; | |
} | |
/** | |
* @param string $pageName | |
* @return string | |
*/ | |
protected function resolvePageUri($pageName) { | |
$uri = NULL; | |
if (strpos($pageName, '/') === 0) { | |
$uri = $pageName; | |
return $uri; | |
} else { | |
$router = $this->getRouter(); | |
/** @var \TYPO3\Flow\Mvc\Routing\Route $route */ | |
foreach ($router->getRoutes() as $route) { | |
if (preg_match('/::\s*' . preg_quote($pageName, '/') . '$/', $route->getName())) { | |
$routeValues = $route->getDefaults(); | |
if ($route->resolves($routeValues)) { | |
$uri = $route->getMatchingUri(); | |
break; | |
} | |
} | |
} | |
if ($uri === NULL) { | |
PHPUnit_Framework_Assert::fail('Could not resolve a route for name "' . $pageName . '"'); | |
return $uri; | |
} | |
return $uri; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment