public function testSerializingCollectionResourceWithMeta() { $this->manager->parseIncludes('author'); $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'); $resource->setMetaValue('foo', 'bar'); $scope = new Scope($this->manager, $resource); $expected = array('book' => array(array('title' => 'Foo', 'year' => 1991), array('title' => 'Bar', 'year' => 1997)), 'linked' => array('author' => array(array('name' => 'Dave'), array('name' => 'Bob'))), 'meta' => array('foo' => 'bar')); $this->assertEquals($expected, $scope->toArray()); $expectedJson = '{"book":[{"title":"Foo","year":1991},{"title":"Bar","year":1997}],"linked":{"author":[{"name":"Dave"},{"name":"Bob"}]},"meta":{"foo":"bar"}}'; $this->assertEquals($expectedJson, $scope->toJson()); }
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()); }
/** * get the transformed json data. * * @return string */ public function getJson() { return $this->scope->toJson(); }