Skip to content

Commit a354c55

Browse files
committed
tests: replace mocks with stubs in tests
1 parent 655fe61 commit a354c55

38 files changed

Lines changed: 172 additions & 171 deletions

tests/Fixtures/AssertingHttpClient.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66

77
use PHPUnit\Framework\MockObject\MockBuilder;
88
use PHPUnit\Framework\MockObject\Rule\InvokedCount;
9+
use PHPUnit\Framework\MockObject\TestStubBuilder;
910
use PHPUnit\Framework\TestCase;
1011
use Redmine\Http\HttpClient;
1112
use Redmine\Http\Request;
@@ -100,8 +101,8 @@ public function request(Request $request): Response
100101
$this->testCase->assertSame($data['content'], $request->getContent());
101102
}
102103

103-
/** @var \PHPUnit\Framework\MockObject\MockObject&Response */
104-
$response = (new MockBuilder($this->testCase, Response::class))->getMock();
104+
/** @var \PHPUnit\Framework\MockObject\Stub&Response $response */
105+
$response = (new TestStubBuilder(Response::class))->getStub();
105106

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

tests/Integration/Psr18ClientRequestGenerationTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,11 +33,11 @@ public function testPsr18ClientCreatesCorrectRequests(
3333
$data,
3434
string $expectedOutput
3535
): void {
36-
$response = $this->createMock(ResponseInterface::class);
36+
$response = $this->createStub(ResponseInterface::class);
3737

3838
/** @var ClientInterface&\PHPUnit\Framework\MockObject\MockObject */
39-
$httpClient = $this->createMock(ClientInterface::class);
40-
$httpClient->method('sendRequest')->willReturnCallback(function ($request) use ($response, $expectedOutput): \PHPUnit\Framework\MockObject\MockObject {
39+
$httpClient = $this->createStub(ClientInterface::class);
40+
$httpClient->method('sendRequest')->willReturnCallback(function ($request) use ($response, $expectedOutput): \PHPUnit\Framework\MockObject\Stub {
4141
// Create a text representation of the HTTP request
4242
$content = $request->getBody()->__toString();
4343

@@ -64,7 +64,7 @@ public function testPsr18ClientCreatesCorrectRequests(
6464
return $response;
6565
});
6666

67-
$requestFactory = $this->createMock(RequestFactoryInterface::class);
67+
$requestFactory = $this->createStub(RequestFactoryInterface::class);
6868
$requestFactory->method('createRequest')->willReturnCallback(fn($method, $uri): \GuzzleHttp\Psr7\Request => new Request($method, $uri));
6969

7070
$streamFactory = new class implements StreamFactoryInterface {

tests/Unit/Api/AbstractApi/DeleteTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function testDeleteWithHttpClient(): void
4949
#[DataProvider('getXmlDecodingFromDeleteMethodData')]
5050
public function testXmlDecodingFromDeleteMethod(string $response, string $expected): void
5151
{
52-
$client = $this->createMock(Client::class);
52+
$client = $this->createStub(Client::class);
5353
$client->method('getLastResponseBody')->willReturn($response);
5454
$client->method('getLastResponseContentType')->willReturn('application/xml');
5555

tests/Unit/Api/AbstractApi/GetTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function testGetWithHttpClient(): void
5151
#[DataProvider('getJsonDecodingFromGetMethodData')]
5252
public function testJsonDecodingFromGetMethod(string $response, ?bool $decode, $expected): void
5353
{
54-
$client = $this->createMock(Client::class);
54+
$client = $this->createStub(Client::class);
5555
$client->method('getLastResponseBody')->willReturn($response);
5656
$client->method('getLastResponseContentType')->willReturn('application/json');
5757

@@ -88,7 +88,7 @@ public static function getJsonDecodingFromGetMethodData(): array
8888
#[DataProvider('getXmlDecodingFromGetMethodData')]
8989
public function testXmlDecodingFromGetMethod(string $response, ?bool $decode, string $expected): void
9090
{
91-
$client = $this->createMock(Client::class);
91+
$client = $this->createStub(Client::class);
9292
$client->method('getLastResponseBody')->willReturn($response);
9393
$client->method('getLastResponseContentType')->willReturn('application/xml');
9494

tests/Unit/Api/AbstractApi/PostTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function testPostWithHttpClient(): void
5151
#[DataProvider('getXmlDecodingFromPostMethodData')]
5252
public function testXmlDecodingFromPostMethod(string $response, string $expected): void
5353
{
54-
$client = $this->createMock(Client::class);
54+
$client = $this->createStub(Client::class);
5555
$client->method('getLastResponseBody')->willReturn($response);
5656
$client->method('getLastResponseContentType')->willReturn('application/xml');
5757

tests/Unit/Api/AbstractApi/PutTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public function testPutWithHttpClient(): void
5151
#[DataProvider('getXmlDecodingFromPutMethodData')]
5252
public function testXmlDecodingFromPutMethod(string $response, string $expected): void
5353
{
54-
$client = $this->createMock(Client::class);
54+
$client = $this->createStub(Client::class);
5555
$client->method('getLastResponseBody')->willReturn($response);
5656
$client->method('getLastResponseContentType')->willReturn('application/xml');
5757

tests/Unit/Api/AbstractApiTest.php

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class AbstractApiTest extends TestCase
2020
{
2121
public function testCreateWithHttpClientWorks(): void
2222
{
23-
$client = $this->createMock(HttpClient::class);
23+
$client = $this->createStub(HttpClient::class);
2424

2525
$api = new class ($client) extends AbstractApi {};
2626

@@ -34,7 +34,7 @@ public function testCreateWithHttpClientWorks(): void
3434

3535
public function testCreateWitClientWorks(): void
3636
{
37-
$client = $this->createMock(Client::class);
37+
$client = $this->createStub(Client::class);
3838

3939
$api = new class ($client) extends AbstractApi {};
4040

@@ -57,7 +57,7 @@ public function testCreateWithoutClitentOrHttpClientThrowsException(): void
5757

5858
public function testGetTriggersDeprecationWarning(): void
5959
{
60-
$client = $this->createMock(HttpClient::class);
60+
$client = $this->createStub(HttpClient::class);
6161

6262
$api = new class ($client) extends AbstractApi {
6363
public function runGet($path)
@@ -85,7 +85,7 @@ function ($errno, $errstr): bool {
8585

8686
public function testGetLastResponseWithHttpClientWorks(): void
8787
{
88-
$client = $this->createMock(HttpClient::class);
88+
$client = $this->createStub(HttpClient::class);
8989

9090
$api = new class ($client) extends AbstractApi {};
9191

@@ -94,7 +94,7 @@ public function testGetLastResponseWithHttpClientWorks(): void
9494

9595
public function testPostTriggersDeprecationWarning(): void
9696
{
97-
$client = $this->createMock(HttpClient::class);
97+
$client = $this->createStub(HttpClient::class);
9898

9999
$api = new class ($client) extends AbstractApi {
100100
public function runPost($path, $data)
@@ -122,7 +122,7 @@ function ($errno, $errstr): bool {
122122

123123
public function testPutTriggersDeprecationWarning(): void
124124
{
125-
$client = $this->createMock(HttpClient::class);
125+
$client = $this->createStub(HttpClient::class);
126126

127127
$api = new class ($client) extends AbstractApi {
128128
public function runPut($path, $data)
@@ -150,7 +150,7 @@ function ($errno, $errstr): bool {
150150

151151
public function testDeleteTriggersDeprecationWarning(): void
152152
{
153-
$client = $this->createMock(HttpClient::class);
153+
$client = $this->createStub(HttpClient::class);
154154

155155
$api = new class ($client) extends AbstractApi {
156156
public function runDelete($path)
@@ -182,7 +182,7 @@ function ($errno, $errstr): bool {
182182
#[DataProvider('getIsNotNullReturnsCorrectBooleanData')]
183183
public function testIsNotNullReturnsCorrectBoolean(bool $expected, $value): void
184184
{
185-
$client = $this->createMock(Client::class);
185+
$client = $this->createStub(Client::class);
186186

187187
$api = new class ($client) extends AbstractApi {};
188188

@@ -265,7 +265,7 @@ public function __construct($client)
265265
#[DataProvider('getLastCallFailedData')]
266266
public function testLastCallFailedWithClientReturnsCorrectBoolean(int $statusCode, bool $expectedBoolean): void
267267
{
268-
$client = $this->createMock(Client::class);
268+
$client = $this->createStub(Client::class);
269269
$client->method('getLastResponseStatusCode')->willReturn($statusCode);
270270

271271
$api = new class ($client) extends AbstractApi {};
@@ -279,10 +279,10 @@ public function testLastCallFailedWithClientReturnsCorrectBoolean(int $statusCod
279279
#[DataProvider('getLastCallFailedData')]
280280
public function testLastCallFailedWithHttpClientReturnsCorrectBoolean(int $statusCode, bool $expectedBoolean): void
281281
{
282-
$response = $this->createMock(Response::class);
282+
$response = $this->createStub(Response::class);
283283
$response->method('getStatusCode')->willReturn($statusCode);
284284

285-
$client = $this->createMock(HttpClient::class);
285+
$client = $this->createStub(HttpClient::class);
286286
$client->method('request')->willReturn($response);
287287

288288
$api = new class ($client) extends AbstractApi {
@@ -373,7 +373,7 @@ public static function getLastCallFailedData(): array
373373
#[DataProvider('retrieveDataData')]
374374
public function testRetrieveData(string $path, string $contentType, string $response, array $expected): void
375375
{
376-
$client = $this->createMock(Client::class);
376+
$client = $this->createStub(Client::class);
377377
$client->method('requestGet')->willReturn(true);
378378
$client->method('getLastResponseBody')->willReturn($response);
379379
$client->method('getLastResponseContentType')->willReturn($contentType);
@@ -458,7 +458,7 @@ public function testRetrieveDataWith250ResultsMakes3Requests(): void
458458
#[DataProvider('getRetrieveDataToExceptionData')]
459459
public function testRetrieveDataThrowsException(string $response, string $contentType, string $expectedException, string $expectedMessage): void
460460
{
461-
$client = $this->createMock(Client::class);
461+
$client = $this->createStub(Client::class);
462462
$client->method('requestGet')->willReturn(true);
463463
$client->method('getLastResponseBody')->willReturn($response);
464464
$client->method('getLastResponseContentType')->willReturn($contentType);
@@ -489,7 +489,7 @@ public static function getRetrieveDataToExceptionData(): array
489489
#[DataProvider('getRetrieveAllData')]
490490
public function testDeprecatedRetrieveAll(string $content, string $contentType, $expected): void
491491
{
492-
$client = $this->createMock(Client::class);
492+
$client = $this->createStub(Client::class);
493493
$client->method('requestGet')->willReturn(true);
494494
$client->method('getLastResponseBody')->willReturn($content);
495495
$client->method('getLastResponseContentType')->willReturn($contentType);
@@ -515,7 +515,7 @@ public static function getRetrieveAllData(): array
515515

516516
public function testDeprecatedAttachCustomFieldXML(): void
517517
{
518-
$client = $this->createMock(Client::class);
518+
$client = $this->createStub(Client::class);
519519

520520
$api = new class ($client) extends AbstractApi {};
521521

tests/Unit/Api/CustomFieldTest.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -286,7 +286,7 @@ public function testListingCallsGetEveryTimeWithForceUpdate(): void
286286
*/
287287
public function testListingTriggersDeprecationWarning(): void
288288
{
289-
$client = $this->createMock(Client::class);
289+
$client = $this->createStub(Client::class);
290290
$client->method('requestGet')
291291
->willReturn(true);
292292
$client->method('getLastResponseBody')
@@ -346,7 +346,7 @@ public function testGetIdByNameMakesGetRequest(): void
346346

347347
public function testGetIdByNameTriggersDeprecationWarning(): void
348348
{
349-
$client = $this->createMock(Client::class);
349+
$client = $this->createStub(Client::class);
350350
$client->method('requestGet')
351351
->willReturn(true);
352352
$client->method('getLastResponseBody')

tests/Unit/Api/Group/CreateTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ public function testCreateThrowsExceptionIfNameIsMissing(): void
137137
$postParameter = [];
138138

139139
// Create the used mock objects
140-
$client = $this->createMock(HttpClient::class);
140+
$client = $this->createStub(HttpClient::class);
141141

142142
// Create the object under test
143143
$api = new Group($client);

tests/Unit/Api/GroupTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ public function testAllReturnsClientGetResponseWithParameters(): void
118118
*/
119119
public function testListingTriggersDeprecationWarning(): void
120120
{
121-
$client = $this->createMock(Client::class);
121+
$client = $this->createStub(Client::class);
122122
$client->method('requestGet')
123123
->willReturn(true);
124124
$client->method('getLastResponseBody')

0 commit comments

Comments
 (0)