123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376 |
- <?php namespace League\Fractal\Test;
- use League\Fractal\Manager;
- use League\Fractal\Resource\Collection;
- use League\Fractal\Resource\Item;
- use League\Fractal\Scope;
- use Mockery as m;
- use PHPUnit\Framework\TestCase;
- class TransformerAbstractTest extends TestCase
- {
- /**
- * @covers \League\Fractal\TransformerAbstract::setAvailableIncludes
- */
- public function testSetAvailableIncludes()
- {
- $transformer = m::mock('League\Fractal\TransformerAbstract')->makePartial();
- $this->assertInstanceOf('League\Fractal\TransformerAbstract', $transformer->setAvailableIncludes(['foo']));
- }
- /**
- * @covers \League\Fractal\TransformerAbstract::getAvailableIncludes
- */
- public function testGetAvailableIncludes()
- {
- $transformer = m::mock('League\Fractal\TransformerAbstract')->makePartial();
- $transformer->setAvailableIncludes(['foo', 'bar']);
- $this->assertSame(['foo', 'bar'], $transformer->getAvailableIncludes());
- }
- /**
- * @covers \League\Fractal\TransformerAbstract::setDefaultIncludes
- */
- public function testSetDefaultIncludes()
- {
- $transformer = m::mock('League\Fractal\TransformerAbstract')->makePartial();
- $this->assertInstanceOf('League\Fractal\TransformerAbstract', $transformer->setDefaultIncludes(['foo']));
- }
- /**
- * @covers \League\Fractal\TransformerAbstract::getDefaultIncludes
- */
- public function testGetDefaultIncludes()
- {
- $transformer = m::mock('League\Fractal\TransformerAbstract')->makePartial();
- $transformer->setDefaultIncludes(['foo', 'bar']);
- $this->assertSame(['foo', 'bar'], $transformer->getDefaultIncludes());
- }
- /**
- * @covers \League\Fractal\TransformerAbstract::setCurrentScope
- */
- public function testSetCurrentScope()
- {
- $transformer = $this->getMockForAbstractClass('League\Fractal\TransformerAbstract');
- $manager = new Manager();
- $scope = new Scope($manager, m::mock('League\Fractal\Resource\ResourceAbstract'));
- $this->assertInstanceOf('League\Fractal\TransformerAbstract', $transformer->setCurrentScope($scope));
- }
- /**
- * @covers \League\Fractal\TransformerAbstract::getCurrentScope
- */
- public function testGetCurrentScope()
- {
- $transformer = m::mock('League\Fractal\TransformerAbstract')->makePartial();
- $manager = new Manager();
- $scope = new Scope($manager, m::mock('League\Fractal\Resource\ResourceAbstract'));
- $transformer->setCurrentScope($scope);
- $this->assertSame($transformer->getCurrentScope(), $scope);
- }
- public function testProcessEmbeddedResourcesNoAvailableIncludes()
- {
- $transformer = m::mock('League\Fractal\TransformerAbstract')->makePartial();
- $manager = new Manager();
- $manager->parseIncludes('foo');
- $scope = new Scope($manager, m::mock('League\Fractal\Resource\ResourceAbstract'));
- $this->assertFalse($transformer->processIncludedResources($scope, ['some' => 'data']));
- }
- public function testProcessEmbeddedResourcesNoDefaultIncludes()
- {
- $transformer = m::mock('League\Fractal\TransformerAbstract')->makePartial();
- $manager = new Manager();
- $manager->parseIncludes('foo');
- $scope = new Scope($manager, m::mock('League\Fractal\Resource\ResourceAbstract'));
- $this->assertFalse($transformer->processIncludedResources($scope, ['some' => 'data']));
- }
- /**
- * @covers \League\Fractal\TransformerAbstract::processIncludedResources
- * @covers \League\Fractal\TransformerAbstract::callIncludeMethod
- * @expectedException \BadMethodCallException
- */
- public function testProcessEmbeddedResourcesInvalidAvailableEmbed()
- {
- $transformer = m::mock('League\Fractal\TransformerAbstract')->makePartial();
- $manager = new Manager();
- $manager->parseIncludes('book');
- $scope = new Scope($manager, m::mock('League\Fractal\Resource\ResourceAbstract'));
- $transformer->setCurrentScope($scope);
- $transformer->setAvailableIncludes(['book']);
- $transformer->processIncludedResources($scope, []);
- }
- /**
- * @covers \League\Fractal\TransformerAbstract::processIncludedResources
- * @covers \League\Fractal\TransformerAbstract::callIncludeMethod
- * @expectedException \BadMethodCallException
- */
- public function testProcessEmbeddedResourcesInvalidDefaultEmbed()
- {
- $transformer = m::mock('League\Fractal\TransformerAbstract')->makePartial();
- $manager = new Manager();
- $scope = new Scope($manager, m::mock('League\Fractal\Resource\ResourceAbstract'));
- $transformer->setDefaultIncludes(['book']);
- $transformer->processIncludedResources($scope, []);
- }
- /**
- * @covers \League\Fractal\TransformerAbstract::processIncludedResources
- * @covers \League\Fractal\TransformerAbstract::callIncludeMethod
- */
- public function testProcessIncludedAvailableResources()
- {
- $manager = new Manager();
- $manager->parseIncludes('book');
- $transformer = m::mock('League\Fractal\TransformerAbstract[transform]');
- $transformer->shouldReceive('includeBook')->once()->andReturnUsing(function ($data) {
- return new Item(['included' => 'thing'], function ($data) {
- return $data;
- });
- });
- $transformer->setAvailableIncludes(['book', 'publisher']);
- $scope = new Scope($manager, new Item([], $transformer));
- $included = $transformer->processIncludedResources($scope, ['meh']);
- $this->assertSame(['book' => ['data' => ['included' => 'thing']]], $included);
- }
- /**
- * @covers \League\Fractal\TransformerAbstract::processIncludedResources
- * @covers \League\Fractal\TransformerAbstract::figureOutWhichIncludes
- */
- public function testProcessExcludedAvailableResources()
- {
- $manager = new Manager();
- $transformer = m::mock('League\Fractal\TransformerAbstract[transform]');
- $scope = new Scope($manager, new Item([], $transformer));
- $transformer->shouldReceive('includeBook')->never();
- $transformer->shouldReceive('includePublisher')->once()->andReturnUsing(function ($data) {
- return new Item(['another' => 'thing'], function ($data) {
- return $data;
- });
- });
- // available includes that have been requested are excluded
- $manager->parseIncludes('book,publisher');
- $manager->parseExcludes('book');
- $transformer->setAvailableIncludes(['book', 'publisher']);
- $included = $transformer->processIncludedResources($scope, ['meh']);
- $this->assertSame(['publisher' => ['data' => ['another' => 'thing']]], $included);
- }
- /**
- * @covers \League\Fractal\TransformerAbstract::processIncludedResources
- * @covers \League\Fractal\TransformerAbstract::figureOutWhichIncludes
- */
- public function testProcessExcludedDefaultResources()
- {
- $manager = new Manager();
- $transformer = m::mock('League\Fractal\TransformerAbstract[transform]');
- $scope = new Scope($manager, new Item([], $transformer));
- $transformer->shouldReceive('includeBook')->never();
- $transformer->shouldReceive('includePublisher')->once()->andReturnUsing(function ($data) {
- return new Item(['another' => 'thing'], function ($data) {
- return $data;
- });
- });
- $manager->parseIncludes('book,publisher');
- $manager->parseExcludes('book');
- $transformer->setDefaultIncludes(['book', 'publisher']);
- $included = $transformer->processIncludedResources($scope, ['meh']);
- $this->assertSame(['publisher' => ['data' => ['another' => 'thing']]], $included);
- }
- /**
- * @covers \League\Fractal\TransformerAbstract::processIncludedResources
- * @covers \League\Fractal\TransformerAbstract::callIncludeMethod
- */
- public function testProcessIncludedAvailableResourcesEmptyEmbed()
- {
- $manager = new Manager();
- $manager->parseIncludes(['book']);
- $transformer = m::mock('League\Fractal\TransformerAbstract[transform]');
- $transformer->shouldReceive('includeBook')->once()->andReturn(null);
- $transformer->setAvailableIncludes(['book']);
- $scope = new Scope($manager, new Item([], $transformer));
- $included = $transformer->processIncludedResources($scope, ['meh']);
- $this->assertFalse($included);
- }
- /**
- * @covers \League\Fractal\TransformerAbstract::callIncludeMethod
- * @expectedException \Exception
- * @expectedExceptionMessage Invalid return value from League\Fractal\TransformerAbstract::includeBook().
- */
- public function testCallEmbedMethodReturnsCrap()
- {
- $manager = new Manager();
- $manager->parseIncludes('book');
- $transformer = m::mock('League\Fractal\TransformerAbstract[transform]');
- $transformer->shouldReceive('includeBook')->once()->andReturn(new \stdClass());
- $transformer->setAvailableIncludes(['book']);
- $scope = new Scope($manager, new Item([], $transformer));
- $transformer->processIncludedResources($scope, ['meh']);
- }
- /**
- * @covers \League\Fractal\TransformerAbstract::processIncludedResources
- * @covers \League\Fractal\TransformerAbstract::callIncludeMethod
- */
- public function testProcessEmbeddedDefaultResources()
- {
- $manager = new Manager();
- $transformer = m::mock('League\Fractal\TransformerAbstract[transform]');
- $transformer->shouldReceive('includeBook')->once()->andReturnUsing(function ($data) {
- return new Item(['included' => 'thing'], function ($data) {
- return $data;
- });
- });
- $transformer->setDefaultIncludes(['book']);
- $scope = new Scope($manager, new Item([], $transformer));
- $included = $transformer->processIncludedResources($scope, ['meh']);
- $this->assertSame(['book' => ['data' => ['included' => 'thing']]], $included);
- }
- /**
- * @covers \League\Fractal\TransformerAbstract::processIncludedResources
- * @covers \League\Fractal\TransformerAbstract::callIncludeMethod
- */
- public function testIncludedItem()
- {
- $manager = new Manager();
- $manager->parseIncludes('book');
- $transformer = m::mock('League\Fractal\TransformerAbstract[transform]');
- $transformer->shouldReceive('includeBook')->once()->andReturnUsing(function ($data) {
- return new Item(['included' => 'thing'], function ($data) {
- return $data;
- });
- });
- $transformer->setAvailableIncludes(['book']);
- $scope = new Scope($manager, new Item([], $transformer));
- $included = $transformer->processIncludedResources($scope, ['meh']);
- $this->assertSame(['book' => ['data' => ['included' => 'thing']]], $included);
- }
- public function testParamBagIsProvidedForIncludes()
- {
- $manager = new Manager();
- $manager->parseIncludes('book:foo(bar)');
- $transformer = m::mock('League\Fractal\TransformerAbstract')->makePartial();
- $transformer->shouldReceive('includeBook')
- ->with(m::any(), m::type('\League\Fractal\ParamBag'))
- ->once();
- $transformer->setAvailableIncludes(['book']);
- $scope = new Scope($manager, new Item([], $transformer));
- $included = $transformer->processIncludedResources($scope, []);
- }
- /**
- * @covers \League\Fractal\TransformerAbstract::processIncludedResources
- * @covers \League\Fractal\TransformerAbstract::callIncludeMethod
- */
- public function testIncludedCollection()
- {
- $manager = new Manager();
- $manager->parseIncludes('book');
- $collectionData = [
- ['included' => 'thing'],
- ['another' => 'thing'],
- ];
- $transformer = m::mock('League\Fractal\TransformerAbstract[transform]');
- $transformer->shouldReceive('includeBook')->once()->andReturnUsing(function ($data) use ($collectionData) {
- return new Collection($collectionData, function ($data) {
- return $data;
- });
- });
- $transformer->setAvailableIncludes(['book']);
- $scope = new Scope($manager, new Collection([], $transformer));
- $included = $transformer->processIncludedResources($scope, ['meh']);
- $this->assertSame(['book' => ['data' => $collectionData]], $included);
- }
- /**
- * @covers \League\Fractal\TransformerAbstract::processIncludedResources
- * @covers \League\Fractal\TransformerAbstract::callIncludeMethod
- */
- public function testProcessEmbeddedDefaultResourcesEmptyEmbed()
- {
- $transformer = m::mock('League\Fractal\TransformerAbstract[transform]');
- $transformer->shouldReceive('includeBook')->once()->andReturn(null);
- $transformer->setDefaultIncludes(['book']);
- $scope = new Scope(new Manager(), new Item([], $transformer));
- $included = $transformer->processIncludedResources($scope, ['meh']);
- $this->assertFalse($included);
- }
- /**
- * @covers \League\Fractal\TransformerAbstract::item
- */
- public function testItem()
- {
- $mock = m::mock('League\Fractal\TransformerAbstract');
- $item = $mock->item([], function () {
- });
- $this->assertInstanceOf('League\Fractal\Resource\Item', $item);
- }
- /**
- * @covers \League\Fractal\TransformerAbstract::collection
- */
- public function testCollection()
- {
- $mock = m::mock('League\Fractal\TransformerAbstract');
- $collection = $mock->collection([], function () {
- });
- $this->assertInstanceOf('League\Fractal\Resource\Collection', $collection);
- }
- public function tearDown()
- {
- m::close();
- }
- }
|