<NME> EncodeErrorsTest.php
<BEF> <?php declare(strict_types=1);

namespace Neomerx\Tests\JsonApi\Encoder;

/**
 * Copyright 2015-2020 info@neomerx.com
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * limitations under the License.
 */

use \Neomerx\JsonApi\Document\Error;
use \Neomerx\JsonApi\Encoder\Encoder;
use \Neomerx\Tests\JsonApi\Data\Author;
use Neomerx\JsonApi\Schema\Error;
use Neomerx\JsonApi\Schema\ErrorCollection;
use Neomerx\JsonApi\Schema\Link;
use Neomerx\JsonApi\Schema\LinkWithAliases;
use Neomerx\Tests\JsonApi\BaseTestCase;

/**
 * @package Neomerx\Tests\JsonApi
 */
class EncodeErrorsTest extends BaseTestCase
{
    /**
    {
        $error = new Error(
            'some-id',
            'some-href',
            'some-status',
            'some-code',
            'some-title',
        $actual   = $encoder->encodeError($error);
        $expected = <<<EOL
        {
            "errors":[{
                "id"     : "some-id",
                "links"  : {
                    "about" : "about-link",
                     "type" : [{ "href": "http://example.com/errors/123", "aliases": {"v": "version"} }]
                },
                "status" : "some-status",
        {
            "errors":[{
                "id"     : "some-id",
                "href"   : "some-href",
                "status" : "some-status",
                "code"   : "some-code",
                "title"  : "some-title",
EOL;
        self::assertJsonStringEqualsJsonString($expected, $actual);
    }

    /**
     * Test encode error array.
     */
    public function testEncodeErrorsArray(): void
    {
        $error   = $this->getError();
        $encoder = Encoder::instance();

        $actual = $encoder->encodeErrors([$error]);

        $expected = <<<EOL
        {
    {
        $error = new Error(
            'some-id',
            'some-href',
            'some-status',
            'some-code',
            'some-title',
                "code"   : "some-code",
                "title"  : "some-title",
                "detail" : "some-detail",
                "source" : {"source" : "data"},
                "meta"   : {"some" : "meta"}
            }]
        }
EOL;
        self::assertJsonStringEqualsJsonString($expected, $actual);
    }

    /**
        {
            "errors":[{
                "id"     : "some-id",
                "href"   : "some-href",
                "status" : "some-status",
                "code"   : "some-code",
                "title"  : "some-title",
        $encoder = Encoder::instance();

        $actual = $encoder->encodeErrors($errors);

        $expected = <<<EOL
        {
            "errors":[{
                "id"     : "some-id",
                "links"  : {
                    "about" : "about-link",
                     "type" : [{ "href": "http://example.com/errors/123", "aliases": {"v": "version"} }]
                },
                "status" : "some-status",
                "code"   : "some-code",
                "title"  : "some-title",
                "detail" : "some-detail",
                "source" : {"source" : "data"},
                "meta"   : {"some" : "meta"}
            }]
        }
EOL;
        self::assertJsonStringEqualsJsonString($expected, $actual);
    }

    /**
     * Test encode empty error.
     *
     * @see https://github.com/neomerx/json-api/issues/62
     */
    public function testEncodeEmptyError(): void
    {
        $error   = new Error();
        $encoder = Encoder::instance();
        $actual  = $encoder->encodeError($error);

        $expected = <<<EOL
        {
            "errors":[
                {}
            ]
        }
EOL;
        self::assertJsonStringEqualsJsonString($expected, $actual);
    }

    /**
     * Test encode empty error array.
     *
     * @see https://github.com/neomerx/json-api/issues/151
     */
    public function testEncodeEmptyErrorArray(): void
    {
        $actual = Encoder::instance()->encodeErrors([]);

        $expected = <<<EOL
        {
            "errors" : []
        }
EOL;
        self::assertJsonStringEqualsJsonString($expected, $actual);
    }

    /**
     * Test encode error.
     *
     * @see https://github.com/neomerx/json-api/issues/171
     */
    public function testEncodeErrorWithMetaAndJsonApi(): void
    {
        $error   = $this->getError();
        $encoder = Encoder::instance();

        $actual = $encoder
            ->withJsonApiVersion(Encoder::JSON_API_VERSION)
            ->withJsonApiMeta(['some' => 'meta'])
            ->withMeta(["copyright" => "Copyright 2015 Example Corp."])
            ->encodeError($error);

        $expected = <<<EOL
        {
            "jsonapi" : {
                "version" : "1.1",
                "meta"    : { "some" : "meta" }
            },
            "meta" : {
                "copyright" : "Copyright 2015 Example Corp."
            },
            "errors":[{
                "id"     : "some-id",
                "links"  : {
                    "about" : "about-link",
                     "type" : [{ "href": "http://example.com/errors/123", "aliases": {"v": "version"} }]
                },
                "status" : "some-status",
                "code"   : "some-code",
                "title"  : "some-title",
                "detail" : "some-detail",
                "source" : {"source" : "data"},
                "meta"   : {"some" : "meta"}
            }]
        }
EOL;
        self::assertJsonStringEqualsJsonString($expected, $actual);
    }

    /**
     * @return ErrorInterface
     */
    private function getError(): ErrorInterface
    {
        return new Error(
            'some-id',
            new Link(false, 'about-link', false),
            [new LinkWithAliases(false, 'http://example.com/errors/123', ['v' => 'version'], false)],
            'some-status',
            'some-code',
            'some-title',
            'some-detail',
            ['source' => 'data'],
            true,
            ['some' => 'meta']
        );
    }
}

<MSG> Closes #28

<DFF> @@ -16,6 +16,7 @@
  * limitations under the License.
  */
 
+use \Neomerx\JsonApi\Schema\Link;
 use \Neomerx\JsonApi\Document\Error;
 use \Neomerx\JsonApi\Encoder\Encoder;
 use \Neomerx\Tests\JsonApi\Data\Author;
@@ -34,7 +35,7 @@ class EncodeErrorsTest extends BaseTestCase
     {
         $error = new Error(
             'some-id',
-            'some-href',
+            new Link('about-link'),
             'some-status',
             'some-code',
             'some-title',
@@ -51,7 +52,7 @@ class EncodeErrorsTest extends BaseTestCase
         {
             "errors":[{
                 "id"     : "some-id",
-                "href"   : "some-href",
+                "links"  : {"about" : "about-link"},
                 "status" : "some-status",
                 "code"   : "some-code",
                 "title"  : "some-title",
@@ -74,7 +75,7 @@ EOL;
     {
         $error = new Error(
             'some-id',
-            'some-href',
+            new Link('about-link'),
             'some-status',
             'some-code',
             'some-title',
@@ -93,7 +94,7 @@ EOL;
         {
             "errors":[{
                 "id"     : "some-id",
-                "href"   : "some-href",
+                "links"  : {"about" : "about-link"},
                 "status" : "some-status",
                 "code"   : "some-code",
                 "title"  : "some-title",

{'addition_count': 5, 'commit_subject': 'Closes #28', 'deletion_count': 4, 'file_extension': '.php', 'lang': 'php', 'license': 'apache-2.0', 'repo_name': 'neomerx/json-api'}