From 44b4c920d50002ab02f8a33b5e8a493b7d9f2b4d Mon Sep 17 00:00:00 2001 From: elb98rm Date: Fri, 26 Mar 2021 13:57:44 +0000 Subject: [PATCH] Testing completion to 100% --- src/Models/JsonApiFormatter.php | 6 +- tests/Unit/JsonApiFormatterTest.php | 94 +++++++++++++++++++++++++++-- 2 files changed, 92 insertions(+), 8 deletions(-) diff --git a/src/Models/JsonApiFormatter.php b/src/Models/JsonApiFormatter.php index b1d8186..b66d6fe 100644 --- a/src/Models/JsonApiFormatter.php +++ b/src/Models/JsonApiFormatter.php @@ -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); } } } diff --git a/tests/Unit/JsonApiFormatterTest.php b/tests/Unit/JsonApiFormatterTest.php index 8216400..27a087c 100644 --- a/tests/Unit/JsonApiFormatterTest.php +++ b/tests/Unit/JsonApiFormatterTest.php @@ -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; @@ -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]); } /** @@ -777,12 +820,44 @@ 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); @@ -790,7 +865,16 @@ public function testImportErrors() $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()); } /**