Skip to content

Commit 215c9c1

Browse files
committed
Refactor tests, use AssertingHttpClient
1 parent 6db2cea commit 215c9c1

File tree

5 files changed

+180
-283
lines changed

5 files changed

+180
-283
lines changed

tests/Fixtures/AssertingHttpClient.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public function request(Request $request): Response
8888

8989
if ($data['content'] !== '' && $data['contentType'] === 'application/xml') {
9090
$this->testCase->assertXmlStringEqualsXmlString($data['content'], $request->getContent());
91-
} else if ($data['content'] !== '' && $data['contentType'] === 'application/json') {
91+
} elseif ($data['content'] !== '' && $data['contentType'] === 'application/json') {
9292
$this->testCase->assertJsonStringEqualsJsonString($data['content'], $request->getContent());
9393
} else {
9494
$this->testCase->assertSame($data['content'], $request->getContent());

tests/Unit/Api/AbstractApiTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
use Redmine\Client\Client;
99
use Redmine\Exception\SerializerException;
1010
use Redmine\Http\HttpClient;
11-
use Redmine\Http\Request;
1211
use Redmine\Http\Response;
1312
use Redmine\Tests\Fixtures\AssertingHttpClient;
1413
use ReflectionMethod;

tests/Unit/Api/Group/CreateTest.php

Lines changed: 37 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,7 @@
88
use Redmine\Api\Group;
99
use Redmine\Exception\MissingParameterException;
1010
use Redmine\Http\HttpClient;
11-
use Redmine\Http\Request;
12-
use Redmine\Http\Response;
11+
use Redmine\Tests\Fixtures\AssertingHttpClient;
1312
use SimpleXMLElement;
1413

1514
/**
@@ -19,23 +18,18 @@ class CreateTest extends TestCase
1918
{
2019
public function testCreateWithNameCreatesGroup()
2120
{
22-
$client = $this->createMock(HttpClient::class);
23-
$client->expects($this->exactly(1))
24-
->method('request')
25-
->willReturnCallback(function (Request $request) {
26-
$this->assertSame('POST', $request->getMethod());
27-
$this->assertSame('/groups.xml', $request->getPath());
28-
$this->assertSame('application/xml', $request->getContentType());
29-
$this->assertXmlStringEqualsXmlString('<?xml version="1.0"?><group><name>Group Name</name></group>', $request->getContent());
30-
31-
return $this->createConfiguredMock(
32-
Response::class,
33-
[
34-
'getContentType' => 'application/xml',
35-
'getContent' => '<?xml version="1.0"?><group></group>',
36-
]
37-
);
38-
});
21+
$client = AssertingHttpClient::create(
22+
$this,
23+
[
24+
'POST',
25+
'/groups.xml',
26+
'application/xml',
27+
'<?xml version="1.0"?><group><name>Group Name</name></group>',
28+
200,
29+
'application/xml',
30+
'<?xml version="1.0"?><group></group>'
31+
]
32+
);
3933

4034
// Create the object under test
4135
$api = new Group($client);
@@ -52,23 +46,18 @@ public function testCreateWithNameCreatesGroup()
5246

5347
public function testCreateWithNameAndUserIdsCreatesGroup()
5448
{
55-
$client = $this->createMock(HttpClient::class);
56-
$client->expects($this->exactly(1))
57-
->method('request')
58-
->willReturnCallback(function (Request $request) {
59-
$this->assertSame('POST', $request->getMethod());
60-
$this->assertSame('/groups.xml', $request->getPath());
61-
$this->assertSame('application/xml', $request->getContentType());
62-
$this->assertXmlStringEqualsXmlString('<?xml version="1.0"?><group><name>Group Name</name><user_ids type="array"><user_id>1</user_id><user_id>2</user_id><user_id>3</user_id></user_ids></group>', $request->getContent());
63-
64-
return $this->createConfiguredMock(
65-
Response::class,
66-
[
67-
'getContentType' => 'application/xml',
68-
'getContent' => '<?xml version="1.0"?><group></group>',
69-
]
70-
);
71-
});
49+
$client = AssertingHttpClient::create(
50+
$this,
51+
[
52+
'POST',
53+
'/groups.xml',
54+
'application/xml',
55+
'<?xml version="1.0"?><group><name>Group Name</name><user_ids type="array"><user_id>1</user_id><user_id>2</user_id><user_id>3</user_id></user_ids></group>',
56+
200,
57+
'application/xml',
58+
'<?xml version="1.0"?><group></group>'
59+
]
60+
);
7261

7362
// Create the object under test
7463
$api = new Group($client);
@@ -85,23 +74,18 @@ public function testCreateWithNameAndUserIdsCreatesGroup()
8574

8675
public function testCreateWithNameAndCustomFieldsCreatesGroup()
8776
{
88-
$client = $this->createMock(HttpClient::class);
89-
$client->expects($this->exactly(1))
90-
->method('request')
91-
->willReturnCallback(function (Request $request) {
92-
$this->assertSame('POST', $request->getMethod());
93-
$this->assertSame('/groups.xml', $request->getPath());
94-
$this->assertSame('application/xml', $request->getContentType());
95-
$this->assertXmlStringEqualsXmlString('<?xml version="1.0"?><group><name>Group Name</name><custom_fields type="array"><custom_field id="1"><value>5</value></custom_field></custom_fields></group>', $request->getContent());
96-
97-
return $this->createConfiguredMock(
98-
Response::class,
99-
[
100-
'getContentType' => 'application/xml',
101-
'getContent' => '<?xml version="1.0"?><group></group>',
102-
]
103-
);
104-
});
77+
$client = AssertingHttpClient::create(
78+
$this,
79+
[
80+
'POST',
81+
'/groups.xml',
82+
'application/xml',
83+
'<?xml version="1.0"?><group><name>Group Name</name><custom_fields type="array"><custom_field id="1"><value>5</value></custom_field></custom_fields></group>',
84+
200,
85+
'application/xml',
86+
'<?xml version="1.0"?><group></group>'
87+
]
88+
);
10589

10690
// Create the object under test
10791
$api = new Group($client);

tests/Unit/Api/Group/UpdateTest.php

Lines changed: 37 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,7 @@
66

77
use PHPUnit\Framework\TestCase;
88
use Redmine\Api\Group;
9-
use Redmine\Http\HttpClient;
10-
use Redmine\Http\Request;
11-
use Redmine\Http\Response;
12-
use SimpleXMLElement;
9+
use Redmine\Tests\Fixtures\AssertingHttpClient;
1310

1411
/**
1512
* @covers \Redmine\Api\Group::update
@@ -18,23 +15,18 @@ class UpdateTest extends TestCase
1815
{
1916
public function testUpdateWithNameUpdatesGroup()
2017
{
21-
$client = $this->createMock(HttpClient::class);
22-
$client->expects($this->exactly(1))
23-
->method('request')
24-
->willReturnCallback(function (Request $request) {
25-
$this->assertSame('PUT', $request->getMethod());
26-
$this->assertSame('/groups/1.xml', $request->getPath());
27-
$this->assertSame('application/xml', $request->getContentType());
28-
$this->assertXmlStringEqualsXmlString('<?xml version="1.0"?><group><name>Group Name</name></group>', $request->getContent());
29-
30-
return $this->createConfiguredMock(
31-
Response::class,
32-
[
33-
'getContentType' => 'application/xml',
34-
'getContent' => '',
35-
]
36-
);
37-
});
18+
$client = AssertingHttpClient::create(
19+
$this,
20+
[
21+
'PUT',
22+
'/groups/1.xml',
23+
'application/xml',
24+
'<?xml version="1.0"?><group><name>Group Name</name></group>',
25+
200,
26+
'application/xml',
27+
''
28+
]
29+
);
3830

3931
// Create the object under test
4032
$api = new Group($client);
@@ -47,23 +39,18 @@ public function testUpdateWithNameUpdatesGroup()
4739

4840
public function testUpdateWithUserIdsUpdatesGroup()
4941
{
50-
$client = $this->createMock(HttpClient::class);
51-
$client->expects($this->exactly(1))
52-
->method('request')
53-
->willReturnCallback(function (Request $request) {
54-
$this->assertSame('PUT', $request->getMethod());
55-
$this->assertSame('/groups/1.xml', $request->getPath());
56-
$this->assertSame('application/xml', $request->getContentType());
57-
$this->assertXmlStringEqualsXmlString('<?xml version="1.0"?><group><user_ids type="array"><user_id>1</user_id><user_id>2</user_id><user_id>3</user_id></user_ids></group>', $request->getContent());
58-
59-
return $this->createConfiguredMock(
60-
Response::class,
61-
[
62-
'getContentType' => 'application/xml',
63-
'getContent' => '',
64-
]
65-
);
66-
});
42+
$client = AssertingHttpClient::create(
43+
$this,
44+
[
45+
'PUT',
46+
'/groups/1.xml',
47+
'application/xml',
48+
'<?xml version="1.0"?><group><user_ids type="array"><user_id>1</user_id><user_id>2</user_id><user_id>3</user_id></user_ids></group>',
49+
200,
50+
'application/xml',
51+
''
52+
]
53+
);
6754

6855
// Create the object under test
6956
$api = new Group($client);
@@ -76,23 +63,18 @@ public function testUpdateWithUserIdsUpdatesGroup()
7663

7764
public function testUpdateWithCustomFieldsUpdatesGroup()
7865
{
79-
$client = $this->createMock(HttpClient::class);
80-
$client->expects($this->exactly(1))
81-
->method('request')
82-
->willReturnCallback(function (Request $request) {
83-
$this->assertSame('PUT', $request->getMethod());
84-
$this->assertSame('/groups/1.xml', $request->getPath());
85-
$this->assertSame('application/xml', $request->getContentType());
86-
$this->assertXmlStringEqualsXmlString('<?xml version="1.0"?><group><custom_fields type="array"><custom_field id="1"><value>5</value></custom_field></custom_fields></group>', $request->getContent());
87-
88-
return $this->createConfiguredMock(
89-
Response::class,
90-
[
91-
'getContentType' => 'application/xml',
92-
'getContent' => '',
93-
]
94-
);
95-
});
66+
$client = AssertingHttpClient::create(
67+
$this,
68+
[
69+
'PUT',
70+
'/groups/1.xml',
71+
'application/xml',
72+
'<?xml version="1.0"?><group><custom_fields type="array"><custom_field id="1"><value>5</value></custom_field></custom_fields></group>',
73+
200,
74+
'application/xml',
75+
''
76+
]
77+
);
9678

9779
// Create the object under test
9880
$api = new Group($client);

0 commit comments

Comments
 (0)