Skip to content

Commit 3047549

Browse files
committed
Fix: restore compatibility with PHPUnit 9-11
1 parent 6cc8e4c commit 3047549

5 files changed

Lines changed: 66 additions & 70 deletions

File tree

docker-compose.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ services:
22
php:
33
build: # Info to build the Docker image
44
context: ./.docker # Specify where the Dockerfile is located (e.g. in the root directory of the project)
5-
dockerfile: PHP85-Dockerfile # Specify the name of the Dockerfile
5+
dockerfile: PHP74-Dockerfile # Specify the name of the Dockerfile
66
ports:
77
- 8111:80
88
depends_on:

tests/Fixtures/AssertingHttpClient.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,8 +101,14 @@ public function request(Request $request): Response
101101
$this->testCase->assertSame($data['content'], $request->getContent());
102102
}
103103

104+
$createStubMethod = new \ReflectionMethod($this->testCase, 'createStub');
105+
106+
if (PHP_VERSION_ID < 80100) {
107+
$createStubMethod->setAccessible(true);
108+
}
109+
104110
/** @var \PHPUnit\Framework\MockObject\Stub&Response $response */
105-
$response = (new TestStubBuilder(Response::class))->getStub();
111+
$response = $createStubMethod->invoke($this->testCase, Response::class);
106112

107113
$response->method('getStatusCode')->willReturn($data['responseCode']);
108114
$response->method('getContentType')->willReturn($data['responseContentType']);

tests/Unit/Client/NativeCurlClient/RequestTest.php

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -50,13 +50,11 @@ public function testRequestReturnsCorrectResponse(string $method, string $data,
5050
'access_token',
5151
);
5252

53-
/** @var Request&\PHPUnit\Framework\MockObject\MockObject */
54-
$request = $this->createConfiguredStub(Request::class, [
55-
'getMethod' => $method,
56-
'getPath' => '/path',
57-
'getContentType' => $contentType,
58-
'getContent' => $data,
59-
]);
53+
$request = $this->createStub(Request::class);
54+
$request->method('getMethod')->willReturn($method);
55+
$request->method('getPath')->willReturn('/path');
56+
$request->method('getContentType')->willReturn($contentType);
57+
$request->method('getContent')->willReturn($data);
6058

6159
$response = $client->request($request);
6260

@@ -137,13 +135,11 @@ function ($errno, $errstr): bool {
137135
E_USER_DEPRECATED,
138136
);
139137

140-
/** @var Request&\PHPUnit\Framework\MockObject\MockObject */
141-
$request = $this->createConfiguredStub(Request::class, [
142-
'getMethod' => 'POST',
143-
'getPath' => '/uploads.json',
144-
'getContentType' => 'application/octet-stream',
145-
'getContent' => realpath(__DIR__ . '/../../../Fixtures/testfile_01.txt'),
146-
]);
138+
$request = $this->createStub(Request::class);
139+
$request->method('getMethod')->willReturn('POST');
140+
$request->method('getPath')->willReturn('/uploads.json');
141+
$request->method('getContentType')->willReturn('application/octet-stream');
142+
$request->method('getContent')->willReturn(realpath(__DIR__ . '/../../../Fixtures/testfile_01.txt'));
147143

148144
$response = $client->request($request);
149145

tests/Unit/Client/Psr18Client/RequestTest.php

Lines changed: 32 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -27,25 +27,23 @@ class RequestTest extends TestCase
2727
#[DataProvider('getRequestReponseData')]
2828
public function testRequestReturnsCorrectResponse(string $method, string $data, int $statusCode, string $contentType, string $content): void
2929
{
30-
$httpClient = $this->createConfiguredStub(ClientInterface::class, [
31-
'sendRequest' => $this->createConfiguredStub(ResponseInterface::class, [
32-
'getStatusCode' => $statusCode,
33-
'getHeaderLine' => $contentType,
34-
'getBody' => $this->createConfiguredStub(StreamInterface::class, [
35-
'__toString' => $content,
36-
]),
37-
]),
38-
]);
39-
40-
$requestFactory = $this->createConfiguredStub(RequestFactoryInterface::class, [
41-
'createRequest' => (function (): \PHPUnit\Framework\MockObject\Stub {
42-
$request = $this->createStub(RequestInterface::class);
43-
$request->method('withHeader')->willReturn($request);
44-
$request->method('withBody')->willReturn($request);
45-
46-
return $request;
47-
})(),
48-
]);
30+
$stream = $this->createStub(StreamInterface::class);
31+
$stream->method('__toString')->willReturn($content);
32+
33+
$response = $this->createStub(ResponseInterface::class);
34+
$response->method('getStatusCode')->willReturn($statusCode);
35+
$response->method('getHeaderLine')->willReturn($contentType);
36+
$response->method('getBody')->willReturn($stream);
37+
38+
$httpClient = $this->createStub(ClientInterface::class);
39+
$httpClient->method('sendRequest')->willReturn($response);
40+
41+
$request = $this->createStub(RequestInterface::class);
42+
$request->method('withHeader')->willReturn($request);
43+
$request->method('withBody')->willReturn($request);
44+
45+
$requestFactory = $this->createStub(RequestFactoryInterface::class);
46+
$requestFactory->method('createRequest')->willReturn($request);
4947

5048
$client = new Psr18Client(
5149
$httpClient,
@@ -55,12 +53,11 @@ public function testRequestReturnsCorrectResponse(string $method, string $data,
5553
'access_token',
5654
);
5755

58-
$request = $this->createConfiguredStub(Request::class, [
59-
'getMethod' => $method,
60-
'getPath' => '/path',
61-
'getContentType' => $contentType,
62-
'getContent' => $data,
63-
]);
56+
$request = $this->createStub(Request::class);
57+
$request->method('getMethod')->willReturn($method);
58+
$request->method('getPath')->willReturn('/path');
59+
$request->method('getContentType')->willReturn($contentType);
60+
$request->method('getContent')->willReturn($data);
6461

6562
$response = $client->request($request);
6663

@@ -104,15 +101,12 @@ public function testRequestThrowsClientException(): void
104101
new class ('error message') extends Exception implements ClientExceptionInterface {},
105102
);
106103

107-
$requestFactory = $this->createConfiguredStub(RequestFactoryInterface::class, [
108-
'createRequest' => (function (): \PHPUnit\Framework\MockObject\Stub {
109-
$request = $this->createStub(RequestInterface::class);
110-
$request->method('withHeader')->willReturn($request);
111-
$request->method('withBody')->willReturn($request);
104+
$request = $this->createStub(RequestInterface::class);
105+
$request->method('withHeader')->willReturn($request);
106+
$request->method('withBody')->willReturn($request);
112107

113-
return $request;
114-
})(),
115-
]);
108+
$requestFactory = $this->createStub(RequestFactoryInterface::class);
109+
$requestFactory->method('createRequest')->willReturn($request);
116110

117111
$client = new Psr18Client(
118112
$httpClient,
@@ -122,12 +116,11 @@ public function testRequestThrowsClientException(): void
122116
'access_token',
123117
);
124118

125-
$request = $this->createConfiguredStub(Request::class, [
126-
'getMethod' => 'GET',
127-
'getPath' => '/path',
128-
'getContentType' => 'application/json',
129-
'getContent' => '',
130-
]);
119+
$request = $this->createStub(Request::class);
120+
$request->method('getMethod')->willReturn('GET');
121+
$request->method('getPath')->willReturn('/path');
122+
$request->method('getContentType')->willReturn('application/json');
123+
$request->method('getContent')->willReturn('');
131124

132125
$this->expectException(ClientException::class);
133126
$this->expectExceptionMessage('error message');

tests/Unit/Client/Psr18ClientTest.php

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -41,26 +41,27 @@ public function testShouldPassApiKeyToConstructor(): void
4141

4242
public function testServerRequestFactoryIsAcceptedInConstructorForBC(): void
4343
{
44-
$client = new Psr18Client(
44+
// PHPUnit 10 compatible way to test trigger_error().
45+
set_error_handler(
46+
function ($errno, $errstr): bool {
47+
$this->assertSame(
48+
'Redmine\Client\Psr18Client::__construct(): Providing Argument #2 ($requestFactory) as Psr\Http\Message\ServerRequestFactoryInterface is deprecated since v2.3.0, please provide as Psr\Http\Message\RequestFactoryInterface instead.',
49+
$errstr,
50+
);
51+
52+
restore_error_handler();
53+
return true;
54+
},
55+
E_USER_DEPRECATED,
56+
);
57+
58+
new Psr18Client(
4559
$this->createStub(ClientInterface::class),
46-
$this->createConfiguredStub(ServerRequestFactoryInterface::class, [
47-
'createServerRequest' => (function (): \PHPUnit\Framework\MockObject\Stub {
48-
$request = $this->createStub(ServerRequestInterface::class);
49-
$request->method('withHeader')->willReturn($request);
50-
$request->method('withBody')->willReturn($request);
51-
52-
return $request;
53-
})(),
54-
]),
60+
$this->createStub(ServerRequestFactoryInterface::class),
5561
$this->createStub(StreamFactoryInterface::class),
5662
'http://test.local',
5763
'access_token',
5864
);
59-
60-
$this->assertInstanceOf(Psr18Client::class, $client);
61-
$this->assertInstanceOf(Client::class, $client);
62-
63-
$client->requestGet('/path.xml');
6465
}
6566

6667
public function testShouldPassUsernameAndPasswordToConstructor(): void

0 commit comments

Comments
 (0)