public function testHiddenProperties()
 {
     $this->mapping->setHiddenProperties(['propertyName']);
     $this->assertEquals(['propertyName'], $this->mapping->getHiddenProperties());
     $this->mapping->hideProperty('secondProperty');
     $this->assertEquals(['propertyName', 'secondProperty'], $this->mapping->getHiddenProperties());
 }
 /**
  * @param array   $mappedClass
  * @param Mapping $mapping
  * @param string  $className
  *
  * @throws MappingException
  */
 protected static function setHideProperties(array &$mappedClass, Mapping $mapping, string $className)
 {
     if (false === empty($mappedClass[static::HIDE_PROPERTIES_KEY])) {
         $mapping->setHiddenProperties($mappedClass[static::HIDE_PROPERTIES_KEY]);
         foreach ($mapping->getHiddenProperties() as $propertyName) {
             if (false === \in_array($propertyName, static::getClassProperties($className), true)) {
                 throw new MappingException(\sprintf('Could not hide property %s in class %s because it does not exist.', $propertyName, $className));
             }
         }
     }
 }
    /**
     *
     */
    public function testItWillBuildUrlUsingAliasOrTypeNameIfIdFieldNotPresentInUrl()
    {
        $post = HelperFactory::simplePost();
        $postMapping = new Mapping(SimplePost::class, '/post/{post}', ['postId']);
        $postMapping->setHiddenProperties(['title', 'body']);
        $mapper = new Mapper();
        $mapper->setClassMap([$postMapping->getClassName() => $postMapping]);
        $jsonApiJsonApiSerializer = new JsonApiTransformer($mapper);
        $expected = <<<JSON
{
    "data": {
        "type": "post",
        "id": "1",
        "attributes": {
            "author_id": 2,
            "comments": [
                {
                    "comment_id": 10,
                    "comment": "I am writing comment no. 1",
                    "user_id": "User 5",
                    "created_at": "2015-07-19T12:48:00+02:00"
                },
                {
                    "comment_id": 20,
                    "comment": "I am writing comment no. 2",
                    "user_id": "User 10",
                    "created_at": "2015-07-20T12:48:00+02:00"
                },
                {
                    "comment_id": 30,
                    "comment": "I am writing comment no. 3",
                    "user_id": "User 15",
                    "created_at": "2015-07-21T12:48:00+02:00"
                },
                {
                    "comment_id": 40,
                    "comment": "I am writing comment no. 4",
                    "user_id": "User 20",
                    "created_at": "2015-07-22T12:48:00+02:00"
                },
                {
                    "comment_id": 50,
                    "comment": "I am writing comment no. 5",
                    "user_id": "User 25",
                    "created_at": "2015-07-23T12:48:00+02:00"
                }
            ], 
            "post_id": 1
        },
        "links": {
            "self": { "href": "/post/1" }
        }
    },
    "links": {
        "self": { "href": "/post/1" }
    },
    "jsonapi": {
        "version": "1.0"
    }
}
JSON;
        $this->assertEquals(\json_decode($expected, true), \json_decode((new JsonApiSerializer($jsonApiJsonApiSerializer))->serialize($post), true));
    }
Esempio n. 4
0
    /**
     *
     */
    public function testItWillHidePropertiesFromClass()
    {
        $post = $this->createSimplePost();
        $postMapping = new Mapping(SimplePost::class, '/post/{postId}', ['postId']);
        $postMapping->setHiddenProperties(['title', 'body']);
        $mapper = new Mapper();
        $mapper->setClassMap([$postMapping->getClassName() => $postMapping]);
        $transformer = new JsonTransformer($mapper);
        $expected = <<<JSON
{
    "post_id": 1,
    "author_id": 2,
    "comments": [
        {
            "comment_id": 10,
            "comment": "I am writing comment no. 1",
            "user_id": "User 5",
            "created_at": "2015-07-19T12:48:00+02:00"
        },
        {
            "comment_id": 20,
            "comment": "I am writing comment no. 2",
            "user_id": "User 10",
            "created_at": "2015-07-20T12:48:00+02:00"
        },
        {
            "comment_id": 30,
            "comment": "I am writing comment no. 3",
            "user_id": "User 15",
            "created_at": "2015-07-21T12:48:00+02:00"
        },
        {
            "comment_id": 40,
            "comment": "I am writing comment no. 4",
            "user_id": "User 20",
            "created_at": "2015-07-22T12:48:00+02:00"
        },
        {
            "comment_id": 50,
            "comment": "I am writing comment no. 5",
            "user_id": "User 25",
            "created_at": "2015-07-23T12:48:00+02:00"
        }
    ],
    "_links": {
        "self": {
            "href": "/post/1"
        } 
    }
}
JSON;
        $this->assertEquals(\json_decode($expected, true), \json_decode((new HalSerializer($transformer))->serialize($post), true));
    }