-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9b77251
commit 1987a3d
Showing
5 changed files
with
424 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,6 +5,7 @@ | |
"type": "library", | ||
"keywords": [ | ||
"codeception", | ||
"functional testing", | ||
"symfony" | ||
], | ||
"authors": [ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
137 changes: 137 additions & 0 deletions
137
src/Codeception/Module/Symfony/DomCrawlerAssertionsTrait.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,137 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Codeception\Module\Symfony; | ||
|
||
use PHPUnit\Framework\Constraint\LogicalAnd; | ||
use PHPUnit\Framework\Constraint\LogicalNot; | ||
use Symfony\Component\DomCrawler\Crawler; | ||
use Symfony\Component\DomCrawler\Test\Constraint\CrawlerAnySelectorTextContains; | ||
use Symfony\Component\DomCrawler\Test\Constraint\CrawlerAnySelectorTextSame; | ||
use Symfony\Component\DomCrawler\Test\Constraint\CrawlerSelectorAttributeValueSame; | ||
use Symfony\Component\DomCrawler\Test\Constraint\CrawlerSelectorCount; | ||
use Symfony\Component\DomCrawler\Test\Constraint\CrawlerSelectorExists; | ||
use Symfony\Component\DomCrawler\Test\Constraint\CrawlerSelectorTextContains; | ||
use Symfony\Component\DomCrawler\Test\Constraint\CrawlerSelectorTextSame; | ||
|
||
trait DomCrawlerAssertionsTrait | ||
{ | ||
public function assertSelectorExists(string $selector, string $message = ''): void | ||
{ | ||
$this->assertThat($this->getCrawler(), new CrawlerSelectorExists($selector), $message); | ||
} | ||
|
||
public function assertSelectorNotExists(string $selector, string $message = ''): void | ||
{ | ||
$this->assertThat($this->getCrawler(), new LogicalNot(new CrawlerSelectorExists($selector)), $message); | ||
} | ||
|
||
public function assertSelectorCount(int $expectedCount, string $selector, string $message = ''): void | ||
{ | ||
$this->assertThat($this->getCrawler(), new CrawlerSelectorCount($expectedCount, $selector), $message); | ||
} | ||
|
||
public function assertAnySelectorTextContains(string $selector, string $text, string $message = ''): void | ||
{ | ||
$this->assertThat($this->getCrawler(), LogicalAnd::fromConstraints( | ||
new CrawlerSelectorExists($selector), | ||
new CrawlerAnySelectorTextContains($selector, $text) | ||
), $message); | ||
} | ||
|
||
public function assertAnySelectorTextSame(string $selector, string $text, string $message = ''): void | ||
{ | ||
$this->assertThat($this->getCrawler(), LogicalAnd::fromConstraints( | ||
new CrawlerSelectorExists($selector), | ||
new CrawlerAnySelectorTextSame($selector, $text) | ||
), $message); | ||
} | ||
|
||
public function assertSelectorTextNotContains(string $selector, string $text, string $message = ''): void | ||
{ | ||
$this->assertThat($this->getCrawler(), LogicalAnd::fromConstraints( | ||
new CrawlerSelectorExists($selector), | ||
new LogicalNot(new CrawlerSelectorTextContains($selector, $text)) | ||
), $message); | ||
} | ||
|
||
public function assertAnySelectorTextNotContains(string $selector, string $text, string $message = ''): void | ||
{ | ||
$this->assertThat($this->getCrawler(), LogicalAnd::fromConstraints( | ||
new CrawlerSelectorExists($selector), | ||
new LogicalNot(new CrawlerAnySelectorTextContains($selector, $text)) | ||
), $message); | ||
} | ||
|
||
public function assertPageTitleSame(string $expectedTitle, string $message = ''): void | ||
{ | ||
$this->assertSelectorTextSame('title', $expectedTitle, $message); | ||
} | ||
|
||
public function assertSelectorTextSame(string $selector, string $text, string $message = ''): void | ||
{ | ||
$this->assertThat($this->getCrawler(), LogicalAnd::fromConstraints( | ||
new CrawlerSelectorExists($selector), | ||
new CrawlerSelectorTextSame($selector, $text) | ||
), $message); | ||
} | ||
|
||
public function assertPageTitleContains(string $expectedTitle, string $message = ''): void | ||
{ | ||
$this->assertSelectorTextContains('title', $expectedTitle, $message); | ||
} | ||
|
||
public function assertSelectorTextContains(string $selector, string $text, string $message = ''): void | ||
{ | ||
$this->assertThat($this->getCrawler(), LogicalAnd::fromConstraints( | ||
new CrawlerSelectorExists($selector), | ||
new CrawlerSelectorTextContains($selector, $text) | ||
), $message); | ||
} | ||
|
||
public function assertInputValueSame(string $fieldName, string $expectedValue, string $message = ''): void | ||
{ | ||
$this->assertThat($this->getCrawler(), LogicalAnd::fromConstraints( | ||
new CrawlerSelectorExists("input[name=\"$fieldName\"]"), | ||
new CrawlerSelectorAttributeValueSame("input[name=\"$fieldName\"]", 'value', $expectedValue) | ||
), $message); | ||
} | ||
|
||
public function assertInputValueNotSame(string $fieldName, string $expectedValue, string $message = ''): void | ||
{ | ||
$this->assertThat($this->getCrawler(), LogicalAnd::fromConstraints( | ||
new CrawlerSelectorExists("input[name=\"$fieldName\"]"), | ||
new LogicalNot(new CrawlerSelectorAttributeValueSame("input[name=\"$fieldName\"]", 'value', $expectedValue)) | ||
), $message); | ||
} | ||
|
||
/** | ||
* Asserts that the checkbox with the given name is checked. | ||
*/ | ||
public function assertCheckboxChecked(string $fieldName, string $message = ''): void | ||
{ | ||
$this->assertThat( | ||
$this->getCrawler(), | ||
new CrawlerSelectorExists("input[name=\"$fieldName\"]:checked"), | ||
$message | ||
); | ||
} | ||
|
||
/** | ||
* Asserts that the checkbox with the given name is not checked. | ||
*/ | ||
public function assertCheckboxNotChecked(string $fieldName, string $message = ''): void | ||
{ | ||
$this->assertThat( | ||
$this->getCrawler(), | ||
new LogicalNot(new CrawlerSelectorExists("input[name=\"$fieldName\"]:checked")), | ||
$message | ||
); | ||
} | ||
|
||
protected function getCrawler(): Crawler | ||
{ | ||
return $this->client->getCrawler(); | ||
} | ||
} |
Oops, something went wrong.