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