/** * Checks, that page contains specified texts in order. * * @Then /^(?:|I )should see following texts in order:$/ */ public function assertPageContainsTextsInOrder(TableNode $table) { $texts = array(); foreach ($table->getRows() as $row) { $texts[] = $row[0]; } $pattern = "/" . implode(".*", $texts) . "/s"; $actual = $this->getSession()->getPage()->getText(); try { assertRegExp($pattern, $actual); } catch (AssertException $e) { $message = sprintf('The texts "%s" was not found in order anywhere on the current page', implode('", "', $texts)); throw new ExpectationException($message, $this->getSession(), $e); } }
/** * Checks, that page contains text matching specified pattern. * * @Then /^(?:|I )should see text matching (?P<pattern>"(?:[^"]|\\")*")$/ */ public function assertPageMatchesText($pattern) { $actual = $this->getSession()->getPage()->getText(); try { assertRegExp($pattern, $actual); } catch (AssertException $e) { $message = sprintf('The pattern %s was not found anywhere in the text of the current page.', $pattern); throw new ResponseTextException($message, $this->getSession(), $e); } }
/** * @Given /^the response body (contains|is|matches):$/ */ public function assertResponseBody($match, PyStringNode $expected) { $expected = trim((string) $expected); $actual = trim((string) $this->getLastResponse()->getBody()); if ($match === 'is') { assertSame($expected, $actual, sprintf('Expected %s, got %s', $expected, $actual)); } else { if ($match === 'matches') { assertRegExp($expected, $actual); } else { assertContains($expected, $actual); } } }
/** * @Then /^I should see "(?P<message>[^"]*)"$/ */ public function iShouldSee($message) { assertRegExp('/' . preg_quote($message, '/') . '/sm', $this->applicationTester->getDisplay()); }
/** @test */ public function findActivitySummaryTest() { $client = $this->createClient(); $crawler = $client->request('GET', '/api/activitySummary'); assertRegExp('/total_rows/', $client->getResponse()->getContent()); }
/** @test */ public function unSubscribeTest() { $client = $this->createClient(); $sendParams = array('group_id' => 'test'); $crawler = $client->request('POST', '/api/unSubscribeGroup', array(), array(), array('CONTENT_TYPE' => 'application/json'), json_encode($sendParams)); assertRegExp("/OK/", $client->getResponse()->getContent()); }
/** @test */ public function searchUserByAge() { $client = $this->createClient(); $crawler = $client->request('GET', '/api/searchUsers?af=30&at=35'); assertRegExp('/\\[\\]/', $client->getResponse()->getContent()); }
/** @test */ public function getCommentsTest() { $client = $this->createClient(); $crawler = $client->request('GET', '/api/comments/test/test/test'); assertRegExp('/OK/', $client->getResponse()->getContent()); }
/** * Checks, that current page PATH matches regular expression. * * @Then /^the (?i)url(?-i) should match "(?P<pattern>(?:[^"]|\\")*)"$/ */ public function assertUrlRegExp($pattern) { $pattern = str_replace('\\"', '"', $pattern); if (!preg_match('/^\\/.*\\/$/', $pattern)) { $this->assertPageAddress($pattern); return; } $actual = parse_url($this->getSession()->getCurrentUrl(), PHP_URL_PATH); try { assertRegExp($pattern, $actual); } catch (AssertException $e) { $message = sprintf('Current page "%s" does not match the pattern "%s"', $actual, $pattern); throw new ExpectationException($message, $this->getSession(), $e); } }
/** * Checks that response body contains specific text. * * @param string $text * * @Then /^(?:the )?response should contain "([^"]*)"$/ */ public function theResponseShouldContain($text) { assertRegExp('/' . preg_quote($text) . '/', $this->browser->getLastResponse()->getContent()); }
public function like($exp, $regex, $message = '') { assertRegExp($regex, $exp); }
/** * Asserts HTML tags. * * Takes an array $expected and generates a regex from it to match the provided $string. * Samples for $expected: * * Checks for an input tag with a name attribute (contains any non-empty value) and an id * attribute that contains 'my-input': * * ``` * ['input' => ['name', 'id' => 'my-input']] * ``` * * Checks for two p elements with some text in them: * * ``` * [ * ['p' => true], * 'textA', * '/p', * ['p' => true], * 'textB', * '/p' * ] * ``` * * You can also specify a pattern expression as part of the attribute values, or the tag * being defined, if you prepend the value with preg: and enclose it with slashes, like so: * * ``` * [ * ['input' => ['name', 'id' => 'preg:/FieldName\d+/']], * 'preg:/My\s+field/' * ] * ``` * * Important: This function is very forgiving about whitespace and also accepts any * permutation of attribute order. It will also allow whitespace between specified tags. * * @param array $expected An array, see above * @param string $string An HTML/XHTML/XML string * @return bool * @SuppressWarnings(PHPMD.NPathComplexity) */ function isHtml($expected, $string) { $count = 0; $regex = array(); $normalized = _normalizeHtmlExp($expected); foreach ($normalized as $tags) { $tags = _tagsToString($tags); $count++; if (is_string($tags) && $tags[0] === '<') { $tags = array(substr($tags, 1) => array()); } elseif (is_string($tags)) { $tagsTrimmed = preg_replace('/\\s+/m', '', $tags); if (preg_match('/^\\*?\\//', $tags, $match) && $tagsTrimmed !== '//') { $prefix = array(null, null); if ($match[0] === '*/') { $prefix = array('Anything, ', '.*?'); } $regex[] = array(sprintf('%sClose %s tag', $prefix[0], substr($tags, strlen($match[0]))), sprintf('%s<[\\s]*\\/[\\s]*%s[\\s]*>[\\n\\r]*', $prefix[1], substr($tags, strlen($match[0]))), $count); continue; } $regex[] = _getRegexByTagStr($tags, $count); continue; } foreach ($tags as $tag => $attributes) { $regex[] = array(sprintf('Open %s tag', $tag), sprintf('[\\s]*<%s', preg_quote($tag, '/')), $count); if ($attributes === true) { $attributes = array(); } $count = 1; $attrs = array(); $explanations = array(); foreach ($attributes as $attr => $val) { if (is_numeric($attr) && preg_match('/^preg\\:\\/(.+)\\/$/i', $val, $matches)) { $attrs[] = $matches[1]; $explanations[] = sprintf('Regex "%s" matches', $matches[1]); continue; } else { list($explanations, $newAttrs) = _checkArrayAttrs($attr, $val, $explanations); $attrs[] = $newAttrs; } $count++; } if ($attrs) { $regex[] = array('explains' => $explanations, 'attrs' => $attrs); } $regex[] = array(sprintf('End %s tag', $tag), '[\\s]*\\/?[\\s]*>[\\n\\r]*', $count); } } foreach ($regex as $count => $assertion) { $matches = false; if (isset($assertion['attrs'])) { $string = _assertAttributes($assertion, $string); continue; } list($description, $expressions, $itemNum) = $assertion; foreach ((array) $expressions as $expression) { $expression = sprintf('/^%s/s', $expression); if (preg_match($expression, $string, $match)) { $matches = true; $string = substr($string, strlen($match[0])); break; } } if (!$matches) { //@codingStandardsIgnoreStart assertRegExp($expression, $string, sprintf('Item #%d / regex #%d failed: %s', $itemNum, $count, $description)); //@codingStandardsIgnoreEnd return false; } } isTrue(true, '%s'); return true; }
/** * @Then Assert the value :arg1 match a given regex :arg2. * * @param $string * @param $pattern */ public function assertRegExp($string, $pattern) { assertRegExp($pattern, $string, sprintf("Assert the value [%s] match a given regex [%s]", $string, $pattern)); }