1 | <?php |
||
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) |
|
95 | } |
||
96 |