Skip to content

Instantly share code, notes, and snippets.

@tikitikipoo
Created February 1, 2013 01:54
Show Gist options
  • Save tikitikipoo/4688544 to your computer and use it in GitHub Desktop.
Save tikitikipoo/4688544 to your computer and use it in GitHub Desktop.
CakePHP Behat 表示されるべき個数をテストしたいとき
$steps->Given('/^"([^"]*)" が (\d+) 個表示されていること$/', function($world, $text, $num) {
$actual = $world->getSession()->getPage()->getContent();
$regex = '/'.preg_quote($text, '/').'/u';
$result = preg_match_all($regex, $actual, $matches);
if (!$result) {
$message = sprintf('The text "%s" was not found anywhere in the text of the current page.', $text);
throw new \Exception($message, E_USER_ERROR);
}
if (count($matches[0]) != $num) {
$message = sprintf('The text "%s" was not found "%d" times. actualy %d times.', $text, $num, count($matches[0]));
throw new \Exception($message, E_USER_ERROR);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment