1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the Liip/FunctionalTestBundle |
5
|
|
|
* |
6
|
|
|
* (c) Lukas Kahwe Smith <[email protected]> |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the MIT license that is bundled |
9
|
|
|
* with this source code in the file LICENSE. |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Liip\FunctionalTestBundle\Utils; |
13
|
|
|
|
14
|
|
|
use PHPUnit\Framework\TestCase; |
15
|
|
|
use Symfony\Bundle\FrameworkBundle\Client; |
16
|
|
|
use Symfony\Component\DependencyInjection\ContainerInterface; |
17
|
|
|
use Symfony\Component\DomCrawler\Crawler; |
18
|
|
|
use Symfony\Component\HttpFoundation\Response; |
19
|
|
|
use Liip\FunctionalTestBundle\Test\ValidationErrorsConstraint; |
20
|
|
|
|
21
|
|
|
class HttpAssertions extends TestCase |
22
|
|
|
{ |
23
|
|
|
/** |
24
|
|
|
* Checks the success state of a response. |
25
|
|
|
* |
26
|
|
|
* @param Response $response Response object |
27
|
|
|
* @param bool $success to define whether the response is expected to be successful |
28
|
|
|
* @param string $type |
29
|
|
|
*/ |
30
|
6 |
|
public static function isSuccessful(Response $response, $success = true, $type = 'text/html') |
31
|
|
|
{ |
32
|
|
|
try { |
33
|
6 |
|
$crawler = new Crawler(); |
34
|
6 |
|
$crawler->addContent($response->getContent(), $type); |
35
|
5 |
|
if (!count($crawler->filter('title'))) { |
36
|
1 |
|
$title = '['.$response->getStatusCode().'] - '.$response->getContent(); |
37
|
1 |
|
} else { |
38
|
4 |
|
$title = $crawler->filter('title')->text(); |
39
|
|
|
} |
40
|
6 |
|
} catch (\Exception $e) { |
41
|
1 |
|
$title = $e->getMessage(); |
42
|
|
|
} |
43
|
|
|
|
44
|
6 |
|
if ($success) { |
45
|
5 |
|
self::assertTrue($response->isSuccessful(), 'The Response was not successful: '.$title); |
46
|
4 |
|
} else { |
47
|
2 |
|
self::assertFalse($response->isSuccessful(), 'The Response was successful: '.$title); |
48
|
|
|
} |
49
|
5 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Asserts that the HTTP response code of the last request performed by |
53
|
|
|
* $client matches the expected code. If not, raises an error with more |
54
|
|
|
* information. |
55
|
|
|
* |
56
|
|
|
* @param $expectedStatusCode |
57
|
|
|
* @param Client $client |
58
|
|
|
*/ |
59
|
12 |
|
public static function assertStatusCode($expectedStatusCode, Client $client) |
60
|
|
|
{ |
61
|
12 |
|
$helpfulErrorMessage = null; |
62
|
|
|
|
63
|
12 |
|
if ($expectedStatusCode !== $client->getResponse()->getStatusCode()) { |
64
|
|
|
// Get a more useful error message, if available |
65
|
3 |
|
if ($exception = $client->getContainer()->get('liip_functional_test.exception_listener')->getLastException()) { |
66
|
1 |
|
$helpfulErrorMessage = $exception->getMessage(); |
67
|
1 |
|
} elseif ( |
68
|
2 |
|
$client->getContainer()->has('liip_functional_test.validator') && |
69
|
2 |
|
count($validationErrors = $client->getContainer()->get('liip_functional_test.validator')->getLastErrors()) |
70
|
2 |
|
) { |
71
|
1 |
|
$helpfulErrorMessage = "Unexpected validation errors:\n"; |
72
|
|
|
|
73
|
1 |
|
foreach ($validationErrors as $error) { |
74
|
1 |
|
$helpfulErrorMessage .= sprintf("+ %s: %s\n", $error->getPropertyPath(), $error->getMessage()); |
75
|
1 |
|
} |
76
|
1 |
|
} else { |
77
|
1 |
|
$helpfulErrorMessage = substr($client->getResponse(), 0, 200); |
78
|
|
|
} |
79
|
3 |
|
} |
80
|
|
|
|
81
|
12 |
|
self::assertEquals($expectedStatusCode, $client->getResponse()->getStatusCode(), $helpfulErrorMessage); |
82
|
9 |
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Assert that the last validation errors within $container match the |
86
|
|
|
* expected keys. |
87
|
|
|
* |
88
|
|
|
* @param array $expected A flat array of field names |
89
|
|
|
* @param ContainerInterface $container |
90
|
|
|
*/ |
91
|
3 |
|
public static function assertValidationErrors(array $expected, ContainerInterface $container) |
92
|
|
|
{ |
93
|
3 |
|
if (!$container->has('liip_functional_test.validator')) { |
94
|
1 |
|
trigger_error(sprintf('Method %s() can not be used as the validation component of the Symfony framework is disabled.', __METHOD__, __CLASS__), E_USER_WARNING); |
95
|
|
|
} |
96
|
|
|
|
97
|
2 |
|
self::assertThat( |
98
|
2 |
|
$container->get('liip_functional_test.validator')->getLastErrors(), |
99
|
2 |
|
new ValidationErrorsConstraint($expected), |
100
|
|
|
'Validation errors should match.' |
101
|
2 |
|
); |
102
|
1 |
|
} |
103
|
|
|
} |
104
|
|
|
|