/ResqueBoard2/vendor/behat/mink/src/Behat/Mink/WebAssert.php

https://bitbucket.org/galvani/flow-resque-board · PHP · 593 lines · 277 code · 63 blank · 253 comment · 29 complexity · acf1424368afcd19fd2369708dad0f83 MD5 · raw file

  1. <?php
  2. namespace Behat\Mink;
  3. use Behat\Mink\Element\Element,
  4. Behat\Mink\Element\NodeElement,
  5. Behat\Mink\Exception\ElementNotFoundException,
  6. Behat\Mink\Exception\ExpectationException,
  7. Behat\Mink\Exception\ResponseTextException,
  8. Behat\Mink\Exception\ElementHtmlException,
  9. Behat\Mink\Exception\ElementTextException;
  10. /*
  11. * This file is part of the Behat\Mink.
  12. * (c) Konstantin Kudryashov <ever.zet@gmail.com>
  13. *
  14. * For the full copyright and license information, please view the LICENSE
  15. * file that was distributed with this source code.
  16. */
  17. /**
  18. * Mink web assertions tool.
  19. *
  20. * @author Konstantin Kudryashov <ever.zet@gmail.com>
  21. */
  22. class WebAssert
  23. {
  24. protected $session;
  25. /**
  26. * Initializes assertion engine.
  27. *
  28. * @param Session $session
  29. */
  30. public function __construct(Session $session)
  31. {
  32. $this->session = $session;
  33. }
  34. /**
  35. * Checks that current session address is equals to provided one.
  36. *
  37. * @param string $page
  38. *
  39. * @throws ExpectationException
  40. */
  41. public function addressEquals($page)
  42. {
  43. $expected = $this->cleanScriptnameFromPath(parse_url($page, PHP_URL_PATH));
  44. $actual = $this->getCurrentUrlPath();
  45. if ($actual !== $expected) {
  46. $message = sprintf('Current page is "%s", but "%s" expected.', $actual, $expected);
  47. throw new ExpectationException($message, $this->session);
  48. }
  49. }
  50. /**
  51. * Checks that current session address is not equals to provided one.
  52. *
  53. * @param string $page
  54. *
  55. * @throws ExpectationException
  56. */
  57. public function addressNotEquals($page)
  58. {
  59. $expected = $this->cleanScriptnameFromPath(parse_url($page, PHP_URL_PATH));
  60. $actual = $this->getCurrentUrlPath();
  61. if ($actual === $expected) {
  62. $message = sprintf('Current page is "%s", but should not be.', $actual);
  63. throw new ExpectationException($message, $this->session);
  64. }
  65. }
  66. /**
  67. * Checks that current session address matches regex.
  68. *
  69. * @param string $regex
  70. *
  71. * @throws ExpectationException
  72. */
  73. public function addressMatches($regex)
  74. {
  75. $actual = $this->getCurrentUrlPath();
  76. if (!preg_match($regex, $actual)) {
  77. $message = sprintf('Current page "%s" does not match the regex "%s".', $actual, $regex);
  78. throw new ExpectationException($message, $this->session);
  79. }
  80. }
  81. /**
  82. * Checks that specified cookie exists and its value equals to a given one
  83. *
  84. * @param string $name cookie name
  85. * @param string $value cookie value
  86. *
  87. * @throws Behat\Mink\Exception\ExpectationException
  88. */
  89. public function cookieEquals($name, $value)
  90. {
  91. $this->cookieExists($name);
  92. $actualValue = $this->session->getCookie($name);
  93. if ($actualValue != $value) {
  94. $message = sprintf('Cookie "%s" value is "%s", but should be "%s".', $name,
  95. $actualValue, $value);
  96. throw new ExpectationException($message, $this->session);
  97. }
  98. }
  99. /**
  100. * Checks that specified cookie exists
  101. *
  102. * @param string $name cookie name
  103. *
  104. * @throws Behat\Mink\Exception\ExpectationException
  105. */
  106. public function cookieExists($name)
  107. {
  108. if ($this->session->getCookie($name) === null) {
  109. $message = sprintf('Cookie "%s" is not set, but should be.', $name);
  110. throw new ExpectationException($message, $this->session);
  111. }
  112. }
  113. /**
  114. * Checks that current response code equals to provided one.
  115. *
  116. * @param integer $code
  117. *
  118. * @throws ExpectationException
  119. */
  120. public function statusCodeEquals($code)
  121. {
  122. $actual = $this->session->getStatusCode();
  123. if (intval($code) !== intval($actual)) {
  124. $message = sprintf('Current response status code is %d, but %d expected.', $actual, $code);
  125. throw new ExpectationException($message, $this->session);
  126. }
  127. }
  128. /**
  129. * Checks that current response code not equals to provided one.
  130. *
  131. * @param integer $code
  132. *
  133. * @throws ExpectationException
  134. */
  135. public function statusCodeNotEquals($code)
  136. {
  137. $actual = $this->session->getStatusCode();
  138. if (intval($code) === intval($actual)) {
  139. $message = sprintf('Current response status code is %d, but should not be.', $actual);
  140. throw new ExpectationException($message, $this->session);
  141. }
  142. }
  143. /**
  144. * Checks that current page contains text.
  145. *
  146. * @param string $text
  147. *
  148. * @throws ResponseTextException
  149. */
  150. public function pageTextContains($text)
  151. {
  152. $actual = $this->session->getPage()->getText();
  153. $actual = preg_replace('/\s+/u', ' ', $actual);
  154. $regex = '/'.preg_quote($text, '/').'/ui';
  155. if (!preg_match($regex, $actual)) {
  156. $message = sprintf('The text "%s" was not found anywhere in the text of the current page.', $text);
  157. throw new ResponseTextException($message, $this->session);
  158. }
  159. }
  160. /**
  161. * Checks that current page does not contains text.
  162. *
  163. * @param string $text
  164. *
  165. * @throws ResponseTextException
  166. */
  167. public function pageTextNotContains($text)
  168. {
  169. $actual = $this->session->getPage()->getText();
  170. $actual = preg_replace('/\s+/u', ' ', $actual);
  171. $regex = '/'.preg_quote($text, '/').'/ui';
  172. if (preg_match($regex, $actual)) {
  173. $message = sprintf('The text "%s" appears in the text of this page, but it should not.', $text);
  174. throw new ResponseTextException($message, $this->session);
  175. }
  176. }
  177. /**
  178. * Checks that current page text matches regex.
  179. *
  180. * @param string $regex
  181. *
  182. * @throws ResponseTextException
  183. */
  184. public function pageTextMatches($regex)
  185. {
  186. $actual = $this->session->getPage()->getText();
  187. if (!preg_match($regex, $actual)) {
  188. $message = sprintf('The pattern %s was not found anywhere in the text of the current page.', $regex);
  189. throw new ResponseTextException($message, $this->session);
  190. }
  191. }
  192. /**
  193. * Checks that current page text does not matches regex.
  194. *
  195. * @param string $regex
  196. *
  197. * @throws ResponseTextException
  198. */
  199. public function pageTextNotMatches($regex)
  200. {
  201. $actual = $this->session->getPage()->getText();
  202. if (preg_match($regex, $actual)) {
  203. $message = sprintf('The pattern %s was found in the text of the current page, but it should not.', $regex);
  204. throw new ResponseTextException($message, $this->session);
  205. }
  206. }
  207. /**
  208. * Checks that page HTML (response content) contains text.
  209. *
  210. * @param string $text
  211. *
  212. * @throws ExpectationException
  213. */
  214. public function responseContains($text)
  215. {
  216. $actual = $this->session->getPage()->getContent();
  217. $regex = '/'.preg_quote($text, '/').'/ui';
  218. if (!preg_match($regex, $actual)) {
  219. $message = sprintf('The string "%s" was not found anywhere in the HTML response of the current page.', $text);
  220. throw new ExpectationException($message, $this->session);
  221. }
  222. }
  223. /**
  224. * Checks that page HTML (response content) does not contains text.
  225. *
  226. * @param string $text
  227. *
  228. * @throws ExpectationException
  229. */
  230. public function responseNotContains($text)
  231. {
  232. $actual = $this->session->getPage()->getContent();
  233. $regex = '/'.preg_quote($text, '/').'/ui';
  234. if (preg_match($regex, $actual)) {
  235. $message = sprintf('The string "%s" appears in the HTML response of this page, but it should not.', $text);
  236. throw new ExpectationException($message, $this->session);
  237. }
  238. }
  239. /**
  240. * Checks that page HTML (response content) matches regex.
  241. *
  242. * @param string $regex
  243. *
  244. * @throws ExpectationException
  245. */
  246. public function responseMatches($regex)
  247. {
  248. $actual = $this->session->getPage()->getContent();
  249. if (!preg_match($regex, $actual)) {
  250. $message = sprintf('The pattern %s was not found anywhere in the HTML response of the page.', $regex);
  251. throw new ExpectationException($message, $this->session);
  252. }
  253. }
  254. /**
  255. * Checks that page HTML (response content) does not matches regex.
  256. *
  257. * @param $regex
  258. *
  259. * @throws ExpectationException
  260. */
  261. public function responseNotMatches($regex)
  262. {
  263. $actual = $this->session->getPage()->getContent();
  264. if (preg_match($regex, $actual)) {
  265. $message = sprintf('The pattern %s was found in the HTML response of the page, but it should not.', $regex);
  266. throw new ExpectationException($message, $this->session);
  267. }
  268. }
  269. /**
  270. * Checks that there is specified number of specific elements on the page.
  271. *
  272. * @param string $selectorType element selector type (css, xpath)
  273. * @param string $selector element selector
  274. * @param integer $count expected count
  275. * @param Element $container document to check against
  276. *
  277. * @throws ExpectationException
  278. */
  279. public function elementsCount($selectorType, $selector, $count, Element $container = null)
  280. {
  281. $container = $container ?: $this->session->getPage();
  282. $nodes = $container->findAll($selectorType, $selector);
  283. if (intval($count) !== count($nodes)) {
  284. $message = sprintf('%d elements matching %s "%s" found on the page, but should be %d.', count($nodes), $selectorType, $selector, $count);
  285. throw new ExpectationException($message, $this->session);
  286. }
  287. }
  288. /**
  289. * Checks that specific element exists on the current page.
  290. *
  291. * @param string $selectorType element selector type (css, xpath)
  292. * @param string $selector element selector
  293. * @param Element $container document to check against
  294. *
  295. * @return NodeElement
  296. *
  297. * @throws ElementNotFoundException
  298. */
  299. public function elementExists($selectorType, $selector, Element $container = null)
  300. {
  301. $container = $container ?: $this->session->getPage();
  302. $node = $container->find($selectorType, $selector);
  303. if (null === $node) {
  304. throw new ElementNotFoundException($this->session, 'element', $selectorType, $selector);
  305. }
  306. return $node;
  307. }
  308. /**
  309. * Checks that specific element does not exists on the current page.
  310. *
  311. * @param string $selectorType element selector type (css, xpath)
  312. * @param string $selector element selector
  313. * @param Element $container document to check against
  314. *
  315. * @throws ExpectationException
  316. */
  317. public function elementNotExists($selectorType, $selector, Element $container = null)
  318. {
  319. $container = $container ?: $this->session->getPage();
  320. $node = $container->find($selectorType, $selector);
  321. if (null !== $node) {
  322. $message = sprintf('An element matching %s "%s" appears on this page, but it should not.', $selectorType, $selector);
  323. throw new ExpectationException($message, $this->session);
  324. }
  325. }
  326. /**
  327. * Checks that specific element contains text.
  328. *
  329. * @param string $selectorType element selector type (css, xpath)
  330. * @param string $selector element selector
  331. * @param string $text expected text
  332. *
  333. * @throws ElementTextException
  334. */
  335. public function elementTextContains($selectorType, $selector, $text)
  336. {
  337. $element = $this->elementExists($selectorType, $selector);
  338. $actual = $element->getText();
  339. $regex = '/'.preg_quote($text, '/').'/ui';
  340. if (!preg_match($regex, $actual)) {
  341. $message = sprintf('The text "%s" was not found in the text of the element matching %s "%s".', $text, $selectorType, $selector);
  342. throw new ElementTextException($message, $this->session, $element);
  343. }
  344. }
  345. /**
  346. * Checks that specific element does not contains text.
  347. *
  348. * @param string $selectorType element selector type (css, xpath)
  349. * @param string $selector element selector
  350. * @param string $text expected text
  351. *
  352. * @throws ElementTextException
  353. */
  354. public function elementTextNotContains($selectorType, $selector, $text)
  355. {
  356. $element = $this->elementExists($selectorType, $selector);
  357. $actual = $element->getText();
  358. $regex = '/'.preg_quote($text, '/').'/ui';
  359. if (preg_match($regex, $actual)) {
  360. $message = sprintf('The text "%s" appears in the text of the element matching %s "%s", but it should not.', $text, $selectorType, $selector);
  361. throw new ElementTextException($message, $this->session, $element);
  362. }
  363. }
  364. /**
  365. * Checks that specific element contains HTML.
  366. *
  367. * @param string $selectorType element selector type (css, xpath)
  368. * @param string $selector element selector
  369. * @param string $html expected text
  370. *
  371. * @throws ElementHtmlException
  372. */
  373. public function elementContains($selectorType, $selector, $html)
  374. {
  375. $element = $this->elementExists($selectorType, $selector);
  376. $actual = $element->getHtml();
  377. $regex = '/'.preg_quote($html, '/').'/ui';
  378. if (!preg_match($regex, $actual)) {
  379. $message = sprintf('The string "%s" was not found in the HTML of the element matching %s "%s".', $html, $selectorType, $selector);
  380. throw new ElementHtmlException($message, $this->session, $element);
  381. }
  382. }
  383. /**
  384. * Checks that specific element does not contains HTML.
  385. *
  386. * @param string $selectorType element selector type (css, xpath)
  387. * @param string $selector element selector
  388. * @param string $html expected text
  389. *
  390. * @throws ElementHtmlException
  391. */
  392. public function elementNotContains($selectorType, $selector, $html)
  393. {
  394. $element = $this->elementExists($selectorType, $selector);
  395. $actual = $element->getHtml();
  396. $regex = '/'.preg_quote($html, '/').'/ui';
  397. if (preg_match($regex, $actual)) {
  398. $message = sprintf('The string "%s" appears in the HTML of the element matching %s "%s", but it should not.', $html, $selectorType, $selector);
  399. throw new ElementHtmlException($message, $this->session, $element);
  400. }
  401. }
  402. /**
  403. * Checks that specific field exists on the current page.
  404. *
  405. * @param string $field field id|name|label|value
  406. * @param Element $container document to check against
  407. *
  408. * @return NodeElement
  409. *
  410. * @throws ElementNotFoundException
  411. */
  412. public function fieldExists($field, Element $container = null)
  413. {
  414. $container = $container ?: $this->session->getPage();
  415. $node = $container->findField($field);
  416. if (null === $node) {
  417. throw new ElementNotFoundException($this->session, 'form field', 'id|name|label|value', $field);
  418. }
  419. return $node;
  420. }
  421. /**
  422. * Checks that specific field does not exists on the current page.
  423. *
  424. * @param string $field field id|name|label|value
  425. * @param Element $container document to check against
  426. *
  427. * @throws ExpectationException
  428. */
  429. public function fieldNotExists($field, Element $container = null)
  430. {
  431. $container = $container ?: $this->session->getPage();
  432. $node = $container->findField($field);
  433. if (null !== $node) {
  434. $message = sprintf('A field "%s" appears on this page, but it should not.', $field);
  435. throw new ExpectationException($message, $this->session);
  436. }
  437. }
  438. /**
  439. * Checks that specific field have provided value.
  440. *
  441. * @param string $field field id|name|label|value
  442. * @param string $value field value
  443. * @param Element $container document to check against
  444. *
  445. * @throws ExpectationException
  446. */
  447. public function fieldValueEquals($field, $value, Element $container = null)
  448. {
  449. $node = $this->fieldExists($field, $container);
  450. $actual = $node->getValue();
  451. $regex = '/^'.preg_quote($value, '/').'/ui';
  452. if (!preg_match($regex, $actual)) {
  453. $message = sprintf('The field "%s" value is "%s", but "%s" expected.', $field, $actual, $value);
  454. throw new ExpectationException($message, $this->session);
  455. }
  456. }
  457. /**
  458. * Checks that specific field have provided value.
  459. *
  460. * @param string $field field id|name|label|value
  461. * @param string $value field value
  462. * @param Element $container document to check against
  463. *
  464. * @throws ExpectationException
  465. */
  466. public function fieldValueNotEquals($field, $value, Element $container = null)
  467. {
  468. $node = $this->fieldExists($field, $container);
  469. $actual = $node->getValue();
  470. $regex = '/^'.preg_quote($value, '/').'/ui';
  471. if (preg_match($regex, $actual)) {
  472. $message = sprintf('The field "%s" value is "%s", but it should not be.', $field, $actual);
  473. throw new ExpectationException($message, $this->session);
  474. }
  475. }
  476. /**
  477. * Checks that specific checkbox is checked.
  478. *
  479. * @param string $field field id|name|label|value
  480. * @param Element $container document to check against
  481. *
  482. * @throws ExpectationException
  483. */
  484. public function checkboxChecked($field, Element $container = null)
  485. {
  486. $node = $this->fieldExists($field, $container);
  487. if (!$node->isChecked()) {
  488. $message = sprintf('Checkbox "%s" is not checked, but it should be.', $field);
  489. throw new ExpectationException($message, $this->session);
  490. }
  491. }
  492. /**
  493. * Checks that specific checkbox is unchecked.
  494. *
  495. * @param string $field field id|name|label|value
  496. * @param Element $container document to check against
  497. *
  498. * @throws ExpectationException
  499. */
  500. public function checkboxNotChecked($field, Element $container = null)
  501. {
  502. $node = $this->fieldExists($field, $container);
  503. if ($node->isChecked()) {
  504. $message = sprintf('Checkbox "%s" is checked, but it should not be.', $field);
  505. throw new ExpectationException($message, $this->session);
  506. }
  507. }
  508. /**
  509. * Gets current url of the page.
  510. *
  511. * @return string
  512. */
  513. protected function getCurrentUrlPath()
  514. {
  515. return $this->cleanScriptnameFromPath(
  516. parse_url($this->session->getCurrentUrl(), PHP_URL_PATH)
  517. );
  518. }
  519. /**
  520. * Trims scriptname from the URL.
  521. *
  522. * @param string $path
  523. *
  524. * @return string
  525. */
  526. protected function cleanScriptnameFromPath($path)
  527. {
  528. return preg_replace('/^\/[^\.\/]+\.php/', '', $path);
  529. }
  530. }