‪TYPO3CMS  11.5
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 Prophecy\PhpUnit\ProphecyTrait;
29 use Psr\Http\Client\ClientExceptionInterface;
30 use Psr\Http\Client\ClientInterface;
31 use Psr\Http\Client\NetworkExceptionInterface;
32 use Psr\Http\Client\RequestExceptionInterface;
34 use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
35 
39 class ‪ClientTest extends UnitTestCase
40 {
41  use ProphecyTrait;
42 
46  public function ‪implementsPsr18ClientInterface(): void
47  {
48  $client = new Client();
49  self::assertInstanceOf(ClientInterface::class, $client);
50  }
51 
55  public function ‪sendRequest(): void
56  {
57  $transactions = [];
58  // Create a guzzle mock and queue two responses.
59  $mock = new GuzzleMockHandler([
60  new GuzzleResponse(200, ['X-Foo' => 'Bar']),
61  new GuzzleResponse(202, ['X-Foo' => 'Baz']),
62  ]);
63  $handler = GuzzleHandlerStack::create($mock);
64  $handler->push(GuzzleMiddleware::history($transactions));
65  $client = new Client(['handler' => $handler]);
66 
67  $request1 = new ‪Request('https://example.com', 'GET', 'php://temp');
68  $response1 = $client->sendRequest($request1);
69  $request2 = new ‪Request('https://example.com/action', 'POST', 'php://temp');
70  $response2 = $client->sendRequest($request2);
71 
72  self::assertCount(2, $transactions);
73 
74  self::assertSame('GET', $transactions[0]['request']->getMethod());
75  self::assertSame('https://example.com', $transactions[0]['request']->getUri()->__toString());
76  self::assertSame(200, $response1->getStatusCode());
77  self::assertSame('Bar', $response1->getHeaderLine('X-Foo'));
78 
79  self::assertSame('POST', $transactions[1]['request']->getMethod());
80  self::assertSame('https://example.com/action', $transactions[1]['request']->getUri()->__toString());
81  self::assertSame(202, $response2->getStatusCode());
82  self::assertSame('Baz', $response2->getHeaderLine('X-Foo'));
83  }
84 
88  public function ‪requestException(): void
89  {
90  $request = new ‪Request('https://example.com', 'GET', 'php://temp');
91  $exception = $this->prophesize(GuzzleRequestException::class);
92  $exception->getRequest()->willReturn($request);
93  $mock = new GuzzleMockHandler([
94  $exception->reveal(),
95  ]);
96  $handler = GuzzleHandlerStack::create($mock);
97  $client = new Client(['handler' => $handler]);
98 
99  $this->expectException(RequestExceptionInterface::class);
100  $client->sendRequest($request);
101  }
102 
106  public function ‪networkException(): void
107  {
108  $request = new ‪Request('https://example.com', 'GET', 'php://temp');
109  $exception = $this->prophesize(GuzzleConnectException::class);
110  $exception->getRequest()->willReturn($request);
111  $mock = new GuzzleMockHandler([
112  $exception->reveal(),
113  ]);
114  $handler = GuzzleHandlerStack::create($mock);
115  $client = new Client(['handler' => $handler]);
116 
117  $this->expectException(NetworkExceptionInterface::class);
118  $client->sendRequest($request);
119  }
120 
124  public function ‪genericGuzzleException(): void
125  {
126  $request = new ‪Request('https://example.com', 'GET', 'php://temp');
127  $mock = new GuzzleMockHandler([
128  new class () extends \RuntimeException implements GuzzleExceptionInterface {},
129  ]);
130  $handler = GuzzleHandlerStack::create($mock);
131  $client = new Client(['handler' => $handler]);
132 
133  $this->expectException(ClientExceptionInterface::class);
134  $client->sendRequest($request);
135  }
136 
141  {
142  $transactions = [];
143  $mock = new GuzzleMockHandler([
144  new GuzzleResponse(303, ['Location' => 'https://example.com']),
145  ]);
146  $handler = GuzzleHandlerStack::create($mock);
147  $handler->push(GuzzleMiddleware::history($transactions));
148  $client = new Client(['handler' => $handler]);
149 
150  $request = new ‪Request('https://example.com', 'GET', 'php://temp');
151  $response = $client->sendRequest($request);
152 
153  self::assertCount(1, $transactions);
154  self::assertSame(303, $response->getStatusCode());
155  self::assertSame('https://example.com', $response->getHeaderLine('Location'));
156  }
157 
161  public function ‪errorResponsesDoNotThrowAnException(): void
162  {
163  $mock = new GuzzleMockHandler([
164  new GuzzleResponse(404),
165  new GuzzleResponse(500),
166  ]);
167  $handler = GuzzleHandlerStack::create($mock);
168  $client = new Client(['handler' => $handler]);
169 
170  $request = new ‪Request('https://example.com', 'GET', 'php://temp');
171  $response1 = $client->sendRequest($request);
172  $response2 = $client->sendRequest($request);
173 
174  self::assertSame(404, $response1->getStatusCode());
175  self::assertSame(500, $response2->getStatusCode());
176  }
177 }
‪TYPO3\CMS\Core\Tests\Unit\Http\ClientTest\redirectIsNotHandledRecursivelyButReturnedAsResponse
‪redirectIsNotHandledRecursivelyButReturnedAsResponse()
Definition: ClientTest.php:139
‪TYPO3\CMS\Core\Tests\Unit\Http\ClientTest\sendRequest
‪sendRequest()
Definition: ClientTest.php:54
‪TYPO3\CMS\Core\Tests\Unit\Http\ClientTest\errorResponsesDoNotThrowAnException
‪errorResponsesDoNotThrowAnException()
Definition: ClientTest.php:160
‪TYPO3\CMS\Core\Tests\Unit\Http
Definition: ApplicationTypeTest.php:18
‪TYPO3\CMS\Core\Tests\Unit\Http\ClientTest\implementsPsr18ClientInterface
‪implementsPsr18ClientInterface()
Definition: ClientTest.php:45
‪TYPO3\CMS\Core\Tests\Unit\Http\ClientTest
Definition: ClientTest.php:40
‪TYPO3\CMS\Core\Tests\Unit\Http\ClientTest\networkException
‪networkException()
Definition: ClientTest.php:105
‪TYPO3\CMS\Core\Tests\Unit\Http\ClientTest\genericGuzzleException
‪genericGuzzleException()
Definition: ClientTest.php:123
‪TYPO3\CMS\Core\Tests\Unit\Http\ClientTest\requestException
‪requestException()
Definition: ClientTest.php:87
‪TYPO3\CMS\Core\Http\Request
Definition: Request.php:33