PageRenderTime 52ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 1ms

/vendor/ZF2/library/Zend/Test/PHPUnit/Controller/AbstractHttpControllerTestCase.php

https://github.com/XataWork/zf2-project
PHP | 873 lines | 499 code | 74 blank | 300 comment | 54 complexity | dcb4d3b886575f5896267b43a5213522 MD5 | raw file
Possible License(s): BSD-3-Clause
  1. <?php
  2. /**
  3. * Zend Framework (http://framework.zend.com/)
  4. *
  5. * @link http://github.com/zendframework/zf2 for the canonical source repository
  6. * @copyright Copyright (c) 2005-2014 Zend Technologies USA Inc. (http://www.zend.com)
  7. * @license http://framework.zend.com/license/new-bsd New BSD License
  8. */
  9. namespace Zend\Test\PHPUnit\Controller;
  10. use PHPUnit_Framework_ExpectationFailedException;
  11. use Zend\Dom\Document;
  12. abstract class AbstractHttpControllerTestCase extends AbstractControllerTestCase
  13. {
  14. /**
  15. * HTTP controller must not use the console request
  16. * @var bool
  17. */
  18. protected $useConsoleRequest = false;
  19. /**
  20. * XPath namespaces
  21. * @var array
  22. */
  23. protected $xpathNamespaces = array();
  24. /**
  25. * Get response header by key
  26. *
  27. * @param string $header
  28. * @return \Zend\Http\Header\HeaderInterface|false
  29. */
  30. protected function getResponseHeader($header)
  31. {
  32. $response = $this->getResponse();
  33. $headers = $response->getHeaders();
  34. $responseHeader = $headers->get($header, false);
  35. return $responseHeader;
  36. }
  37. /**
  38. * Assert response has the given reason phrase
  39. *
  40. * @param string $phrase
  41. */
  42. public function assertResponseReasonPhrase($phrase)
  43. {
  44. $this->assertEquals($phrase, $this->getResponse()->getReasonPhrase());
  45. }
  46. /**
  47. * Assert response header exists
  48. *
  49. * @param string $header
  50. */
  51. public function assertHasResponseHeader($header)
  52. {
  53. $responseHeader = $this->getResponseHeader($header);
  54. if (false === $responseHeader) {
  55. throw new PHPUnit_Framework_ExpectationFailedException(sprintf(
  56. 'Failed asserting response header "%s" found',
  57. $header
  58. ));
  59. }
  60. $this->assertNotEquals(false, $responseHeader);
  61. }
  62. /**
  63. * Assert response header does not exist
  64. *
  65. * @param string $header
  66. */
  67. public function assertNotHasResponseHeader($header)
  68. {
  69. $responseHeader = $this->getResponseHeader($header);
  70. if (false !== $responseHeader) {
  71. throw new PHPUnit_Framework_ExpectationFailedException(sprintf(
  72. 'Failed asserting response header "%s" WAS NOT found',
  73. $header
  74. ));
  75. }
  76. $this->assertFalse($responseHeader);
  77. }
  78. /**
  79. * Assert response header exists and contains the given string
  80. *
  81. * @param string $header
  82. * @param string $match
  83. */
  84. public function assertResponseHeaderContains($header, $match)
  85. {
  86. $responseHeader = $this->getResponseHeader($header);
  87. if (!$responseHeader) {
  88. throw new PHPUnit_Framework_ExpectationFailedException(sprintf(
  89. 'Failed asserting response header, header "%s" do not exists',
  90. $header
  91. ));
  92. }
  93. if (!$responseHeader instanceof \ArrayIterator) {
  94. $responseHeader = array($responseHeader);
  95. }
  96. $headerMatched = false;
  97. foreach ($responseHeader as $currentHeader) {
  98. if ($match == $currentHeader->getFieldValue()) {
  99. $headerMatched = true;
  100. break;
  101. }
  102. }
  103. if (!$headerMatched) {
  104. throw new \PHPUnit_Framework_ExpectationFailedException(sprintf(
  105. 'Failed asserting response header "%s" exists and contains "%s", actual content is "%s"',
  106. $header,
  107. $match,
  108. $currentHeader->getFieldValue()
  109. ));
  110. }
  111. $this->assertEquals($match, $currentHeader->getFieldValue());
  112. }
  113. /**
  114. * Assert response header exists and contains the given string
  115. *
  116. * @param string $header
  117. * @param string $match
  118. */
  119. public function assertNotResponseHeaderContains($header, $match)
  120. {
  121. $responseHeader = $this->getResponseHeader($header);
  122. if (!$responseHeader) {
  123. throw new PHPUnit_Framework_ExpectationFailedException(sprintf(
  124. 'Failed asserting response header, header "%s" do not exists',
  125. $header
  126. ));
  127. }
  128. if (!$responseHeader instanceof \ArrayIterator) {
  129. $responseHeader = array($responseHeader);
  130. }
  131. foreach ($responseHeader as $currentHeader) {
  132. if ($match == $currentHeader->getFieldValue()) {
  133. throw new PHPUnit_Framework_ExpectationFailedException(sprintf(
  134. 'Failed asserting response header "%s" DOES NOT CONTAIN "%s"',
  135. $header,
  136. $match
  137. ));
  138. }
  139. }
  140. $this->assertNotEquals($match, $currentHeader->getFieldValue());
  141. }
  142. /**
  143. * Assert response header exists and matches the given pattern
  144. *
  145. * @param string $header
  146. * @param string $pattern
  147. */
  148. public function assertResponseHeaderRegex($header, $pattern)
  149. {
  150. $responseHeader = $this->getResponseHeader($header);
  151. if (!$responseHeader) {
  152. throw new PHPUnit_Framework_ExpectationFailedException(sprintf(
  153. 'Failed asserting response header, header "%s" do not exists',
  154. $header
  155. ));
  156. }
  157. if (!$responseHeader instanceof \ArrayIterator) {
  158. $responseHeader = array($responseHeader);
  159. }
  160. $headerMatched = false;
  161. foreach ($responseHeader as $currentHeader) {
  162. $headerMatched = (bool) preg_match($pattern, $currentHeader->getFieldValue());
  163. if ($headerMatched) {
  164. break;
  165. }
  166. }
  167. if (!$headerMatched) {
  168. throw new PHPUnit_Framework_ExpectationFailedException(sprintf(
  169. 'Failed asserting response header "%s" exists and matches regex "%s", actual content is "%s"',
  170. $header,
  171. $pattern,
  172. $currentHeader->getFieldValue()
  173. ));
  174. }
  175. $this->assertTrue($headerMatched);
  176. }
  177. /**
  178. * Assert response header does not exist and/or does not match the given regex
  179. *
  180. * @param string $header
  181. * @param string $pattern
  182. */
  183. public function assertNotResponseHeaderRegex($header, $pattern)
  184. {
  185. $responseHeader = $this->getResponseHeader($header);
  186. if (!$responseHeader) {
  187. throw new PHPUnit_Framework_ExpectationFailedException(sprintf(
  188. 'Failed asserting response header, header "%s" do not exists',
  189. $header
  190. ));
  191. }
  192. if (!$responseHeader instanceof \ArrayIterator) {
  193. $responseHeader = array($responseHeader);
  194. }
  195. $headerMatched = false;
  196. foreach ($responseHeader as $currentHeader) {
  197. $headerMatched = (bool) preg_match($pattern, $currentHeader->getFieldValue());
  198. if ($headerMatched) {
  199. throw new PHPUnit_Framework_ExpectationFailedException(sprintf(
  200. 'Failed asserting response header "%s" DOES NOT MATCH regex "%s"',
  201. $header,
  202. $pattern
  203. ));
  204. }
  205. }
  206. $this->assertFalse($headerMatched);
  207. }
  208. /**
  209. * Assert that response is a redirect
  210. */
  211. public function assertRedirect()
  212. {
  213. $responseHeader = $this->getResponseHeader('Location');
  214. if (false === $responseHeader) {
  215. throw new PHPUnit_Framework_ExpectationFailedException(
  216. 'Failed asserting response is a redirect'
  217. );
  218. }
  219. $this->assertNotEquals(false, $responseHeader);
  220. }
  221. /**
  222. * Assert that response is NOT a redirect
  223. */
  224. public function assertNotRedirect()
  225. {
  226. $responseHeader = $this->getResponseHeader('Location');
  227. if (false !== $responseHeader) {
  228. throw new PHPUnit_Framework_ExpectationFailedException(sprintf(
  229. 'Failed asserting response is NOT a redirect, actual redirection is "%s"',
  230. $responseHeader->getFieldValue()
  231. ));
  232. }
  233. $this->assertFalse($responseHeader);
  234. }
  235. /**
  236. * Assert that response redirects to given URL
  237. *
  238. * @param string $url
  239. */
  240. public function assertRedirectTo($url)
  241. {
  242. $responseHeader = $this->getResponseHeader('Location');
  243. if (!$responseHeader) {
  244. throw new PHPUnit_Framework_ExpectationFailedException(
  245. 'Failed asserting response is a redirect'
  246. );
  247. }
  248. if ($url != $responseHeader->getFieldValue()) {
  249. throw new PHPUnit_Framework_ExpectationFailedException(sprintf(
  250. 'Failed asserting response redirects to "%s", actual redirection is "%s"',
  251. $url,
  252. $responseHeader->getFieldValue()
  253. ));
  254. }
  255. $this->assertEquals($url, $responseHeader->getFieldValue());
  256. }
  257. /**
  258. * Assert that response does not redirect to given URL
  259. *
  260. * @param string $url
  261. */
  262. public function assertNotRedirectTo($url)
  263. {
  264. $responseHeader = $this->getResponseHeader('Location');
  265. if (!$responseHeader) {
  266. throw new PHPUnit_Framework_ExpectationFailedException(
  267. 'Failed asserting response is a redirect'
  268. );
  269. }
  270. if ($url == $responseHeader->getFieldValue()) {
  271. throw new PHPUnit_Framework_ExpectationFailedException(sprintf(
  272. 'Failed asserting response redirects to "%s"',
  273. $url
  274. ));
  275. }
  276. $this->assertNotEquals($url, $responseHeader->getFieldValue());
  277. }
  278. /**
  279. * Assert that redirect location matches pattern
  280. *
  281. * @param string $pattern
  282. */
  283. public function assertRedirectRegex($pattern)
  284. {
  285. $responseHeader = $this->getResponseHeader('Location');
  286. if (!$responseHeader) {
  287. throw new PHPUnit_Framework_ExpectationFailedException(
  288. 'Failed asserting response is a redirect'
  289. );
  290. }
  291. if (!preg_match($pattern, $responseHeader->getFieldValue())) {
  292. throw new PHPUnit_Framework_ExpectationFailedException(sprintf(
  293. 'Failed asserting response redirects to URL MATCHING "%s", actual redirection is "%s"',
  294. $pattern,
  295. $responseHeader->getFieldValue()
  296. ));
  297. }
  298. $this->assertTrue((bool) preg_match($pattern, $responseHeader->getFieldValue()));
  299. }
  300. /**
  301. * Assert that redirect location does not match pattern
  302. *
  303. * @param string $pattern
  304. */
  305. public function assertNotRedirectRegex($pattern)
  306. {
  307. $responseHeader = $this->getResponseHeader('Location');
  308. if (!$responseHeader) {
  309. throw new PHPUnit_Framework_ExpectationFailedException(
  310. 'Failed asserting response is a redirect'
  311. );
  312. }
  313. if (preg_match($pattern, $responseHeader->getFieldValue())) {
  314. throw new PHPUnit_Framework_ExpectationFailedException(sprintf(
  315. 'Failed asserting response DOES NOT redirect to URL MATCHING "%s"',
  316. $pattern
  317. ));
  318. }
  319. $this->assertFalse((bool) preg_match($pattern, $responseHeader->getFieldValue()));
  320. }
  321. /**
  322. * Register XPath namespaces
  323. *
  324. * @param array $xpathNamespaces
  325. */
  326. public function registerXpathNamespaces(array $xpathNamespaces)
  327. {
  328. $this->xpathNamespaces = $xpathNamespaces;
  329. }
  330. /**
  331. * Execute a DOM/XPath query
  332. *
  333. * @param string $path
  334. * @param bool $useXpath
  335. * @return Document\NodeList
  336. */
  337. private function query($path, $useXpath = false)
  338. {
  339. $response = $this->getResponse();
  340. $document = new Document($response->getContent());
  341. if ($useXpath) {
  342. $document->registerXpathNamespaces($this->xpathNamespaces);
  343. }
  344. $result = Document\Query::execute($path, $document, $useXpath ? Document\Query::TYPE_XPATH : Document\Query::TYPE_CSS);
  345. return $result;
  346. }
  347. /**
  348. * Execute a xpath query
  349. *
  350. * @param string $path
  351. * @return array
  352. */
  353. private function xpathQuery($path)
  354. {
  355. return $this->query($path, true);
  356. }
  357. /**
  358. * Count the dom query executed
  359. *
  360. * @param string $path
  361. * @return int
  362. */
  363. private function queryCount($path)
  364. {
  365. return count($this->query($path, false));
  366. }
  367. /**
  368. * Count the dom query executed
  369. *
  370. * @param string $path
  371. * @return int
  372. */
  373. private function xpathQueryCount($path)
  374. {
  375. return count($this->xpathQuery($path));
  376. }
  377. /**
  378. * Assert against DOM/XPath selection
  379. *
  380. * @param string $path
  381. * @param bool $useXpath
  382. */
  383. private function queryAssertion($path, $useXpath = false)
  384. {
  385. $method = $useXpath ? 'xpathQueryCount' : 'queryCount';
  386. $match = $this->$method($path);
  387. if (!$match > 0) {
  388. throw new PHPUnit_Framework_ExpectationFailedException(sprintf(
  389. 'Failed asserting node DENOTED BY %s EXISTS',
  390. $path
  391. ));
  392. }
  393. $this->assertTrue($match > 0);
  394. }
  395. /**
  396. * Assert against DOM selection
  397. *
  398. * @param string $path CSS selector path
  399. */
  400. public function assertQuery($path)
  401. {
  402. $this->queryAssertion($path, false);
  403. }
  404. /**
  405. * Assert against XPath selection
  406. *
  407. * @param string $path XPath path
  408. */
  409. public function assertXpathQuery($path)
  410. {
  411. $this->queryAssertion($path, true);
  412. }
  413. /**
  414. * Assert against DOM/XPath selection
  415. *
  416. * @param string $path CSS selector path
  417. * @param bool $useXpath
  418. */
  419. private function notQueryAssertion($path, $useXpath = false)
  420. {
  421. $method = $useXpath ? 'xpathQueryCount' : 'queryCount';
  422. $match = $this->$method($path);
  423. if ($match != 0) {
  424. throw new PHPUnit_Framework_ExpectationFailedException(sprintf(
  425. 'Failed asserting node DENOTED BY %s DOES NOT EXIST',
  426. $path
  427. ));
  428. }
  429. $this->assertEquals(0, $match);
  430. }
  431. /**
  432. * Assert against DOM selection
  433. *
  434. * @param string $path CSS selector path
  435. */
  436. public function assertNotQuery($path)
  437. {
  438. $this->notQueryAssertion($path, false);
  439. }
  440. /**
  441. * Assert against XPath selection
  442. *
  443. * @param string $path XPath path
  444. */
  445. public function assertNotXpathQuery($path)
  446. {
  447. $this->notQueryAssertion($path, true);
  448. }
  449. /**
  450. * Assert against DOM/XPath selection; should contain exact number of nodes
  451. *
  452. * @param string $path CSS selector path
  453. * @param string $count Number of nodes that should match
  454. * @param bool $useXpath
  455. */
  456. private function queryCountAssertion($path, $count, $useXpath = false)
  457. {
  458. $method = $useXpath ? 'xpathQueryCount' : 'queryCount';
  459. $match = $this->$method($path);
  460. if ($match != $count) {
  461. throw new PHPUnit_Framework_ExpectationFailedException(sprintf(
  462. 'Failed asserting node DENOTED BY %s OCCURS EXACTLY %d times, actually occurs %d times',
  463. $path,
  464. $count,
  465. $match
  466. ));
  467. }
  468. $this->assertEquals($match, $count);
  469. }
  470. /**
  471. * Assert against DOM selection; should contain exact number of nodes
  472. *
  473. * @param string $path CSS selector path
  474. * @param string $count Number of nodes that should match
  475. */
  476. public function assertQueryCount($path, $count)
  477. {
  478. $this->queryCountAssertion($path, $count, false);
  479. }
  480. /**
  481. * Assert against XPath selection; should contain exact number of nodes
  482. *
  483. * @param string $path XPath path
  484. * @param string $count Number of nodes that should match
  485. */
  486. public function assertXpathQueryCount($path, $count)
  487. {
  488. $this->queryCountAssertion($path, $count, true);
  489. }
  490. /**
  491. * Assert against DOM/XPath selection; should NOT contain exact number of nodes
  492. *
  493. * @param string $path CSS selector path
  494. * @param string $count Number of nodes that should NOT match
  495. * @param bool $useXpath
  496. */
  497. private function notQueryCountAssertion($path, $count, $useXpath = false)
  498. {
  499. $method = $useXpath ? 'xpathQueryCount' : 'queryCount';
  500. $match = $this->$method($path);
  501. if ($match == $count) {
  502. throw new PHPUnit_Framework_ExpectationFailedException(sprintf(
  503. 'Failed asserting node DENOTED BY %s DOES NOT OCCUR EXACTLY %d times',
  504. $path,
  505. $count
  506. ));
  507. }
  508. $this->assertNotEquals($match, $count);
  509. }
  510. /**
  511. * Assert against DOM selection; should NOT contain exact number of nodes
  512. *
  513. * @param string $path CSS selector path
  514. * @param string $count Number of nodes that should NOT match
  515. */
  516. public function assertNotQueryCount($path, $count)
  517. {
  518. $this->notQueryCountAssertion($path, $count, false);
  519. }
  520. /**
  521. * Assert against XPath selection; should NOT contain exact number of nodes
  522. *
  523. * @param string $path XPath path
  524. * @param string $count Number of nodes that should NOT match
  525. */
  526. public function assertNotXpathQueryCount($path, $count)
  527. {
  528. $this->notQueryCountAssertion($path, $count, true);
  529. }
  530. /**
  531. * Assert against DOM/XPath selection; should contain at least this number of nodes
  532. *
  533. * @param string $path CSS selector path
  534. * @param string $count Minimum number of nodes that should match
  535. * @param bool $useXpath
  536. */
  537. private function queryCountMinAssertion($path, $count, $useXpath = false)
  538. {
  539. $method = $useXpath ? 'xpathQueryCount' : 'queryCount';
  540. $match = $this->$method($path);
  541. if ($match < $count) {
  542. throw new PHPUnit_Framework_ExpectationFailedException(sprintf(
  543. 'Failed asserting node DENOTED BY %s OCCURS AT LEAST %d times, actually occurs %d times',
  544. $path,
  545. $count,
  546. $match
  547. ));
  548. }
  549. $this->assertTrue($match >= $count);
  550. }
  551. /**
  552. * Assert against DOM selection; should contain at least this number of nodes
  553. *
  554. * @param string $path CSS selector path
  555. * @param string $count Minimum number of nodes that should match
  556. */
  557. public function assertQueryCountMin($path, $count)
  558. {
  559. $this->queryCountMinAssertion($path, $count, false);
  560. }
  561. /**
  562. * Assert against XPath selection; should contain at least this number of nodes
  563. *
  564. * @param string $path XPath path
  565. * @param string $count Minimum number of nodes that should match
  566. */
  567. public function assertXpathQueryCountMin($path, $count)
  568. {
  569. $this->queryCountMinAssertion($path, $count, true);
  570. }
  571. /**
  572. * Assert against DOM/XPath selection; should contain no more than this number of nodes
  573. *
  574. * @param string $path CSS selector path
  575. * @param string $count Maximum number of nodes that should match
  576. * @param bool $useXpath
  577. */
  578. private function queryCountMaxAssertion($path, $count, $useXpath = false)
  579. {
  580. $method = $useXpath ? 'xpathQueryCount' : 'queryCount';
  581. $match = $this->$method($path);
  582. if ($match > $count) {
  583. throw new PHPUnit_Framework_ExpectationFailedException(sprintf(
  584. 'Failed asserting node DENOTED BY %s OCCURS AT MOST %d times, actually occurs %d times',
  585. $path,
  586. $count,
  587. $match
  588. ));
  589. }
  590. $this->assertTrue($match <= $count);
  591. }
  592. /**
  593. * Assert against DOM selection; should contain no more than this number of nodes
  594. *
  595. * @param string $path CSS selector path
  596. * @param string $count Maximum number of nodes that should match
  597. */
  598. public function assertQueryCountMax($path, $count)
  599. {
  600. $this->queryCountMaxAssertion($path, $count, false);
  601. }
  602. /**
  603. * Assert against XPath selection; should contain no more than this number of nodes
  604. *
  605. * @param string $path XPath path
  606. * @param string $count Maximum number of nodes that should match
  607. */
  608. public function assertXpathQueryCountMax($path, $count)
  609. {
  610. $this->queryCountMaxAssertion($path, $count, true);
  611. }
  612. /**
  613. * Assert against DOM/XPath selection; node should contain content
  614. *
  615. * @param string $path CSS selector path
  616. * @param string $match content that should be contained in matched nodes
  617. * @param bool $useXpath
  618. */
  619. private function queryContentContainsAssertion($path, $match, $useXpath = false)
  620. {
  621. $result = $this->query($path, $useXpath);
  622. if ($result->count() == 0) {
  623. throw new PHPUnit_Framework_ExpectationFailedException(sprintf(
  624. 'Failed asserting node DENOTED BY %s EXISTS',
  625. $path
  626. ));
  627. }
  628. foreach ($result as $node) {
  629. if ($node->nodeValue == $match) {
  630. $this->assertEquals($match, $node->nodeValue);
  631. return;
  632. }
  633. }
  634. throw new PHPUnit_Framework_ExpectationFailedException(sprintf(
  635. 'Failed asserting node denoted by %s CONTAINS content "%s"',
  636. $path,
  637. $match
  638. ));
  639. }
  640. /**
  641. * Assert against DOM selection; node should contain content
  642. *
  643. * @param string $path CSS selector path
  644. * @param string $match content that should be contained in matched nodes
  645. */
  646. public function assertQueryContentContains($path, $match)
  647. {
  648. $this->queryContentContainsAssertion($path, $match, false);
  649. }
  650. /**
  651. * Assert against XPath selection; node should contain content
  652. *
  653. * @param string $path XPath path
  654. * @param string $match content that should be contained in matched nodes
  655. */
  656. public function assertXpathQueryContentContains($path, $match)
  657. {
  658. $this->queryContentContainsAssertion($path, $match, true);
  659. }
  660. /**
  661. * Assert against DOM/XPath selection; node should NOT contain content
  662. *
  663. * @param string $path CSS selector path
  664. * @param string $match content that should NOT be contained in matched nodes
  665. * @param bool $useXpath
  666. */
  667. private function notQueryContentContainsAssertion($path, $match, $useXpath = false)
  668. {
  669. $result = $this->query($path, $useXpath);
  670. if ($result->count() == 0) {
  671. throw new PHPUnit_Framework_ExpectationFailedException(sprintf(
  672. 'Failed asserting node DENOTED BY %s EXISTS',
  673. $path
  674. ));
  675. }
  676. foreach ($result as $node) {
  677. if ($node->nodeValue == $match) {
  678. throw new PHPUnit_Framework_ExpectationFailedException(sprintf(
  679. 'Failed asserting node DENOTED BY %s DOES NOT CONTAIN content "%s"',
  680. $path,
  681. $match
  682. ));
  683. }
  684. }
  685. $currentValue = $node->nodeValue;
  686. $this->assertNotEquals($currentValue, $match);
  687. }
  688. /**
  689. * Assert against DOM selection; node should NOT contain content
  690. *
  691. * @param string $path CSS selector path
  692. * @param string $match content that should NOT be contained in matched nodes
  693. */
  694. public function assertNotQueryContentContains($path, $match)
  695. {
  696. $this->notQueryContentContainsAssertion($path, $match, false);
  697. }
  698. /**
  699. * Assert against XPath selection; node should NOT contain content
  700. *
  701. * @param string $path XPath path
  702. * @param string $match content that should NOT be contained in matched nodes
  703. */
  704. public function assertNotXpathQueryContentContains($path, $match)
  705. {
  706. $this->notQueryContentContainsAssertion($path, $match, true);
  707. }
  708. /**
  709. * Assert against DOM/XPath selection; node should match content
  710. *
  711. * @param string $path CSS selector path
  712. * @param string $pattern Pattern that should be contained in matched nodes
  713. * @param bool $useXpath
  714. */
  715. private function queryContentRegexAssertion($path, $pattern, $useXpath = false)
  716. {
  717. $result = $this->query($path, $useXpath);
  718. if ($result->count() == 0) {
  719. throw new PHPUnit_Framework_ExpectationFailedException(sprintf(
  720. 'Failed asserting node DENOTED BY %s EXISTS',
  721. $path
  722. ));
  723. }
  724. if (!preg_match($pattern, $result->current()->nodeValue)) {
  725. throw new PHPUnit_Framework_ExpectationFailedException(sprintf(
  726. 'Failed asserting node denoted by %s CONTAINS content MATCHING "%s", actual content is "%s"',
  727. $path,
  728. $pattern,
  729. $result->current()->nodeValue
  730. ));
  731. }
  732. $this->assertTrue((bool) preg_match($pattern, $result->current()->nodeValue));
  733. }
  734. /**
  735. * Assert against DOM selection; node should match content
  736. *
  737. * @param string $path CSS selector path
  738. * @param string $pattern Pattern that should be contained in matched nodes
  739. */
  740. public function assertQueryContentRegex($path, $pattern)
  741. {
  742. $this->queryContentRegexAssertion($path, $pattern, false);
  743. }
  744. /**
  745. * Assert against XPath selection; node should match content
  746. *
  747. * @param string $path XPath path
  748. * @param string $pattern Pattern that should be contained in matched nodes
  749. */
  750. public function assertXpathQueryContentRegex($path, $pattern)
  751. {
  752. $this->queryContentRegexAssertion($path, $pattern, true);
  753. }
  754. /**
  755. * Assert against DOM/XPath selection; node should NOT match content
  756. *
  757. * @param string $path CSS selector path
  758. * @param string $pattern pattern that should NOT be contained in matched nodes
  759. * @param bool $useXpath
  760. */
  761. private function notQueryContentRegexAssertion($path, $pattern, $useXpath = false)
  762. {
  763. $result = $this->query($path, $useXpath);
  764. if ($result->count() == 0) {
  765. throw new PHPUnit_Framework_ExpectationFailedException(sprintf(
  766. 'Failed asserting node DENOTED BY %s EXISTS',
  767. $path
  768. ));
  769. }
  770. if (preg_match($pattern, $result->current()->nodeValue)) {
  771. throw new PHPUnit_Framework_ExpectationFailedException(sprintf(
  772. 'Failed asserting node DENOTED BY %s DOES NOT CONTAIN content MATCHING "%s"',
  773. $path,
  774. $pattern
  775. ));
  776. }
  777. $this->assertFalse((bool) preg_match($pattern, $result->current()->nodeValue));
  778. }
  779. /**
  780. * Assert against DOM selection; node should NOT match content
  781. *
  782. * @param string $path CSS selector path
  783. * @param string $pattern pattern that should NOT be contained in matched nodes
  784. */
  785. public function assertNotQueryContentRegex($path, $pattern)
  786. {
  787. $this->notQueryContentRegexAssertion($path, $pattern, false);
  788. }
  789. /**
  790. * Assert against XPath selection; node should NOT match content
  791. *
  792. * @param string $path XPath path
  793. * @param string $pattern pattern that should NOT be contained in matched nodes
  794. */
  795. public function assertNotXpathQueryContentRegex($path, $pattern)
  796. {
  797. $this->notQueryContentRegexAssertion($path, $pattern, true);
  798. }
  799. }