CrawlerConstraintTest.php 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. <?php
  2. class CrawlerConstraintTest 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\Crawler('hello', '/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(
  24. "Failed asserting that any element by 'selector' on page /user",
  25. $fail->getMessage()
  26. );
  27. $this->assertContains('+ <p>Bye world</p>', $fail->getMessage());
  28. $this->assertContains('+ <p>Bye warcraft</p>', $fail->getMessage());
  29. return;
  30. }
  31. $this->fail("should have failed, but not");
  32. }
  33. public function testFailMessageResponseWhenMoreNodes()
  34. {
  35. $html = '';
  36. for ($i = 0; $i < 15; $i++) {
  37. $html .= "<p>item $i</p>";
  38. }
  39. $nodes = new Symfony\Component\DomCrawler\Crawler($html);
  40. try {
  41. $this->constraint->evaluate($nodes->filter('p'), 'selector');
  42. } catch (\PHPUnit\Framework\AssertionFailedError $fail) {
  43. $this->assertContains(
  44. "Failed asserting that any element by 'selector' on page /user",
  45. $fail->getMessage()
  46. );
  47. $this->assertNotContains('+ <p>item 0</p>', $fail->getMessage());
  48. $this->assertNotContains('+ <p>item 14</p>', $fail->getMessage());
  49. $this->assertContains('[total 15 elements]', $fail->getMessage());
  50. return;
  51. }
  52. $this->fail("should have failed, but not");
  53. }
  54. public function testFailMessageResponseWithoutUrl()
  55. {
  56. $this->constraint = new Codeception\PHPUnit\Constraint\Crawler('hello');
  57. $nodes = new Symfony\Component\DomCrawler\Crawler('<p>Bye world</p><p>Bye warcraft</p>');
  58. try {
  59. $this->constraint->evaluate($nodes->filter('p'), 'selector');
  60. } catch (\PHPUnit\Framework\AssertionFailedError $fail) {
  61. $this->assertContains("Failed asserting that any element by 'selector'", $fail->getMessage());
  62. $this->assertNotContains("Failed asserting that any element by 'selector' on page", $fail->getMessage());
  63. return;
  64. }
  65. $this->fail("should have failed, but not");
  66. }
  67. }