1 | <?php |
||
14 | final class TaggedResponse implements ResponseInterface |
||
15 | { |
||
16 | /** |
||
17 | * @var Tag |
||
18 | */ |
||
19 | private $tag; |
||
20 | /** |
||
21 | * @var string |
||
22 | */ |
||
23 | private $line; |
||
24 | |||
25 | /** |
||
26 | * TaggedResponse constructor. |
||
27 | * @param Tag $tag |
||
28 | * @param string $line |
||
29 | */ |
||
30 | 22 | public function __construct(Tag $tag, string $line) |
|
35 | |||
36 | /** |
||
37 | * @return string |
||
38 | */ |
||
39 | 1 | public function __toString(): string |
|
40 | { |
||
41 | 1 | return implode(' ', [(string)$this->tag, $this->line]); |
|
42 | } |
||
43 | |||
44 | /** |
||
45 | * @param string $data |
||
46 | * @return ResponseInterface |
||
47 | */ |
||
48 | 2 | public function withAddedBody(string $data): ResponseInterface |
|
49 | { |
||
50 | 2 | $clone = clone $this; |
|
51 | 2 | $clone->line .= $data; |
|
52 | 2 | return $clone; |
|
53 | } |
||
54 | |||
55 | /** |
||
56 | * @param CompletionResult $expectedResult |
||
57 | * @return ResponseInterface |
||
58 | * @throws AssertionFailedException |
||
59 | */ |
||
60 | 9 | public function assertCompletion(CompletionResult $expectedResult): ResponseInterface |
|
61 | { |
||
62 | try { |
||
63 | 9 | $completionResult = CompletionResult::fromLine($this->line); |
|
64 | 8 | if (!$completionResult->equals($expectedResult)) { |
|
65 | 1 | throw new AssertionFailedException( |
|
66 | 1 | sprintf( |
|
67 | 1 | 'Returned completion, but %s is not equals to expected %s', |
|
68 | 1 | (string)$completionResult, |
|
69 | 8 | (string)$expectedResult |
|
70 | ) |
||
71 | ); |
||
72 | } |
||
73 | 2 | } catch (\InvalidArgumentException $e) { |
|
74 | 1 | throw new AssertionFailedException( |
|
75 | 1 | sprintf( |
|
76 | 1 | 'Response %s does not include a completion result', |
|
77 | 1 | $this->line |
|
78 | ) |
||
79 | ); |
||
80 | } |
||
81 | |||
82 | 7 | return $this; |
|
83 | } |
||
84 | |||
85 | /** |
||
86 | * @return ResponseInterface |
||
87 | * @throws AssertionFailedException |
||
88 | */ |
||
89 | 1 | public function assertContinuation(): ResponseInterface |
|
90 | { |
||
91 | 1 | throw new AssertionFailedException(); |
|
92 | } |
||
93 | |||
94 | /** |
||
95 | * @return ResponseInterface |
||
96 | */ |
||
97 | 10 | public function assertTagged(): ResponseInterface |
|
101 | |||
102 | /** |
||
103 | * @return string |
||
104 | */ |
||
105 | 2 | public function getBody(): string |
|
109 | } |