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