Skip to content

Commit cd0341e

Browse files
dunglasfabpot
authored andcommitted
[FrameworkBundle] Allow to use the BrowserKit assertions with Panther and API Platform's test client
1 parent b79a1bf commit cd0341e

File tree

4 files changed

+256
-210
lines changed

4 files changed

+256
-210
lines changed
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\Test;
13+
14+
use PHPUnit\Framework\Constraint\LogicalAnd;
15+
use PHPUnit\Framework\Constraint\LogicalNot;
16+
use Symfony\Component\BrowserKit\AbstractBrowser;
17+
use Symfony\Component\BrowserKit\Test\Constraint as BrowserKitConstraint;
18+
use Symfony\Component\HttpFoundation\Request;
19+
use Symfony\Component\HttpFoundation\Response;
20+
use Symfony\Component\HttpFoundation\Test\Constraint as ResponseConstraint;
21+
22+
/**
23+
* Ideas borrowed from Laravel Dusk's assertions.
24+
*
25+
* @see https://laravel.com/docs/5.7/dusk#available-assertions
26+
*/
27+
trait BrowserKitAssertionsTrait
28+
{
29+
public static function assertResponseIsSuccessful(string $message = ''): void
30+
{
31+
self::assertThat(self::getResponse(), new ResponseConstraint\ResponseIsSuccessful(), $message);
32+
}
33+
34+
public static function assertResponseStatusCodeSame(int $expectedCode, string $message = ''): void
35+
{
36+
self::assertThat(self::getResponse(), new ResponseConstraint\ResponseStatusCodeSame($expectedCode), $message);
37+
}
38+
39+
public static function assertResponseRedirects(string $expectedLocation = null, int $expectedCode = null, string $message = ''): void
40+
{
41+
$constraint = new ResponseConstraint\ResponseIsRedirected();
42+
if ($expectedLocation) {
43+
$constraint = LogicalAnd::fromConstraints($constraint, new ResponseConstraint\ResponseHeaderSame('Location', $expectedLocation));
44+
}
45+
if ($expectedCode) {
46+
$constraint = LogicalAnd::fromConstraints($constraint, new ResponseConstraint\ResponseStatusCodeSame($expectedCode));
47+
}
48+
49+
self::assertThat(self::getResponse(), $constraint, $message);
50+
}
51+
52+
public static function assertResponseHasHeader(string $headerName, string $message = ''): void
53+
{
54+
self::assertThat(self::getResponse(), new ResponseConstraint\ResponseHasHeader($headerName), $message);
55+
}
56+
57+
public static function assertResponseNotHasHeader(string $headerName, string $message = ''): void
58+
{
59+
self::assertThat(self::getResponse(), new LogicalNot(new ResponseConstraint\ResponseHasHeader($headerName)), $message);
60+
}
61+
62+
public static function assertResponseHeaderSame(string $headerName, string $expectedValue, string $message = ''): void
63+
{
64+
self::assertThat(self::getResponse(), new ResponseConstraint\ResponseHeaderSame($headerName, $expectedValue), $message);
65+
}
66+
67+
public static function assertResponseHeaderNotSame(string $headerName, string $expectedValue, string $message = ''): void
68+
{
69+
self::assertThat(self::getResponse(), new LogicalNot(new ResponseConstraint\ResponseHeaderSame($headerName, $expectedValue)), $message);
70+
}
71+
72+
public static function assertResponseHasCookie(string $name, string $path = '/', string $domain = null, string $message = ''): void
73+
{
74+
self::assertThat(self::getResponse(), new ResponseConstraint\ResponseHasCookie($name, $path, $domain), $message);
75+
}
76+
77+
public static function assertResponseNotHasCookie(string $name, string $path = '/', string $domain = null, string $message = ''): void
78+
{
79+
self::assertThat(self::getResponse(), new LogicalNot(new ResponseConstraint\ResponseHasCookie($name, $path, $domain)), $message);
80+
}
81+
82+
public static function assertResponseCookieValueSame(string $name, string $expectedValue, string $path = '/', string $domain = null, string $message = ''): void
83+
{
84+
self::assertThat(self::getResponse(), LogicalAnd::fromConstraints(
85+
new ResponseConstraint\ResponseHasCookie($name, $path, $domain),
86+
new ResponseConstraint\ResponseCookieValueSame($name, $expectedValue, $path, $domain)
87+
), $message);
88+
}
89+
90+
public static function assertBrowserHasCookie(string $name, string $path = '/', string $domain = null, string $message = ''): void
91+
{
92+
self::assertThat(self::getClient(), new BrowserKitConstraint\BrowserHasCookie($name, $path, $domain), $message);
93+
}
94+
95+
public static function assertBrowserNotHasCookie(string $name, string $path = '/', string $domain = null, string $message = ''): void
96+
{
97+
self::assertThat(self::getClient(), new LogicalNot(new BrowserKitConstraint\BrowserHasCookie($name, $path, $domain)), $message);
98+
}
99+
100+
public static function assertBrowserCookieValueSame(string $name, string $expectedValue, bool $raw = false, string $path = '/', string $domain = null, string $message = ''): void
101+
{
102+
self::assertThat(self::getClient(), LogicalAnd::fromConstraints(
103+
new BrowserKitConstraint\BrowserHasCookie($name, $path, $domain),
104+
new BrowserKitConstraint\BrowserCookieValueSame($name, $expectedValue, $raw, $path, $domain)
105+
), $message);
106+
}
107+
108+
public static function assertRequestAttributeValueSame(string $name, string $expectedValue, string $message = ''): void
109+
{
110+
self::assertThat(self::getRequest(), new ResponseConstraint\RequestAttributeValueSame($name, $expectedValue), $message);
111+
}
112+
113+
public static function assertRouteSame($expectedRoute, array $parameters = [], string $message = ''): void
114+
{
115+
$constraint = new ResponseConstraint\RequestAttributeValueSame('_route', $expectedRoute);
116+
$constraints = [];
117+
foreach ($parameters as $key => $value) {
118+
$constraints[] = new ResponseConstraint\RequestAttributeValueSame($key, $value);
119+
}
120+
if ($constraints) {
121+
$constraint = LogicalAnd::fromConstraints($constraint, ...$constraints);
122+
}
123+
124+
self::assertThat(self::getRequest(), $constraint, $message);
125+
}
126+
127+
private static function getClient(AbstractBrowser $newClient = null): ?AbstractBrowser
128+
{
129+
static $client;
130+
131+
if (0 < \func_num_args()) {
132+
return $client = $newClient;
133+
}
134+
135+
if (!$client instanceof AbstractBrowser) {
136+
static::fail(sprintf('A client must be set to make assertions on it. Did you forget to call "%s::createClient()"?', __CLASS__));
137+
}
138+
139+
return $client;
140+
}
141+
142+
private static function getResponse(): Response
143+
{
144+
if (!$response = self::getClient()->getResponse()) {
145+
static::fail('A client must have an HTTP Response to make assertions. Did you forget to make an HTTP request?');
146+
}
147+
148+
return $response;
149+
}
150+
151+
private static function getRequest(): Request
152+
{
153+
if (!$request = self::getClient()->getRequest()) {
154+
static::fail('A client must have an HTTP Request to make assertions. Did you forget to make an HTTP request?');
155+
}
156+
157+
return $request;
158+
}
159+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Bundle\FrameworkBundle\Test;
13+
14+
use PHPUnit\Framework\Constraint\LogicalAnd;
15+
use PHPUnit\Framework\Constraint\LogicalNot;
16+
use Symfony\Component\DomCrawler\Crawler;
17+
use Symfony\Component\DomCrawler\Test\Constraint as DomCrawlerConstraint;
18+
19+
/**
20+
* Ideas borrowed from Laravel Dusk's assertions.
21+
*
22+
* @see https://laravel.com/docs/5.7/dusk#available-assertions
23+
*/
24+
trait DomCrawlerAssertionsTrait
25+
{
26+
public static function assertSelectorExists(string $selector, string $message = ''): void
27+
{
28+
self::assertThat(self::getCrawler(), new DomCrawlerConstraint\CrawlerSelectorExists($selector), $message);
29+
}
30+
31+
public static function assertSelectorNotExists(string $selector, string $message = ''): void
32+
{
33+
self::assertThat(self::getCrawler(), new LogicalNot(new DomCrawlerConstraint\CrawlerSelectorExists($selector)), $message);
34+
}
35+
36+
public static function assertSelectorTextContains(string $selector, string $text, string $message = ''): void
37+
{
38+
self::assertThat(self::getCrawler(), LogicalAnd::fromConstraints(
39+
new DomCrawlerConstraint\CrawlerSelectorExists($selector),
40+
new DomCrawlerConstraint\CrawlerSelectorTextContains($selector, $text)
41+
), $message);
42+
}
43+
44+
public static function assertSelectorTextSame(string $selector, string $text, string $message = ''): void
45+
{
46+
self::assertThat(self::getCrawler(), LogicalAnd::fromConstraints(
47+
new DomCrawlerConstraint\CrawlerSelectorExists($selector),
48+
new DomCrawlerConstraint\CrawlerSelectorTextSame($selector, $text)
49+
), $message);
50+
}
51+
52+
public static function assertSelectorTextNotContains(string $selector, string $text, string $message = ''): void
53+
{
54+
self::assertThat(self::getCrawler(), LogicalAnd::fromConstraints(
55+
new DomCrawlerConstraint\CrawlerSelectorExists($selector),
56+
new LogicalNot(new DomCrawlerConstraint\CrawlerSelectorTextContains($selector, $text))
57+
), $message);
58+
}
59+
60+
public static function assertPageTitleSame(string $expectedTitle, string $message = ''): void
61+
{
62+
self::assertSelectorTextSame('title', $expectedTitle, $message);
63+
}
64+
65+
public static function assertPageTitleContains(string $expectedTitle, string $message = ''): void
66+
{
67+
self::assertSelectorTextContains('title', $expectedTitle, $message);
68+
}
69+
70+
public static function assertInputValueSame(string $fieldName, string $expectedValue, string $message = ''): void
71+
{
72+
self::assertThat(self::getCrawler(), LogicalAnd::fromConstraints(
73+
new DomCrawlerConstraint\CrawlerSelectorExists("input[name=\"$fieldName\"]"),
74+
new DomCrawlerConstraint\CrawlerSelectorAttributeValueSame("input[name=\"$fieldName\"]", 'value', $expectedValue)
75+
), $message);
76+
}
77+
78+
public static function assertInputValueNotSame(string $fieldName, string $expectedValue, string $message = ''): void
79+
{
80+
self::assertThat(self::getCrawler(), LogicalAnd::fromConstraints(
81+
new DomCrawlerConstraint\CrawlerSelectorExists("input[name=\"$fieldName\"]"),
82+
new LogicalNot(new DomCrawlerConstraint\CrawlerSelectorAttributeValueSame("input[name=\"$fieldName\"]", 'value', $expectedValue))
83+
), $message);
84+
}
85+
86+
private static function getCrawler(): Crawler
87+
{
88+
if (!$crawler = self::getClient()->getCrawler()) {
89+
static::fail('A client must have a crawler to make assertions. Did you forget to make an HTTP request?');
90+
}
91+
92+
return $crawler;
93+
}
94+
}

0 commit comments

Comments
 (0)