-
-
Notifications
You must be signed in to change notification settings - Fork 9.7k
[FrameworkBundle] Allow to use the BrowserKit assertions with Panther and API Platform's test client #32207
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
[FrameworkBundle] Allow to use the BrowserKit assertions with Panther and API Platform's test client #32207
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
159 changes: 159 additions & 0 deletions
159
src/Symfony/Bundle/FrameworkBundle/Test/BrowserKitAssertionsTrait.php
This file contains hidden or 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,159 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\FrameworkBundle\Test; | ||
|
||
use PHPUnit\Framework\Constraint\LogicalAnd; | ||
use PHPUnit\Framework\Constraint\LogicalNot; | ||
use Symfony\Component\BrowserKit\AbstractBrowser; | ||
use Symfony\Component\BrowserKit\Test\Constraint as BrowserKitConstraint; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\Response; | ||
use Symfony\Component\HttpFoundation\Test\Constraint as ResponseConstraint; | ||
|
||
/** | ||
* Ideas borrowed from Laravel Dusk's assertions. | ||
* | ||
* @see https://laravel.com/docs/5.7/dusk#available-assertions | ||
*/ | ||
trait BrowserKitAssertionsTrait | ||
{ | ||
public static function assertResponseIsSuccessful(string $message = ''): void | ||
{ | ||
self::assertThat(self::getResponse(), new ResponseConstraint\ResponseIsSuccessful(), $message); | ||
} | ||
|
||
public static function assertResponseStatusCodeSame(int $expectedCode, string $message = ''): void | ||
{ | ||
self::assertThat(self::getResponse(), new ResponseConstraint\ResponseStatusCodeSame($expectedCode), $message); | ||
} | ||
|
||
public static function assertResponseRedirects(string $expectedLocation = null, int $expectedCode = null, string $message = ''): void | ||
{ | ||
$constraint = new ResponseConstraint\ResponseIsRedirected(); | ||
if ($expectedLocation) { | ||
$constraint = LogicalAnd::fromConstraints($constraint, new ResponseConstraint\ResponseHeaderSame('Location', $expectedLocation)); | ||
} | ||
if ($expectedCode) { | ||
$constraint = LogicalAnd::fromConstraints($constraint, new ResponseConstraint\ResponseStatusCodeSame($expectedCode)); | ||
} | ||
|
||
self::assertThat(self::getResponse(), $constraint, $message); | ||
} | ||
|
||
public static function assertResponseHasHeader(string $headerName, string $message = ''): void | ||
{ | ||
self::assertThat(self::getResponse(), new ResponseConstraint\ResponseHasHeader($headerName), $message); | ||
} | ||
|
||
public static function assertResponseNotHasHeader(string $headerName, string $message = ''): void | ||
{ | ||
self::assertThat(self::getResponse(), new LogicalNot(new ResponseConstraint\ResponseHasHeader($headerName)), $message); | ||
} | ||
|
||
public static function assertResponseHeaderSame(string $headerName, string $expectedValue, string $message = ''): void | ||
{ | ||
self::assertThat(self::getResponse(), new ResponseConstraint\ResponseHeaderSame($headerName, $expectedValue), $message); | ||
} | ||
|
||
public static function assertResponseHeaderNotSame(string $headerName, string $expectedValue, string $message = ''): void | ||
{ | ||
self::assertThat(self::getResponse(), new LogicalNot(new ResponseConstraint\ResponseHeaderSame($headerName, $expectedValue)), $message); | ||
} | ||
|
||
public static function assertResponseHasCookie(string $name, string $path = '/', string $domain = null, string $message = ''): void | ||
{ | ||
self::assertThat(self::getResponse(), new ResponseConstraint\ResponseHasCookie($name, $path, $domain), $message); | ||
} | ||
|
||
public static function assertResponseNotHasCookie(string $name, string $path = '/', string $domain = null, string $message = ''): void | ||
{ | ||
self::assertThat(self::getResponse(), new LogicalNot(new ResponseConstraint\ResponseHasCookie($name, $path, $domain)), $message); | ||
} | ||
|
||
public static function assertResponseCookieValueSame(string $name, string $expectedValue, string $path = '/', string $domain = null, string $message = ''): void | ||
{ | ||
self::assertThat(self::getResponse(), LogicalAnd::fromConstraints( | ||
new ResponseConstraint\ResponseHasCookie($name, $path, $domain), | ||
new ResponseConstraint\ResponseCookieValueSame($name, $expectedValue, $path, $domain) | ||
), $message); | ||
} | ||
|
||
public static function assertBrowserHasCookie(string $name, string $path = '/', string $domain = null, string $message = ''): void | ||
{ | ||
self::assertThat(self::getClient(), new BrowserKitConstraint\BrowserHasCookie($name, $path, $domain), $message); | ||
} | ||
|
||
public static function assertBrowserNotHasCookie(string $name, string $path = '/', string $domain = null, string $message = ''): void | ||
{ | ||
self::assertThat(self::getClient(), new LogicalNot(new BrowserKitConstraint\BrowserHasCookie($name, $path, $domain)), $message); | ||
} | ||
|
||
public static function assertBrowserCookieValueSame(string $name, string $expectedValue, bool $raw = false, string $path = '/', string $domain = null, string $message = ''): void | ||
{ | ||
self::assertThat(self::getClient(), LogicalAnd::fromConstraints( | ||
new BrowserKitConstraint\BrowserHasCookie($name, $path, $domain), | ||
new BrowserKitConstraint\BrowserCookieValueSame($name, $expectedValue, $raw, $path, $domain) | ||
), $message); | ||
} | ||
|
||
public static function assertRequestAttributeValueSame(string $name, string $expectedValue, string $message = ''): void | ||
{ | ||
self::assertThat(self::getRequest(), new ResponseConstraint\RequestAttributeValueSame($name, $expectedValue), $message); | ||
} | ||
|
||
public static function assertRouteSame($expectedRoute, array $parameters = [], string $message = ''): void | ||
{ | ||
$constraint = new ResponseConstraint\RequestAttributeValueSame('_route', $expectedRoute); | ||
$constraints = []; | ||
foreach ($parameters as $key => $value) { | ||
$constraints[] = new ResponseConstraint\RequestAttributeValueSame($key, $value); | ||
} | ||
if ($constraints) { | ||
$constraint = LogicalAnd::fromConstraints($constraint, ...$constraints); | ||
} | ||
|
||
self::assertThat(self::getRequest(), $constraint, $message); | ||
} | ||
|
||
private static function getClient(AbstractBrowser $newClient = null): ?AbstractBrowser | ||
{ | ||
static $client; | ||
|
||
if (0 < \func_num_args()) { | ||
return $client = $newClient; | ||
} | ||
|
||
if (!$client instanceof AbstractBrowser) { | ||
static::fail(sprintf('A client must be set to make assertions on it. Did you forget to call "%s::createClient()"?', __CLASS__)); | ||
} | ||
|
||
return $client; | ||
} | ||
|
||
private static function getResponse(): Response | ||
{ | ||
if (!$response = self::getClient()->getResponse()) { | ||
static::fail('A client must have an HTTP Response to make assertions. Did you forget to make an HTTP request?'); | ||
} | ||
|
||
return $response; | ||
} | ||
|
||
private static function getRequest(): Request | ||
{ | ||
if (!$request = self::getClient()->getRequest()) { | ||
static::fail('A client must have an HTTP Request to make assertions. Did you forget to make an HTTP request?'); | ||
} | ||
|
||
return $request; | ||
} | ||
} |
94 changes: 94 additions & 0 deletions
94
src/Symfony/Bundle/FrameworkBundle/Test/DomCrawlerAssertionsTrait.php
This file contains hidden or 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,94 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of the Symfony package. | ||
* | ||
* (c) Fabien Potencier <fabien@symfony.com> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Symfony\Bundle\FrameworkBundle\Test; | ||
|
||
use PHPUnit\Framework\Constraint\LogicalAnd; | ||
use PHPUnit\Framework\Constraint\LogicalNot; | ||
use Symfony\Component\DomCrawler\Crawler; | ||
use Symfony\Component\DomCrawler\Test\Constraint as DomCrawlerConstraint; | ||
|
||
/** | ||
* Ideas borrowed from Laravel Dusk's assertions. | ||
* | ||
* @see https://laravel.com/docs/5.7/dusk#available-assertions | ||
*/ | ||
trait DomCrawlerAssertionsTrait | ||
{ | ||
public static function assertSelectorExists(string $selector, string $message = ''): void | ||
{ | ||
self::assertThat(self::getCrawler(), new DomCrawlerConstraint\CrawlerSelectorExists($selector), $message); | ||
} | ||
|
||
public static function assertSelectorNotExists(string $selector, string $message = ''): void | ||
{ | ||
self::assertThat(self::getCrawler(), new LogicalNot(new DomCrawlerConstraint\CrawlerSelectorExists($selector)), $message); | ||
} | ||
|
||
public static function assertSelectorTextContains(string $selector, string $text, string $message = ''): void | ||
{ | ||
self::assertThat(self::getCrawler(), LogicalAnd::fromConstraints( | ||
new DomCrawlerConstraint\CrawlerSelectorExists($selector), | ||
new DomCrawlerConstraint\CrawlerSelectorTextContains($selector, $text) | ||
), $message); | ||
} | ||
|
||
public static function assertSelectorTextSame(string $selector, string $text, string $message = ''): void | ||
{ | ||
self::assertThat(self::getCrawler(), LogicalAnd::fromConstraints( | ||
new DomCrawlerConstraint\CrawlerSelectorExists($selector), | ||
new DomCrawlerConstraint\CrawlerSelectorTextSame($selector, $text) | ||
), $message); | ||
} | ||
|
||
public static function assertSelectorTextNotContains(string $selector, string $text, string $message = ''): void | ||
{ | ||
self::assertThat(self::getCrawler(), LogicalAnd::fromConstraints( | ||
new DomCrawlerConstraint\CrawlerSelectorExists($selector), | ||
new LogicalNot(new DomCrawlerConstraint\CrawlerSelectorTextContains($selector, $text)) | ||
), $message); | ||
} | ||
|
||
public static function assertPageTitleSame(string $expectedTitle, string $message = ''): void | ||
{ | ||
self::assertSelectorTextSame('title', $expectedTitle, $message); | ||
} | ||
|
||
public static function assertPageTitleContains(string $expectedTitle, string $message = ''): void | ||
{ | ||
self::assertSelectorTextContains('title', $expectedTitle, $message); | ||
} | ||
|
||
public static function assertInputValueSame(string $fieldName, string $expectedValue, string $message = ''): void | ||
{ | ||
self::assertThat(self::getCrawler(), LogicalAnd::fromConstraints( | ||
new DomCrawlerConstraint\CrawlerSelectorExists("input[name=\"$fieldName\"]"), | ||
new DomCrawlerConstraint\CrawlerSelectorAttributeValueSame("input[name=\"$fieldName\"]", 'value', $expectedValue) | ||
), $message); | ||
} | ||
|
||
public static function assertInputValueNotSame(string $fieldName, string $expectedValue, string $message = ''): void | ||
{ | ||
self::assertThat(self::getCrawler(), LogicalAnd::fromConstraints( | ||
new DomCrawlerConstraint\CrawlerSelectorExists("input[name=\"$fieldName\"]"), | ||
new LogicalNot(new DomCrawlerConstraint\CrawlerSelectorAttributeValueSame("input[name=\"$fieldName\"]", 'value', $expectedValue)) | ||
), $message); | ||
} | ||
|
||
private static function getCrawler(): Crawler | ||
{ | ||
if (!$crawler = self::getClient()->getCrawler()) { | ||
static::fail('A client must have a crawler to make assertions. Did you forget to make an HTTP request?'); | ||
} | ||
|
||
return $crawler; | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.