Skip to content

Commit

Permalink
Refactor tests, use AssertingHttpClient
Browse files Browse the repository at this point in the history
  • Loading branch information
Art4 committed Jan 23, 2024
1 parent 6db2cea commit 215c9c1
Show file tree
Hide file tree
Showing 5 changed files with 180 additions and 283 deletions.
2 changes: 1 addition & 1 deletion tests/Fixtures/AssertingHttpClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ public function request(Request $request): Response

if ($data['content'] !== '' && $data['contentType'] === 'application/xml') {
$this->testCase->assertXmlStringEqualsXmlString($data['content'], $request->getContent());
} else if ($data['content'] !== '' && $data['contentType'] === 'application/json') {
} elseif ($data['content'] !== '' && $data['contentType'] === 'application/json') {
$this->testCase->assertJsonStringEqualsJsonString($data['content'], $request->getContent());
} else {
$this->testCase->assertSame($data['content'], $request->getContent());
Expand Down
1 change: 0 additions & 1 deletion tests/Unit/Api/AbstractApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Redmine\Client\Client;
use Redmine\Exception\SerializerException;
use Redmine\Http\HttpClient;
use Redmine\Http\Request;
use Redmine\Http\Response;
use Redmine\Tests\Fixtures\AssertingHttpClient;
use ReflectionMethod;
Expand Down
90 changes: 37 additions & 53 deletions tests/Unit/Api/Group/CreateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@
use Redmine\Api\Group;
use Redmine\Exception\MissingParameterException;
use Redmine\Http\HttpClient;
use Redmine\Http\Request;
use Redmine\Http\Response;
use Redmine\Tests\Fixtures\AssertingHttpClient;
use SimpleXMLElement;

/**
Expand All @@ -19,23 +18,18 @@ class CreateTest extends TestCase
{
public function testCreateWithNameCreatesGroup()
{
$client = $this->createMock(HttpClient::class);
$client->expects($this->exactly(1))
->method('request')
->willReturnCallback(function (Request $request) {
$this->assertSame('POST', $request->getMethod());
$this->assertSame('/groups.xml', $request->getPath());
$this->assertSame('application/xml', $request->getContentType());
$this->assertXmlStringEqualsXmlString('<?xml version="1.0"?><group><name>Group Name</name></group>', $request->getContent());

return $this->createConfiguredMock(
Response::class,
[
'getContentType' => 'application/xml',
'getContent' => '<?xml version="1.0"?><group></group>',
]
);
});
$client = AssertingHttpClient::create(
$this,
[
'POST',
'/groups.xml',
'application/xml',
'<?xml version="1.0"?><group><name>Group Name</name></group>',
200,
'application/xml',
'<?xml version="1.0"?><group></group>'
]
);

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

public function testCreateWithNameAndUserIdsCreatesGroup()
{
$client = $this->createMock(HttpClient::class);
$client->expects($this->exactly(1))
->method('request')
->willReturnCallback(function (Request $request) {
$this->assertSame('POST', $request->getMethod());
$this->assertSame('/groups.xml', $request->getPath());
$this->assertSame('application/xml', $request->getContentType());
$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());

return $this->createConfiguredMock(
Response::class,
[
'getContentType' => 'application/xml',
'getContent' => '<?xml version="1.0"?><group></group>',
]
);
});
$client = AssertingHttpClient::create(
$this,
[
'POST',
'/groups.xml',
'application/xml',
'<?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>',
200,
'application/xml',
'<?xml version="1.0"?><group></group>'
]
);

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

public function testCreateWithNameAndCustomFieldsCreatesGroup()
{
$client = $this->createMock(HttpClient::class);
$client->expects($this->exactly(1))
->method('request')
->willReturnCallback(function (Request $request) {
$this->assertSame('POST', $request->getMethod());
$this->assertSame('/groups.xml', $request->getPath());
$this->assertSame('application/xml', $request->getContentType());
$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());

return $this->createConfiguredMock(
Response::class,
[
'getContentType' => 'application/xml',
'getContent' => '<?xml version="1.0"?><group></group>',
]
);
});
$client = AssertingHttpClient::create(
$this,
[
'POST',
'/groups.xml',
'application/xml',
'<?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>',
200,
'application/xml',
'<?xml version="1.0"?><group></group>'
]
);

// Create the object under test
$api = new Group($client);
Expand Down
92 changes: 37 additions & 55 deletions tests/Unit/Api/Group/UpdateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,7 @@

use PHPUnit\Framework\TestCase;
use Redmine\Api\Group;
use Redmine\Http\HttpClient;
use Redmine\Http\Request;
use Redmine\Http\Response;
use SimpleXMLElement;
use Redmine\Tests\Fixtures\AssertingHttpClient;

/**
* @covers \Redmine\Api\Group::update
Expand All @@ -18,23 +15,18 @@ class UpdateTest extends TestCase
{
public function testUpdateWithNameUpdatesGroup()
{
$client = $this->createMock(HttpClient::class);
$client->expects($this->exactly(1))
->method('request')
->willReturnCallback(function (Request $request) {
$this->assertSame('PUT', $request->getMethod());
$this->assertSame('/groups/1.xml', $request->getPath());
$this->assertSame('application/xml', $request->getContentType());
$this->assertXmlStringEqualsXmlString('<?xml version="1.0"?><group><name>Group Name</name></group>', $request->getContent());

return $this->createConfiguredMock(
Response::class,
[
'getContentType' => 'application/xml',
'getContent' => '',
]
);
});
$client = AssertingHttpClient::create(
$this,
[
'PUT',
'/groups/1.xml',
'application/xml',
'<?xml version="1.0"?><group><name>Group Name</name></group>',
200,
'application/xml',
''
]
);

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

public function testUpdateWithUserIdsUpdatesGroup()
{
$client = $this->createMock(HttpClient::class);
$client->expects($this->exactly(1))
->method('request')
->willReturnCallback(function (Request $request) {
$this->assertSame('PUT', $request->getMethod());
$this->assertSame('/groups/1.xml', $request->getPath());
$this->assertSame('application/xml', $request->getContentType());
$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());

return $this->createConfiguredMock(
Response::class,
[
'getContentType' => 'application/xml',
'getContent' => '',
]
);
});
$client = AssertingHttpClient::create(
$this,
[
'PUT',
'/groups/1.xml',
'application/xml',
'<?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>',
200,
'application/xml',
''
]
);

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

public function testUpdateWithCustomFieldsUpdatesGroup()
{
$client = $this->createMock(HttpClient::class);
$client->expects($this->exactly(1))
->method('request')
->willReturnCallback(function (Request $request) {
$this->assertSame('PUT', $request->getMethod());
$this->assertSame('/groups/1.xml', $request->getPath());
$this->assertSame('application/xml', $request->getContentType());
$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());

return $this->createConfiguredMock(
Response::class,
[
'getContentType' => 'application/xml',
'getContent' => '',
]
);
});
$client = AssertingHttpClient::create(
$this,
[
'PUT',
'/groups/1.xml',
'application/xml',
'<?xml version="1.0"?><group><custom_fields type="array"><custom_field id="1"><value>5</value></custom_field></custom_fields></group>',
200,
'application/xml',
''
]
);

// Create the object under test
$api = new Group($client);
Expand Down
Loading

0 comments on commit 215c9c1

Please sign in to comment.