‪TYPO3CMS  ‪main
ClientTest.php
Go to the documentation of this file.
1 <?php
2 
3 declare(strict_types=1);
4 
5 /*
6  * This file is part of the TYPO3 CMS project.
7  *
8  * It is free software; you can redistribute it and/or modify it under
9  * the terms of the GNU General Public License, either version 2
10  * of the License, or any later version.
11  *
12  * For the full copyright and license information, please read the
13  * LICENSE.txt file that was distributed with this source code.
14  *
15  * The TYPO3 project - inspiring people to share!
16  */
17 
19 
20 use GuzzleHttp\Client;
21 use GuzzleHttp\Exception\ConnectException as GuzzleConnectException;
22 use GuzzleHttp\Exception\GuzzleException as GuzzleExceptionInterface;
23 use GuzzleHttp\Exception\RequestException as GuzzleRequestException;
24 use GuzzleHttp\Handler\MockHandler as GuzzleMockHandler;
25 use GuzzleHttp\HandlerStack as GuzzleHandlerStack;
26 use GuzzleHttp\Middleware as GuzzleMiddleware;
27 use GuzzleHttp\Psr7\Response as GuzzleResponse;
28 use Psr\Http\Client\ClientExceptionInterface;
29 use Psr\Http\Client\ClientInterface;
30 use Psr\Http\Client\NetworkExceptionInterface;
31 use Psr\Http\Client\RequestExceptionInterface;
33 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
34 
38 final class ‪ClientTest extends UnitTestCase
39 {
43  public function ‪implementsPsr18ClientInterface(): void
44  {
45  $client = new Client();
46  self::assertInstanceOf(ClientInterface::class, $client);
47  }
48 
52  public function ‪sendRequest(): void
53  {
54  $transactions = [];
55  // Create a guzzle mock and queue two responses.
56  $mock = new GuzzleMockHandler([
57  new GuzzleResponse(200, ['X-Foo' => 'Bar']),
58  new GuzzleResponse(202, ['X-Foo' => 'Baz']),
59  ]);
60  $handler = GuzzleHandlerStack::create($mock);
61  $handler->push(GuzzleMiddleware::history($transactions));
62  $client = new Client(['handler' => $handler]);
63 
64  $request1 = new ‪Request('https://example.com', 'GET', 'php://temp');
65  $response1 = $client->sendRequest($request1);
66  $request2 = new ‪Request('https://example.com/action', 'POST', 'php://temp');
67  $response2 = $client->sendRequest($request2);
68 
69  self::assertCount(2, $transactions);
70 
71  self::assertSame('GET', $transactions[0]['request']->getMethod());
72  self::assertSame('https://example.com', $transactions[0]['request']->getUri()->__toString());
73  self::assertSame(200, $response1->getStatusCode());
74  self::assertSame('Bar', $response1->getHeaderLine('X-Foo'));
75 
76  self::assertSame('POST', $transactions[1]['request']->getMethod());
77  self::assertSame('https://example.com/action', $transactions[1]['request']->getUri()->__toString());
78  self::assertSame(202, $response2->getStatusCode());
79  self::assertSame('Baz', $response2->getHeaderLine('X-Foo'));
80  }
81 
85  public function ‪requestException(): void
86  {
87  $request = new ‪Request('https://example.com', 'GET', 'php://temp');
88  $exception = $this->createMock(GuzzleRequestException::class);
89  $exception->method('getRequest')->willReturn($request);
90  $mock = new GuzzleMockHandler([$exception]);
91  $handler = GuzzleHandlerStack::create($mock);
92  $client = new Client(['handler' => $handler]);
93 
94  $this->expectException(RequestExceptionInterface::class);
95  $client->sendRequest($request);
96  }
97 
101  public function ‪networkException(): void
102  {
103  $request = new ‪Request('https://example.com', 'GET', 'php://temp');
104  $exception = $this->createMock(GuzzleConnectException::class);
105  $exception->method('getRequest')->willReturn($request);
106  $mock = new GuzzleMockHandler([$exception]);
107  $handler = GuzzleHandlerStack::create($mock);
108  $client = new Client(['handler' => $handler]);
109 
110  $this->expectException(NetworkExceptionInterface::class);
111  $client->sendRequest($request);
112  }
113 
117  public function ‪genericGuzzleException(): void
118  {
119  $request = new ‪Request('https://example.com', 'GET', 'php://temp');
120  $mock = new GuzzleMockHandler([
121  new class () extends \RuntimeException implements GuzzleExceptionInterface {},
122  ]);
123  $handler = GuzzleHandlerStack::create($mock);
124  $client = new Client(['handler' => $handler]);
125 
126  $this->expectException(ClientExceptionInterface::class);
127  $client->sendRequest($request);
128  }
129 
134  {
135  $transactions = [];
136  $mock = new GuzzleMockHandler([
137  new GuzzleResponse(303, ['Location' => 'https://example.com']),
138  ]);
139  $handler = GuzzleHandlerStack::create($mock);
140  $handler->push(GuzzleMiddleware::history($transactions));
141  $client = new Client(['handler' => $handler]);
142 
143  $request = new ‪Request('https://example.com', 'GET', 'php://temp');
144  $response = $client->sendRequest($request);
145 
146  self::assertCount(1, $transactions);
147  self::assertSame(303, $response->getStatusCode());
148  self::assertSame('https://example.com', $response->getHeaderLine('Location'));
149  }
150 
155  {
156  $mock = new GuzzleMockHandler([
157  new GuzzleResponse(404),
158  new GuzzleResponse(500),
159  ]);
160  $handler = GuzzleHandlerStack::create($mock);
161  $client = new Client(['handler' => $handler]);
162 
163  $request = new ‪Request('https://example.com', 'GET', 'php://temp');
164  $response1 = $client->sendRequest($request);
165  $response2 = $client->sendRequest($request);
166 
167  self::assertSame(404, $response1->getStatusCode());
168  self::assertSame(500, $response2->getStatusCode());
169  }
170 }
‪TYPO3\CMS\Core\Tests\Unit\Http\ClientTest\redirectIsNotHandledRecursivelyButReturnedAsResponse
‪redirectIsNotHandledRecursivelyButReturnedAsResponse()
Definition: ClientTest.php:133
‪TYPO3\CMS\Core\Tests\Unit\Http\ClientTest\sendRequest
‪sendRequest()
Definition: ClientTest.php:52
‪TYPO3\CMS\Core\Tests\Unit\Http\ClientTest\errorResponsesDoNotThrowAnException
‪errorResponsesDoNotThrowAnException()
Definition: ClientTest.php:154
‪TYPO3\CMS\Core\Tests\Unit\Http
Definition: ApplicationTypeTest.php:18
‪TYPO3\CMS\Core\Tests\Unit\Http\ClientTest\implementsPsr18ClientInterface
‪implementsPsr18ClientInterface()
Definition: ClientTest.php:43
‪TYPO3\CMS\Core\Tests\Unit\Http\ClientTest
Definition: ClientTest.php:39
‪TYPO3\CMS\Core\Tests\Unit\Http\ClientTest\networkException
‪networkException()
Definition: ClientTest.php:101
‪TYPO3\CMS\Core\Tests\Unit\Http\ClientTest\genericGuzzleException
‪genericGuzzleException()
Definition: ClientTest.php:117
‪TYPO3\CMS\Core\Tests\Unit\Http\ClientTest\requestException
‪requestException()
Definition: ClientTest.php:85
‪TYPO3\CMS\Core\Http\Request
Definition: Request.php:35