Skip to content

Commit b62fe77

Browse files
committed
fix attachCustomFieldXML + add TestClient class returning raw xml on post/put calls + add issue xml tests
1 parent 33bd9ef commit b62fe77

File tree

4 files changed

+174
-9
lines changed

4 files changed

+174
-9
lines changed

lib/Redmine/Api/AbstractApi.php

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -144,17 +144,17 @@ protected function retrieveAll($endpoint, array $params = array())
144144
* @return $xml Element
145145
* @see http://www.redmine.org/projects/redmine/wiki/Rest_api#Working-with-custom-fields
146146
*/
147-
protected function attachCustomFieldXML(SimpleXMLElement $xml, $fields)
147+
protected function attachCustomFieldXML(SimpleXMLElement $xml, array $fields)
148148
{
149-
$fields = $xml->addChild('custom_fields');
150-
$fields->addAttribute('type', 'array');
149+
$_fields = $xml->addChild('custom_fields');
150+
$_fields->addAttribute('type', 'array');
151151
foreach ($fields as $field) {
152-
$field = $fields->addChild('custom_field');
152+
$_field = $_fields->addChild('custom_field');
153153
if (isset($field['name'])) {
154-
$field->addAttribute('name', $field['name']);
154+
$_field->addAttribute('name', $field['name']);
155155
}
156-
$field->addAttribute('id', $field['id']);
157-
$field->addChild('value', $field['value']);
156+
$_field->addAttribute('id', $field['id']);
157+
$_field->addChild('value', $field['value']);
158158
}
159159

160160
return $xml;

lib/Redmine/Client.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -371,11 +371,15 @@ private function runRequest($path, $method = 'GET', $data = '')
371371
switch ($method) {
372372
case 'POST':
373373
curl_setopt($curl, CURLOPT_POST, 1);
374-
if (isset($data)) {curl_setopt($curl, CURLOPT_POSTFIELDS, $data);}
374+
if (isset($data)) {
375+
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
376+
}
375377
break;
376378
case 'PUT':
377379
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'PUT');
378-
if (isset($data)) {curl_setopt($curl, CURLOPT_POSTFIELDS, $data);}
380+
if (isset($data)) {
381+
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
382+
}
379383
break;
380384
case 'DELETE':
381385
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'DELETE');

lib/Redmine/TestClient.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
3+
namespace Redmine;
4+
5+
class TestClient extends Client
6+
{
7+
/**
8+
* @param string $path
9+
* @throws Exception
10+
*/
11+
public function get($path)
12+
{
13+
throw new \Exception('not available');
14+
}
15+
16+
/**
17+
* returns raw $data
18+
* @param string $path
19+
* @param string $data
20+
* @return string $data
21+
*/
22+
public function post($path, $data)
23+
{
24+
return $data;
25+
}
26+
27+
/**
28+
* returns raw $data
29+
* @param string $path
30+
* @param string $data
31+
* @return strgin $data
32+
*/
33+
public function put($path, $data)
34+
{
35+
return $data;
36+
}
37+
38+
/**
39+
* @param string $path
40+
* @throws Exception
41+
*/
42+
public function delete($path)
43+
{
44+
throw new \Exception('not available');
45+
}
46+
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php
2+
3+
namespace Redmine\Tests;
4+
5+
use Redmine\Client;
6+
use Redmine\TestClient;
7+
8+
class IssueXmlTest extends \PHPUnit_Framework_TestCase
9+
{
10+
private $client;
11+
12+
public function setup()
13+
{
14+
$this->client = new TestClient('http://test.local', 'asdf');
15+
}
16+
17+
public function testCreateBlank()
18+
{
19+
$this->assertInstanceOf('Redmine\Api\Issue', $this->client->api('issue'));
20+
21+
$xml = '<?xml version="1.0"?>
22+
<issue/>';
23+
$res = $this->client->api('issue')->create();
24+
$this->assertEquals($this->formatXml($xml), $this->formatXml($res));
25+
}
26+
27+
public function testCreateComplex()
28+
{
29+
$res = $this->client->api('issue')->create(array(
30+
'project_id' => 'test',
31+
'subject' => 'test api (xml) 3',
32+
'description' => 'test api',
33+
'assigned_to_id' => 1,
34+
'custom_fields' => array(
35+
array(
36+
'id' => 2,
37+
'name' => 'Issuer',
38+
'value' => 'asdf',
39+
),
40+
array(
41+
'id' => 5,
42+
'name' => 'Phone',
43+
'value' => '9939494',
44+
),
45+
array(
46+
'id' => '8',
47+
'name' => 'Email',
48+
'value' => '[email protected]',
49+
),
50+
),
51+
'watcher_user_ids' => array(),
52+
));
53+
54+
$xml = '<?xml version="1.0"?>
55+
<issue>
56+
<subject>test api (xml) 3</subject>
57+
<description>test api</description>
58+
<project_id>test</project_id>
59+
<assigned_to_id>1</assigned_to_id>
60+
<custom_fields type="array">
61+
<custom_field name="Issuer" id="2"><value>asdf</value></custom_field>
62+
<custom_field name="Phone" id="5"><value>9939494</value></custom_field>
63+
<custom_field name="Email" id="8"><value>[email protected]</value></custom_field>
64+
</custom_fields>
65+
</issue>';
66+
$this->assertEquals($this->formatXml($xml), $this->formatXml($res));
67+
}
68+
69+
public function testUpdateIssue()
70+
{
71+
$res = $this->client->api('issue')->update(1, array(
72+
'subject' => 'test note (xml) 1',
73+
'notes' => 'test note api',
74+
'assigned_to_id' => 1,
75+
'status_id' => 2,
76+
'priority_id' => 5,
77+
'due_date' => date('Y-m-d'),
78+
79+
// not testable because this will trigger a status name to id resolving
80+
// 'status' => 'Resolved',
81+
));
82+
$xml = '<?xml version="1.0"?>
83+
<issue>
84+
<id>1</id>
85+
<subject>test note (xml) 1</subject>
86+
<notes>test note api</notes>
87+
<priority_id>5</priority_id>
88+
<status_id>2</status_id>
89+
<assigned_to_id>1</assigned_to_id>
90+
<due_date>2014-05-13</due_date>
91+
</issue>';
92+
$this->assertEquals($this->formatXml($xml), $this->formatXml($res));
93+
}
94+
95+
public function testAddNoteToIssue()
96+
{
97+
$res = $this->client->api('issue')->addNoteToIssue(1, 'some comment');
98+
$xml = '<?xml version="1.0"?>
99+
<issue>
100+
<id>1</id>
101+
<notes>some comment</notes>
102+
</issue>';
103+
$this->assertEquals($this->formatXml($xml), $this->formatXml($res));
104+
}
105+
106+
private function formatXml($xml)
107+
{
108+
$dom = new \DOMDocument('1.0');
109+
$dom->preserveWhiteSpace = false;
110+
$dom->formatOutput = true;
111+
$dom->loadXML((new \SimpleXMLElement($xml))->asXML());
112+
113+
return $dom->saveXML();
114+
}
115+
}

0 commit comments

Comments
 (0)