-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMock_Http_Sequence.php
More file actions
130 lines (115 loc) · 2.99 KB
/
Copy pathMock_Http_Sequence.php
File metadata and controls
130 lines (115 loc) · 2.99 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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
/**
* This file contains the Mock_Http_Sequence class
*
* @package Mantle
*/
namespace Mantle\Testing;
/**
* Support faking HTTP requests in a specific sequence.
*/
class Mock_Http_Sequence {
/**
* Responses in the sequence.
*
* @var Mock_Http_Response[]
*/
protected array $responses;
/**
* Indicates that invoking this sequence when it is empty should throw an
* exception.
*/
protected bool $fail_when_empty = true;
/**
* Empty response when the sequence is empty.
*/
protected ?Mock_Http_Response $empty_response = null;
/**
* Create a Mock_Http_Sequence instance.
*/
public static function create(): static {
return new static();
}
/**
* Push a specific response to the sequence
*
* @param Mock_Http_Response $response Response to push.
*/
public function push( Mock_Http_Response $response ): static {
$this->responses[] = $response;
return $this;
}
/**
* Push a response with a specific status code to the sequence.
*
* @param int $status Http Status.
* @param array $headers Http Headers.
*/
public function push_status( int $status, array $headers = [] ): static {
return $this->push(
Mock_Http_Response::create()
->with_response_code( $status )
->with_headers( $headers )
);
}
/**
* Push a response with a specific body to the sequence.
*
* @param string $body Response body.
* @param array $headers Response headers.
*/
public function push_body( string $body, array $headers = [] ): static {
return $this->push(
Mock_Http_Response::create( $body, $headers )
);
}
/**
* Push a JSON response to the sequence.
*
* @param array|string $payload Data to encode as JSON.
* @param array $headers Headers to include in the response.
*/
public function push_json( array|string $payload, array $headers = [] ): static {
return $this->push(
Mock_Http_Response::create( '', $headers )
->with_json( $payload )
->with_headers( [ 'Content-Type' => 'application/json' ] )
);
}
/**
* Make the sequence return a default response when empty.
*
* @param Mock_Http_Response $response Response to return when empty.
*/
public function when_empty( Mock_Http_Response $response ): static {
$this->fail_when_empty = false;
$this->empty_response = $response;
return $this;
}
/**
* Don't throw an exception when empty.
*/
public function dont_fail_when_empty(): static {
return $this->when_empty( Mock_Http_Response::create() );
}
/**
* Indicates if the sequence has any responses remaining.
*/
public function is_empty(): bool {
return empty( $this->responses );
}
/**
* Get the nex response in the sequence.
*
* @throws \RuntimeException Thrown on empty sequence.
*/
public function __invoke(): ?\Mantle\Testing\Mock_Http_Response {
if ( empty( $this->responses ) ) {
if ( $this->fail_when_empty ) {
throw new \RuntimeException( 'No more responses in sequence.' );
}
return $this->empty_response;
}
return array_shift( $this->responses );
}
}