Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve integration tests with assertXmlStringEqualsXmlString() #325

Merged
merged 1 commit into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 16 additions & 23 deletions tests/Integration/GroupXmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@

namespace Redmine\Tests\Integration;

use DOMDocument;
use Exception;
use PHPUnit\Framework\TestCase;
use Redmine\Exception\MissingParameterException;
use Redmine\Tests\Fixtures\MockClient;
use SimpleXMLElement;

class GroupXmlTest extends TestCase
{
Expand Down Expand Up @@ -38,18 +36,23 @@ public function testCreateComplex()
'name' => 'Developers',
'user_ids' => [3, 5],
]);
$res = json_decode($res, true);
$response = json_decode($res, true);

$xml = '<?xml version="1.0"?>
<group>
<name>Developers</name>
<user_ids type="array">
<user_id>3</user_id>
<user_id>5</user_id>
</user_ids>
</group>
';
$this->assertEquals($this->formatXml($xml), $this->formatXml($res['data']));
$this->assertEquals('POST', $response['method']);
$this->assertEquals('/groups.xml', $response['path']);
$this->assertXmlStringEqualsXmlString(
<<< XML
<?xml version="1.0"?>
<group>
<name>Developers</name>
<user_ids type="array">
<user_id>3</user_id>
<user_id>5</user_id>
</user_ids>
</group>
XML,
$response['data']
);
}

public function testUpdateNotImplemented()
Expand All @@ -62,14 +65,4 @@ public function testUpdateNotImplemented()

$api->update(1);
}

private function formatXml($xml)
{
$dom = new DOMDocument('1.0');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML((new SimpleXMLElement($xml))->asXML());

return $dom->saveXML();
}
}
48 changes: 24 additions & 24 deletions tests/Integration/IssueCategoryXmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@

namespace Redmine\Tests\Integration;

use DOMDocument;
use PHPUnit\Framework\TestCase;
use Redmine\Exception\MissingParameterException;
use Redmine\Tests\Fixtures\MockClient;
use SimpleXMLElement;

class IssueCategoryXmlTest extends TestCase
{
Expand Down Expand Up @@ -37,13 +35,19 @@ public function testCreateComplex()
$res = $api->create('otherProject', [
'name' => 'test category',
]);
$res = json_decode($res, true);
$response = json_decode($res, true);

$xml = '<?xml version="1.0"?>
<issue_category>
<name>test category</name>
</issue_category>';
$this->assertEquals($this->formatXml($xml), $this->formatXml($res['data']));
$this->assertEquals('POST', $response['method']);
$this->assertEquals('/projects/otherProject/issue_categories.xml', $response['path']);
$this->assertXmlStringEqualsXmlString(
<<< XML
<?xml version="1.0"?>
<issue_category>
<name>test category</name>
</issue_category>
XML,
$response['data']
);
}

public function testUpdate()
Expand All @@ -52,22 +56,18 @@ public function testUpdate()
$res = $api->update(1, [
'name' => 'new category name',
]);
$res = json_decode($res, true);
$response = json_decode($res, true);

$xml = '<?xml version="1.0"?>
<issue_category>
<name>new category name</name>
</issue_category>';
$this->assertEquals($this->formatXml($xml), $this->formatXml($res['data']));
}

private function formatXml($xml)
{
$dom = new DOMDocument('1.0');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML((new SimpleXMLElement($xml))->asXML());

return $dom->saveXML();
$this->assertEquals('PUT', $response['method']);
$this->assertEquals('/issue_categories/1.xml', $response['path']);
$this->assertXmlStringEqualsXmlString(
<<< XML
<?xml version="1.0"?>
<issue_category>
<name>new category name</name>
</issue_category>
XML,
$response['data']
);
}
}
196 changes: 110 additions & 86 deletions tests/Integration/IssueXmlTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@

namespace Redmine\Tests\Integration;

use DOMDocument;
use PHPUnit\Framework\TestCase;
use Redmine\Tests\Fixtures\MockClient;
use SimpleXMLElement;

class IssueXmlTest extends TestCase
{
Expand All @@ -24,12 +22,18 @@ public function testCreateBlank()
$api = $this->client->getApi('issue');
$this->assertInstanceOf('Redmine\Api\Issue', $api);

$xml = '<?xml version="1.0"?>
<issue/>';
$res = $api->create();
$res = json_decode($res, true);

$this->assertEquals($this->formatXml($xml), $this->formatXml($res['data']));
$response = json_decode($res, true);

$this->assertEquals('POST', $response['method']);
$this->assertEquals('/issues.xml', $response['path']);
$this->assertXmlStringEqualsXmlString(
<<< XML
<?xml version="1.0"?>
<issue/>
XML,
$response['data']
);
}

public function testCreateComplexWithUpload()
Expand All @@ -48,23 +52,29 @@ public function testCreateComplexWithUpload()
],
],
]);
$res = json_decode($res, true);

$xml = '<?xml version="1.0"?>
<issue>
<subject>A test issue</subject>
<description>Here goes the issue description</description>
<project_id>myproject</project_id>
<uploads type="array">
<upload>
<token>asdfasdfasdfasdf</token>
<filename>MyFile.pdf</filename>
<description>MyFile is better then YourFile...</description>
<content_type>application/pdf</content_type>
</upload>
</uploads>
</issue>';
$this->assertEquals($this->formatXml($xml), $this->formatXml($res['data']));
$response = json_decode($res, true);

$this->assertEquals('POST', $response['method']);
$this->assertEquals('/issues.xml', $response['path']);
$this->assertXmlStringEqualsXmlString(
<<< XML
<?xml version="1.0"?>
<issue>
<subject>A test issue</subject>
<description>Here goes the issue description</description>
<project_id>myproject</project_id>
<uploads type="array">
<upload>
<token>asdfasdfasdfasdf</token>
<filename>MyFile.pdf</filename>
<description>MyFile is better then YourFile...</description>
<content_type>application/pdf</content_type>
</upload>
</uploads>
</issue>
XML,
$response['data']
);
}

public function testCreateComplex()
Expand Down Expand Up @@ -94,21 +104,27 @@ public function testCreateComplex()
],
'watcher_user_ids' => [],
]);
$res = json_decode($res, true);

$xml = '<?xml version="1.0"?>
<issue>
<subject>test api (xml) 3</subject>
<description>test api</description>
<project_id>test</project_id>
<assigned_to_id>1</assigned_to_id>
<custom_fields type="array">
<custom_field name="Issuer" id="2"><value>asdf</value></custom_field>
<custom_field name="Phone" id="5"><value>9939494</value></custom_field>
<custom_field name="Email" id="8"><value>[email protected]</value></custom_field>
</custom_fields>
</issue>';
$this->assertEquals($this->formatXml($xml), $this->formatXml($res['data']));
$response = json_decode($res, true);

$this->assertEquals('POST', $response['method']);
$this->assertEquals('/issues.xml', $response['path']);
$this->assertXmlStringEqualsXmlString(
<<< XML
<?xml version="1.0"?>
<issue>
<subject>test api (xml) 3</subject>
<description>test api</description>
<project_id>test</project_id>
<assigned_to_id>1</assigned_to_id>
<custom_fields type="array">
<custom_field name="Issuer" id="2"><value>asdf</value></custom_field>
<custom_field name="Phone" id="5"><value>9939494</value></custom_field>
<custom_field name="Email" id="8"><value>[email protected]</value></custom_field>
</custom_fields>
</issue>
XML,
$response['data']
);
}

public function testCreateComplexWithLineBreakInDescription()
Expand Down Expand Up @@ -138,22 +154,28 @@ public function testCreateComplexWithLineBreakInDescription()
],
'watcher_user_ids' => [],
]);
$res = json_decode($res, true);

$xml = '<?xml version="1.0"?>
<issue>
<subject>test api (xml) 3</subject>
<description>line1
line2</description>
<project_id>test</project_id>
<assigned_to_id>1</assigned_to_id>
<custom_fields type="array">
<custom_field name="Issuer" id="2"><value>asdf</value></custom_field>
<custom_field name="Phone" id="5"><value>9939494</value></custom_field>
<custom_field name="Email" id="8"><value>[email protected]</value></custom_field>
</custom_fields>
</issue>';
$this->assertEquals($this->formatXml($xml), $this->formatXml($res['data']));
$response = json_decode($res, true);

$this->assertEquals('POST', $response['method']);
$this->assertEquals('/issues.xml', $response['path']);
$this->assertXmlStringEqualsXmlString(
<<< XML
<?xml version="1.0"?>
<issue>
<subject>test api (xml) 3</subject>
<description>line1
line2</description>
<project_id>test</project_id>
<assigned_to_id>1</assigned_to_id>
<custom_fields type="array">
<custom_field name="Issuer" id="2"><value>asdf</value></custom_field>
<custom_field name="Phone" id="5"><value>9939494</value></custom_field>
<custom_field name="Email" id="8"><value>[email protected]</value></custom_field>
</custom_fields>
</issue>
XML,
$response['data']
);
}

public function testUpdateIssue()
Expand All @@ -170,42 +192,44 @@ public function testUpdateIssue()
// not testable because this will trigger a status name to id resolving
// 'status' => 'Resolved',
]);
$res = json_decode($res, true);

$xml = '<?xml version="1.0"?>
<issue>
<id>1</id>
<subject>test note (xml) 1</subject>
<notes>test note api</notes>
<priority_id>5</priority_id>
<status_id>2</status_id>
<assigned_to_id>1</assigned_to_id>
<due_date>2014-05-13</due_date>
</issue>';
$this->assertEquals($this->formatXml($xml), $this->formatXml($res['data']));
$response = json_decode($res, true);

$this->assertEquals('PUT', $response['method']);
$this->assertEquals('/issues/1.xml', $response['path']);
$this->assertXmlStringEqualsXmlString(
<<< XML
<?xml version="1.0"?>
<issue>
<id>1</id>
<subject>test note (xml) 1</subject>
<notes>test note api</notes>
<priority_id>5</priority_id>
<status_id>2</status_id>
<assigned_to_id>1</assigned_to_id>
<due_date>2014-05-13</due_date>
</issue>
XML,
$response['data']
);
}

public function testAddNoteToIssue()
{
$api = $this->client->getApi('issue');
$res = $api->addNoteToIssue(1, 'some comment');
$res = json_decode($res, true);

$xml = '<?xml version="1.0"?>
<issue>
<id>1</id>
<notes>some comment</notes>
</issue>';
$this->assertEquals($this->formatXml($xml), $this->formatXml($res['data']));
}

private function formatXml($xml)
{
$dom = new DOMDocument('1.0');
$dom->preserveWhiteSpace = false;
$dom->formatOutput = true;
$dom->loadXML((new SimpleXMLElement($xml))->asXML());

return $dom->saveXML();
$response = json_decode($res, true);

$this->assertEquals('PUT', $response['method']);
$this->assertEquals('/issues/1.xml', $response['path']);
$this->assertXmlStringEqualsXmlString(
<<< XML
<?xml version="1.0"?>
<issue>
<id>1</id>
<notes>some comment</notes>
</issue>
XML,
$response['data']
);
}
}
Loading