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 | public static function isSuccessful(Response $response, $success = true, $type = 'text/html') |
||
30 | { |
||
31 | try { |
||
32 | $crawler = new Crawler(); |
||
33 | $crawler->addContent($response->getContent(), $type); |
||
34 | if (!count($crawler->filter('title'))) { |
||
35 | $title = '['.$response->getStatusCode().'] - '.$response->getContent(); |
||
36 | } else { |
||
37 | $title = $crawler->filter('title')->text(); |
||
38 | } |
||
39 | } catch (\Exception $e) { |
||
40 | $title = $e->getMessage(); |
||
41 | } |
||
42 | |||
43 | if ($success) { |
||
44 | self::assertTrue($response->isSuccessful(), 'The Response was not successful: '.$title); |
||
45 | } else { |
||
46 | self::assertFalse($response->isSuccessful(), 'The Response was successful: '.$title); |
||
47 | } |
||
48 | } |
||
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 | public static function assertStatusCode($expectedStatusCode, Client $client) |
||
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 | public static function assertValidationErrors(array $expected, ContainerInterface $container) |
||
95 | } |
||
96 |