diff --git a/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md b/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
index ac3d971898..3d19141469 100644
--- a/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
+++ b/src/Symfony/Bundle/FrameworkBundle/CHANGELOG.md
@@ -4,6 +4,7 @@ CHANGELOG
4.3.0
-----
+ * added `WebTestAssertions` trait (included by default in `WebTestCase`)
* renamed `Client` to `KernelBrowser`
* Not passing the project directory to the constructor of the `AssetsInstallCommand` is deprecated. This argument will
be mandatory in 5.0.
diff --git a/src/Symfony/Bundle/FrameworkBundle/Test/WebTestAssertions.php b/src/Symfony/Bundle/FrameworkBundle/Test/WebTestAssertions.php
index dbc6ef65ff..25eac6fe0a 100644
--- a/src/Symfony/Bundle/FrameworkBundle/Test/WebTestAssertions.php
+++ b/src/Symfony/Bundle/FrameworkBundle/Test/WebTestAssertions.php
@@ -11,12 +11,15 @@
namespace Symfony\Bundle\FrameworkBundle\Test;
-use PHPUnit\Framework\Assert;
-use Symfony\Bundle\FrameworkBundle\Client;
+use PHPUnit\Framework\Constraint\LogicalAnd;
+use PHPUnit\Framework\Constraint\LogicalNot;
+use Symfony\Bundle\FrameworkBundle\KernelBrowser;
+use Symfony\Component\BrowserKit\Test\Constraint as BrowserKitConstraint;
use Symfony\Component\DomCrawler\Crawler;
-use Symfony\Component\HttpFoundation\Cookie;
+use Symfony\Component\DomCrawler\Test\Constraint as DomCrawlerConstraint;
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.
@@ -25,324 +28,168 @@ use Symfony\Component\HttpFoundation\Response;
*/
trait WebTestAssertions
{
- /** @var Client|null */
- protected static $client;
-
- public static function assertResponseIsSuccessful(): void
+ public static function assertResponseIsSuccessful(string $message = ''): void
{
- $response = static::getResponse();
-
- Assert::assertTrue(
- $response->isSuccessful(),
- sprintf('Response was expected to be successful, but actual HTTP code is %d.', $response->getStatusCode())
- );
+ self::assertThat(static::getResponse(), new ResponseConstraint\ResponseIsSuccessful(), $message);
}
- public static function assertHttpCodeEquals(int $expectedCode): void
+ public static function assertResponseStatusCodeSame(int $expectedCode, string $message = ''): void
{
- Assert::assertSame(
- $expectedCode,
- $code = static::getResponse()->getStatusCode(),
- sprintf('Response code "%s" does not match actual HTTP code "%s".', $expectedCode, $code)
- );
+ self::assertThat(static::getResponse(), new ResponseConstraint\ResponseStatusCodeSame($expectedCode), $message);
}
- public static function assertResponseHasHeader(string $headerName): void
+ public static function assertResponseRedirects(string $expectedLocation = null, int $expectedCode = null, string $message = ''): void
{
- Assert::assertTrue(
- static::getResponse()->headers->has($headerName),
- sprintf('Header "%s" was not found in the Response.', $headerName)
- );
- }
-
- public static function assertResponseNotHasHeader(string $headerName): void
- {
- Assert::assertFalse(
- static::getResponse()->headers->has($headerName),
- sprintf('Header "%s" was not expected to be found in the Response.', $headerName)
- );
- }
-
- public static function assertResponseHeaderEquals(string $headerName, $expectedValue): void
- {
- Assert::assertSame(
- $expectedValue,
- $value = static::getResponse()->headers->get($headerName, null, true),
- sprintf('Header "%s" with value "%s" does not equal actual value "%s".', $headerName, $expectedValue, $value)
- );
- }
-
- public static function assertResponseHeaderNotEquals(string $headerName, $expectedValue): void
- {
- Assert::assertNotSame(
- $expectedValue,
- $value = static::getResponse()->headers->get($headerName, null, true),
- sprintf('Header "%s" with value "%s" was not expected to equal actual value "%s".', $headerName, $expectedValue, $value)
- );
- }
-
- public static function assertResponseRedirects(string $expectedLocation = null, int $expectedCode = null): void
- {
- $response = static::getResponse();
-
- Assert::assertTrue(
- $response->isRedirect(),
- sprintf('Response was expected to be a redirection, but actual HTTP code is %s.', $response->getStatusCode())
- );
-
+ $constraint = new ResponseConstraint\ResponseIsRedirected();
+ if ($expectedLocation) {
+ $constraint = LogicalAnd::fromConstraints($constraint, new ResponseConstraint\ResponseHeaderSame('Location', $expectedLocation));
+ }
if ($expectedCode) {
- static::assertHttpCodeEquals($expectedCode);
+ $constraint = LogicalAnd::fromConstraints($constraint, new ResponseConstraint\ResponseStatusCodeSame($expectedCode));
}
- if (null !== $expectedLocation) {
- Assert::assertSame(
- $expectedLocation,
- $location = $response->headers->get('Location'),
- sprintf('Location "%s" does not match actual redirection URL "%s".', $expectedLocation, $location)
- );
+ self::assertThat(static::getResponse(), $constraint, $message);
+ }
+
+ public static function assertResponseHasHeader(string $headerName, string $message = ''): void
+ {
+ self::assertThat(static::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);
+ }
+
+ public static function assertResponseHeaderSame(string $headerName, string $expectedValue, string $message = ''): void
+ {
+ self::assertThat(static::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);
+ }
+
+ 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);
+ }
+
+ 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);
+ }
+
+ public static function assertResponseCookieValueSame(string $name, string $expectedValue, string $path = '/', string $domain = null, string $message = ''): void
+ {
+ self::assertThat(static::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);
+ }
+
+ 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);
+ }
+
+ 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(
+ 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);
+ }
+
+ public static function assertSelectorNotExists(string $selector, string $message = ''): void
+ {
+ self::assertThat(static::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(
+ 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(
+ 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(
+ 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(static::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(
+ 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);
+ }
+
+ 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);
}
- }
-
- public static function assertPageTitleEquals(string $expectedTitle): void
- {
- $titleNode = static::getCrawler()->filter('title');
-
- Assert::assertSame(1, $count = $titleNode->count(), sprintf('There must be one
tag in the current page but there is actually %s.', $count));
-
- Assert::assertEquals(
- $expectedTitle,
- trim($title = $titleNode->text()),
- sprintf('Expected title "%s" does not equal actual title "%s".', $expectedTitle, $title)
- );
- }
-
- public static function assertPageTitleContains(string $expectedTitle): void
- {
- $titleNode = static::getCrawler()->filter('title');
-
- Assert::assertSame(1, $count = $titleNode->count(), sprintf('There must be one tag in the current page but there is actually %s.', $count));
-
- Assert::assertContains(
- $expectedTitle,
- trim($title = $titleNode->text()),
- sprintf('Expected title "%s" does not contain "%s".', $expectedTitle, $title)
- );
- }
-
- public static function assertClientHasCookie(string $name, string $path = '/', string $domain = null): void
- {
- static::getClientForAssertion();
-
- Assert::assertNotNull(
- static::$client->getCookieJar()->get($name, $path, $domain),
- sprintf('Did not find expected cookie "%s".', $name)
- );
- }
-
- public static function assertClientNotHasCookie(string $name): void
- {
- static::getClientForAssertion();
-
- $cookie = static::$client->getCookieJar()->get($name);
-
- Assert::assertNull(
- $cookie,
- sprintf('Cookie "%s" was not expected to be set.', $name)
- );
- }
-
- public static function assertClientCookieValueEquals(string $name, $expectedValue, string $path = '/', string $domain = null): void
- {
- static::getClientForAssertion();
-
- $cookie = static::$client->getCookieJar()->get($name, $path, $domain);
-
- Assert::assertNotNull(
- $cookie,
- sprintf('Did not find expected cookie "%s".', $name)
- );
- Assert::assertSame(
- $expectedValue,
- $value = $cookie->getValue(),
- sprintf('Cookie name "%s" with value "%s" does not match actual value "%s".', $name, $expectedValue, $value)
- );
- }
-
- public static function assertClientRawCookieValueEquals(string $name, $expectedValue, string $path = '/', string $domain = null): void
- {
- static::getClientForAssertion();
-
- $cookie = static::$client->getCookieJar()->get($name, $path, $domain);
-
- Assert::assertNotNull(
- $cookie,
- sprintf('Did not find expected cookie "%s".', $name)
- );
- Assert::assertSame(
- $expectedValue,
- $value = $cookie->getRawValue(),
- sprintf('Cookie name "%s" with raw value "%s" does not match actual value "%s".', $name, $expectedValue, $value)
- );
- }
-
- public static function assertResponseHasCookie(string $name, string $path = '/', string $domain = null): void
- {
- $cookie = static::getResponseCookieFromClient($name, $path, $domain);
-
- Assert::assertNotNull(
- $cookie,
- sprintf('Did not find expected cookie "%s".', $name)
- );
- }
-
- public static function assertResponseNotHasCookie(string $name, string $path = '/', string $domain = null): void
- {
- $cookie = static::getResponseCookieFromClient($name, $path, $domain);
-
- Assert::assertNull(
- $cookie,
- sprintf('Cookie "%s" was not expected to be set.', $name)
- );
- }
-
- public static function assertResponseCookieValueEquals(string $name, $expectedValue, string $path = '/', string $domain = null): void
- {
- $cookie = static::getResponseCookieFromClient($name, $path, $domain);
-
- Assert::assertNotNull(
- $cookie,
- sprintf('Did not find expected cookie "%s".', $name)
- );
- Assert::assertSame(
- $expectedValue,
- $value = $cookie->getValue(),
- sprintf('Cookie name "%s" with value "%s" does not match actual value "%s".', $name, $expectedValue, $value)
- );
- }
-
- public static function assertResponseCookieValueNotEquals(string $name, $expectedValue, string $path = '/', string $domain = null): void
- {
- $cookie = static::getResponseCookieFromClient($name, $path, $domain);
-
- Assert::assertNotNull(
- $cookie,
- sprintf('Did not find expected cookie "%s".', $name)
- );
- Assert::assertNotSame(
- $expectedValue,
- $value = $cookie->getValue(),
- sprintf('Cookie name "%s" with value "%s" was not expected to be equal to actual value "%s".', $name, $expectedValue, $value)
- );
- }
-
- public static function assertSelectorExists(string $selector): void
- {
- $nodes = static::getCrawler()->filter($selector);
-
- Assert::assertGreaterThan(0, $nodes->count(), sprintf('Selector "%s" does not resolve to any node.', $selector));
- }
-
- public static function assertSelectorNotExists(string $selector): void
- {
- $nodes = static::getCrawler()->filter($selector);
-
- Assert::assertEquals(0, $count = $nodes->count(), sprintf('Selector "%s" resolves to "%s" nodes where it expected 0.', $selector, $count));
- }
-
- public static function assertSelectorContainsText(string $selector, string $text): void
- {
- $nodes = static::getCrawler()->filter($selector);
-
- Assert::assertGreaterThan(0, $nodes->count(), sprintf('Selector "%s" does not resolve to any node.', $selector));
-
- Assert::assertContains($text, $nodes->text(), sprintf('Selector "%s" does not contain text "%s".', $selector, $text));
- }
-
- public static function assertSelectorNotContainsText(string $selector, string $text): void
- {
- $nodes = static::getCrawler()->filter($selector);
-
- Assert::assertGreaterThan(0, $nodes->count(), sprintf('Selector "%s" does not resolve to any node.', $selector));
-
- Assert::assertNotContains($text, $nodes->text(), sprintf('Selector "%s" was expected to not contain text "%s".', $selector, $text));
- }
-
- public static function assertInputValueEquals(string $fieldName, string $expectedValue): void
- {
- $inputNode = static::getCrawler()->filter("input[name=\"$fieldName\"]");
-
- Assert::assertGreaterThan(0, $inputNode->count(), sprintf('Input with name "%s" not found on the page.', $fieldName));
-
- Assert::assertEquals(
- $expectedValue,
- $value = $inputNode->getNode(0)->getAttribute('value'),
- sprintf('Expected value "%s" for the "%s" input does not equal the actual value "%s".', $value, $fieldName, $value)
- );
- }
-
- public static function assertInputValueNotEquals(string $fieldName, string $expectedValue): void
- {
- $inputNode = static::getCrawler()->filter("input[name=\"$fieldName\"]");
-
- Assert::assertGreaterThan(0, $inputNode->count(), sprintf('Input with name "%s" not found on the page.', $fieldName));
-
- Assert::assertNotEquals(
- $expectedValue,
- $value = $inputNode->getNode(0)->getAttribute('value'),
- sprintf('Expected value "%s" for the "%s" input was expected to not equal the actual value "%s".', $value, $fieldName, $value)
- );
- }
-
- public static function assertRouteEquals($expectedRoute, array $parameters = []): void
- {
- $request = static::checkRequestAvailable();
-
- Assert::assertSame(
- $expectedRoute,
- $route = $request->attributes->get('_route'),
- sprintf('Expected route name "%s" does not match the actual value "%s".', $expectedRoute, $route)
- );
-
- if (\count($parameters)) {
- foreach ($parameters as $key => $expectedValue) {
- static::assertRequestAttributeValueEquals($key, $expectedValue);
- }
+ if ($constraints) {
+ $constraint = LogicalAnd::fromConstraints($constraint, ...$constraints);
}
+
+ self::assertThat(static::getRequest(), $constraint, $message);
}
- public static function assertRequestAttributeValueEquals(string $key, $expectedValue): void
+ private static function getClient(): KernelBrowser
{
- $request = static::checkRequestAvailable();
-
- Assert::assertSame(
- $expectedValue,
- $value = $request->attributes->get($key),
- sprintf('Expected request attribute "%s" value "%s" does not match actual value "%s".', $key, $expectedValue, $value)
- );
- }
-
- protected static function getResponseCookieFromClient(string $name, string $path = '/', string $domain = null): ?Cookie
- {
- $cookies = static::getResponse()->headers->getCookies();
-
- $filteredCookies = array_filter($cookies, function (Cookie $cookie) use ($name, $path, $domain) {
- return
- $cookie->getName() === $name
- && $cookie->getPath() === $path
- && $cookie->getDomain() === $domain
- ;
- });
-
- return reset($filteredCookies) ?: null;
- }
-
- private static function getClientForAssertion(): Client
- {
- if (!static::$client instanceof Client) {
- static::fail(\sprintf(
- 'A client must be set to make assertions on it. Did you forget to call "%s::createClient"?',
- static::class
- ));
+ 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__));
}
return static::$client;
@@ -350,34 +197,28 @@ trait WebTestAssertions
private static function getCrawler(): Crawler
{
- $client = static::getClientForAssertion();
-
- if (!$client->getCrawler()) {
+ if (!$crawler = static::getClient()->getCrawler()) {
static::fail('A client must have a crawler to make assertions. Did you forget to make an HTTP request?');
}
- return $client->getCrawler();
+ return $crawler;
}
private static function getResponse(): Response
{
- $client = static::getClientForAssertion();
-
- if (!$client->getResponse()) {
+ if (!$response = static::getClient()->getResponse()) {
static::fail('A client must have an HTTP Response to make assertions. Did you forget to make an HTTP request?');
}
- return $client->getResponse();
+ return $response;
}
- private static function checkRequestAvailable(): Request
+ private static function getRequest(): Request
{
- $client = static::getClientForAssertion();
-
- if (!$client->getRequest()) {
+ if (!$request = static::getClient()->getRequest()) {
static::fail('A client must have an HTTP Request to make assertions. Did you forget to make an HTTP request?');
}
- return $client->getRequest();
+ return $request;
}
}
diff --git a/src/Symfony/Bundle/FrameworkBundle/Test/WebTestCase.php b/src/Symfony/Bundle/FrameworkBundle/Test/WebTestCase.php
index cf6ab72cc5..aa2b12ea29 100644
--- a/src/Symfony/Bundle/FrameworkBundle/Test/WebTestCase.php
+++ b/src/Symfony/Bundle/FrameworkBundle/Test/WebTestCase.php
@@ -23,12 +23,14 @@ abstract class WebTestCase extends KernelTestCase
{
use WebTestAssertions;
+ /** @var Client|null */
+ protected static $client;
+
protected function doTearDown(): void
{
parent::doTearDown();
- if (static::$client) {
- static::$client = null;
- }
+
+ static::$client = null;
}
/**
diff --git a/src/Symfony/Bundle/FrameworkBundle/Tests/Test/WebTestCaseTest.php b/src/Symfony/Bundle/FrameworkBundle/Tests/Test/WebTestCaseTest.php
new file mode 100644
index 0000000000..92f200be1f
--- /dev/null
+++ b/src/Symfony/Bundle/FrameworkBundle/Tests/Test/WebTestCaseTest.php
@@ -0,0 +1,288 @@
+
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Bundle\FrameworkBundle\Tests\Test;
+
+use PHPUnit\Framework\AssertionFailedError;
+use PHPUnit\Framework\TestCase;
+use Symfony\Bundle\FrameworkBundle\KernelBrowser;
+use Symfony\Bundle\FrameworkBundle\Test\WebTestAssertions;
+use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
+use Symfony\Component\BrowserKit\Cookie;
+use Symfony\Component\BrowserKit\CookieJar;
+use Symfony\Component\DomCrawler\Crawler;
+use Symfony\Component\HttpFoundation\Cookie as HttpFoundationCookie;
+use Symfony\Component\HttpFoundation\Request;
+use Symfony\Component\HttpFoundation\Response;
+
+class WebTestCaseTest extends TestCase
+{
+ public function testAssertResponseIsSuccessful()
+ {
+ $this->getResponseTester(new Response())->assertResponseIsSuccessful();
+ $this->expectException(AssertionFailedError::class);
+ $this->expectExceptionMessage("Failed asserting that the Response is successful.\nHTTP/1.0 404 Not Found");
+ $this->getResponseTester(new Response('', 404))->assertResponseIsSuccessful();
+ }
+
+ public function testAssertResponseStatusCodeSame()
+ {
+ $this->getResponseTester(new Response())->assertResponseStatusCodeSame(200);
+ $this->getResponseTester(new Response('', 404))->assertResponseStatusCodeSame(404);
+ $this->expectException(AssertionFailedError::class);
+ $this->expectExceptionMessage("Failed asserting that the Response status code is 200.\nHTTP/1.0 404 Not Found");
+ $this->getResponseTester(new Response('', 404))->assertResponseStatusCodeSame(200);
+ }
+
+ public function testAssertResponseRedirects()
+ {
+ $this->getResponseTester(new Response('', 301))->assertResponseRedirects();
+ $this->expectException(AssertionFailedError::class);
+ $this->expectExceptionMessage("Failed asserting that the Response is redirected.\nHTTP/1.0 200 OK");
+ $this->getResponseTester(new Response())->assertResponseRedirects();
+ }
+
+ public function testAssertResponseRedirectsWithLocation()
+ {
+ $this->getResponseTester(new Response('', 301, ['Location' => 'https://example.com/']))->assertResponseRedirects('https://example.com/');
+ $this->expectException(AssertionFailedError::class);
+ $this->expectExceptionMessage('is redirected and has header "Location" with value "https://example.com/".');
+ $this->getResponseTester(new Response('', 301))->assertResponseRedirects('https://example.com/');
+ }
+
+ public function testAssertResponseRedirectsWithStatusCode()
+ {
+ $this->getResponseTester(new Response('', 302))->assertResponseRedirects(null, 302);
+ $this->expectException(AssertionFailedError::class);
+ $this->expectExceptionMessage('is redirected and status code is 301.');
+ $this->getResponseTester(new Response('', 302))->assertResponseRedirects(null, 301);
+ }
+
+ public function testAssertResponseRedirectsWithLocationAndStatusCode()
+ {
+ $this->getResponseTester(new Response('', 302, ['Location' => 'https://example.com/']))->assertResponseRedirects('https://example.com/', 302);
+ $this->expectException(AssertionFailedError::class);
+ $this->expectExceptionMessage('is redirected and has header "Location" with value "https://example.com/" and status code is 301.');
+ $this->getResponseTester(new Response('', 302))->assertResponseRedirects('https://example.com/', 301);
+ }
+
+ public function testAssertResponseHasHeader()
+ {
+ $this->getResponseTester(new Response())->assertResponseHasHeader('Date');
+ $this->expectException(AssertionFailedError::class);
+ $this->expectExceptionMessage('Failed asserting that the Response has header "X-Date".');
+ $this->getResponseTester(new Response())->assertResponseHasHeader('X-Date');
+ }
+
+ public function testAssertResponseNotHasHeader()
+ {
+ $this->getResponseTester(new Response())->assertResponseNotHasHeader('X-Date');
+ $this->expectException(AssertionFailedError::class);
+ $this->expectExceptionMessage('Failed asserting that the Response does not have header "Date".');
+ $this->getResponseTester(new Response())->assertResponseNotHasHeader('Date');
+ }
+
+ public function testAssertResponseHeaderSame()
+ {
+ $this->getResponseTester(new Response())->assertResponseHeaderSame('Cache-Control', 'no-cache, private');
+ $this->expectException(AssertionFailedError::class);
+ $this->expectExceptionMessage('Failed asserting that the Response has header "Cache-Control" with value "public".');
+ $this->getResponseTester(new Response())->assertResponseHeaderSame('Cache-Control', 'public');
+ }
+
+ public function testAssertResponseHeaderNotSame()
+ {
+ $this->getResponseTester(new Response())->assertResponseHeaderNotSame('Cache-Control', 'public');
+ $this->expectException(AssertionFailedError::class);
+ $this->expectExceptionMessage('Failed asserting that the Response does not have header "Cache-Control" with value "no-cache, private".');
+ $this->getResponseTester(new Response())->assertResponseHeaderNotSame('Cache-Control', 'no-cache, private');
+ }
+
+ public function testAssertResponseHasCookie()
+ {
+ $response = new Response();
+ $response->headers->setCookie(HttpFoundationCookie::create('foo', 'bar'));
+
+ $this->getResponseTester($response)->assertResponseHasCookie('foo');
+ $this->expectException(AssertionFailedError::class);
+ $this->expectExceptionMessage('Failed asserting that the Response has cookie "bar".');
+ $this->getResponseTester($response)->assertResponseHasCookie('bar');
+ }
+
+ public function testAssertResponseNotHasCookie()
+ {
+ $response = new Response();
+ $response->headers->setCookie(HttpFoundationCookie::create('foo', 'bar'));
+
+ $this->getResponseTester($response)->assertResponseNotHasCookie('bar');
+ $this->expectException(AssertionFailedError::class);
+ $this->expectExceptionMessage('Failed asserting that the Response does not have cookie "foo".');
+ $this->getResponseTester($response)->assertResponseNotHasCookie('foo');
+ }
+
+ public function testAssertResponseCookieValueSame()
+ {
+ $response = new Response();
+ $response->headers->setCookie(HttpFoundationCookie::create('foo', 'bar'));
+
+ $this->getResponseTester($response)->assertResponseCookieValueSame('foo', 'bar');
+ $this->expectException(AssertionFailedError::class);
+ $this->expectExceptionMessage('has cookie "bar" and has cookie "bar" with value "bar".');
+ $this->getResponseTester($response)->assertResponseCookieValueSame('bar', 'bar');
+ }
+
+ public function testAssertBrowserHasCookie()
+ {
+ $this->getClientTester()->assertBrowserHasCookie('foo', '/path');
+ $this->expectException(AssertionFailedError::class);
+ $this->expectExceptionMessage('Failed asserting that the Browser has cookie "bar".');
+ $this->getClientTester()->assertBrowserHasCookie('bar');
+ }
+
+ public function testAssertBrowserNotHasCookie()
+ {
+ $this->getClientTester()->assertBrowserNotHasCookie('bar');
+ $this->expectException(AssertionFailedError::class);
+ $this->expectExceptionMessage('Failed asserting that the Browser does not have cookie "foo" with path "/path".');
+ $this->getClientTester()->assertBrowserNotHasCookie('foo', '/path');
+ }
+
+ public function testAssertBrowserCookieValueSame()
+ {
+ $this->getClientTester()->assertBrowserCookieValueSame('foo', 'bar', false, '/path');
+ $this->expectException(AssertionFailedError::class);
+ $this->expectExceptionMessage('has cookie "foo" with path "/path" and has cookie "foo" with path "/path" with value "babar".');
+ $this->getClientTester()->assertBrowserCookieValueSame('foo', 'babar', false, '/path');
+ }
+
+ public function testAssertSelectorExists()
+ {
+ $this->getCrawlerTester(new Crawler(''))->assertSelectorExists('body > h1');
+ $this->expectException(AssertionFailedError::class);
+ $this->expectExceptionMessage('matches selector "body > h1".');
+ $this->getCrawlerTester(new Crawler('Foo'))->assertSelectorExists('body > h1');
+ }
+
+ public function testAssertSelectorNotExists()
+ {
+ $this->getCrawlerTester(new Crawler('Foo'))->assertSelectorNotExists('body > h1');
+ $this->expectException(AssertionFailedError::class);
+ $this->expectExceptionMessage('does not match selector "body > h1".');
+ $this->getCrawlerTester(new Crawler(''))->assertSelectorNotExists('body > h1');
+ }
+
+ public function testAssertSelectorTextNotContains()
+ {
+ $this->getCrawlerTester(new Crawler('Foo'))->assertSelectorTextNotContains('body > h1', 'Bar');
+ $this->expectException(AssertionFailedError::class);
+ $this->expectExceptionMessage('matches selector "body > h1" and does not have a node matching selector "body > h1" with content containing "Foo".');
+ $this->getCrawlerTester(new Crawler('Foo'))->assertSelectorTextNotContains('body > h1', 'Foo');
+ }
+
+ public function testAssertPageTitleSame()
+ {
+ $this->getCrawlerTester(new Crawler('Foo'))->assertPageTitleSame('Foo');
+ $this->expectException(AssertionFailedError::class);
+ $this->expectExceptionMessage('matches selector "title" and has a node matching selector "title" with content "Bar".');
+ $this->getCrawlerTester(new Crawler('Foo'))->assertPageTitleSame('Bar');
+ }
+
+ public function testAssertPageTitleContains()
+ {
+ $this->getCrawlerTester(new Crawler('Foobar'))->assertPageTitleContains('Foo');
+ $this->expectException(AssertionFailedError::class);
+ $this->expectExceptionMessage('matches selector "title" and has a node matching selector "title" with content containing "Bar".');
+ $this->getCrawlerTester(new Crawler('Foo'))->assertPageTitleContains('Bar');
+ }
+
+ public function testAssertInputValueSame()
+ {
+ $this->getCrawlerTester(new Crawler('