123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373 |
- <?php namespace League\Fractal\Test;
- use League\Fractal\Manager;
- use League\Fractal\Pagination\Cursor;
- use League\Fractal\Resource\Collection;
- use League\Fractal\Resource\Item;
- use League\Fractal\Scope;
- use League\Fractal\Serializer\ArraySerializer;
- use League\Fractal\Test\Stub\Transformer\DefaultIncludeBookTransformer;
- use Mockery;
- class ScopeTest extends \PHPUnit_Framework_TestCase
- {
- protected $simpleItem = array('foo' => 'bar');
- protected $simpleCollection = array(array('foo' => 'bar'));
- public function testEmbedChildScope()
- {
- $manager = new Manager();
- $resource = new Item(array('foo' => 'bar'), function () {
- });
- $scope = new Scope($manager, $resource, 'book');
- $this->assertEquals($scope->getScopeIdentifier(), 'book');
- $childScope = $scope->embedChildScope('author', $resource);
- $this->assertInstanceOf('League\Fractal\Scope', $childScope);
- }
- public function testGetManager()
- {
- $resource = new Item(array('foo' => 'bar'), function () {
- });
- $scope = new Scope(new Manager(), $resource, 'book');
- $this->assertInstanceOf('League\Fractal\Manager', $scope->getManager());
- }
- /**
- * @covers League\Fractal\Scope::toArray
- */
- public function testToArray()
- {
- $manager = new Manager();
- $resource = new Item(array('foo' => 'bar'), function ($data) {
- return $data;
- });
- $scope = new Scope($manager, $resource);
- $this->assertEquals(array('data' => array('foo' => 'bar')), $scope->toArray());
- }
- public function testGetCurrentScope()
- {
- $manager = new Manager();
- $resource = new Item(array('name' => 'Larry Ullman'), function () {
- });
- $scope = new Scope($manager, $resource, 'book');
- $this->assertEquals('book', $scope->getScopeIdentifier());
- $childScope = $scope->embedChildScope('author', $resource);
- $this->assertEquals('author', $childScope->getScopeIdentifier());
- $grandChildScope = $childScope->embedChildScope('profile', $resource);
- $this->assertEquals('profile', $grandChildScope->getScopeIdentifier());
- }
- public function testGetIdentifier()
- {
- $manager = new Manager();
- $resource = new Item(array('name' => 'Larry Ullman'), function () {
- });
- $scope = new Scope($manager, $resource, 'book');
- $this->assertEquals('book', $scope->getIdentifier());
- $childScope = $scope->embedChildScope('author', $resource);
- $this->assertEquals('book.author', $childScope->getIdentifier());
- $grandChildScope = $childScope->embedChildScope('profile', $resource);
- $this->assertEquals('book.author.profile', $grandChildScope->getIdentifier());
- }
- public function testGetParentScopes()
- {
- $manager = new Manager();
- $resource = new Item(array('name' => 'Larry Ullman'), function () {
- });
- $scope = new Scope($manager, $resource, 'book');
- $childScope = $scope->embedChildScope('author', $resource);
- $this->assertEquals(array('book'), $childScope->getParentScopes());
- $grandChildScope = $childScope->embedChildScope('profile', $resource);
- $this->assertEquals(array('book', 'author'), $grandChildScope->getParentScopes());
- }
- public function testIsRequested()
- {
- $manager = new Manager();
- $manager->parseIncludes(array('foo', 'bar', 'baz.bart'));
- $scope = new Scope($manager, Mockery::mock('League\Fractal\Resource\ResourceAbstract'));
- $this->assertTrue($scope->isRequested('foo'));
- $this->assertTrue($scope->isRequested('bar'));
- $this->assertTrue($scope->isRequested('baz'));
- $this->assertTrue($scope->isRequested('baz.bart'));
- $this->assertFalse($scope->isRequested('nope'));
- $childScope = $scope->embedChildScope('baz', Mockery::mock('League\Fractal\Resource\ResourceAbstract'));
- $this->assertTrue($childScope->isRequested('bart'));
- $this->assertFalse($childScope->isRequested('foo'));
- $this->assertFalse($childScope->isRequested('bar'));
- $this->assertFalse($childScope->isRequested('baz'));
- }
- /**
- * @expectedException InvalidArgumentException
- */
- public function testScopeRequiresConcreteImplementation()
- {
- $manager = new Manager();
- $manager->parseIncludes('book');
- $resource = Mockery::mock('League\Fractal\Resource\ResourceAbstract', array(
- array('bar' => 'baz'),
- function() {}
- ))->makePartial();
- $scope = new Scope($manager, $resource);
- $scope->toArray();
- }
- public function testToArrayWithIncludes()
- {
- $manager = new Manager();
- $manager->parseIncludes('book');
- $transformer = Mockery::mock('League\Fractal\TransformerAbstract')->makePartial();
- $transformer->shouldReceive('getAvailableIncludes')->twice()->andReturn(array('book'));
- $transformer->shouldReceive('transform')->once()->andReturnUsing(function (array $data) {
- return $data;
- });
- $transformer->shouldReceive('processIncludedResources')->once()->andReturn(array('book' => array('yin' => 'yang')));
- $resource = new Item(array('bar' => 'baz'), $transformer);
- $scope = new Scope($manager, $resource);
- $this->assertEquals(array('data' => array('bar' => 'baz', 'book' => array('yin' => 'yang'))), $scope->toArray());
- }
- public function testToArrayWithSideloadedIncludes()
- {
- $serializer = Mockery::mock('League\Fractal\Serializer\ArraySerializer')->makePartial();
- $serializer->shouldReceive('sideloadIncludes')->andReturn(true);
- $serializer->shouldReceive('item')->andReturnUsing(function ($key, $data) {
- return array('data' => $data);
- });
- $serializer->shouldReceive('includedData')->andReturnUsing(function ($key, $data) {
- return array('sideloaded' => array_pop($data));
- });
- $manager = new Manager();
- $manager->parseIncludes('book');
- $manager->setSerializer($serializer);
- $transformer = Mockery::mock('League\Fractal\TransformerAbstract')->makePartial();
- $transformer->shouldReceive('getAvailableIncludes')->twice()->andReturn(array('book'));
- $transformer->shouldReceive('transform')->once()->andReturnUsing(function (array $data) {
- return $data;
- });
- $transformer->shouldReceive('processIncludedResources')->once()->andReturn(array('book' => array('yin' => 'yang')));
- $resource = new Item(array('bar' => 'baz'), $transformer);
- $scope = new Scope($manager, $resource);
- $expected = array(
- 'data' => array('bar' => 'baz'),
- 'sideloaded' => array('book' => array('yin' => 'yang')),
- );
- $this->assertEquals($expected, $scope->toArray());
- }
- public function testPushParentScope()
- {
- $manager = new Manager();
- $resource = new Item(array('name' => 'Larry Ullman'), function () {
- });
- $scope = new Scope($manager, $resource);
- $this->assertEquals(1, $scope->pushParentScope('book'));
- $this->assertEquals(2, $scope->pushParentScope('author'));
- $this->assertEquals(3, $scope->pushParentScope('profile'));
- $this->assertEquals(array('book', 'author', 'profile'), $scope->getParentScopes());
- }
- public function testRunAppropriateTransformerWithItem()
- {
- $manager = new Manager();
- $transformer = Mockery::mock('League\Fractal\TransformerAbstract');
- $transformer->shouldReceive('transform')->once()->andReturn($this->simpleItem);
- $transformer->shouldReceive('getAvailableIncludes')->once()->andReturn(array());
- $transformer->shouldReceive('getDefaultIncludes')->once()->andReturn(array());
- $resource = new Item($this->simpleItem, $transformer);
- $scope = $manager->createData($resource);
- $this->assertEquals(array('data' => $this->simpleItem), $scope->toArray());
- }
- public function testRunAppropriateTransformerWithCollection()
- {
- $manager = new Manager();
- $transformer = Mockery::mock('League\Fractal\TransformerAbstract');
- $transformer->shouldReceive('transform')->once()->andReturn(array('foo' => 'bar'));
- $transformer->shouldReceive('getAvailableIncludes')->once()->andReturn(array());
- $transformer->shouldReceive('getDefaultIncludes')->once()->andReturn(array());
- $resource = new Collection(array(array('foo' => 'bar')), $transformer);
- $scope = $manager->createData($resource);
- $this->assertEquals(array('data' => array(array('foo' => 'bar'))), $scope->toArray());
- }
- /**
- * @covers League\Fractal\Scope::executeResourceTransformers
- * @expectedException InvalidArgumentException
- * @expectedExceptionMessage Argument $resource should be an instance of League\Fractal\Resource\Item or League\Fractal\Resource\Collection
- */
- public function testCreateDataWithClassFuckKnows()
- {
- $manager = new Manager();
- $transformer = Mockery::mock('League\Fractal\TransformerAbstract')->makePartial();
- $resource = Mockery::mock('League\Fractal\Resource\ResourceAbstract', array($this->simpleItem, $transformer))->makePartial();
- $scope = $manager->createData($resource);
- $scope->toArray();
- }
- public function testPaginatorOutput()
- {
- $manager = new Manager();
- $collection = new Collection(array(array('foo' => 'bar', 'baz' => 'ban')), function (array $data) {
- return $data;
- });
- $paginator = Mockery::mock('League\Fractal\Pagination\IlluminatePaginatorAdapter')->makePartial();
- $total = 100;
- $perPage = $count = 5;
- $currentPage = 2;
- $lastPage = 20;
- $paginator->shouldReceive('getTotal')->once()->andReturn($total);
- $paginator->shouldReceive('getCount')->once()->andReturn($count);
- $paginator->shouldReceive('getPerPage')->once()->andReturn($perPage);
- $paginator->shouldReceive('getCurrentPage')->once()->andReturn($currentPage);
- $paginator->shouldReceive('getLastPage')->once()->andReturn($lastPage);
- $paginator->shouldReceive('getUrl')->times(2)->andReturnUsing(function ($page) {
- return 'http://example.com/foo?page='.$page;
- });
- $collection->setPaginator($paginator);
- $rootScope = $manager->createData($collection);
- $expectedOutput = array(
- 'meta' => array(
- 'pagination' => array(
- 'total' => $total,
- 'count' => $count,
- 'per_page' => $perPage,
- 'current_page' => $currentPage,
- 'total_pages' => $lastPage,
- 'links' => array(
- 'previous' => 'http://example.com/foo?page=1',
- 'next' => 'http://example.com/foo?page=3',
- ),
- ),
- ),
- 'data' => array(
- array(
- 'foo' => 'bar',
- 'baz' => 'ban',
- ),
- )
- );
- $this->assertEquals($expectedOutput, $rootScope->toArray());
- }
- public function testCursorOutput()
- {
- $manager = new Manager();
- $inputData = array(
- array(
- 'foo' => 'bar',
- 'baz' => 'ban',
- )
- );
- $collection = new Collection($inputData, function (array $data) {
- return $data;
- });
- $cursor = new Cursor(0, 'ban', 'ban', 2);
- $collection->setCursor($cursor);
- $rootScope = $manager->createData($collection);
- $expectedOutput = array(
- 'meta' => array(
- 'cursor' => array(
- 'current' => 0,
- 'prev' => 'ban',
- 'next' => 'ban',
- 'count' => 2,
- ),
- ),
- 'data' => $inputData,
- );
- $this->assertEquals($expectedOutput, $rootScope->toArray());
- }
- 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());
- }
- public function tearDown()
- {
- Mockery::close();
- }
- }
|