/**
  * Validate that a resource can be set against the collection for hydration.
  *
  * @test
  */
 public function validateCollectionHydrationResource()
 {
     $collection = new ResourceCollection();
     $this->assertNull($collection->getHydrationResource());
     /** @var MockObject|TransportableInterface $transport */
     $transport = $this->getMock(TransportableInterface::class);
     $this->assertEquals($collection, $collection->setHydrationResource($transport));
     $this->assertEquals($transport, $collection->getHydrationResource());
 }
 /**
  * Expect that a collection can be hydrated using an array.
  *
  * @test
  */
 public function validateHydrateCollection()
 {
     $data = ['data' => [['id' => 100, 'name' => 'A', 'albums' => ['data' => []]], ['id' => 200, 'name' => 'B', 'albums' => ['data' => []]]]];
     $collection = new ResourceCollection();
     $collection->setHydrationResource(new ArtistResourceMock());
     /** @var ResourceCollection|ArtistResourceMock[] $collection */
     $collection = $this->transformer->hydrate($collection, $data);
     $this->assertInstanceOf(ResourceCollection::class, $collection);
     $this->assertCount(2, $collection);
     /** @var ArtistResourceMock $a */
     $a = $collection[0];
     $this->assertEquals(100, $a->getId());
     $this->assertEquals('A', $a->getName());
     $this->assertInstanceOf(ResourceCollection::class, $a->getAlbums());
     $this->assertCount(0, $a->getAlbums());
     /** @var ArtistResourceMock $b */
     $b = $collection[1];
     $this->assertEquals(200, $b->getId());
     $this->assertEquals('B', $b->getName());
     $this->assertInstanceOf(ResourceCollection::class, $b->getAlbums());
     $this->assertCount(0, $b->getAlbums());
 }
Exemple #3
0
 /**
  * @param ResourceCollection $collection
  * @param array $data
  * @return ResourceCollection
  */
 protected function hydrateCollection(ResourceCollection $collection, array $data)
 {
     foreach ($data['data'] as $resourceData) {
         $instance = clone $collection->getHydrationResource();
         //            if (!isset($resourceData[self::KEY_RECURSION])) {
         $instance = $this->hydrateResource($instance, $resourceData);
         //            }
         $collection->add($instance);
     }
     return $collection;
 }
 /**
  * Expects the depth of the dehydration to be respected.
  *
  * @test
  */
 public function validateDehydrateRespectsDepthLevel()
 {
     $artist = ArtistResourceMock::create(1);
     $artist->setName('Fightstar');
     $artist->setAlbums($albums = new ResourceCollection());
     $albums->add($album = AlbumResourceMock::create(100));
     $album->setName('One Day Son');
     $album->setArtist($artist);
     $album->setSongs($songs = new ResourceCollection());
     $album->setArtwork($artwork = ArtworkResourceMock::create(20));
     $artwork->setImage('ryohei-hase.jpg');
     $artwork->setMime('image/jpg');
     $artwork->setAlbum($album);
     $songs->add($song = SongResourceMock::create(1000));
     $song->setName('Deathcar');
     $song->setDescription('Heavy');
     $song->setAlbum($album);
     //  Make sure that the unit of work we are testing is hardcoded.
     //  Other areas of the test that are covered can make use of the resource getters.
     $expected = ['id' => $artist->getId(), 'name' => $artist->getName(), 'albums' => ['data' => []]];
     $array = $this->transformer->dehydrate($artist, 1);
     $this->assertEquals($expected, $array, 'Depth level: 1');
     //  At depth level 2 the albums collection should be dehydrated.
     $expected['albums']['data'] = [['id' => $album->getId(), 'name' => $album->getName(), 'artist' => [ArrayTransformer::KEY_RECURSION => true, 'id' => $artist->getId()], 'songs' => ['data' => []], 'artwork' => ['id' => $artwork->getId()]]];
     $array = $this->transformer->dehydrate($artist, 2);
     $this->assertEquals($expected, $array, 'Depth level: 2');
     //  At depth level 3 the artwork resource should be dehydrated ..
     $expected['albums']['data'][0]['artwork'] = ['id' => $artwork->getId(), 'image' => $artwork->getImage(), 'type' => $artwork->getMime(), 'album' => [ArrayTransformer::KEY_RECURSION => true, 'id' => $album->getId()]];
     //  .. and also the songs collection.
     $expected['albums']['data'][0]['songs']['data'] = [['id' => $song->getId(), 'name' => $song->getName(), 'description' => $song->getDescription(), 'album' => [ArrayTransformer::KEY_RECURSION => true, 'id' => $album->getId()]]];
     $array = $this->transformer->dehydrate($artist, 3);
     $this->assertEquals($expected, $array, 'Depth level: 3');
 }