Skip to content
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
4 changes: 3 additions & 1 deletion src/Redmine/Serializer/XmlSerializer.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,18 @@ private function deserialize(string $encoded): void
$this->deserialized = new SimpleXMLElement($encoded);
} catch (Throwable $e) {
$errors = [];
$code = $e->getCode();

foreach (libxml_get_errors() as $error) {
$errors[] = $error->message;
$code = max($code, $error->level);
}

libxml_clear_errors();

throw new SerializerException(
'Catched errors: "' . implode('", "', $errors) . '" while decoding XML: ' . $encoded,
$e->getCode(),
$code,
$e,
);
} finally {
Expand Down
43 changes: 34 additions & 9 deletions tests/Unit/Api/AbstractApi/GetTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public function testGetWithHttpClient(): void
* @dataProvider getJsonDecodingFromGetMethodData
*/
#[DataProvider('getJsonDecodingFromGetMethodData')]
public function testJsonDecodingFromGetMethod(string $response, ?bool $decode, $expected): void
public function testJsonDecodingFromGetMethod(string $response, ?bool $shouldDecode, $expected): void
{
$client = $this->createStub(Client::class);
$client->method('getLastResponseBody')->willReturn($response);
Expand All @@ -63,8 +63,8 @@ public function testJsonDecodingFromGetMethod(string $response, ?bool $decode, $
}

// Perform the tests
if (is_bool($decode)) {
$this->assertSame($expected, $method->invoke($api, 'path', $decode));
if (is_bool($shouldDecode)) {
$this->assertSame($expected, $method->invoke($api, 'path', $shouldDecode));
} else {
$this->assertSame($expected, $method->invoke($api, 'path'));
}
Expand All @@ -73,12 +73,37 @@ public function testJsonDecodingFromGetMethod(string $response, ?bool $decode, $
public static function getJsonDecodingFromGetMethodData(): array
{
return [
'test decode by default' => ['{"foo_bar": 12345}', null, ['foo_bar' => 12345]],
'test decode by default, JSON decode: false' => ['{"foo_bar": 12345}', false, '{"foo_bar": 12345}'],
'test decode by default, JSON decode: true' => ['{"foo_bar": 12345}', true, ['foo_bar' => 12345]],
'Empty body, JSON decode: false' => ['', false, false],
'Empty body, JSON decode: true' => ['', true, false],
'test invalid JSON' => ['{"foo_bar":', true, 'Error decoding body as JSON: Syntax error'],
'test decode by default' => [
'{"foo_bar": 12345}',
null,
['foo_bar' => 12345],
],
'test decode by default, JSON decode: false' => [
'{"foo_bar": 12345}',
false,
'{"foo_bar": 12345}',
],
'test decode by default, JSON decode: true' => [
'{"foo_bar": 12345}',
true,
['foo_bar' => 12345],
],
'Empty body, JSON decode: false' => [
'',
false,
false,
],
'Empty body, JSON decode: true' => [
'',
true,
false,
],
'test invalid JSON' => [
'{"foo_bar":',
true,
/** @phpstan-ignore smaller.alwaysTrue(Remove this line after release of PHP 8.6) */
(PHP_VERSION_ID < 80600) ? 'Error decoding body as JSON: Syntax error' : 'Error decoding body as JSON: Syntax error near location 1:12',
],
];
}

Expand Down
30 changes: 26 additions & 4 deletions tests/Unit/Api/Attachment/ShowTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,32 @@ public function testShowReturnsCorrectResponse($id, string $expectedPath, string
public static function getShowData(): array
{
return [
'array response with integer id' => [5, '/attachments/5.json', '["API Response"]', ['API Response']],
'array response with string id' => ['5', '/attachments/5.json', '["API Response"]', ['API Response']],
'string response' => [5, '/attachments/5.json', 'string', 'Error decoding body as JSON: Syntax error'],
'false response' => [5, '/attachments/5.json', '', false],
'array response with integer id' => [
5,
'/attachments/5.json',
'["API Response"]',
['API Response'],
],
'array response with string id' =>
[
'5',
'/attachments/5.json',
'["API Response"]',
['API Response'],
],
'string response' => [
5,
'/attachments/5.json',
'string',
/** @phpstan-ignore smaller.alwaysTrue(Remove this line after release of PHP 8.6) */
(PHP_VERSION_ID < 80600) ? 'Error decoding body as JSON: Syntax error' : 'Error decoding body as JSON: Syntax error near location 1:1',
],
'false response' => [
5,
'/attachments/5.json',
'',
false,
],
];
}
}
41 changes: 36 additions & 5 deletions tests/Unit/Api/Group/ShowTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,42 @@ public function testShowReturnsCorrectResponse($groupId, array $params, string $
public static function getShowData(): array
{
return [
'array response with integer id' => [5, [], '/groups/5.json', '["API Response"]', ['API Response']],
'array response with string id' => ['5', [], '/groups/5.json', '["API Response"]', ['API Response']],
'array response with parameters' => [5, ['include' => ['parameter1', 'parameter2'], 'not-used'], '/groups/5.json?include%5B%5D=parameter1&include%5B%5D=parameter2&0=not-used', '["API Response"]', ['API Response']],
'string response' => [5, [], '/groups/5.json', 'string', 'Error decoding body as JSON: Syntax error'],
'false response' => [5, [], '/groups/5.json', '', false],
'array response with integer id' => [
5,
[],
'/groups/5.json',
'["API Response"]',
['API Response'],
],
'array response with string id' => [
'5',
[],
'/groups/5.json',
'["API Response"]',
['API Response'],
],
'array response with parameters' => [
5,
['include' => ['parameter1', 'parameter2'], 'not-used'],
'/groups/5.json?include%5B%5D=parameter1&include%5B%5D=parameter2&0=not-used',
'["API Response"]',
['API Response'],
],
'string response' => [
5,
[],
'/groups/5.json',
'string',
/** @phpstan-ignore smaller.alwaysTrue(Remove this line after release of PHP 8.6) */
(PHP_VERSION_ID < 80600) ? 'Error decoding body as JSON: Syntax error' : 'Error decoding body as JSON: Syntax error near location 1:1',
],
'false response' => [
5,
[],
'/groups/5.json',
'',
false,
],
];
}
}
41 changes: 36 additions & 5 deletions tests/Unit/Api/Issue/ShowTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,42 @@ public function testShowReturnsCorrectResponse($issueId, array $params, string $
public static function getShowData(): array
{
return [
'array response with integer id' => [5, [], '/issues/5.json', '["API Response"]', ['API Response']],
'array response with string id' => ['5', [], '/issues/5.json', '["API Response"]', ['API Response']],
'array response with parameters' => [5, ['include' => ['parameter1', 'parameter2'], 'not-used'], '/issues/5.json?include=parameter1%2Cparameter2&0=not-used', '["API Response"]', ['API Response']],
'string response' => [5, [], '/issues/5.json', 'string', 'Error decoding body as JSON: Syntax error'],
'false response' => [5, [], '/issues/5.json', '', false],
'array response with integer id' => [
5,
[],
'/issues/5.json',
'["API Response"]',
['API Response'],
],
'array response with string id' => [
'5',
[],
'/issues/5.json',
'["API Response"]',
['API Response'],
],
'array response with parameters' => [
5,
['include' => ['parameter1', 'parameter2'], 'not-used'],
'/issues/5.json?include=parameter1%2Cparameter2&0=not-used',
'["API Response"]',
['API Response'],
],
'string response' => [
5,
[],
'/issues/5.json',
'string',
/** @phpstan-ignore smaller.alwaysTrue(Remove this line after release of PHP 8.6) */
(PHP_VERSION_ID < 80600) ? 'Error decoding body as JSON: Syntax error' : 'Error decoding body as JSON: Syntax error near location 1:1',
],
'false response' => [
5,
[],
'/issues/5.json',
'',
false,
],
];
}
}
29 changes: 25 additions & 4 deletions tests/Unit/Api/IssueCategory/ShowTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,31 @@ public function testShowReturnsCorrectResponse($id, string $expectedPath, string
public static function getShowData(): array
{
return [
'array response with integer id' => [5, '/issue_categories/5.json', '["API Response"]', ['API Response']],
'array response with string id' => ['5', '/issue_categories/5.json', '["API Response"]', ['API Response']],
'string response' => [5, '/issue_categories/5.json', 'string', 'Error decoding body as JSON: Syntax error'],
'false response' => [5, '/issue_categories/5.json', '', false],
'array response with integer id' => [
5,
'/issue_categories/5.json',
'["API Response"]',
['API Response'],
],
'array response with string id' => [
'5',
'/issue_categories/5.json',
'["API Response"]',
['API Response'],
],
'string response' => [
5,
'/issue_categories/5.json',
'string',
/** @phpstan-ignore smaller.alwaysTrue(Remove this line after release of PHP 8.6) */
(PHP_VERSION_ID < 80600) ? 'Error decoding body as JSON: Syntax error' : 'Error decoding body as JSON: Syntax error near location 1:1',
],
'false response' => [
5,
'/issue_categories/5.json',
'',
false,
],
];
}
}
35 changes: 30 additions & 5 deletions tests/Unit/Api/IssueRelation/ShowTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,36 @@ public function testShowReturnsCorrectResponse($id, string $expectedPath, string
public static function getShowData(): array
{
return [
'array response with integer id' => [5, '/relations/5.json', '{"relation":{"child":[5,2,3]}}', ['child' => [5, 2, 3]]],
'array response with string id' => ['5', '/relations/5.json', '{"relation":{"child":[5,2,3]}}', ['child' => [5, 2, 3]]],
'array response on object without relation key error' => [5, '/relations/5.json', '{}', []],
'array response on string error' => [5, '/relations/5.json', 'string', []],
'array response on empty error' => [5, '/relations/5.json', '', []],
'array response with integer id' => [
5,
'/relations/5.json',
'{"relation":{"child":[5,2,3]}}',
['child' => [5, 2, 3]],
],
'array response with string id' => [
'5',
'/relations/5.json',
'{"relation":{"child":[5,2,3]}}',
['child' => [5, 2, 3]],
],
'array response on object without relation key error' => [
5,
'/relations/5.json',
'{}',
[],
],
'array response on string error' => [
5,
'/relations/5.json',
'string',
[],
],
'array response on empty error' => [
5,
'/relations/5.json',
'',
[],
],
];
}
}
3 changes: 2 additions & 1 deletion tests/Unit/Api/Project/ShowTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,8 @@ public static function getShowData(): array
[],
'/projects/5.json?include=trackers%2Cissue_categories%2Cattachments%2Crelations',
'string',
'Error decoding body as JSON: Syntax error',
/** @phpstan-ignore smaller.alwaysTrue(Remove this line after release of PHP 8.6) */
(PHP_VERSION_ID < 80600) ? 'Error decoding body as JSON: Syntax error' : 'Error decoding body as JSON: Syntax error near location 1:1',
],
'false response' => [
5,
Expand Down
29 changes: 25 additions & 4 deletions tests/Unit/Api/Role/ShowTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,31 @@ public function testShowReturnsCorrectResponse($id, string $expectedPath, string
public static function getShowData(): array
{
return [
'array response with integer id' => [5, '/roles/5.json', '["API Response"]', ['API Response']],
'array response with string id' => ['5', '/roles/5.json', '["API Response"]', ['API Response']],
'string response' => [5, '/roles/5.json', 'string', 'Error decoding body as JSON: Syntax error'],
'false response' => [5, '/roles/5.json', '', false],
'array response with integer id' => [
5,
'/roles/5.json',
'["API Response"]',
['API Response'],
],
'array response with string id' => [
'5',
'/roles/5.json',
'["API Response"]',
['API Response'],
],
'string response' => [
5,
'/roles/5.json',
'string',
/** @phpstan-ignore smaller.alwaysTrue(Remove this line after release of PHP 8.6) */
(PHP_VERSION_ID < 80600) ? 'Error decoding body as JSON: Syntax error' : 'Error decoding body as JSON: Syntax error near location 1:1',
],
'false response' => [
5,
'/roles/5.json',
'',
false,
],
];
}
}
29 changes: 25 additions & 4 deletions tests/Unit/Api/TimeEntry/ShowTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,31 @@ public function testShowReturnsCorrectResponse($id, string $expectedPath, string
public static function getShowData(): array
{
return [
'array response with integer id' => [5, '/time_entries/5.json', '["API Response"]', ['API Response']],
'array response with string id' => ['5', '/time_entries/5.json', '["API Response"]', ['API Response']],
'string response' => [5, '/time_entries/5.json', 'string', 'Error decoding body as JSON: Syntax error'],
'false response' => [5, '/time_entries/5.json', '', false],
'array response with integer id' => [
5,
'/time_entries/5.json',
'["API Response"]',
['API Response'],
],
'array response with string id' => [
'5',
'/time_entries/5.json',
'["API Response"]',
['API Response'],
],
'string response' => [
5,
'/time_entries/5.json',
'string',
/** @phpstan-ignore smaller.alwaysTrue(Remove this line after release of PHP 8.6) */
(PHP_VERSION_ID < 80600) ? 'Error decoding body as JSON: Syntax error' : 'Error decoding body as JSON: Syntax error near location 1:1',
],
'false response' => [
5,
'/time_entries/5.json',
'',
false,
],
];
}
}
Loading