Skip to content
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

[FrameworkBundle] fix BC-breaking property in WebTestAssertionsTrait #31880

Merged
merged 1 commit into from Jun 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
64 changes: 35 additions & 29 deletions src/Symfony/Bundle/FrameworkBundle/Test/WebTestAssertionsTrait.php
Expand Up @@ -30,12 +30,12 @@ trait WebTestAssertionsTrait
{
public static function assertResponseIsSuccessful(string $message = ''): void
{
self::assertThat(static::getResponse(), new ResponseConstraint\ResponseIsSuccessful(), $message);
self::assertThat(self::getResponse(), new ResponseConstraint\ResponseIsSuccessful(), $message);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Traits should be self contained, we shouldn't provide these as extensibility points.

}

public static function assertResponseStatusCodeSame(int $expectedCode, string $message = ''): void
{
self::assertThat(static::getResponse(), new ResponseConstraint\ResponseStatusCodeSame($expectedCode), $message);
self::assertThat(self::getResponse(), new ResponseConstraint\ResponseStatusCodeSame($expectedCode), $message);
}

public static function assertResponseRedirects(string $expectedLocation = null, int $expectedCode = null, string $message = ''): void
Expand All @@ -48,94 +48,94 @@ public static function assertResponseRedirects(string $expectedLocation = null,
$constraint = LogicalAnd::fromConstraints($constraint, new ResponseConstraint\ResponseStatusCodeSame($expectedCode));
}

self::assertThat(static::getResponse(), $constraint, $message);
self::assertThat(self::getResponse(), $constraint, $message);
}

public static function assertResponseHasHeader(string $headerName, string $message = ''): void
{
self::assertThat(static::getResponse(), new ResponseConstraint\ResponseHasHeader($headerName), $message);
self::assertThat(self::getResponse(), new ResponseConstraint\ResponseHasHeader($headerName), $message);
}

public static function assertResponseNotHasHeader(string $headerName, string $message = ''): void
{
self::assertThat(static::getResponse(), new LogicalNot(new ResponseConstraint\ResponseHasHeader($headerName)), $message);
self::assertThat(self::getResponse(), new LogicalNot(new ResponseConstraint\ResponseHasHeader($headerName)), $message);
}

public static function assertResponseHeaderSame(string $headerName, string $expectedValue, string $message = ''): void
{
self::assertThat(static::getResponse(), new ResponseConstraint\ResponseHeaderSame($headerName, $expectedValue), $message);
self::assertThat(self::getResponse(), new ResponseConstraint\ResponseHeaderSame($headerName, $expectedValue), $message);
}

public static function assertResponseHeaderNotSame(string $headerName, string $expectedValue, string $message = ''): void
{
self::assertThat(static::getResponse(), new LogicalNot(new ResponseConstraint\ResponseHeaderSame($headerName, $expectedValue)), $message);
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(static::getResponse(), new ResponseConstraint\ResponseHasCookie($name, $path, $domain), $message);
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(static::getResponse(), new LogicalNot(new ResponseConstraint\ResponseHasCookie($name, $path, $domain)), $message);
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(static::getResponse(), LogicalAnd::fromConstraints(
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(static::getClient(), new BrowserKitConstraint\BrowserHasCookie($name, $path, $domain), $message);
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(static::getClient(), new LogicalNot(new BrowserKitConstraint\BrowserHasCookie($name, $path, $domain)), $message);
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(static::getClient(), LogicalAnd::fromConstraints(
self::assertThat(self::getClient(), LogicalAnd::fromConstraints(
new BrowserKitConstraint\BrowserHasCookie($name, $path, $domain),
new BrowserKitConstraint\BrowserCookieValueSame($name, $expectedValue, $raw, $path, $domain)
), $message);
}

public static function assertSelectorExists(string $selector, string $message = ''): void
{
self::assertThat(static::getCrawler(), new DomCrawlerConstraint\CrawlerSelectorExists($selector), $message);
self::assertThat(self::getCrawler(), new DomCrawlerConstraint\CrawlerSelectorExists($selector), $message);
}

public static function assertSelectorNotExists(string $selector, string $message = ''): void
{
self::assertThat(static::getCrawler(), new LogicalNot(new DomCrawlerConstraint\CrawlerSelectorExists($selector)), $message);
self::assertThat(self::getCrawler(), new LogicalNot(new DomCrawlerConstraint\CrawlerSelectorExists($selector)), $message);
}

public static function assertSelectorTextContains(string $selector, string $text, string $message = ''): void
{
self::assertThat(static::getCrawler(), LogicalAnd::fromConstraints(
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(static::getCrawler(), LogicalAnd::fromConstraints(
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(static::getCrawler(), LogicalAnd::fromConstraints(
self::assertThat(self::getCrawler(), LogicalAnd::fromConstraints(
new DomCrawlerConstraint\CrawlerSelectorExists($selector),
new LogicalNot(new DomCrawlerConstraint\CrawlerSelectorTextContains($selector, $text))
), $message);
Expand All @@ -153,23 +153,23 @@ public static function assertPageTitleContains(string $expectedTitle, string $me

public static function assertInputValueSame(string $fieldName, string $expectedValue, string $message = ''): void
{
self::assertThat(static::getCrawler(), LogicalAnd::fromConstraints(
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(static::getCrawler(), LogicalAnd::fromConstraints(
self::assertThat(self::getCrawler(), LogicalAnd::fromConstraints(
new DomCrawlerConstraint\CrawlerSelectorExists("input[name=\"$fieldName\"]"),
new LogicalNot(new DomCrawlerConstraint\CrawlerSelectorAttributeValueSame("input[name=\"$fieldName\"]", 'value', $expectedValue))
), $message);
}

public static function assertRequestAttributeValueSame(string $name, string $expectedValue, string $message = ''): void
{
self::assertThat(static::getRequest(), new ResponseConstraint\RequestAttributeValueSame($name, $expectedValue), $message);
self::assertThat(self::getRequest(), new ResponseConstraint\RequestAttributeValueSame($name, $expectedValue), $message);
}

public static function assertRouteSame($expectedRoute, array $parameters = [], string $message = ''): void
Expand All @@ -183,21 +183,27 @@ public static function assertRouteSame($expectedRoute, array $parameters = [], s
$constraint = LogicalAnd::fromConstraints($constraint, ...$constraints);
}

self::assertThat(static::getRequest(), $constraint, $message);
self::assertThat(self::getRequest(), $constraint, $message);
}

private static function getClient(): KernelBrowser
private static function getClient(KernelBrowser $newClient = null): ?KernelBrowser
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why would it return null? If the client is null, it should alway throw an exception, shouldn't it? If it's null, all the rest of the trait's codebase would be forced to check for null pointer 😕

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

related to my answer to your other comment: getClient(null) returns null

{
if (!static::$client instanceof KernelBrowser) {
static::fail(\sprintf('A client must be set to make assertions on it. Did you forget to call "%s::createClient"?', __CLASS__));
static $client;

if (0 < \func_num_args()) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the reason to not use if ($newClient) {?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because we want to be able to reset the state using getClient(null), while getClient() shouldn't reset anything

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems fair

return $client = $newClient;
}

if (!$client instanceof KernelBrowser) {
static::fail(\sprintf('A client must be set to make assertions on it. Did you forget to call "%s::createClient()"?', __CLASS__));
}

return static::$client;
return $client;
}

private static function getCrawler(): Crawler
{
if (!$crawler = static::getClient()->getCrawler()) {
if (!$crawler = self::getClient()->getCrawler()) {
static::fail('A client must have a crawler to make assertions. Did you forget to make an HTTP request?');
}

Expand All @@ -206,7 +212,7 @@ private static function getCrawler(): Crawler

private static function getResponse(): Response
{
if (!$response = static::getClient()->getResponse()) {
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?');
}

Expand All @@ -215,7 +221,7 @@ private static function getResponse(): Response

private static function getRequest(): Request
{
if (!$request = static::getClient()->getRequest()) {
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?');
}

Expand Down
8 changes: 2 additions & 6 deletions src/Symfony/Bundle/FrameworkBundle/Test/WebTestCase.php
Expand Up @@ -23,14 +23,10 @@ abstract class WebTestCase extends KernelTestCase
{
use WebTestAssertionsTrait;

/** @var KernelBrowser|null */
protected static $client;

protected function doTearDown(): void
{
parent::doTearDown();

static::$client = null;
self::getClient(null);
}

/**
Expand All @@ -56,6 +52,6 @@ protected static function createClient(array $options = [], array $server = [])

$client->setServerParameters($server);

return static::$client = $client;
return self::getClient($client);
}
}
Expand Up @@ -277,11 +277,9 @@ private function getTester(KernelBrowser $client): WebTestCase
return new class($client) extends WebTestCase {
use WebTestAssertionsTrait;

protected static $client;

public function __construct(KernelBrowser $client)
{
static::$client = $client;
self::getClient($client);
}
};
}
Expand Down