Skip to content

Commit 3e2dcb2

Browse files
authored
Merge pull request #325 from Art4/improve-xml-testing
Improve integration tests with assertXmlStringEqualsXmlString()
2 parents 0c05477 + 6cf2b72 commit 3e2dcb2

File tree

7 files changed

+290
-267
lines changed

7 files changed

+290
-267
lines changed

tests/Integration/GroupXmlTest.php

Lines changed: 16 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,10 @@
22

33
namespace Redmine\Tests\Integration;
44

5-
use DOMDocument;
65
use Exception;
76
use PHPUnit\Framework\TestCase;
87
use Redmine\Exception\MissingParameterException;
98
use Redmine\Tests\Fixtures\MockClient;
10-
use SimpleXMLElement;
119

1210
class GroupXmlTest extends TestCase
1311
{
@@ -38,18 +36,23 @@ public function testCreateComplex()
3836
'name' => 'Developers',
3937
'user_ids' => [3, 5],
4038
]);
41-
$res = json_decode($res, true);
39+
$response = json_decode($res, true);
4240

43-
$xml = '<?xml version="1.0"?>
44-
<group>
45-
<name>Developers</name>
46-
<user_ids type="array">
47-
<user_id>3</user_id>
48-
<user_id>5</user_id>
49-
</user_ids>
50-
</group>
51-
';
52-
$this->assertEquals($this->formatXml($xml), $this->formatXml($res['data']));
41+
$this->assertEquals('POST', $response['method']);
42+
$this->assertEquals('/groups.xml', $response['path']);
43+
$this->assertXmlStringEqualsXmlString(
44+
<<< XML
45+
<?xml version="1.0"?>
46+
<group>
47+
<name>Developers</name>
48+
<user_ids type="array">
49+
<user_id>3</user_id>
50+
<user_id>5</user_id>
51+
</user_ids>
52+
</group>
53+
XML,
54+
$response['data']
55+
);
5356
}
5457

5558
public function testUpdateNotImplemented()
@@ -62,14 +65,4 @@ public function testUpdateNotImplemented()
6265

6366
$api->update(1);
6467
}
65-
66-
private function formatXml($xml)
67-
{
68-
$dom = new DOMDocument('1.0');
69-
$dom->preserveWhiteSpace = false;
70-
$dom->formatOutput = true;
71-
$dom->loadXML((new SimpleXMLElement($xml))->asXML());
72-
73-
return $dom->saveXML();
74-
}
7568
}

tests/Integration/IssueCategoryXmlTest.php

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,9 @@
22

33
namespace Redmine\Tests\Integration;
44

5-
use DOMDocument;
65
use PHPUnit\Framework\TestCase;
76
use Redmine\Exception\MissingParameterException;
87
use Redmine\Tests\Fixtures\MockClient;
9-
use SimpleXMLElement;
108

119
class IssueCategoryXmlTest extends TestCase
1210
{
@@ -37,13 +35,19 @@ public function testCreateComplex()
3735
$res = $api->create('otherProject', [
3836
'name' => 'test category',
3937
]);
40-
$res = json_decode($res, true);
38+
$response = json_decode($res, true);
4139

42-
$xml = '<?xml version="1.0"?>
43-
<issue_category>
44-
<name>test category</name>
45-
</issue_category>';
46-
$this->assertEquals($this->formatXml($xml), $this->formatXml($res['data']));
40+
$this->assertEquals('POST', $response['method']);
41+
$this->assertEquals('/projects/otherProject/issue_categories.xml', $response['path']);
42+
$this->assertXmlStringEqualsXmlString(
43+
<<< XML
44+
<?xml version="1.0"?>
45+
<issue_category>
46+
<name>test category</name>
47+
</issue_category>
48+
XML,
49+
$response['data']
50+
);
4751
}
4852

4953
public function testUpdate()
@@ -52,22 +56,18 @@ public function testUpdate()
5256
$res = $api->update(1, [
5357
'name' => 'new category name',
5458
]);
55-
$res = json_decode($res, true);
59+
$response = json_decode($res, true);
5660

57-
$xml = '<?xml version="1.0"?>
58-
<issue_category>
59-
<name>new category name</name>
60-
</issue_category>';
61-
$this->assertEquals($this->formatXml($xml), $this->formatXml($res['data']));
62-
}
63-
64-
private function formatXml($xml)
65-
{
66-
$dom = new DOMDocument('1.0');
67-
$dom->preserveWhiteSpace = false;
68-
$dom->formatOutput = true;
69-
$dom->loadXML((new SimpleXMLElement($xml))->asXML());
70-
71-
return $dom->saveXML();
61+
$this->assertEquals('PUT', $response['method']);
62+
$this->assertEquals('/issue_categories/1.xml', $response['path']);
63+
$this->assertXmlStringEqualsXmlString(
64+
<<< XML
65+
<?xml version="1.0"?>
66+
<issue_category>
67+
<name>new category name</name>
68+
</issue_category>
69+
XML,
70+
$response['data']
71+
);
7272
}
7373
}

tests/Integration/IssueXmlTest.php

Lines changed: 110 additions & 86 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,8 @@
22

33
namespace Redmine\Tests\Integration;
44

5-
use DOMDocument;
65
use PHPUnit\Framework\TestCase;
76
use Redmine\Tests\Fixtures\MockClient;
8-
use SimpleXMLElement;
97

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

27-
$xml = '<?xml version="1.0"?>
28-
<issue/>';
2925
$res = $api->create();
30-
$res = json_decode($res, true);
31-
32-
$this->assertEquals($this->formatXml($xml), $this->formatXml($res['data']));
26+
$response = json_decode($res, true);
27+
28+
$this->assertEquals('POST', $response['method']);
29+
$this->assertEquals('/issues.xml', $response['path']);
30+
$this->assertXmlStringEqualsXmlString(
31+
<<< XML
32+
<?xml version="1.0"?>
33+
<issue/>
34+
XML,
35+
$response['data']
36+
);
3337
}
3438

3539
public function testCreateComplexWithUpload()
@@ -48,23 +52,29 @@ public function testCreateComplexWithUpload()
4852
],
4953
],
5054
]);
51-
$res = json_decode($res, true);
52-
53-
$xml = '<?xml version="1.0"?>
54-
<issue>
55-
<subject>A test issue</subject>
56-
<description>Here goes the issue description</description>
57-
<project_id>myproject</project_id>
58-
<uploads type="array">
59-
<upload>
60-
<token>asdfasdfasdfasdf</token>
61-
<filename>MyFile.pdf</filename>
62-
<description>MyFile is better then YourFile...</description>
63-
<content_type>application/pdf</content_type>
64-
</upload>
65-
</uploads>
66-
</issue>';
67-
$this->assertEquals($this->formatXml($xml), $this->formatXml($res['data']));
55+
$response = json_decode($res, true);
56+
57+
$this->assertEquals('POST', $response['method']);
58+
$this->assertEquals('/issues.xml', $response['path']);
59+
$this->assertXmlStringEqualsXmlString(
60+
<<< XML
61+
<?xml version="1.0"?>
62+
<issue>
63+
<subject>A test issue</subject>
64+
<description>Here goes the issue description</description>
65+
<project_id>myproject</project_id>
66+
<uploads type="array">
67+
<upload>
68+
<token>asdfasdfasdfasdf</token>
69+
<filename>MyFile.pdf</filename>
70+
<description>MyFile is better then YourFile...</description>
71+
<content_type>application/pdf</content_type>
72+
</upload>
73+
</uploads>
74+
</issue>
75+
XML,
76+
$response['data']
77+
);
6878
}
6979

7080
public function testCreateComplex()
@@ -94,21 +104,27 @@ public function testCreateComplex()
94104
],
95105
'watcher_user_ids' => [],
96106
]);
97-
$res = json_decode($res, true);
98-
99-
$xml = '<?xml version="1.0"?>
100-
<issue>
101-
<subject>test api (xml) 3</subject>
102-
<description>test api</description>
103-
<project_id>test</project_id>
104-
<assigned_to_id>1</assigned_to_id>
105-
<custom_fields type="array">
106-
<custom_field name="Issuer" id="2"><value>asdf</value></custom_field>
107-
<custom_field name="Phone" id="5"><value>9939494</value></custom_field>
108-
<custom_field name="Email" id="8"><value>asdf@asdf.com</value></custom_field>
109-
</custom_fields>
110-
</issue>';
111-
$this->assertEquals($this->formatXml($xml), $this->formatXml($res['data']));
107+
$response = json_decode($res, true);
108+
109+
$this->assertEquals('POST', $response['method']);
110+
$this->assertEquals('/issues.xml', $response['path']);
111+
$this->assertXmlStringEqualsXmlString(
112+
<<< XML
113+
<?xml version="1.0"?>
114+
<issue>
115+
<subject>test api (xml) 3</subject>
116+
<description>test api</description>
117+
<project_id>test</project_id>
118+
<assigned_to_id>1</assigned_to_id>
119+
<custom_fields type="array">
120+
<custom_field name="Issuer" id="2"><value>asdf</value></custom_field>
121+
<custom_field name="Phone" id="5"><value>9939494</value></custom_field>
122+
<custom_field name="Email" id="8"><value>asdf@asdf.com</value></custom_field>
123+
</custom_fields>
124+
</issue>
125+
XML,
126+
$response['data']
127+
);
112128
}
113129

114130
public function testCreateComplexWithLineBreakInDescription()
@@ -138,22 +154,28 @@ public function testCreateComplexWithLineBreakInDescription()
138154
],
139155
'watcher_user_ids' => [],
140156
]);
141-
$res = json_decode($res, true);
142-
143-
$xml = '<?xml version="1.0"?>
144-
<issue>
145-
<subject>test api (xml) 3</subject>
146-
<description>line1
147-
line2</description>
148-
<project_id>test</project_id>
149-
<assigned_to_id>1</assigned_to_id>
150-
<custom_fields type="array">
151-
<custom_field name="Issuer" id="2"><value>asdf</value></custom_field>
152-
<custom_field name="Phone" id="5"><value>9939494</value></custom_field>
153-
<custom_field name="Email" id="8"><value>asdf@asdf.com</value></custom_field>
154-
</custom_fields>
155-
</issue>';
156-
$this->assertEquals($this->formatXml($xml), $this->formatXml($res['data']));
157+
$response = json_decode($res, true);
158+
159+
$this->assertEquals('POST', $response['method']);
160+
$this->assertEquals('/issues.xml', $response['path']);
161+
$this->assertXmlStringEqualsXmlString(
162+
<<< XML
163+
<?xml version="1.0"?>
164+
<issue>
165+
<subject>test api (xml) 3</subject>
166+
<description>line1
167+
line2</description>
168+
<project_id>test</project_id>
169+
<assigned_to_id>1</assigned_to_id>
170+
<custom_fields type="array">
171+
<custom_field name="Issuer" id="2"><value>asdf</value></custom_field>
172+
<custom_field name="Phone" id="5"><value>9939494</value></custom_field>
173+
<custom_field name="Email" id="8"><value>asdf@asdf.com</value></custom_field>
174+
</custom_fields>
175+
</issue>
176+
XML,
177+
$response['data']
178+
);
157179
}
158180

159181
public function testUpdateIssue()
@@ -170,42 +192,44 @@ public function testUpdateIssue()
170192
// not testable because this will trigger a status name to id resolving
171193
// 'status' => 'Resolved',
172194
]);
173-
$res = json_decode($res, true);
174-
175-
$xml = '<?xml version="1.0"?>
176-
<issue>
177-
<id>1</id>
178-
<subject>test note (xml) 1</subject>
179-
<notes>test note api</notes>
180-
<priority_id>5</priority_id>
181-
<status_id>2</status_id>
182-
<assigned_to_id>1</assigned_to_id>
183-
<due_date>2014-05-13</due_date>
184-
</issue>';
185-
$this->assertEquals($this->formatXml($xml), $this->formatXml($res['data']));
195+
$response = json_decode($res, true);
196+
197+
$this->assertEquals('PUT', $response['method']);
198+
$this->assertEquals('/issues/1.xml', $response['path']);
199+
$this->assertXmlStringEqualsXmlString(
200+
<<< XML
201+
<?xml version="1.0"?>
202+
<issue>
203+
<id>1</id>
204+
<subject>test note (xml) 1</subject>
205+
<notes>test note api</notes>
206+
<priority_id>5</priority_id>
207+
<status_id>2</status_id>
208+
<assigned_to_id>1</assigned_to_id>
209+
<due_date>2014-05-13</due_date>
210+
</issue>
211+
XML,
212+
$response['data']
213+
);
186214
}
187215

188216
public function testAddNoteToIssue()
189217
{
190218
$api = $this->client->getApi('issue');
191219
$res = $api->addNoteToIssue(1, 'some comment');
192-
$res = json_decode($res, true);
193-
194-
$xml = '<?xml version="1.0"?>
195-
<issue>
196-
<id>1</id>
197-
<notes>some comment</notes>
198-
</issue>';
199-
$this->assertEquals($this->formatXml($xml), $this->formatXml($res['data']));
200-
}
201-
202-
private function formatXml($xml)
203-
{
204-
$dom = new DOMDocument('1.0');
205-
$dom->preserveWhiteSpace = false;
206-
$dom->formatOutput = true;
207-
$dom->loadXML((new SimpleXMLElement($xml))->asXML());
208-
209-
return $dom->saveXML();
220+
$response = json_decode($res, true);
221+
222+
$this->assertEquals('PUT', $response['method']);
223+
$this->assertEquals('/issues/1.xml', $response['path']);
224+
$this->assertXmlStringEqualsXmlString(
225+
<<< XML
226+
<?xml version="1.0"?>
227+
<issue>
228+
<id>1</id>
229+
<notes>some comment</notes>
230+
</issue>
231+
XML,
232+
$response['data']
233+
);
210234
}
211235
}

0 commit comments

Comments
 (0)