/** * @expectedException RuntimeException * @expectedExceptionMessage The $resourceKey parameter must be provided when using League\Fractal\Serializer\JsonApiSerializer **/ public function testResourceKeyMissing() { $this->manager->setSerializer(new JsonApiSerializer()); $bookData = array('title' => 'Foo', 'year' => '1991'); $resource = new Item($bookData, new GenericBookTransformer()); $scope = new Scope($this->manager, $resource); $scope->toArray(); }
public function testSerializingCollectionResource() { $manager = new Manager(); $manager->parseIncludes('author'); $manager->setSerializer(new DataArraySerializer()); $booksData = array(array('title' => 'Foo', 'year' => '1991', '_author' => array('name' => 'Dave')), array('title' => 'Bar', 'year' => '1997', '_author' => array('name' => 'Bob'))); // Try without metadata $resource = new Collection($booksData, new GenericBookTransformer(), 'book'); $scope = new Scope($manager, $resource); $expected = array('data' => array(array('title' => 'Foo', 'year' => 1991, 'author' => array('data' => array('name' => 'Dave'))), array('title' => 'Bar', 'year' => 1997, 'author' => array('data' => array('name' => 'Bob'))))); $this->assertEquals($expected, $scope->toArray()); $expectedJson = '{"data":[{"title":"Foo","year":1991,"author":{"data":{"name":"Dave"}}},{"title":"Bar","year":1997,"author":{"data":{"name":"Bob"}}}]}'; $this->assertEquals($expectedJson, $scope->toJson()); // Same again with meta $resource = new Collection($booksData, new GenericBookTransformer(), 'book'); $resource->setMetaValue('foo', 'bar'); $scope = new Scope($manager, $resource); $expected = array('meta' => array('foo' => 'bar'), 'data' => array(array('title' => 'Foo', 'year' => 1991, 'author' => array('data' => array('name' => 'Dave'))), array('title' => 'Bar', 'year' => 1997, 'author' => array('data' => array('name' => 'Bob'))))); $this->assertEquals($expected, $scope->toArray()); $expectedJson = '{"data":[{"title":"Foo","year":1991,"author":{"data":{"name":"Dave"}}},{"title":"Bar","year":1997,"author":{"data":{"name":"Bob"}}}],"meta":{"foo":"bar"}}'; $this->assertEquals($expectedJson, $scope->toJson()); }
public function testSerializingCollectionResource() { $manager = new Manager(); $manager->parseIncludes('author'); $manager->setSerializer(new ArraySerializer()); $booksData = array(array('title' => 'Foo', 'year' => '1991', '_author' => array('name' => 'Dave')), array('title' => 'Bar', 'year' => '1997', '_author' => array('name' => 'Bob'))); $resource = new Collection($booksData, new GenericBookTransformer(), 'book'); // Try without metadata $scope = new Scope($manager, $resource); $expected = array(array('title' => 'Foo', 'year' => 1991, 'author' => array('name' => 'Dave')), array('title' => 'Bar', 'year' => 1997, 'author' => array('name' => 'Bob'))); $this->assertEquals($expected, $scope->toArray()); // JSON array of JSON objects $expectedJson = '[{"title":"Foo","year":1991,"author":{"name":"Dave"}},{"title":"Bar","year":1997,"author":{"name":"Bob"}}]'; $this->assertEquals($expectedJson, $scope->toJson()); // Same again with metadata $resource->setMetaValue('foo', 'bar'); $scope = new Scope($manager, $resource); $expected = array(array('title' => 'Foo', 'year' => 1991, 'author' => array('name' => 'Dave')), array('title' => 'Bar', 'year' => 1997, 'author' => array('name' => 'Bob')), 'meta' => array('foo' => 'bar')); $this->assertEquals($expected, $scope->toArray()); // This JSON sucks, because when you add a string key then it has to string up all the other keys. Using meta in Array is shit $expectedJson = '{"0":{"title":"Foo","year":1991,"author":{"name":"Dave"}},"1":{"title":"Bar","year":1997,"author":{"name":"Bob"}},"meta":{"foo":"bar"}}'; $this->assertEquals($expectedJson, $scope->toJson()); }
/** * @param int $options * * @return string */ public function toJson($options = 0) { return json_encode($this->scope->toArray(), $options); }
public function testDefaultIncludeSuccess() { $manager = new Manager(); $manager->setSerializer(new ArraySerializer()); // Send this stub junk, it has a specific format anyhow $resource = new Item(array(), new DefaultIncludeBookTransformer()); // Try without metadata $scope = new Scope($manager, $resource); $expected = array('a' => 'b', 'author' => array('c' => 'd')); $this->assertEquals($expected, $scope->toArray()); }
/** * get the transformed array data * @return array */ public function getArray() { return $this->scope->toArray(); }