‪TYPO3CMS  10.4
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 as GuzzleClient;
21 use GuzzleHttp\ClientInterface as GuzzleClientInterface;
22 use GuzzleHttp\Exception\ConnectException as GuzzleConnectException;
23 use GuzzleHttp\Exception\GuzzleException as GuzzleExceptionInterface;
24 use GuzzleHttp\Exception\RequestException as GuzzleRequestException;
25 use GuzzleHttp\Handler\MockHandler as GuzzleMockHandler;
26 use GuzzleHttp\HandlerStack as GuzzleHandlerStack;
27 use GuzzleHttp\Middleware as GuzzleMiddleware;
28 use GuzzleHttp\Psr7\Response as GuzzleResponse;
29 use Psr\Http\Client\ClientExceptionInterface;
30 use Psr\Http\Client\ClientInterface;
31 use Psr\Http\Client\NetworkExceptionInterface;
32 use Psr\Http\Client\RequestExceptionInterface;
35 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
36 
40 class ‪ClientTest extends UnitTestCase
41 {
42  public function ‪testImplementsPsr18ClientInterface(): void
43  {
44  $client = new ‪Client($this->prophesize(GuzzleClientInterface::class)->reveal());
45  self::assertInstanceOf(ClientInterface::class, $client);
46  }
47 
48  public function ‪testSendRequest(): void
49  {
50  $transactions = [];
51  // Create a guzzle mock and queue two responses.
52  $mock = new GuzzleMockHandler([
53  new GuzzleResponse(200, ['X-Foo' => 'Bar']),
54  new GuzzleResponse(202, ['X-Foo' => 'Baz']),
55  ]);
56  $handler = GuzzleHandlerStack::create($mock);
57  $handler->push(GuzzleMiddleware::history($transactions));
58  $guzzleClient = new GuzzleClient(['handler' => $handler]);
59 
60  $client = new ‪Client($guzzleClient);
61 
62  $request1 = new ‪Request('https://example.com', 'GET', 'php://temp');
63  $response1 = $client->sendRequest($request1);
64  $request2 = new ‪Request('https://example.com/action', 'POST', 'php://temp');
65  $response2 = $client->sendRequest($request2);
66 
67  self::assertCount(2, $transactions);
68 
69  self::assertSame('GET', $transactions[0]['request']->getMethod());
70  self::assertSame('https://example.com', $transactions[0]['request']->getUri()->__toString());
71  self::assertSame(200, $response1->getStatusCode());
72  self::assertSame('Bar', $response1->getHeaderLine('X-Foo'));
73 
74  self::assertSame('POST', $transactions[1]['request']->getMethod());
75  self::assertSame('https://example.com/action', $transactions[1]['request']->getUri()->__toString());
76  self::assertSame(202, $response2->getStatusCode());
77  self::assertSame('Baz', $response2->getHeaderLine('X-Foo'));
78  }
79 
80  public function ‪testRequestException(): void
81  {
82  $request = new ‪Request('https://example.com', 'GET', 'php://temp');
83  $exception = $this->prophesize(GuzzleRequestException::class);
84  $exception->getRequest()->willReturn($request);
85  $mock = new GuzzleMockHandler([
86  $exception->reveal()
87  ]);
88  $handler = GuzzleHandlerStack::create($mock);
89  $guzzleClient = new GuzzleClient(['handler' => $handler]);
90 
91  $client = new ‪Client($guzzleClient);
92 
93  $this->expectException(RequestExceptionInterface::class);
94  $client->sendRequest($request);
95  }
96 
97  public function ‪testNetworkException(): void
98  {
99  $request = new ‪Request('https://example.com', 'GET', 'php://temp');
100  $exception = $this->prophesize(GuzzleConnectException::class);
101  $exception->getRequest()->willReturn($request);
102  $mock = new GuzzleMockHandler([
103  $exception->reveal()
104  ]);
105  $handler = GuzzleHandlerStack::create($mock);
106  $guzzleClient = new GuzzleClient(['handler' => $handler]);
107 
108  $client = new ‪Client($guzzleClient);
109 
110  $this->expectException(NetworkExceptionInterface::class);
111  $client->sendRequest($request);
112  }
113 
114  public function ‪testGenericGuzzleException(): void
115  {
116  $request = new ‪Request('https://example.com', 'GET', 'php://temp');
117  $mock = new GuzzleMockHandler([
118  new class() extends \RuntimeException implements GuzzleExceptionInterface {
119  }
120  ]);
121  $handler = GuzzleHandlerStack::create($mock);
122  $guzzleClient = new GuzzleClient(['handler' => $handler]);
123 
124  $client = new ‪Client($guzzleClient);
125 
126  $this->expectException(ClientExceptionInterface::class);
127  $client->sendRequest($request);
128  }
129 
131  {
132  $transactions = [];
133  $mock = new GuzzleMockHandler([
134  new GuzzleResponse(303, ['Location' => 'https://example.com']),
135  ]);
136  $handler = GuzzleHandlerStack::create($mock);
137  $handler->push(GuzzleMiddleware::history($transactions));
138  $guzzleClient = new GuzzleClient(['handler' => $handler]);
139 
140  $client = new ‪Client($guzzleClient);
141 
142  $request = new ‪Request('https://example.com', 'GET', 'php://temp');
143  $response = $client->sendRequest($request);
144 
145  self::assertCount(1, $transactions);
146  self::assertSame(303, $response->getStatusCode());
147  self::assertSame('https://example.com', $response->getHeaderLine('Location'));
148  }
149 
151  {
152  $mock = new GuzzleMockHandler([
153  new GuzzleResponse(404),
154  new GuzzleResponse(500),
155  ]);
156  $handler = GuzzleHandlerStack::create($mock);
157  $guzzleClient = new GuzzleClient(['handler' => $handler]);
158 
159  $client = new ‪Client($guzzleClient);
160 
161  $request = new ‪Request('https://example.com', 'GET', 'php://temp');
162  $response1 = $client->sendRequest($request);
163  $response2 = $client->sendRequest($request);
164 
165  self::assertSame(404, $response1->getStatusCode());
166  self::assertSame(500, $response2->getStatusCode());
167  }
168 }
‪TYPO3\CMS\Core\Tests\Unit\Http
Definition: ApplicationTypeTest.php:16
‪TYPO3\CMS\Core\Tests\Unit\Http\ClientTest\testErrorResponsesDoNotThrowAnException
‪testErrorResponsesDoNotThrowAnException()
Definition: ClientTest.php:150
‪TYPO3\CMS\Core\Tests\Unit\Http\ClientTest\testRedirectIsNotHandledRecursivelyButReturnedAsResponse
‪testRedirectIsNotHandledRecursivelyButReturnedAsResponse()
Definition: ClientTest.php:130
‪TYPO3\CMS\Core\Http\Client
Definition: Client.php:42
‪TYPO3\CMS\Core\Tests\Unit\Http\ClientTest
Definition: ClientTest.php:41
‪TYPO3\CMS\Core\Tests\Unit\Http\ClientTest\testNetworkException
‪testNetworkException()
Definition: ClientTest.php:97
‪TYPO3\CMS\Core\Http\Client
Definition: ClientException.php:18
‪TYPO3\CMS\Core\Tests\Unit\Http\ClientTest\testSendRequest
‪testSendRequest()
Definition: ClientTest.php:48
‪TYPO3\CMS\Core\Tests\Unit\Http\ClientTest\testGenericGuzzleException
‪testGenericGuzzleException()
Definition: ClientTest.php:114
‪TYPO3\CMS\Core\Tests\Unit\Http\ClientTest\testImplementsPsr18ClientInterface
‪testImplementsPsr18ClientInterface()
Definition: ClientTest.php:42
‪TYPO3\CMS\Core\Http\Request
Definition: Request.php:33
‪TYPO3\CMS\Core\Tests\Unit\Http\ClientTest\testRequestException
‪testRequestException()
Definition: ClientTest.php:80