CoreFiles by mySites.guru
CollectionTest.php65 lines, 1,779 bytes, from Joomla_4.1.2-Stable-Full_Package.zip
Raw History Compare with 4.1.1
md5
e519a76e3b78b51c2c16065f9b2af4b8
sha1
476e1a1125b48f859100385cbb9288796215f621
sha256
8cd9595f4a62a96dea26649fb4bf9869fe46105a2910a172640002f2abe1276f
<?php

/*
 * This file is part of JSON-API.
 *
 * (c) Toby Zerner <[email protected]>
 *
 * For the full copyright and license information, please view the LICENSE
 * file that was distributed with this source code.
 */

namespace Tobscure\Tests\JsonApi\Element;

use Tobscure\JsonApi\AbstractSerializer;
use Tobscure\JsonApi\Collection;
use Tobscure\JsonApi\Resource;
use Tobscure\Tests\JsonApi\AbstractTestCase;

/**
 * This is the collection test class.
 *
 * @author Toby Zerner <[email protected]>
 */
class CollectionTest extends AbstractTestCase
{
    public function testToArrayReturnsArrayOfResources()
    {
        $serializer = new PostSerializer3;

        $post1 = (object) ['id' => 1, 'foo' => 'bar'];
        $post2 = new Resource((object) ['id' => 2, 'foo' => 'baz'], $serializer);

        $collection = new Collection([$post1, $post2], $serializer);

        $resource1 = new Resource($post1, $serializer);
        $resource2 = $post2;

        $this->assertEquals([$resource1->toArray(), $resource2->toArray()], $collection->toArray());
    }

    public function testToIdentifierReturnsArrayOfResourceIdentifiers()
    {
        $serializer = new PostSerializer3;

        $post1 = (object) ['id' => 1];
        $post2 = (object) ['id' => 2];

        $collection = new Collection([$post1, $post2], $serializer);

        $resource1 = new Resource($post1, $serializer);
        $resource2 = new Resource($post2, $serializer);

        $this->assertEquals([$resource1->toIdentifier(), $resource2->toIdentifier()], $collection->toIdentifier());
    }
}

class PostSerializer3 extends AbstractSerializer
{
    protected $type = 'posts';

    public function getAttributes($post, array $fields = null)
    {
        return ['foo' => $post->foo];
    }
}

The same content is in 9 releases, Joomla 4.1.0-rc1 to 4.1.2. See them all, or every version of this file.