Skip to content

Commit

Permalink
Testing completion to 100%
Browse files Browse the repository at this point in the history
  • Loading branch information
elb98rm committed Mar 26, 2021
1 parent 5b53512 commit 44b4c92
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 8 deletions.
6 changes: 3 additions & 3 deletions src/Models/JsonApiFormatter.php
Original file line number Diff line number Diff line change
Expand Up @@ -528,12 +528,12 @@ public function import(string $json): JsonApiFormatter
if ($error['links'] ?? null) {
$links = new Links();

foreach ($error['links'] as $link) {
foreach ($error['links'] as $key => $link) {
if (is_string($link)) {
$links[] = $link;
$links->addLink($key, $link);
} else {
$new_link = new Link($link);
$links[] = $new_link;
$links->addLink($key, $new_link);
}
}
}
Expand Down
94 changes: 89 additions & 5 deletions tests/Unit/JsonApiFormatterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Floor9design\JsonApiFormatter\Models\DataResource;
use Floor9design\JsonApiFormatter\Models\Error;
use Floor9design\JsonApiFormatter\Models\JsonApiFormatter;
use Floor9design\JsonApiFormatter\Models\Link;
use Floor9design\JsonApiFormatter\Models\Links;
use Floor9design\JsonApiFormatter\Models\Meta;
use PHPUnit\Framework\TestCase;
Expand Down Expand Up @@ -179,6 +180,48 @@ public function testErrorsAccessors()
$test_object->unsetErrors();
$response = $reflection->invokeArgs($test_object, []);
$this->assertFalse(isset($response['errors']));

$json_api_formatter = new JsonApiFormatter();
$test_bad_object = new StdClass();

// Check bad error object exception
$this->expectException(JsonApiFormatterException::class);
$this->expectExceptionMessage('$errors needs to be an array of Error objects');
$json_api_formatter->setErrors([$test_bad_object]);
}

/**
* Test errors accessors.
*
* @return void
* @throws \ReflectionException
*/
public function testErrorsAccessorsSetException()
{
$json_api_formatter = new JsonApiFormatter();
$test_bad_object = new StdClass();

// Check bad error object exception
$this->expectException(JsonApiFormatterException::class);
$this->expectExceptionMessage('$errors needs to be an array of Error objects');
$json_api_formatter->setErrors([$test_bad_object]);
}

/**
* Test errors accessors.
*
* @return void
* @throws \ReflectionException
*/
public function testErrorsAccessorsAddException()
{
$json_api_formatter = new JsonApiFormatter();
$test_bad_object = new StdClass();

// Check bad error object exception
$this->expectException(JsonApiFormatterException::class);
$this->expectExceptionMessage('$errors needs to be an array of Error objects');
$json_api_formatter->addErrors([$test_bad_object]);
}

/**
Expand Down Expand Up @@ -777,20 +820,61 @@ public function testImportDataArray()
public function testImportErrors()
{
$error = new Error();
$links = new Links(
[
'http://link.com',
new Link(
['hello', 'world']
)
]
);
$source = new StdClass();
$source->hello = 'world';
$status = '400';
$code = '400';
$title = 'Bad request';
$detail = 'The request was not formed well';

$error
->setStatus('400')
->setTitle('Bad request')
->setDetail('The request was not formed well');
->setStatus($status)
->setCode($code)
->setTitle($title)
->setDetail($detail)
->setLinks($links)
->setSource($source);

$json_array = [
'errors' => [$error]
'errors' =>
[
[
'status' => $status,
'code' => $code,
'title' => $title,
'detail' => $detail,
'links' => [
'http://link.com',
['hello', 'world']
],
'source' => ['hello' => 'world']
]
]
];

$errors_json = json_encode($json_array, true);
$json_api_formatter = new JsonApiFormatter();

$json_api_formatter->import($errors_json);

$this->assertEquals($json_api_formatter->getErrors(), $json_array['errors']);
$errors = $json_api_formatter->getErrors();
$error = $errors[0];

// test
$this->assertEquals($status, $error->getStatus());
$this->assertEquals($code, $error->getCode());
$this->assertEquals($title, $error->getTitle());
$this->assertEquals($detail, $error->getDetail());
$this->assertEquals($links, $error->getLinks());
$this->assertEquals($source, $error->getSource());
}

/**
Expand Down

0 comments on commit 44b4c92

Please sign in to comment.