CrawlerNotConstraintTest.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. <?php
  2. class CrawlerNotConstraintTest extends \PHPUnit\Framework\TestCase
  3. {
  4. /**
  5. * @var Codeception\PHPUnit\Constraint\Crawler
  6. */
  7. protected $constraint;
  8. public function setUp()
  9. {
  10. $this->constraint = new Codeception\PHPUnit\Constraint\CrawlerNot('warcraft', '/user');
  11. }
  12. public function testEvaluation()
  13. {
  14. $nodes = new Symfony\Component\DomCrawler\Crawler("<p>Bye world</p><p>Hello world</p>");
  15. $this->constraint->evaluate($nodes);
  16. }
  17. public function testFailMessageResponse()
  18. {
  19. $nodes = new Symfony\Component\DomCrawler\Crawler('<p>Bye world</p><p>Bye warcraft</p>');
  20. try {
  21. $this->constraint->evaluate($nodes->filter('p'), 'selector');
  22. } catch (\PHPUnit\Framework\AssertionFailedError $fail) {
  23. $this->assertContains("There was 'selector' element on page /user", $fail->getMessage());
  24. $this->assertNotContains('+ <p>Bye world</p>', $fail->getMessage());
  25. $this->assertContains('+ <p>Bye warcraft</p>', $fail->getMessage());
  26. return;
  27. }
  28. $this->fail("should have failed, but not");
  29. }
  30. public function testFailMessageResponseWhenMoreNodes()
  31. {
  32. $html = '';
  33. for ($i = 0; $i < 15; $i++) {
  34. $html .= "<p>warcraft $i</p>";
  35. }
  36. $nodes = new Symfony\Component\DomCrawler\Crawler($html);
  37. try {
  38. $this->constraint->evaluate($nodes->filter('p'), 'selector');
  39. } catch (\PHPUnit\Framework\AssertionFailedError $fail) {
  40. $this->assertContains("There was 'selector' element on page /user", $fail->getMessage());
  41. $this->assertContains('+ <p>warcraft 0</p>', $fail->getMessage());
  42. $this->assertContains('+ <p>warcraft 14</p>', $fail->getMessage());
  43. return;
  44. }
  45. $this->fail("should have failed, but not");
  46. }
  47. public function testFailMessageResponseWithoutUrl()
  48. {
  49. $this->constraint = new Codeception\PHPUnit\Constraint\CrawlerNot('warcraft');
  50. $nodes = new Symfony\Component\DomCrawler\Crawler('<p>Bye world</p><p>Bye warcraft</p>');
  51. try {
  52. $this->constraint->evaluate($nodes->filter('p'), 'selector');
  53. } catch (\PHPUnit\Framework\AssertionFailedError $fail) {
  54. $this->assertContains("There was 'selector' element", $fail->getMessage());
  55. $this->assertNotContains("There was 'selector' element on page /user", $fail->getMessage());
  56. return;
  57. }
  58. $this->fail("should have failed, but not");
  59. }
  60. }