feature #30813 New PHPUnit assertions for the WebTestCase (Pierstoval, fabpot)
This PR was merged into the 4.3-dev branch. Discussion ---------- New PHPUnit assertions for the WebTestCase | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | replaces #29990 | License | MIT | Doc PR | n/a While reviewing #29990, and working on some tests, I realized that we could do better by adding PHPUnit constraint classes in various components that are then used in WebTextCase. **Before** ```php <?php namespace App\Tests; use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; class DefaultControllerTest extends WebTestCase { public function testSomething() { $client = static::createClient(); $crawler = $client->request('GET', '/test'); $this->assertSame(200, $client->getResponse()->getStatusCode()); $this->assertContains('Hello World', $crawler->filter('h1')->text()); } } ``` **After** ```php <?php namespace App\Tests; use Symfony\Bundle\FrameworkBundle\Test\WebTestCase; class DefaultControllerTest extends WebTestCase { public function testSomething() { $client = static::createClient(); $client->request('GET', '/test'); $this->assertResponseIsSuccessful(); $this->assertSelectorTextContains('h1', 'Hello World'); } } ``` Commits -------4f91020c8d
added PHPUnit assertions in various components2f8040ee84
Create new PHPUnit assertions for the WebTestCase
This commit is contained in:
commit
17a3ccf4b3
@ -4,6 +4,7 @@ CHANGELOG
|
|||||||
4.3.0
|
4.3.0
|
||||||
-----
|
-----
|
||||||
|
|
||||||
|
* added `WebTestAssertions` trait (included by default in `WebTestCase`)
|
||||||
* renamed `Client` to `KernelBrowser`
|
* renamed `Client` to `KernelBrowser`
|
||||||
* Not passing the project directory to the constructor of the `AssetsInstallCommand` is deprecated. This argument will
|
* Not passing the project directory to the constructor of the `AssetsInstallCommand` is deprecated. This argument will
|
||||||
be mandatory in 5.0.
|
be mandatory in 5.0.
|
||||||
|
@ -23,7 +23,7 @@ use Symfony\Contracts\Service\ResetInterface;
|
|||||||
*/
|
*/
|
||||||
abstract class KernelTestCase extends TestCase
|
abstract class KernelTestCase extends TestCase
|
||||||
{
|
{
|
||||||
use KernelShutdownOnTearDownTrait;
|
use TestCaseSetUpTearDownTrait;
|
||||||
|
|
||||||
protected static $class;
|
protected static $class;
|
||||||
|
|
||||||
@ -37,6 +37,11 @@ abstract class KernelTestCase extends TestCase
|
|||||||
*/
|
*/
|
||||||
protected static $container;
|
protected static $container;
|
||||||
|
|
||||||
|
protected function doTearDown(): void
|
||||||
|
{
|
||||||
|
static::ensureKernelShutdown();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return string The Kernel class name
|
* @return string The Kernel class name
|
||||||
*
|
*
|
||||||
|
@ -13,31 +13,60 @@ namespace Symfony\Bundle\FrameworkBundle\Test;
|
|||||||
|
|
||||||
use PHPUnit\Framework\TestCase;
|
use PHPUnit\Framework\TestCase;
|
||||||
|
|
||||||
// Auto-adapt to PHPUnit 8 that added a `void` return-type to the tearDown method
|
// Auto-adapt to PHPUnit 8 that added a `void` return-type to the setUp/tearDown methods
|
||||||
|
|
||||||
if ((new \ReflectionMethod(TestCase::class, 'tearDown'))->hasReturnType()) {
|
if ((new \ReflectionMethod(TestCase::class, 'tearDown'))->hasReturnType()) {
|
||||||
/**
|
/**
|
||||||
* @internal
|
* @internal
|
||||||
*/
|
*/
|
||||||
trait KernelShutdownOnTearDownTrait
|
trait TestCaseSetUpTearDownTrait
|
||||||
{
|
{
|
||||||
|
private function doSetUp(): void
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
private function doTearDown(): void
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function setUp(): void
|
||||||
|
{
|
||||||
|
$this->doSetUp();
|
||||||
|
}
|
||||||
|
|
||||||
protected function tearDown(): void
|
protected function tearDown(): void
|
||||||
{
|
{
|
||||||
static::ensureKernelShutdown();
|
$this->doTearDown();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
/**
|
/**
|
||||||
* @internal
|
* @internal
|
||||||
*/
|
*/
|
||||||
trait KernelShutdownOnTearDownTrait
|
trait TestCaseSetUpTearDownTrait
|
||||||
{
|
{
|
||||||
|
private function doSetUp(): void
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
private function doTearDown(): void
|
||||||
|
{
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
protected function setUp()
|
||||||
|
{
|
||||||
|
$this->doSetUp();
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @return void
|
* @return void
|
||||||
*/
|
*/
|
||||||
protected function tearDown()
|
protected function tearDown()
|
||||||
{
|
{
|
||||||
static::ensureKernelShutdown();
|
$this->doTearDown();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
224
src/Symfony/Bundle/FrameworkBundle/Test/WebTestAssertions.php
Normal file
224
src/Symfony/Bundle/FrameworkBundle/Test/WebTestAssertions.php
Normal file
@ -0,0 +1,224 @@
|
|||||||
|
<?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\Bundle\FrameworkBundle\KernelBrowser;
|
||||||
|
use Symfony\Component\BrowserKit\Test\Constraint as BrowserKitConstraint;
|
||||||
|
use Symfony\Component\DomCrawler\Crawler;
|
||||||
|
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.
|
||||||
|
*
|
||||||
|
* @see https://laravel.com/docs/5.7/dusk#available-assertions
|
||||||
|
*/
|
||||||
|
trait WebTestAssertions
|
||||||
|
{
|
||||||
|
public static function assertResponseIsSuccessful(string $message = ''): void
|
||||||
|
{
|
||||||
|
self::assertThat(static::getResponse(), new ResponseConstraint\ResponseIsSuccessful(), $message);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function assertResponseStatusCodeSame(int $expectedCode, string $message = ''): void
|
||||||
|
{
|
||||||
|
self::assertThat(static::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(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);
|
||||||
|
}
|
||||||
|
if ($constraints) {
|
||||||
|
$constraint = LogicalAnd::fromConstraints($constraint, ...$constraints);
|
||||||
|
}
|
||||||
|
|
||||||
|
self::assertThat(static::getRequest(), $constraint, $message);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static function getClient(): KernelBrowser
|
||||||
|
{
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static function getCrawler(): Crawler
|
||||||
|
{
|
||||||
|
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 $crawler;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static function getResponse(): Response
|
||||||
|
{
|
||||||
|
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 $response;
|
||||||
|
}
|
||||||
|
|
||||||
|
private static function getRequest(): Request
|
||||||
|
{
|
||||||
|
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 $request;
|
||||||
|
}
|
||||||
|
}
|
@ -21,6 +21,18 @@ use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
|
|||||||
*/
|
*/
|
||||||
abstract class WebTestCase extends KernelTestCase
|
abstract class WebTestCase extends KernelTestCase
|
||||||
{
|
{
|
||||||
|
use WebTestAssertions;
|
||||||
|
|
||||||
|
/** @var Client|null */
|
||||||
|
protected static $client;
|
||||||
|
|
||||||
|
protected function doTearDown(): void
|
||||||
|
{
|
||||||
|
parent::doTearDown();
|
||||||
|
|
||||||
|
static::$client = null;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a KernelBrowser.
|
* Creates a KernelBrowser.
|
||||||
*
|
*
|
||||||
@ -44,6 +56,6 @@ abstract class WebTestCase extends KernelTestCase
|
|||||||
|
|
||||||
$client->setServerParameters($server);
|
$client->setServerParameters($server);
|
||||||
|
|
||||||
return $client;
|
return static::$client = $client;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -0,0 +1,288 @@
|
|||||||
|
<?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\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('<html><body><h1>'))->assertSelectorExists('body > h1');
|
||||||
|
$this->expectException(AssertionFailedError::class);
|
||||||
|
$this->expectExceptionMessage('matches selector "body > h1".');
|
||||||
|
$this->getCrawlerTester(new Crawler('<html><head><title>Foo'))->assertSelectorExists('body > h1');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testAssertSelectorNotExists()
|
||||||
|
{
|
||||||
|
$this->getCrawlerTester(new Crawler('<html><head><title>Foo'))->assertSelectorNotExists('body > h1');
|
||||||
|
$this->expectException(AssertionFailedError::class);
|
||||||
|
$this->expectExceptionMessage('does not match selector "body > h1".');
|
||||||
|
$this->getCrawlerTester(new Crawler('<html><body><h1>'))->assertSelectorNotExists('body > h1');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testAssertSelectorTextNotContains()
|
||||||
|
{
|
||||||
|
$this->getCrawlerTester(new Crawler('<html><body><h1>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('<html><body><h1>Foo'))->assertSelectorTextNotContains('body > h1', 'Foo');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testAssertPageTitleSame()
|
||||||
|
{
|
||||||
|
$this->getCrawlerTester(new Crawler('<html><head><title>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('<html><head><title>Foo'))->assertPageTitleSame('Bar');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testAssertPageTitleContains()
|
||||||
|
{
|
||||||
|
$this->getCrawlerTester(new Crawler('<html><head><title>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('<html><head><title>Foo'))->assertPageTitleContains('Bar');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testAssertInputValueSame()
|
||||||
|
{
|
||||||
|
$this->getCrawlerTester(new Crawler('<html><body><form><input type="text" name="username" value="Fabien">'))->assertInputValueSame('username', 'Fabien');
|
||||||
|
$this->expectException(AssertionFailedError::class);
|
||||||
|
$this->expectExceptionMessage('matches selector "input[name="password"]" and has a node matching selector "input[name="password"]" with attribute "value" of value "pa$$".');
|
||||||
|
$this->getCrawlerTester(new Crawler('<html><head><title>Foo'))->assertInputValueSame('password', 'pa$$');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testAssertInputValueNotSame()
|
||||||
|
{
|
||||||
|
$this->getCrawlerTester(new Crawler('<html><body><input type="text" name="username" value="Helene">'))->assertInputValueNotSame('username', 'Fabien');
|
||||||
|
$this->expectException(AssertionFailedError::class);
|
||||||
|
$this->expectExceptionMessage('matches selector "input[name="password"]" and does not have a node matching selector "input[name="password"]" with attribute "value" of value "pa$$".');
|
||||||
|
$this->getCrawlerTester(new Crawler('<html><body><form><input type="text" name="password" value="pa$$">'))->assertInputValueNotSame('password', 'pa$$');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testAssertRequestAttributeValueSame()
|
||||||
|
{
|
||||||
|
$this->getRequestTester()->assertRequestAttributeValueSame('foo', 'bar');
|
||||||
|
$this->expectException(AssertionFailedError::class);
|
||||||
|
$this->expectExceptionMessage('Failed asserting that the Request has attribute "foo" with value "baz".');
|
||||||
|
$this->getRequestTester()->assertRequestAttributeValueSame('foo', 'baz');
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testAssertRouteSame()
|
||||||
|
{
|
||||||
|
$this->getRequestTester()->assertRouteSame('homepage', ['foo' => 'bar']);
|
||||||
|
$this->expectException(AssertionFailedError::class);
|
||||||
|
$this->expectExceptionMessage('Failed asserting that the Request has attribute "_route" with value "articles".');
|
||||||
|
$this->getRequestTester()->assertRouteSame('articles');
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getResponseTester(Response $response): WebTestCase
|
||||||
|
{
|
||||||
|
$client = $this->createMock(KernelBrowser::class);
|
||||||
|
$client->expects($this->any())->method('getResponse')->will($this->returnValue($response));
|
||||||
|
|
||||||
|
return $this->getTester($client);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getCrawlerTester(Crawler $crawler): WebTestCase
|
||||||
|
{
|
||||||
|
$client = $this->createMock(KernelBrowser::class);
|
||||||
|
$client->expects($this->any())->method('getCrawler')->will($this->returnValue($crawler));
|
||||||
|
|
||||||
|
return $this->getTester($client);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getClientTester(): WebTestCase
|
||||||
|
{
|
||||||
|
$client = $this->createMock(KernelBrowser::class);
|
||||||
|
$jar = new CookieJar();
|
||||||
|
$jar->set(new Cookie('foo', 'bar', null, '/path', 'example.com'));
|
||||||
|
$client->expects($this->any())->method('getCookieJar')->will($this->returnValue($jar));
|
||||||
|
|
||||||
|
return $this->getTester($client);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getRequestTester(): WebTestCase
|
||||||
|
{
|
||||||
|
$client = $this->createMock(KernelBrowser::class);
|
||||||
|
$request = new Request();
|
||||||
|
$request->attributes->set('foo', 'bar');
|
||||||
|
$request->attributes->set('_route', 'homepage');
|
||||||
|
$client->expects($this->any())->method('getRequest')->will($this->returnValue($request));
|
||||||
|
|
||||||
|
return $this->getTester($client);
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getTester(KernelBrowser $client): WebTestCase
|
||||||
|
{
|
||||||
|
return new class($client) extends WebTestCase {
|
||||||
|
use WebTestAssertions;
|
||||||
|
|
||||||
|
protected static $client;
|
||||||
|
|
||||||
|
public function __construct(KernelBrowser $client)
|
||||||
|
{
|
||||||
|
static::$client = $client;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
@ -36,7 +36,7 @@
|
|||||||
"symfony/browser-kit": "^4.3",
|
"symfony/browser-kit": "^4.3",
|
||||||
"symfony/console": "^4.3",
|
"symfony/console": "^4.3",
|
||||||
"symfony/css-selector": "~3.4|~4.0",
|
"symfony/css-selector": "~3.4|~4.0",
|
||||||
"symfony/dom-crawler": "~3.4|~4.0",
|
"symfony/dom-crawler": "^4.3",
|
||||||
"symfony/polyfill-intl-icu": "~1.0",
|
"symfony/polyfill-intl-icu": "~1.0",
|
||||||
"symfony/security": "~3.4|~4.0",
|
"symfony/security": "~3.4|~4.0",
|
||||||
"symfony/form": "^4.3",
|
"symfony/form": "^4.3",
|
||||||
@ -72,6 +72,7 @@
|
|||||||
"symfony/browser-kit": "<4.3",
|
"symfony/browser-kit": "<4.3",
|
||||||
"symfony/console": "<4.3",
|
"symfony/console": "<4.3",
|
||||||
"symfony/dotenv": "<4.2",
|
"symfony/dotenv": "<4.2",
|
||||||
|
"symfony/dom-crawler": "<4.3",
|
||||||
"symfony/form": "<4.3",
|
"symfony/form": "<4.3",
|
||||||
"symfony/messenger": "<4.3",
|
"symfony/messenger": "<4.3",
|
||||||
"symfony/property-info": "<3.4",
|
"symfony/property-info": "<3.4",
|
||||||
|
@ -4,6 +4,7 @@ CHANGELOG
|
|||||||
4.3.0
|
4.3.0
|
||||||
-----
|
-----
|
||||||
|
|
||||||
|
* Added PHPUnit constraints: `BrowserCookieValueSame` and `BrowserHasCookie`
|
||||||
* Added `HttpBrowser`, an implementation of a browser with the HttpClient component
|
* Added `HttpBrowser`, an implementation of a browser with the HttpClient component
|
||||||
* Renamed `Client` to `AbstractBrowser`
|
* Renamed `Client` to `AbstractBrowser`
|
||||||
* Marked `Response` final.
|
* Marked `Response` final.
|
||||||
|
@ -0,0 +1,75 @@
|
|||||||
|
<?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\Component\BrowserKit\Test\Constraint;
|
||||||
|
|
||||||
|
use PHPUnit\Framework\Constraint\Constraint;
|
||||||
|
use Symfony\Component\BrowserKit\AbstractBrowser;
|
||||||
|
|
||||||
|
final class BrowserCookieValueSame extends Constraint
|
||||||
|
{
|
||||||
|
private $name;
|
||||||
|
private $value;
|
||||||
|
private $raw;
|
||||||
|
private $path;
|
||||||
|
private $domain;
|
||||||
|
|
||||||
|
public function __construct(string $name, string $value, bool $raw = false, string $path = '/', string $domain = null)
|
||||||
|
{
|
||||||
|
$this->name = $name;
|
||||||
|
$this->path = $path;
|
||||||
|
$this->domain = $domain;
|
||||||
|
$this->value = $value;
|
||||||
|
$this->raw = $raw;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function toString(): string
|
||||||
|
{
|
||||||
|
$str = sprintf('has cookie "%s"', $this->name);
|
||||||
|
if ('/' !== $this->path) {
|
||||||
|
$str .= sprintf(' with path "%s"', $this->path);
|
||||||
|
}
|
||||||
|
if ($this->domain) {
|
||||||
|
$str .= sprintf(' for domain "%s"', $this->domain);
|
||||||
|
}
|
||||||
|
$str .= sprintf(' with %svalue "%s"', $this->raw ? 'raw ' : '', $this->value);
|
||||||
|
|
||||||
|
return $str;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param AbstractBrowser $browser
|
||||||
|
*
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
protected function matches($browser): bool
|
||||||
|
{
|
||||||
|
$cookie = $browser->getCookieJar()->get($this->name, $this->path, $this->domain);
|
||||||
|
if (!$cookie) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->value === ($this->raw ? $cookie->getRawValue() : $cookie->getValue());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param AbstractBrowser $browser
|
||||||
|
*
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
protected function failureDescription($browser): string
|
||||||
|
{
|
||||||
|
return 'the Browser '.$this->toString();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,65 @@
|
|||||||
|
<?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\Component\BrowserKit\Test\Constraint;
|
||||||
|
|
||||||
|
use PHPUnit\Framework\Constraint\Constraint;
|
||||||
|
use Symfony\Component\BrowserKit\AbstractBrowser;
|
||||||
|
|
||||||
|
final class BrowserHasCookie extends Constraint
|
||||||
|
{
|
||||||
|
private $name;
|
||||||
|
private $path;
|
||||||
|
private $domain;
|
||||||
|
|
||||||
|
public function __construct(string $name, string $path = '/', string $domain = null)
|
||||||
|
{
|
||||||
|
$this->name = $name;
|
||||||
|
$this->path = $path;
|
||||||
|
$this->domain = $domain;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function toString(): string
|
||||||
|
{
|
||||||
|
$str = sprintf('has cookie "%s"', $this->name);
|
||||||
|
if ('/' !== $this->path) {
|
||||||
|
$str .= sprintf(' with path "%s"', $this->path);
|
||||||
|
}
|
||||||
|
if ($this->domain) {
|
||||||
|
$str .= sprintf(' for domain "%s"', $this->domain);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $str;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param AbstractBrowser $browser
|
||||||
|
*
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
protected function matches($browser): bool
|
||||||
|
{
|
||||||
|
return null !== $browser->getCookieJar()->get($this->name, $this->path, $this->domain);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param AbstractBrowser $browser
|
||||||
|
*
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
protected function failureDescription($browser): string
|
||||||
|
{
|
||||||
|
return 'the Browser '.$this->toString();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,54 @@
|
|||||||
|
<?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\Component\BrowserKit\Tests\Test\Constraint;
|
||||||
|
|
||||||
|
use PHPUnit\Framework\ExpectationFailedException;
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
use PHPUnit\Framework\TestFailure;
|
||||||
|
use Symfony\Component\BrowserKit\AbstractBrowser;
|
||||||
|
use Symfony\Component\BrowserKit\Cookie;
|
||||||
|
use Symfony\Component\BrowserKit\CookieJar;
|
||||||
|
use Symfony\Component\BrowserKit\Test\Constraint\BrowserCookieValueSame;
|
||||||
|
|
||||||
|
class BrowserCookieValueSameTest extends TestCase
|
||||||
|
{
|
||||||
|
public function testConstraint(): void
|
||||||
|
{
|
||||||
|
$browser = $this->getBrowser();
|
||||||
|
$constraint = new BrowserCookieValueSame('foo', 'bar', false, '/path');
|
||||||
|
$this->assertTrue($constraint->evaluate($browser, '', true));
|
||||||
|
$constraint = new BrowserCookieValueSame('foo', 'bar', true, '/path');
|
||||||
|
$this->assertTrue($constraint->evaluate($browser, '', true));
|
||||||
|
$constraint = new BrowserCookieValueSame('foo', 'babar', false, '/path');
|
||||||
|
$this->assertFalse($constraint->evaluate($browser, '', true));
|
||||||
|
|
||||||
|
try {
|
||||||
|
$constraint->evaluate($browser);
|
||||||
|
} catch (ExpectationFailedException $e) {
|
||||||
|
$this->assertEquals("Failed asserting that the Browser has cookie \"foo\" with path \"/path\" with value \"babar\".\n", TestFailure::exceptionToString($e));
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->fail();
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getBrowser(): AbstractBrowser
|
||||||
|
{
|
||||||
|
$browser = $this->createMock(AbstractBrowser::class);
|
||||||
|
$jar = new CookieJar();
|
||||||
|
$jar->set(new Cookie('foo', 'bar', null, '/path', 'example.com'));
|
||||||
|
$browser->expects($this->any())->method('getCookieJar')->will($this->returnValue($jar));
|
||||||
|
|
||||||
|
return $browser;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,84 @@
|
|||||||
|
<?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\Component\BrowserKit\Tests\Test\Constraint;
|
||||||
|
|
||||||
|
use PHPUnit\Framework\ExpectationFailedException;
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
use PHPUnit\Framework\TestFailure;
|
||||||
|
use Symfony\Component\BrowserKit\AbstractBrowser;
|
||||||
|
use Symfony\Component\BrowserKit\Cookie;
|
||||||
|
use Symfony\Component\BrowserKit\CookieJar;
|
||||||
|
use Symfony\Component\BrowserKit\Test\Constraint\BrowserHasCookie;
|
||||||
|
|
||||||
|
class BrowserHasCookieTest extends TestCase
|
||||||
|
{
|
||||||
|
public function testConstraint(): void
|
||||||
|
{
|
||||||
|
$browser = $this->getBrowser();
|
||||||
|
$constraint = new BrowserHasCookie('foo', '/path');
|
||||||
|
$this->assertTrue($constraint->evaluate($browser, '', true));
|
||||||
|
$constraint = new BrowserHasCookie('foo', '/path', 'example.com');
|
||||||
|
$this->assertTrue($constraint->evaluate($browser, '', true));
|
||||||
|
$constraint = new BrowserHasCookie('bar');
|
||||||
|
$this->assertFalse($constraint->evaluate($browser, '', true));
|
||||||
|
|
||||||
|
try {
|
||||||
|
$constraint->evaluate($browser);
|
||||||
|
} catch (ExpectationFailedException $e) {
|
||||||
|
$this->assertEquals("Failed asserting that the Browser has cookie \"bar\".\n", TestFailure::exceptionToString($e));
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->fail();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testConstraintWithWrongPath(): void
|
||||||
|
{
|
||||||
|
$browser = $this->getBrowser();
|
||||||
|
$constraint = new BrowserHasCookie('foo', '/other');
|
||||||
|
try {
|
||||||
|
$constraint->evaluate($browser);
|
||||||
|
} catch (ExpectationFailedException $e) {
|
||||||
|
$this->assertEquals("Failed asserting that the Browser has cookie \"foo\" with path \"/other\".\n", TestFailure::exceptionToString($e));
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->fail();
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testConstraintWithWrongDomain(): void
|
||||||
|
{
|
||||||
|
$browser = $this->getBrowser();
|
||||||
|
$constraint = new BrowserHasCookie('foo', '/path', 'example.org');
|
||||||
|
try {
|
||||||
|
$constraint->evaluate($browser);
|
||||||
|
} catch (ExpectationFailedException $e) {
|
||||||
|
$this->assertEquals("Failed asserting that the Browser has cookie \"foo\" with path \"/path\" for domain \"example.org\".\n", TestFailure::exceptionToString($e));
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->fail();
|
||||||
|
}
|
||||||
|
|
||||||
|
private function getBrowser(): AbstractBrowser
|
||||||
|
{
|
||||||
|
$browser = $this->createMock(AbstractBrowser::class);
|
||||||
|
$jar = new CookieJar();
|
||||||
|
$jar->set(new Cookie('foo', 'bar', null, '/path', 'example.com'));
|
||||||
|
$browser->expects($this->any())->method('getCookieJar')->will($this->returnValue($jar));
|
||||||
|
|
||||||
|
return $browser;
|
||||||
|
}
|
||||||
|
}
|
@ -4,6 +4,8 @@ CHANGELOG
|
|||||||
4.3.0
|
4.3.0
|
||||||
-----
|
-----
|
||||||
|
|
||||||
|
* Added PHPUnit constraints: `CrawlerSelectorAttributeValueSame`, `CrawlerSelectorExists`, `CrawlerSelectorTextContains``
|
||||||
|
and `CrawlerSelectorTextSame`
|
||||||
* Added return of element name (`_name`) in `extract()` method.
|
* Added return of element name (`_name`) in `extract()` method.
|
||||||
* Added ability to return a default value in `text()` and `html()` instead of throwing an exception when node is empty.
|
* Added ability to return a default value in `text()` and `html()` instead of throwing an exception when node is empty.
|
||||||
|
|
||||||
|
@ -0,0 +1,62 @@
|
|||||||
|
<?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\Component\DomCrawler\Test\Constraint;
|
||||||
|
|
||||||
|
use PHPUnit\Framework\Constraint\Constraint;
|
||||||
|
use Symfony\Component\DomCrawler\Crawler;
|
||||||
|
|
||||||
|
final class CrawlerSelectorAttributeValueSame extends Constraint
|
||||||
|
{
|
||||||
|
private $selector;
|
||||||
|
private $attribute;
|
||||||
|
private $expectedText;
|
||||||
|
|
||||||
|
public function __construct(string $selector, string $attribute, string $expectedText)
|
||||||
|
{
|
||||||
|
$this->selector = $selector;
|
||||||
|
$this->attribute = $attribute;
|
||||||
|
$this->expectedText = $expectedText;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function toString(): string
|
||||||
|
{
|
||||||
|
return sprintf('has a node matching selector "%s" with attribute "%s" of value "%s"', $this->selector, $this->attribute, $this->expectedText);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Crawler $crawler
|
||||||
|
*
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
protected function matches($crawler): bool
|
||||||
|
{
|
||||||
|
$crawler = $crawler->filter($this->selector);
|
||||||
|
if (!\count($crawler)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->expectedText === trim($crawler->getNode(0)->getAttribute($this->attribute));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Crawler $crawler
|
||||||
|
*
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
protected function failureDescription($crawler): string
|
||||||
|
{
|
||||||
|
return 'the Crawler '.$this->toString();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,53 @@
|
|||||||
|
<?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\Component\DomCrawler\Test\Constraint;
|
||||||
|
|
||||||
|
use PHPUnit\Framework\Constraint\Constraint;
|
||||||
|
use Symfony\Component\DomCrawler\Crawler;
|
||||||
|
|
||||||
|
final class CrawlerSelectorExists extends Constraint
|
||||||
|
{
|
||||||
|
private $selector;
|
||||||
|
|
||||||
|
public function __construct(string $selector)
|
||||||
|
{
|
||||||
|
$this->selector = $selector;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function toString(): string
|
||||||
|
{
|
||||||
|
return sprintf('matches selector "%s"', $this->selector);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Crawler $crawler
|
||||||
|
*
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
protected function matches($crawler): bool
|
||||||
|
{
|
||||||
|
return 0 < \count($crawler->filter($this->selector));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Crawler $crawler
|
||||||
|
*
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
protected function failureDescription($crawler): string
|
||||||
|
{
|
||||||
|
return 'the Crawler '.$this->toString();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,60 @@
|
|||||||
|
<?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\Component\DomCrawler\Test\Constraint;
|
||||||
|
|
||||||
|
use PHPUnit\Framework\Constraint\Constraint;
|
||||||
|
use Symfony\Component\DomCrawler\Crawler;
|
||||||
|
|
||||||
|
final class CrawlerSelectorTextContains extends Constraint
|
||||||
|
{
|
||||||
|
private $selector;
|
||||||
|
private $expectedText;
|
||||||
|
|
||||||
|
public function __construct(string $selector, string $expectedText)
|
||||||
|
{
|
||||||
|
$this->selector = $selector;
|
||||||
|
$this->expectedText = $expectedText;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function toString(): string
|
||||||
|
{
|
||||||
|
return sprintf('has a node matching selector "%s" with content containing "%s"', $this->selector, $this->expectedText);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Crawler $crawler
|
||||||
|
*
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
protected function matches($crawler): bool
|
||||||
|
{
|
||||||
|
$crawler = $crawler->filter($this->selector);
|
||||||
|
if (!\count($crawler)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return false !== \mb_strpos($crawler->text(), $this->expectedText);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Crawler $crawler
|
||||||
|
*
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
protected function failureDescription($crawler): string
|
||||||
|
{
|
||||||
|
return 'the Crawler '.$this->toString();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,60 @@
|
|||||||
|
<?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\Component\DomCrawler\Test\Constraint;
|
||||||
|
|
||||||
|
use PHPUnit\Framework\Constraint\Constraint;
|
||||||
|
use Symfony\Component\DomCrawler\Crawler;
|
||||||
|
|
||||||
|
final class CrawlerSelectorTextSame extends Constraint
|
||||||
|
{
|
||||||
|
private $selector;
|
||||||
|
private $expectedText;
|
||||||
|
|
||||||
|
public function __construct(string $selector, string $expectedText)
|
||||||
|
{
|
||||||
|
$this->selector = $selector;
|
||||||
|
$this->expectedText = $expectedText;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function toString(): string
|
||||||
|
{
|
||||||
|
return sprintf('has a node matching selector "%s" with content "%s"', $this->selector, $this->expectedText);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Crawler $crawler
|
||||||
|
*
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
protected function matches($crawler): bool
|
||||||
|
{
|
||||||
|
$crawler = $crawler->filter($this->selector);
|
||||||
|
if (!\count($crawler)) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->expectedText === trim($crawler->text());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Crawler $crawler
|
||||||
|
*
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
protected function failureDescription($crawler): string
|
||||||
|
{
|
||||||
|
return 'the Crawler '.$this->toString();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,38 @@
|
|||||||
|
<?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\Component\DomCrawler\Tests\Test\Constraint;
|
||||||
|
|
||||||
|
use PHPUnit\Framework\ExpectationFailedException;
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
use PHPUnit\Framework\TestFailure;
|
||||||
|
use Symfony\Component\DomCrawler\Crawler;
|
||||||
|
use Symfony\Component\DomCrawler\Test\Constraint\CrawlerSelectorAttributeValueSame;
|
||||||
|
|
||||||
|
class CrawlerSelectorAttributeValueSameTest extends TestCase
|
||||||
|
{
|
||||||
|
public function testConstraint(): void
|
||||||
|
{
|
||||||
|
$constraint = new CrawlerSelectorAttributeValueSame('input[name="username"]', 'value', 'Fabien');
|
||||||
|
$this->assertTrue($constraint->evaluate(new Crawler('<html><body><form><input type="text" name="username" value="Fabien">'), '', true));
|
||||||
|
$this->assertFalse($constraint->evaluate(new Crawler('<html><head><title>Bar'), '', true));
|
||||||
|
|
||||||
|
try {
|
||||||
|
$constraint->evaluate(new Crawler('<html><head><title>Bar'));
|
||||||
|
} catch (ExpectationFailedException $e) {
|
||||||
|
$this->assertEquals("Failed asserting that the Crawler has a node matching selector \"input[name=\"username\"]\" with attribute \"value\" of value \"Fabien\".\n", TestFailure::exceptionToString($e));
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->fail();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,39 @@
|
|||||||
|
<?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\Component\DomCrawler\Tests\Test\Constraint;
|
||||||
|
|
||||||
|
use PHPUnit\Framework\ExpectationFailedException;
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
use PHPUnit\Framework\TestFailure;
|
||||||
|
use Symfony\Component\DomCrawler\Crawler;
|
||||||
|
use Symfony\Component\DomCrawler\Test\Constraint\CrawlerSelectorExists;
|
||||||
|
|
||||||
|
class CrawlerSelectorExistsTest extends TestCase
|
||||||
|
{
|
||||||
|
public function testConstraint(): void
|
||||||
|
{
|
||||||
|
$constraint = new CrawlerSelectorExists('title');
|
||||||
|
$this->assertTrue($constraint->evaluate(new Crawler('<html><head><title>'), '', true));
|
||||||
|
$constraint = new CrawlerSelectorExists('h1');
|
||||||
|
$this->assertFalse($constraint->evaluate(new Crawler('<html><head><title>'), '', true));
|
||||||
|
|
||||||
|
try {
|
||||||
|
$constraint->evaluate(new Crawler('<html><head><title>'));
|
||||||
|
} catch (ExpectationFailedException $e) {
|
||||||
|
$this->assertEquals("Failed asserting that the Crawler matches selector \"h1\".\n", TestFailure::exceptionToString($e));
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->fail();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,38 @@
|
|||||||
|
<?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\Component\DomCrawler\Tests\Test\Constraint;
|
||||||
|
|
||||||
|
use PHPUnit\Framework\ExpectationFailedException;
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
use PHPUnit\Framework\TestFailure;
|
||||||
|
use Symfony\Component\DomCrawler\Crawler;
|
||||||
|
use Symfony\Component\DomCrawler\Test\Constraint\CrawlerSelectorTextContains;
|
||||||
|
|
||||||
|
class CrawlerSelectorTextContainsTest extends TestCase
|
||||||
|
{
|
||||||
|
public function testConstraint(): void
|
||||||
|
{
|
||||||
|
$constraint = new CrawlerSelectorTextContains('title', 'Foo');
|
||||||
|
$this->assertTrue($constraint->evaluate(new Crawler('<html><head><title>Foobar'), '', true));
|
||||||
|
$this->assertFalse($constraint->evaluate(new Crawler('<html><head><title>Bar'), '', true));
|
||||||
|
|
||||||
|
try {
|
||||||
|
$constraint->evaluate(new Crawler('<html><head><title>Bar'));
|
||||||
|
} catch (ExpectationFailedException $e) {
|
||||||
|
$this->assertEquals("Failed asserting that the Crawler has a node matching selector \"title\" with content containing \"Foo\".\n", TestFailure::exceptionToString($e));
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->fail();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,38 @@
|
|||||||
|
<?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\Component\DomCrawler\Tests\Test\Constraint;
|
||||||
|
|
||||||
|
use PHPUnit\Framework\ExpectationFailedException;
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
use PHPUnit\Framework\TestFailure;
|
||||||
|
use Symfony\Component\DomCrawler\Crawler;
|
||||||
|
use Symfony\Component\DomCrawler\Test\Constraint\CrawlerSelectorTextSame;
|
||||||
|
|
||||||
|
class CrawlerSelectorTextSameTest extends TestCase
|
||||||
|
{
|
||||||
|
public function testConstraint(): void
|
||||||
|
{
|
||||||
|
$constraint = new CrawlerSelectorTextSame('title', 'Foo');
|
||||||
|
$this->assertTrue($constraint->evaluate(new Crawler('<html><head><title>Foo'), '', true));
|
||||||
|
$this->assertFalse($constraint->evaluate(new Crawler('<html><head><title>Bar'), '', true));
|
||||||
|
|
||||||
|
try {
|
||||||
|
$constraint->evaluate(new Crawler('<html><head><title>Bar'));
|
||||||
|
} catch (ExpectationFailedException $e) {
|
||||||
|
$this->assertEquals("Failed asserting that the Crawler has a node matching selector \"title\" with content \"Foo\".\n", TestFailure::exceptionToString($e));
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->fail();
|
||||||
|
}
|
||||||
|
}
|
@ -4,6 +4,8 @@ CHANGELOG
|
|||||||
4.3.0
|
4.3.0
|
||||||
-----
|
-----
|
||||||
|
|
||||||
|
* added PHPUnit constraints: `RequestAttributeValueSame`, `ResponseCookieValueSame`, `ResponseHasCookie`,
|
||||||
|
`ResponseHasHeader`, `ResponseHeaderSame`, `ResponseIsRedirected`, `ResponseIsSuccessful`, and `ResponseStatusCodeSame`
|
||||||
* deprecated `MimeTypeGuesserInterface` and `ExtensionGuesserInterface` in favor of `Symfony\Component\Mime\MimeTypesInterface`.
|
* deprecated `MimeTypeGuesserInterface` and `ExtensionGuesserInterface` in favor of `Symfony\Component\Mime\MimeTypesInterface`.
|
||||||
* deprecated `MimeType` and `MimeTypeExtensionGuesser` in favor of `Symfony\Component\Mime\MimeTypes`.
|
* deprecated `MimeType` and `MimeTypeExtensionGuesser` in favor of `Symfony\Component\Mime\MimeTypes`.
|
||||||
* deprecated `FileBinaryMimeTypeGuesser` in favor of `Symfony\Component\Mime\FileBinaryMimeTypeGuesser`.
|
* deprecated `FileBinaryMimeTypeGuesser` in favor of `Symfony\Component\Mime\FileBinaryMimeTypeGuesser`.
|
||||||
|
@ -0,0 +1,54 @@
|
|||||||
|
<?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\Component\HttpFoundation\Test\Constraint;
|
||||||
|
|
||||||
|
use PHPUnit\Framework\Constraint\Constraint;
|
||||||
|
|
||||||
|
final class RequestAttributeValueSame extends Constraint
|
||||||
|
{
|
||||||
|
private $name;
|
||||||
|
private $value;
|
||||||
|
|
||||||
|
public function __construct(string $name, string $value)
|
||||||
|
{
|
||||||
|
$this->name = $name;
|
||||||
|
$this->value = $value;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function toString(): string
|
||||||
|
{
|
||||||
|
return sprintf('has attribute "%s" with value "%s"', $this->name, $this->value);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Request $request
|
||||||
|
*
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
protected function matches($request): bool
|
||||||
|
{
|
||||||
|
return $this->value === $request->attributes->get($this->name);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Request $request
|
||||||
|
*
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
protected function failureDescription($request): string
|
||||||
|
{
|
||||||
|
return 'the Request '.$this->toString();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,85 @@
|
|||||||
|
<?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\Component\HttpFoundation\Test\Constraint;
|
||||||
|
|
||||||
|
use PHPUnit\Framework\Constraint\Constraint;
|
||||||
|
use Symfony\Component\HttpFoundation\Cookie;
|
||||||
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
|
|
||||||
|
final class ResponseCookieValueSame extends Constraint
|
||||||
|
{
|
||||||
|
private $name;
|
||||||
|
private $value;
|
||||||
|
private $path;
|
||||||
|
private $domain;
|
||||||
|
|
||||||
|
public function __construct(string $name, string $value, string $path = '/', string $domain = null)
|
||||||
|
{
|
||||||
|
$this->name = $name;
|
||||||
|
$this->value = $value;
|
||||||
|
$this->path = $path;
|
||||||
|
$this->domain = $domain;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function toString(): string
|
||||||
|
{
|
||||||
|
$str = sprintf('has cookie "%s"', $this->name);
|
||||||
|
if ('/' !== $this->path) {
|
||||||
|
$str .= sprintf(' with path "%s"', $this->path);
|
||||||
|
}
|
||||||
|
if ($this->domain) {
|
||||||
|
$str .= sprintf(' for domain "%s"', $this->domain);
|
||||||
|
}
|
||||||
|
$str .= sprintf(' with value "%s"', $this->value);
|
||||||
|
|
||||||
|
return $str;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Response $response
|
||||||
|
*
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
protected function matches($response): bool
|
||||||
|
{
|
||||||
|
$cookie = $this->getCookie($response);
|
||||||
|
if (!$cookie) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return $this->value === $cookie->getValue();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Response $response
|
||||||
|
*
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
protected function failureDescription($response): string
|
||||||
|
{
|
||||||
|
return 'the Response '.$this->toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function getCookie(Response $response): ?Cookie
|
||||||
|
{
|
||||||
|
$cookies = $response->headers->getCookies();
|
||||||
|
|
||||||
|
$filteredCookies = array_filter($cookies, function (Cookie $cookie) {
|
||||||
|
return $cookie->getName() === $this->name && $cookie->getPath() === $this->path && $cookie->getDomain() === $this->domain;
|
||||||
|
});
|
||||||
|
|
||||||
|
return reset($filteredCookies) ?: null;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,77 @@
|
|||||||
|
<?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\Component\HttpFoundation\Test\Constraint;
|
||||||
|
|
||||||
|
use PHPUnit\Framework\Constraint\Constraint;
|
||||||
|
use Symfony\Component\HttpFoundation\Cookie;
|
||||||
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
|
|
||||||
|
final class ResponseHasCookie extends Constraint
|
||||||
|
{
|
||||||
|
private $name;
|
||||||
|
private $path;
|
||||||
|
private $domain;
|
||||||
|
|
||||||
|
public function __construct(string $name, string $path = '/', string $domain = null)
|
||||||
|
{
|
||||||
|
$this->name = $name;
|
||||||
|
$this->path = $path;
|
||||||
|
$this->domain = $domain;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function toString(): string
|
||||||
|
{
|
||||||
|
$str = sprintf('has cookie "%s"', $this->name);
|
||||||
|
if ('/' !== $this->path) {
|
||||||
|
$str .= sprintf(' with path "%s"', $this->path);
|
||||||
|
}
|
||||||
|
if ($this->domain) {
|
||||||
|
$str .= sprintf(' for domain "%s"', $this->domain);
|
||||||
|
}
|
||||||
|
|
||||||
|
return $str;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Response $response
|
||||||
|
*
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
protected function matches($response): bool
|
||||||
|
{
|
||||||
|
return null !== $this->getCookie($response);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Response $response
|
||||||
|
*
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
protected function failureDescription($response): string
|
||||||
|
{
|
||||||
|
return 'the Response '.$this->toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected function getCookie(Response $response): ?Cookie
|
||||||
|
{
|
||||||
|
$cookies = $response->headers->getCookies();
|
||||||
|
|
||||||
|
$filteredCookies = array_filter($cookies, function (Cookie $cookie) {
|
||||||
|
return $cookie->getName() === $this->name && $cookie->getPath() === $this->path && $cookie->getDomain() === $this->domain;
|
||||||
|
});
|
||||||
|
|
||||||
|
return reset($filteredCookies) ?: null;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,53 @@
|
|||||||
|
<?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\Component\HttpFoundation\Test\Constraint;
|
||||||
|
|
||||||
|
use PHPUnit\Framework\Constraint\Constraint;
|
||||||
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
|
|
||||||
|
final class ResponseHasHeader extends Constraint
|
||||||
|
{
|
||||||
|
private $headerName;
|
||||||
|
|
||||||
|
public function __construct(string $headerName)
|
||||||
|
{
|
||||||
|
$this->headerName = $headerName;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function toString(): string
|
||||||
|
{
|
||||||
|
return sprintf('has header "%s"', $this->headerName);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Response $response
|
||||||
|
*
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
protected function matches($response): bool
|
||||||
|
{
|
||||||
|
return $response->headers->has($this->headerName);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Response $response
|
||||||
|
*
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
protected function failureDescription($response): string
|
||||||
|
{
|
||||||
|
return 'the Response '.$this->toString();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,55 @@
|
|||||||
|
<?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\Component\HttpFoundation\Test\Constraint;
|
||||||
|
|
||||||
|
use PHPUnit\Framework\Constraint\Constraint;
|
||||||
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
|
|
||||||
|
final class ResponseHeaderSame extends Constraint
|
||||||
|
{
|
||||||
|
private $headerName;
|
||||||
|
private $expectedValue;
|
||||||
|
|
||||||
|
public function __construct(string $headerName, string $expectedValue)
|
||||||
|
{
|
||||||
|
$this->headerName = $headerName;
|
||||||
|
$this->expectedValue = $expectedValue;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function toString(): string
|
||||||
|
{
|
||||||
|
return sprintf('has header "%s" with value "%s"', $this->headerName, $this->expectedValue);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Response $response
|
||||||
|
*
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
protected function matches($response): bool
|
||||||
|
{
|
||||||
|
return $this->expectedValue === $response->headers->get($this->headerName, null, true);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Response $response
|
||||||
|
*
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
protected function failureDescription($response): string
|
||||||
|
{
|
||||||
|
return 'the Response '.$this->toString();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,56 @@
|
|||||||
|
<?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\Component\HttpFoundation\Test\Constraint;
|
||||||
|
|
||||||
|
use PHPUnit\Framework\Constraint\Constraint;
|
||||||
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
|
|
||||||
|
final class ResponseIsRedirected extends Constraint
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function toString(): string
|
||||||
|
{
|
||||||
|
return 'is redirected';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Response $response
|
||||||
|
*
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
protected function matches($response): bool
|
||||||
|
{
|
||||||
|
return $response->isRedirect();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Response $response
|
||||||
|
*
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
protected function failureDescription($response): string
|
||||||
|
{
|
||||||
|
return 'the Response '.$this->toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Response $response
|
||||||
|
*
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
protected function additionalFailureDescription($response): string
|
||||||
|
{
|
||||||
|
return (string) $response;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,56 @@
|
|||||||
|
<?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\Component\HttpFoundation\Test\Constraint;
|
||||||
|
|
||||||
|
use PHPUnit\Framework\Constraint\Constraint;
|
||||||
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
|
|
||||||
|
final class ResponseIsSuccessful extends Constraint
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function toString(): string
|
||||||
|
{
|
||||||
|
return 'is successful';
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Response $response
|
||||||
|
*
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
protected function matches($response): bool
|
||||||
|
{
|
||||||
|
return $response->isSuccessful();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Response $response
|
||||||
|
*
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
protected function failureDescription($response): string
|
||||||
|
{
|
||||||
|
return 'the Response '.$this->toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Response $response
|
||||||
|
*
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
protected function additionalFailureDescription($response): string
|
||||||
|
{
|
||||||
|
return (string) $response;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,63 @@
|
|||||||
|
<?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\Component\HttpFoundation\Test\Constraint;
|
||||||
|
|
||||||
|
use PHPUnit\Framework\Constraint\Constraint;
|
||||||
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
|
|
||||||
|
final class ResponseStatusCodeSame extends Constraint
|
||||||
|
{
|
||||||
|
private $statusCode;
|
||||||
|
|
||||||
|
public function __construct(int $statusCode)
|
||||||
|
{
|
||||||
|
$this->statusCode = $statusCode;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
public function toString(): string
|
||||||
|
{
|
||||||
|
return 'status code is '.$this->statusCode;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Response $response
|
||||||
|
*
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
protected function matches($response): bool
|
||||||
|
{
|
||||||
|
return $this->statusCode === $response->getStatusCode();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Response $response
|
||||||
|
*
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
protected function failureDescription($response): string
|
||||||
|
{
|
||||||
|
return 'the Response '.$this->toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param Response $response
|
||||||
|
*
|
||||||
|
* {@inheritdoc}
|
||||||
|
*/
|
||||||
|
protected function additionalFailureDescription($response): string
|
||||||
|
{
|
||||||
|
return (string) $response;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,41 @@
|
|||||||
|
<?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\Component\HttpFoundation\Tests\Test\Constraint;
|
||||||
|
|
||||||
|
use PHPUnit\Framework\ExpectationFailedException;
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
use PHPUnit\Framework\TestFailure;
|
||||||
|
use Symfony\Component\HttpFoundation\Request;
|
||||||
|
use Symfony\Component\HttpFoundation\Test\Constraint\RequestAttributeValueSame;
|
||||||
|
|
||||||
|
class RequestAttributeValueSameTest extends TestCase
|
||||||
|
{
|
||||||
|
public function testConstraint(): void
|
||||||
|
{
|
||||||
|
$request = new Request();
|
||||||
|
$request->attributes->set('foo', 'bar');
|
||||||
|
$constraint = new RequestAttributeValueSame('foo', 'bar');
|
||||||
|
$this->assertTrue($constraint->evaluate($request, '', true));
|
||||||
|
$constraint = new RequestAttributeValueSame('bar', 'foo');
|
||||||
|
$this->assertFalse($constraint->evaluate($request, '', true));
|
||||||
|
|
||||||
|
try {
|
||||||
|
$constraint->evaluate($request);
|
||||||
|
} catch (ExpectationFailedException $e) {
|
||||||
|
$this->assertEquals("Failed asserting that the Request has attribute \"bar\" with value \"foo\".\n", TestFailure::exceptionToString($e));
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->fail();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,44 @@
|
|||||||
|
<?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\Component\HttpFoundation\Tests\Test\Constraint;
|
||||||
|
|
||||||
|
use PHPUnit\Framework\ExpectationFailedException;
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
use PHPUnit\Framework\TestFailure;
|
||||||
|
use Symfony\Component\HttpFoundation\Cookie;
|
||||||
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
|
use Symfony\Component\HttpFoundation\Test\Constraint\ResponseCookieValueSame;
|
||||||
|
|
||||||
|
class ResponseCookieValueSameTest extends TestCase
|
||||||
|
{
|
||||||
|
public function testConstraint(): void
|
||||||
|
{
|
||||||
|
$response = new Response();
|
||||||
|
$response->headers->setCookie(Cookie::create('foo', 'bar', 0, '/path'));
|
||||||
|
$constraint = new ResponseCookieValueSame('foo', 'bar', '/path');
|
||||||
|
$this->assertTrue($constraint->evaluate($response, '', true));
|
||||||
|
$constraint = new ResponseCookieValueSame('foo', 'bar', '/path');
|
||||||
|
$this->assertTrue($constraint->evaluate($response, '', true));
|
||||||
|
$constraint = new ResponseCookieValueSame('foo', 'babar', '/path');
|
||||||
|
$this->assertFalse($constraint->evaluate($response, '', true));
|
||||||
|
|
||||||
|
try {
|
||||||
|
$constraint->evaluate($response);
|
||||||
|
} catch (ExpectationFailedException $e) {
|
||||||
|
$this->assertEquals("Failed asserting that the Response has cookie \"foo\" with path \"/path\" with value \"babar\".\n", TestFailure::exceptionToString($e));
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->fail();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,42 @@
|
|||||||
|
<?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\Component\HttpFoundation\Tests\Test\Constraint;
|
||||||
|
|
||||||
|
use PHPUnit\Framework\ExpectationFailedException;
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
use PHPUnit\Framework\TestFailure;
|
||||||
|
use Symfony\Component\HttpFoundation\Cookie;
|
||||||
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
|
use Symfony\Component\HttpFoundation\Test\Constraint\ResponseHasCookie;
|
||||||
|
|
||||||
|
class ResponseHasCookieTest extends TestCase
|
||||||
|
{
|
||||||
|
public function testConstraint(): void
|
||||||
|
{
|
||||||
|
$response = new Response();
|
||||||
|
$response->headers->setCookie(Cookie::create('foo', 'bar'));
|
||||||
|
$constraint = new ResponseHasCookie('foo');
|
||||||
|
$this->assertTrue($constraint->evaluate($response, '', true));
|
||||||
|
$constraint = new ResponseHasCookie('bar');
|
||||||
|
$this->assertFalse($constraint->evaluate($response, '', true));
|
||||||
|
|
||||||
|
try {
|
||||||
|
$constraint->evaluate($response);
|
||||||
|
} catch (ExpectationFailedException $e) {
|
||||||
|
$this->assertEquals("Failed asserting that the Response has cookie \"bar\".\n", TestFailure::exceptionToString($e));
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->fail();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,39 @@
|
|||||||
|
<?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\Component\HttpFoundation\Tests\Test\Constraint;
|
||||||
|
|
||||||
|
use PHPUnit\Framework\ExpectationFailedException;
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
use PHPUnit\Framework\TestFailure;
|
||||||
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
|
use Symfony\Component\HttpFoundation\Test\Constraint\ResponseHasHeader;
|
||||||
|
|
||||||
|
class ResponseHasHeaderTest extends TestCase
|
||||||
|
{
|
||||||
|
public function testConstraint(): void
|
||||||
|
{
|
||||||
|
$constraint = new ResponseHasHeader('Date');
|
||||||
|
$this->assertTrue($constraint->evaluate(new Response(), '', true));
|
||||||
|
$constraint = new ResponseHasHeader('X-Date');
|
||||||
|
$this->assertFalse($constraint->evaluate(new Response(), '', true));
|
||||||
|
|
||||||
|
try {
|
||||||
|
$constraint->evaluate(new Response());
|
||||||
|
} catch (ExpectationFailedException $e) {
|
||||||
|
$this->assertEquals("Failed asserting that the Response has header \"X-Date\".\n", TestFailure::exceptionToString($e));
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->fail();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,39 @@
|
|||||||
|
<?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\Component\HttpFoundation\Tests\Test\Constraint;
|
||||||
|
|
||||||
|
use PHPUnit\Framework\ExpectationFailedException;
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
use PHPUnit\Framework\TestFailure;
|
||||||
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
|
use Symfony\Component\HttpFoundation\Test\Constraint\ResponseHeaderSame;
|
||||||
|
|
||||||
|
class ResponseHeaderSameTest extends TestCase
|
||||||
|
{
|
||||||
|
public function testConstraint(): void
|
||||||
|
{
|
||||||
|
$constraint = new ResponseHeaderSame('Cache-Control', 'no-cache, private');
|
||||||
|
$this->assertTrue($constraint->evaluate(new Response(), '', true));
|
||||||
|
$constraint = new ResponseHeaderSame('Cache-Control', 'public');
|
||||||
|
$this->assertFalse($constraint->evaluate(new Response(), '', true));
|
||||||
|
|
||||||
|
try {
|
||||||
|
$constraint->evaluate(new Response());
|
||||||
|
} catch (ExpectationFailedException $e) {
|
||||||
|
$this->assertEquals("Failed asserting that the Response has header \"Cache-Control\" with value \"public\".\n", TestFailure::exceptionToString($e));
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->fail();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,39 @@
|
|||||||
|
<?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\Component\HttpFoundation\Tests\Test\Constraint;
|
||||||
|
|
||||||
|
use PHPUnit\Framework\ExpectationFailedException;
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
use PHPUnit\Framework\TestFailure;
|
||||||
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
|
use Symfony\Component\HttpFoundation\Test\Constraint\ResponseIsRedirected;
|
||||||
|
|
||||||
|
class ResponseIsRedirectedTest extends TestCase
|
||||||
|
{
|
||||||
|
public function testConstraint(): void
|
||||||
|
{
|
||||||
|
$constraint = new ResponseIsRedirected();
|
||||||
|
|
||||||
|
$this->assertTrue($constraint->evaluate(new Response('', 301), '', true));
|
||||||
|
$this->assertFalse($constraint->evaluate(new Response(), '', true));
|
||||||
|
|
||||||
|
try {
|
||||||
|
$constraint->evaluate(new Response());
|
||||||
|
} catch (ExpectationFailedException $e) {
|
||||||
|
$this->assertContains("Failed asserting that the Response is redirected.\nHTTP/1.0 200 OK", TestFailure::exceptionToString($e));
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->fail();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,39 @@
|
|||||||
|
<?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\Component\HttpFoundation\Tests\Test\Constraint;
|
||||||
|
|
||||||
|
use PHPUnit\Framework\ExpectationFailedException;
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
use PHPUnit\Framework\TestFailure;
|
||||||
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
|
use Symfony\Component\HttpFoundation\Test\Constraint\ResponseIsSuccessful;
|
||||||
|
|
||||||
|
class ResponseIsSuccessfulTest extends TestCase
|
||||||
|
{
|
||||||
|
public function testConstraint(): void
|
||||||
|
{
|
||||||
|
$constraint = new ResponseIsSuccessful();
|
||||||
|
|
||||||
|
$this->assertTrue($constraint->evaluate(new Response(), '', true));
|
||||||
|
$this->assertFalse($constraint->evaluate(new Response('', 404), '', true));
|
||||||
|
|
||||||
|
try {
|
||||||
|
$constraint->evaluate(new Response('', 404));
|
||||||
|
} catch (ExpectationFailedException $e) {
|
||||||
|
$this->assertContains("Failed asserting that the Response is successful.\nHTTP/1.0 404 Not Found", TestFailure::exceptionToString($e));
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->fail();
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,41 @@
|
|||||||
|
<?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\Component\HttpFoundation\Tests\Test\Constraint;
|
||||||
|
|
||||||
|
use PHPUnit\Framework\ExpectationFailedException;
|
||||||
|
use PHPUnit\Framework\TestCase;
|
||||||
|
use PHPUnit\Framework\TestFailure;
|
||||||
|
use Symfony\Component\HttpFoundation\Response;
|
||||||
|
use Symfony\Component\HttpFoundation\Test\Constraint\ResponseStatusCodeSame;
|
||||||
|
|
||||||
|
class ResponseStatusCodeSameTest extends TestCase
|
||||||
|
{
|
||||||
|
public function testConstraint(): void
|
||||||
|
{
|
||||||
|
$constraint = new ResponseStatusCodeSame(200);
|
||||||
|
$this->assertTrue($constraint->evaluate(new Response(), '', true));
|
||||||
|
$this->assertFalse($constraint->evaluate(new Response('', 404), '', true));
|
||||||
|
$constraint = new ResponseStatusCodeSame(404);
|
||||||
|
$this->assertTrue($constraint->evaluate(new Response('', 404), '', true));
|
||||||
|
|
||||||
|
$constraint = new ResponseStatusCodeSame(200);
|
||||||
|
try {
|
||||||
|
$constraint->evaluate(new Response('', 404));
|
||||||
|
} catch (ExpectationFailedException $e) {
|
||||||
|
$this->assertContains("Failed asserting that the Response status code is 200.\nHTTP/1.0 404 Not Found", TestFailure::exceptionToString($e));
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
$this->fail();
|
||||||
|
}
|
||||||
|
}
|
Reference in New Issue
Block a user