|
1
|
|
|
<?php |
|
2
|
|
|
namespace DjThossi\SmokeTestingPhp\Result; |
|
3
|
|
|
|
|
4
|
|
|
use DjThossi\SmokeTestingPhp\Collection\HeaderCollection; |
|
5
|
|
|
use DjThossi\SmokeTestingPhp\ValueObject\Body; |
|
6
|
|
|
use DjThossi\SmokeTestingPhp\ValueObject\StatusCode; |
|
7
|
|
|
use DjThossi\SmokeTestingPhp\ValueObject\TimeToFirstByte; |
|
8
|
|
|
use DjThossi\SmokeTestingPhp\ValueObject\Url; |
|
9
|
|
|
|
|
10
|
|
|
class ValidResult implements Result |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var Url |
|
14
|
|
|
*/ |
|
15
|
|
|
private $url; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var HeaderCollection |
|
19
|
|
|
*/ |
|
20
|
|
|
private $headers; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var Body |
|
24
|
|
|
*/ |
|
25
|
|
|
private $body; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var TimeToFirstByte |
|
29
|
|
|
*/ |
|
30
|
|
|
private $timeToFirstByte; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @var StatusCode |
|
34
|
|
|
*/ |
|
35
|
|
|
private $statusCode; |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param Url $url |
|
39
|
|
|
* @param HeaderCollection $headers |
|
40
|
|
|
* @param Body $body |
|
41
|
|
|
* @param TimeToFirstByte $timeToFirstByte |
|
42
|
|
|
* @param StatusCode $statusCode |
|
43
|
|
|
*/ |
|
44
|
|
|
public function __construct( |
|
45
|
|
|
Url $url, |
|
46
|
|
|
HeaderCollection $headers, |
|
47
|
|
|
Body $body, |
|
48
|
|
|
TimeToFirstByte $timeToFirstByte, |
|
49
|
|
|
StatusCode $statusCode |
|
50
|
|
|
) { |
|
51
|
|
|
$this->url = $url; |
|
52
|
|
|
$this->headers = $headers; |
|
53
|
|
|
$this->body = $body; |
|
54
|
|
|
$this->timeToFirstByte = $timeToFirstByte; |
|
55
|
|
|
$this->statusCode = $statusCode; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param string $url |
|
60
|
|
|
* @param array $headerData |
|
61
|
|
|
* @param string $body |
|
62
|
|
|
* @param int $ttfbInMilliseconds |
|
63
|
|
|
* @param int$statusCode |
|
64
|
|
|
* |
|
65
|
|
|
* @return ValidResult |
|
66
|
|
|
*/ |
|
67
|
|
|
public static function fromPrimitives( |
|
68
|
|
|
$url, |
|
69
|
|
|
array $headerData, |
|
70
|
|
|
$body, |
|
71
|
|
|
$ttfbInMilliseconds, |
|
72
|
|
|
$statusCode |
|
73
|
|
|
) { |
|
74
|
|
|
return new self( |
|
75
|
|
|
new Url($url), |
|
76
|
|
|
HeaderCollection::fromArray($headerData), |
|
77
|
|
|
new Body($body), |
|
78
|
|
|
new TimeToFirstByte($ttfbInMilliseconds), |
|
79
|
|
|
new StatusCode($statusCode) |
|
80
|
|
|
); |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* @return Url |
|
85
|
|
|
*/ |
|
86
|
|
|
public function getUrl() |
|
87
|
|
|
{ |
|
88
|
|
|
return $this->url; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @return Body |
|
93
|
|
|
*/ |
|
94
|
|
|
public function getBody() |
|
95
|
|
|
{ |
|
96
|
|
|
return $this->body; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @return HeaderCollection |
|
101
|
|
|
*/ |
|
102
|
|
|
public function getHeaders() |
|
103
|
|
|
{ |
|
104
|
|
|
return $this->headers; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @return StatusCode |
|
109
|
|
|
*/ |
|
110
|
|
|
public function getStatusCode() |
|
111
|
|
|
{ |
|
112
|
|
|
return $this->statusCode; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @return TimeToFirstByte |
|
117
|
|
|
*/ |
|
118
|
|
|
public function getTimeToFirstByte() |
|
119
|
|
|
{ |
|
120
|
|
|
return $this->timeToFirstByte; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* Returns a string including statuscode, time to first byte and body. |
|
125
|
|
|
* |
|
126
|
|
|
* @return string |
|
127
|
|
|
*/ |
|
128
|
|
|
public function asString() |
|
129
|
|
|
{ |
|
130
|
|
|
return sprintf( |
|
131
|
|
|
"StatusCode: %s\nTimeToFirstByte: %ums\nBody: %s", |
|
132
|
|
|
$this->statusCode->asInteger(), |
|
133
|
|
|
$this->timeToFirstByte->inMilliSeconds(), |
|
134
|
|
|
$this->body->asString() |
|
135
|
|
|
); |
|
136
|
|
|
} |
|
137
|
|
|
|
|
138
|
|
|
/** |
|
139
|
|
|
* @return bool |
|
140
|
|
|
*/ |
|
141
|
|
|
public function isValidResult() |
|
142
|
|
|
{ |
|
143
|
|
|
return true; |
|
144
|
|
|
} |
|
145
|
|
|
} |
|
146
|
|
|
|