|
22 | 22 |
|
23 | 23 | class MockHttpClientTest extends HttpClientTestCase
|
24 | 24 | {
|
| 25 | + /** |
| 26 | + * @dataProvider mockingProvider |
| 27 | + */ |
| 28 | + public function testMocking($factory, array $expectedResponses) |
| 29 | + { |
| 30 | + $client = new MockHttpClient($factory, 'https://example.com/'); |
| 31 | + $this->assertSame(0, $client->getRequestsCount()); |
| 32 | + |
| 33 | + $urls = ['/foo', '/bar']; |
| 34 | + foreach ($urls as $i => $url) { |
| 35 | + $response = $client->request('POST', $url, ['body' => 'payload']); |
| 36 | + $this->assertEquals($expectedResponses[$i], $response->getContent()); |
| 37 | + } |
| 38 | + |
| 39 | + $this->assertSame(2, $client->getRequestsCount()); |
| 40 | + } |
| 41 | + |
| 42 | + public function mockingProvider(): iterable |
| 43 | + { |
| 44 | + yield 'callable' => [ |
| 45 | + static function (string $method, string $url, array $options = []) { |
| 46 | + return new MockResponse($method.': '.$url.' (body='.$options['body'].')'); |
| 47 | + }, |
| 48 | + [ |
| 49 | + 'POST: https://example.com/foo (body=payload)', |
| 50 | + 'POST: https://example.com/bar (body=payload)', |
| 51 | + ], |
| 52 | + ]; |
| 53 | + |
| 54 | + yield 'array of callable' => [ |
| 55 | + [ |
| 56 | + static function (string $method, string $url, array $options = []) { |
| 57 | + return new MockResponse($method.': '.$url.' (body='.$options['body'].') [1]'); |
| 58 | + }, |
| 59 | + static function (string $method, string $url, array $options = []) { |
| 60 | + return new MockResponse($method.': '.$url.' (body='.$options['body'].') [2]'); |
| 61 | + }, |
| 62 | + ], |
| 63 | + [ |
| 64 | + 'POST: https://example.com/foo (body=payload) [1]', |
| 65 | + 'POST: https://example.com/bar (body=payload) [2]', |
| 66 | + ], |
| 67 | + ]; |
| 68 | + |
| 69 | + yield 'array of response objects' => [ |
| 70 | + [ |
| 71 | + new MockResponse('static response [1]'), |
| 72 | + new MockResponse('static response [2]'), |
| 73 | + ], |
| 74 | + [ |
| 75 | + 'static response [1]', |
| 76 | + 'static response [2]', |
| 77 | + ], |
| 78 | + ]; |
| 79 | + |
| 80 | + yield 'iterator' => [ |
| 81 | + new \ArrayIterator( |
| 82 | + [ |
| 83 | + new MockResponse('static response [1]'), |
| 84 | + new MockResponse('static response [2]'), |
| 85 | + ] |
| 86 | + ), |
| 87 | + [ |
| 88 | + 'static response [1]', |
| 89 | + 'static response [2]', |
| 90 | + ], |
| 91 | + ]; |
| 92 | + |
| 93 | + yield 'null' => [ |
| 94 | + null, |
| 95 | + [ |
| 96 | + '', |
| 97 | + '', |
| 98 | + ], |
| 99 | + ]; |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * @dataProvider transportExceptionProvider |
| 104 | + */ |
| 105 | + public function testTransportExceptionThrowsIfPerformedMoreRequestsThanConfigured($factory) |
| 106 | + { |
| 107 | + $client = new MockHttpClient($factory, 'https://example.com/'); |
| 108 | + |
| 109 | + $client->request('POST', '/foo'); |
| 110 | + $client->request('POST', '/foo'); |
| 111 | + |
| 112 | + $this->expectException(TransportException::class); |
| 113 | + $client->request('POST', '/foo'); |
| 114 | + } |
| 115 | + |
| 116 | + public function transportExceptionProvider(): iterable |
| 117 | + { |
| 118 | + yield 'array of callable' => [ |
| 119 | + [ |
| 120 | + static function (string $method, string $url, array $options = []) { |
| 121 | + return new MockResponse(); |
| 122 | + }, |
| 123 | + static function (string $method, string $url, array $options = []) { |
| 124 | + return new MockResponse(); |
| 125 | + }, |
| 126 | + ], |
| 127 | + ]; |
| 128 | + |
| 129 | + yield 'array of response objects' => [ |
| 130 | + [ |
| 131 | + new MockResponse(), |
| 132 | + new MockResponse(), |
| 133 | + ], |
| 134 | + ]; |
| 135 | + |
| 136 | + yield 'iterator' => [ |
| 137 | + new \ArrayIterator( |
| 138 | + [ |
| 139 | + new MockResponse(), |
| 140 | + new MockResponse(), |
| 141 | + ] |
| 142 | + ), |
| 143 | + ]; |
| 144 | + } |
| 145 | + |
25 | 146 | protected function getHttpClient(string $testCase): HttpClientInterface
|
26 | 147 | {
|
27 | 148 | $responses = [];
|
|