-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResponseExtension.php
More file actions
112 lines (95 loc) · 3.4 KB
/
ResponseExtension.php
File metadata and controls
112 lines (95 loc) · 3.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
<?php
namespace Oro\Component\Testing;
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
use Symfony\Component\DomCrawler\Crawler;
use Symfony\Component\HttpFoundation\Response;
/**
* Provides assertion for the Response object.
*/
trait ResponseExtension
{
public function assertResponseStatus(Response $response, int $expectedStatus): void
{
$this->assertInstanceOfResponse($response);
self::assertEquals($expectedStatus, $response->getStatusCode(), $this->getAssertMessage($response));
}
public function assertLastResponseStatus(int $expectedStatus): void
{
$this->assertResponseStatus($this->getClientInstance()->getResponse(), $expectedStatus);
}
public function assertResponseContentTypeHtml(Response $response): void
{
$this->assertInstanceOfResponse($response);
self::assertTrue($response->headers->has('Content-Type'));
static::assertStringContainsString(
'text/html',
$response->headers->get('Content-Type'),
$this->getAssertMessage($response)
);
}
public function assertLastResponseContentTypeHtml(): void
{
$this->assertResponseContentTypeHtml($this->getClientInstance()->getResponse());
}
public function assertResponseContentTypeJson(Response $response): void
{
$this->assertInstanceOfResponse($response);
self::assertTrue($response->headers->has('Content-Type'));
self::assertEquals('application/json', $response->headers->get('Content-Type'));
}
public function assertLastResponseContentTypeJson(): void
{
$this->assertResponseContentTypeJson($this->getClientInstance()->getResponse());
}
/**
* @param Response $response
*
* @return array
*/
public function getResponseJsonContent(Response $response)
{
$this->assertInstanceOfResponse($response);
$content = json_decode($response->getContent(), true);
//guard
self::assertNotNull(
$content,
sprintf("The response content is not valid json.\n\n%s", $response->getContent())
);
return $content;
}
/**
* @return array
*/
public function getLastResponseJsonContent()
{
return $this->getResponseJsonContent($this->getClientInstance()->getResponse());
}
/**
* @param mixed $actual
*/
public function assertInstanceOfResponse($actual): void
{
self::assertInstanceOf(Response::class, $actual);
}
private function getAssertMessage(Response $response): string
{
$responseStatusCode = $response->getStatusCode();
if (500 >= $responseStatusCode && $responseStatusCode < 600) {
$crawler = new Crawler();
$crawler->addHtmlContent($response->getContent());
if ($crawler->filter('.text-exception h1')->count() > 0) {
$exceptionMessage = trim($crawler->filter('.text-exception h1')->text());
$trace = '';
if ($crawler->filter('#traces-0 li')->count() > 0) {
list($trace) = explode("\n", trim($crawler->filter('#traces-0 li')->text()));
}
return 'Internal Server Error: '.$exceptionMessage.' '.$trace;
}
}
return $response->getContent();
}
/**
* @return KernelBrowser
*/
abstract protected static function getClientInstance();
}