Skip to content

Commit 5a0f0df

Browse files
committed
refactor: use Membership::fromHttpClient() in tests
1 parent 15ca555 commit 5a0f0df

File tree

6 files changed

+84
-90
lines changed

6 files changed

+84
-90
lines changed

tests/Unit/Api/Membership/CreateTest.php

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public function testCreateReturnsCorrectResponse(int $identifier, array $paramet
3434
);
3535

3636
// Create the object under test
37-
$api = new Membership($client);
37+
$api = Membership::fromHttpClient($client);
3838

3939
// Perform the tests
4040
$return = $api->create($identifier, $parameters);
@@ -81,7 +81,7 @@ public function testCreateReturnsEmptyString(): void
8181
);
8282

8383
// Create the object under test
84-
$api = new Membership($client);
84+
$api = Membership::fromHttpClient($client);
8585

8686
// Perform the tests
8787
$return = $api->create(5, ['user_id' => 4, 'role_ids' => 2]);
@@ -91,11 +91,10 @@ public function testCreateReturnsEmptyString(): void
9191

9292
public function testCreateThrowsExceptionWithEmptyParameters(): void
9393
{
94-
// Create the used mock objects
9594
$client = $this->createStub(HttpClient::class);
9695

9796
// Create the object under test
98-
$api = new Membership($client);
97+
$api = Membership::fromHttpClient($client);
9998

10099
$this->expectException(MissingParameterException::class);
101100
$this->expectExceptionMessage('Theses parameters are mandatory: `user_id`, `role_ids`');
@@ -110,11 +109,10 @@ public function testCreateThrowsExceptionWithEmptyParameters(): void
110109
#[DataProvider('incompleteCreateParameterProvider')]
111110
public function testCreateThrowsExceptionIfMandatoyParametersAreMissing(array $parameters): void
112111
{
113-
// Create the used mock objects
114112
$client = $this->createStub(HttpClient::class);
115113

116114
// Create the object under test
117-
$api = new Membership($client);
115+
$api = Membership::fromHttpClient($client);
118116

119117
$this->expectException(MissingParameterException::class);
120118
$this->expectExceptionMessage('Theses parameters are mandatory: `user_id`, `role_ids`');

tests/Unit/Api/Membership/ListByProjectTest.php

Lines changed: 44 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@
66
use PHPUnit\Framework\Attributes\DataProviderExternal;
77
use PHPUnit\Framework\TestCase;
88
use Redmine\Api\Membership;
9-
use Redmine\Client\Client;
109
use Redmine\Exception\InvalidParameterException;
1110
use Redmine\Exception\UnexpectedResponseException;
12-
use Redmine\Tests\Fixtures\MockClient;
11+
use Redmine\Http\HttpClient;
12+
use Redmine\Tests\Fixtures\AssertingHttpClient;
1313
use Redmine\Tests\Fixtures\TestDataProvider;
1414

1515
#[CoversClass(Membership::class)]
@@ -21,21 +21,21 @@ public function testListByProjectWithoutParametersReturnsResponse(): void
2121
$response = '["API Response"]';
2222
$expectedReturn = ['API Response'];
2323

24-
// Create the used mock objects
25-
$client = $this->createMock(Client::class);
26-
$client->expects($this->once())
27-
->method('requestGet')
28-
->with('/projects/5/memberships.json')
29-
->willReturn(true);
30-
$client->expects($this->exactly(1))
31-
->method('getLastResponseBody')
32-
->willReturn($response);
33-
$client->expects($this->exactly(1))
34-
->method('getLastResponseContentType')
35-
->willReturn('application/json');
24+
$client = AssertingHttpClient::create(
25+
$this,
26+
[
27+
'GET',
28+
'/projects/5/memberships.json',
29+
'application/json',
30+
'',
31+
200,
32+
'application/json',
33+
$response,
34+
],
35+
);
3636

3737
// Create the object under test
38-
$api = new Membership($client);
38+
$api = Membership::fromHttpClient($client);
3939

4040
// Perform the tests
4141
$this->assertSame($expectedReturn, $api->listByProject(5));
@@ -48,21 +48,21 @@ public function testListByProjectWithParametersReturnsResponse(): void
4848
$response = '["API Response"]';
4949
$expectedReturn = ['API Response'];
5050

51-
// Create the used mock objects
52-
$client = $this->createMock(Client::class);
53-
$client->expects($this->once())
54-
->method('requestGet')
55-
->with('/projects/project-slug/memberships.json?limit=25&offset=0&0=not-used')
56-
->willReturn(true);
57-
$client->expects($this->exactly(1))
58-
->method('getLastResponseBody')
59-
->willReturn($response);
60-
$client->expects($this->exactly(1))
61-
->method('getLastResponseContentType')
62-
->willReturn('application/json');
51+
$client = AssertingHttpClient::create(
52+
$this,
53+
[
54+
'GET',
55+
'/projects/project-slug/memberships.json?limit=25&offset=0&0=not-used',
56+
'application/json',
57+
'',
58+
200,
59+
'application/json',
60+
$response,
61+
],
62+
);
6363

6464
// Create the object under test
65-
$api = new Membership($client);
65+
$api = Membership::fromHttpClient($client);
6666

6767
// Perform the tests
6868
$this->assertSame($expectedReturn, $api->listByProject('project-slug', $parameters));
@@ -74,7 +74,7 @@ public function testListByProjectWithParametersReturnsResponse(): void
7474
#[DataProviderExternal(TestDataProvider::class, 'getInvalidProjectIdentifiers')]
7575
public function testListByProjectWithWrongProjectIdentifierThrowsException($projectIdentifier): void
7676
{
77-
$api = new Membership(MockClient::create());
77+
$api = Membership::fromHttpClient($this->createStub(HttpClient::class));
7878

7979
$this->expectException(InvalidParameterException::class);
8080
$this->expectExceptionMessage('Redmine\Api\Membership::listByProject(): Argument #1 ($projectIdentifier) must be of type int or string');
@@ -84,21 +84,23 @@ public function testListByProjectWithWrongProjectIdentifierThrowsException($proj
8484

8585
public function testListByProjectThrowsException(): void
8686
{
87-
// Create the used mock objects
88-
$client = $this->createMock(Client::class);
89-
$client->expects($this->exactly(1))
90-
->method('requestGet')
91-
->with('/projects/5/memberships.json')
92-
->willReturn(true);
93-
$client->expects($this->exactly(1))
94-
->method('getLastResponseBody')
95-
->willReturn('');
96-
$client->expects($this->exactly(1))
97-
->method('getLastResponseContentType')
98-
->willReturn('application/json');
87+
$response = '';
88+
89+
$client = AssertingHttpClient::create(
90+
$this,
91+
[
92+
'GET',
93+
'/projects/5/memberships.json',
94+
'application/json',
95+
'',
96+
200,
97+
'application/json',
98+
$response,
99+
],
100+
);
99101

100102
// Create the object under test
101-
$api = new Membership($client);
103+
$api = Membership::fromHttpClient($client);
102104

103105
$this->expectException(UnexpectedResponseException::class);
104106
$this->expectExceptionMessage('The Redmine server replied with an unexpected response.');

tests/Unit/Api/Membership/RemoveMemberTest.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public function testRemoveMemberReturnsCorrectResponse(int $projectIdentifier, i
4040
);
4141

4242
// Create the object under test
43-
$api = new Membership($client);
43+
$api = Membership::fromHttpClient($client);
4444

4545
// Perform the tests
4646
$this->assertSame($response, $api->removeMember($projectIdentifier, $userId, $params));
@@ -84,7 +84,7 @@ public function testRemoveMemberReturnsFalseIfUserIsNotMemberOfProject(): void
8484
);
8585

8686
// Create the object under test
87-
$api = new Membership($client);
87+
$api = Membership::fromHttpClient($client);
8888

8989
// Perform the tests
9090
$this->assertFalse($api->removeMember(1, 2));
@@ -106,7 +106,7 @@ public function testRemoveMemberReturnsFalseIfMemberlistIsMissing(): void
106106
);
107107

108108
// Create the object under test
109-
$api = new Membership($client);
109+
$api = Membership::fromHttpClient($client);
110110

111111
// Perform the tests
112112
$this->assertFalse($api->removeMember(1, 2));

tests/Unit/Api/Membership/RemoveTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public function testRemoveReturnsCorrectResponse(int $id, string $expectedPath,
3131
);
3232

3333
// Create the object under test
34-
$api = new Membership($client);
34+
$api = Membership::fromHttpClient($client);
3535

3636
// Perform the tests
3737
$this->assertSame($response, $api->remove($id));

tests/Unit/Api/Membership/UpdateTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function testUpdateReturnsCorrectResponse(int $id, array $parameters, str
3333
);
3434

3535
// Create the object under test
36-
$api = new Membership($client);
36+
$api = Membership::fromHttpClient($client);
3737

3838
// Perform the tests
3939
$this->assertSame('', $api->update($id, $parameters));
@@ -78,7 +78,7 @@ public function testUpdateReturnsEmptyString(): void
7878
);
7979

8080
// Create the object under test
81-
$api = new Membership($client);
81+
$api = Membership::fromHttpClient($client);
8282

8383
// Perform the tests
8484
$return = $api->update(5, ['user_id' => 4, 'role_ids' => 2]);
@@ -92,7 +92,7 @@ public function testUpdateThrowsExceptionWithEmptyParameters(): void
9292
$client = $this->createStub(HttpClient::class);
9393

9494
// Create the object under test
95-
$api = new Membership($client);
95+
$api = Membership::fromHttpClient($client);
9696

9797
$this->expectException(MissingParameterException::class);
9898
$this->expectExceptionMessage('Theses parameters are mandatory: `role_ids`');
@@ -111,7 +111,7 @@ public function testUpdateThrowsExceptionIfMandatoyParametersAreMissing(array $p
111111
$client = $this->createStub(HttpClient::class);
112112

113113
// Create the object under test
114-
$api = new Membership($client);
114+
$api = Membership::fromHttpClient($client);
115115

116116
$this->expectException(MissingParameterException::class);
117117
$this->expectExceptionMessage('Theses parameters are mandatory: `role_ids`');

tests/Unit/Api/MembershipTest.php

Lines changed: 28 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,8 @@
66
use PHPUnit\Framework\Attributes\DataProvider;
77
use PHPUnit\Framework\TestCase;
88
use Redmine\Api\Membership;
9-
use Redmine\Client\Client;
109
use Redmine\Http\HttpClient;
11-
use Redmine\Tests\Fixtures\MockClient;
10+
use Redmine\Tests\Fixtures\AssertingHttpClient;
1211

1312
/**
1413
* @author Malte Gerth <mail@malte-gerth.de>
@@ -59,7 +58,7 @@ function ($errno, $errstr): bool {
5958
*/
6059
public function testAllTriggersDeprecationWarning(): void
6160
{
62-
$api = new Membership(MockClient::create());
61+
$api = Membership::fromHttpClient($this->createStub(HttpClient::class));
6362

6463
// PHPUnit 10 compatible way to test trigger_error().
6564
set_error_handler(
@@ -86,21 +85,21 @@ function ($errno, $errstr): bool {
8685
#[DataProvider('getAllData')]
8786
public function testAllReturnsClientGetResponseWithProject(string $response, string $responseType, $expectedResponse): void
8887
{
89-
// Create the used mock objects
90-
$client = $this->createMock(Client::class);
91-
$client->expects($this->exactly(1))
92-
->method('requestGet')
93-
->with('/projects/5/memberships.json')
94-
->willReturn(true);
95-
$client->expects($this->atLeast(1))
96-
->method('getLastResponseBody')
97-
->willReturn($response);
98-
$client->expects($this->exactly(1))
99-
->method('getLastResponseContentType')
100-
->willReturn($responseType);
88+
$client = AssertingHttpClient::create(
89+
$this,
90+
[
91+
'GET',
92+
'/projects/5/memberships.json',
93+
'application/json',
94+
'',
95+
200,
96+
$responseType,
97+
$response,
98+
],
99+
);
101100

102101
// Create the object under test
103-
$api = new Membership($client);
102+
$api = Membership::fromHttpClient($client);
104103

105104
// Perform the tests
106105
$this->assertSame($expectedResponse, $api->all(5));
@@ -125,26 +124,21 @@ public function testAllReturnsClientGetResponseWithParametersAndProject(): void
125124
$response = '["API Response"]';
126125
$expectedReturn = ['API Response'];
127126

128-
// Create the used mock objects
129-
$client = $this->createMock(Client::class);
130-
$client->expects($this->once())
131-
->method('requestGet')
132-
->with(
133-
$this->logicalAnd(
134-
$this->stringStartsWith('/projects/5/memberships.json'),
135-
$this->stringContains('not-used'),
136-
),
137-
)
138-
->willReturn(true);
139-
$client->expects($this->exactly(1))
140-
->method('getLastResponseBody')
141-
->willReturn($response);
142-
$client->expects($this->exactly(1))
143-
->method('getLastResponseContentType')
144-
->willReturn('application/json');
127+
$client = AssertingHttpClient::create(
128+
$this,
129+
[
130+
'GET',
131+
'/projects/5/memberships.json?limit=25&offset=0&0=not-used',
132+
'application/json',
133+
'',
134+
200,
135+
'application/json',
136+
$response,
137+
],
138+
);
145139

146140
// Create the object under test
147-
$api = new Membership($client);
141+
$api = Membership::fromHttpClient($client);
148142

149143
// Perform the tests
150144
$this->assertSame($expectedReturn, $api->all(5, $parameters));

0 commit comments

Comments
 (0)