DomCrawlerAssertionsTrait.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Bundle\FrameworkBundle\Test;
  11. use PHPUnit\Framework\Constraint\LogicalAnd;
  12. use PHPUnit\Framework\Constraint\LogicalNot;
  13. use Symfony\Component\DomCrawler\Crawler;
  14. use Symfony\Component\DomCrawler\Test\Constraint as DomCrawlerConstraint;
  15. use Symfony\Component\DomCrawler\Test\Constraint\CrawlerSelectorAttributeValueSame;
  16. use Symfony\Component\DomCrawler\Test\Constraint\CrawlerSelectorExists;
  17. /**
  18. * Ideas borrowed from Laravel Dusk's assertions.
  19. *
  20. * @see https://laravel.com/docs/5.7/dusk#available-assertions
  21. */
  22. trait DomCrawlerAssertionsTrait
  23. {
  24. public static function assertSelectorExists(string $selector, string $message = ''): void
  25. {
  26. self::assertThat(self::getCrawler(), new DomCrawlerConstraint\CrawlerSelectorExists($selector), $message);
  27. }
  28. public static function assertSelectorNotExists(string $selector, string $message = ''): void
  29. {
  30. self::assertThat(self::getCrawler(), new LogicalNot(new DomCrawlerConstraint\CrawlerSelectorExists($selector)), $message);
  31. }
  32. public static function assertSelectorTextContains(string $selector, string $text, string $message = ''): void
  33. {
  34. self::assertThat(self::getCrawler(), LogicalAnd::fromConstraints(
  35. new DomCrawlerConstraint\CrawlerSelectorExists($selector),
  36. new DomCrawlerConstraint\CrawlerSelectorTextContains($selector, $text)
  37. ), $message);
  38. }
  39. public static function assertSelectorTextSame(string $selector, string $text, string $message = ''): void
  40. {
  41. self::assertThat(self::getCrawler(), LogicalAnd::fromConstraints(
  42. new DomCrawlerConstraint\CrawlerSelectorExists($selector),
  43. new DomCrawlerConstraint\CrawlerSelectorTextSame($selector, $text)
  44. ), $message);
  45. }
  46. public static function assertSelectorTextNotContains(string $selector, string $text, string $message = ''): void
  47. {
  48. self::assertThat(self::getCrawler(), LogicalAnd::fromConstraints(
  49. new DomCrawlerConstraint\CrawlerSelectorExists($selector),
  50. new LogicalNot(new DomCrawlerConstraint\CrawlerSelectorTextContains($selector, $text))
  51. ), $message);
  52. }
  53. public static function assertPageTitleSame(string $expectedTitle, string $message = ''): void
  54. {
  55. self::assertSelectorTextSame('title', $expectedTitle, $message);
  56. }
  57. public static function assertPageTitleContains(string $expectedTitle, string $message = ''): void
  58. {
  59. self::assertSelectorTextContains('title', $expectedTitle, $message);
  60. }
  61. public static function assertInputValueSame(string $fieldName, string $expectedValue, string $message = ''): void
  62. {
  63. self::assertThat(self::getCrawler(), LogicalAnd::fromConstraints(
  64. new DomCrawlerConstraint\CrawlerSelectorExists("input[name=\"$fieldName\"]"),
  65. new DomCrawlerConstraint\CrawlerSelectorAttributeValueSame("input[name=\"$fieldName\"]", 'value', $expectedValue)
  66. ), $message);
  67. }
  68. public static function assertInputValueNotSame(string $fieldName, string $expectedValue, string $message = ''): void
  69. {
  70. self::assertThat(self::getCrawler(), LogicalAnd::fromConstraints(
  71. new DomCrawlerConstraint\CrawlerSelectorExists("input[name=\"$fieldName\"]"),
  72. new LogicalNot(new DomCrawlerConstraint\CrawlerSelectorAttributeValueSame("input[name=\"$fieldName\"]", 'value', $expectedValue))
  73. ), $message);
  74. }
  75. public static function assertCheckboxChecked(string $fieldName, string $message = ''): void
  76. {
  77. self::assertThat(self::getCrawler(), LogicalAnd::fromConstraints(
  78. new CrawlerSelectorExists("input[name=\"$fieldName\"]"),
  79. new CrawlerSelectorAttributeValueSame("input[name=\"$fieldName\"]", 'checked', 'checked')
  80. ), $message);
  81. }
  82. public static function assertCheckboxNotChecked(string $fieldName, string $message = ''): void
  83. {
  84. self::assertThat(self::getCrawler(), LogicalAnd::fromConstraints(
  85. new CrawlerSelectorExists("input[name=\"$fieldName\"]"),
  86. new LogicalNot(new CrawlerSelectorAttributeValueSame("input[name=\"$fieldName\"]", 'checked', 'checked'))
  87. ), $message);
  88. }
  89. public static function assertFormValue(string $formSelector, string $fieldName, string $value, string $message = ''): void
  90. {
  91. $node = self::getCrawler()->filter($formSelector);
  92. self::assertNotEmpty($node, sprintf('Form "%s" not found.', $formSelector));
  93. $values = $node->form()->getValues();
  94. self::assertArrayHasKey($fieldName, $values, $message ?: sprintf('Field "%s" not found in form "%s".', $fieldName, $formSelector));
  95. self::assertSame($value, $values[$fieldName]);
  96. }
  97. public static function assertNoFormValue(string $formSelector, string $fieldName, string $message = ''): void
  98. {
  99. $node = self::getCrawler()->filter($formSelector);
  100. self::assertNotEmpty($node, sprintf('Form "%s" not found.', $formSelector));
  101. $values = $node->form()->getValues();
  102. self::assertArrayNotHasKey($fieldName, $values, $message ?: sprintf('Field "%s" has a value in form "%s".', $fieldName, $formSelector));
  103. }
  104. private static function getCrawler(): Crawler
  105. {
  106. if (!$crawler = self::getClient()->getCrawler()) {
  107. static::fail('A client must have a crawler to make assertions. Did you forget to make an HTTP request?');
  108. }
  109. return $crawler;
  110. }
  111. }