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