ScopeTest.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731
  1. <?php namespace League\Fractal\Test;
  2. use League\Fractal\Manager;
  3. use League\Fractal\Pagination\Cursor;
  4. use League\Fractal\Resource\Collection;
  5. use League\Fractal\Resource\Item;
  6. use League\Fractal\Resource\NullResource;
  7. use League\Fractal\Resource\Primitive;
  8. use League\Fractal\Scope;
  9. use League\Fractal\Serializer\ArraySerializer;
  10. use League\Fractal\Test\Stub\ArraySerializerWithNull;
  11. use League\Fractal\Test\Stub\Transformer\DefaultIncludeBookTransformer;
  12. use League\Fractal\Test\Stub\Transformer\NullIncludeBookTransformer;
  13. use League\Fractal\Test\Stub\Transformer\PrimitiveIncludeBookTransformer;
  14. use Mockery;
  15. use PHPUnit\Framework\TestCase;
  16. class ScopeTest extends TestCase
  17. {
  18. protected $simpleItem = ['foo' => 'bar'];
  19. protected $simpleCollection = [['foo' => 'bar']];
  20. public function testEmbedChildScope()
  21. {
  22. $manager = new Manager();
  23. $resource = new Item(['foo' => 'bar'], function () {
  24. });
  25. $scope = new Scope($manager, $resource, 'book');
  26. $this->assertSame($scope->getScopeIdentifier(), 'book');
  27. $childScope = $scope->embedChildScope('author', $resource);
  28. $this->assertInstanceOf('League\Fractal\Scope', $childScope);
  29. }
  30. public function testGetManager()
  31. {
  32. $resource = new Item(['foo' => 'bar'], function () {
  33. });
  34. $scope = new Scope(new Manager(), $resource, 'book');
  35. $this->assertInstanceOf('League\Fractal\Manager', $scope->getManager());
  36. }
  37. public function testGetResource()
  38. {
  39. $resource = new Item(['foo' => 'bar'], function () {
  40. });
  41. $scope = new Scope(new Manager(), $resource, 'book');
  42. $this->assertInstanceOf('League\Fractal\Resource\ResourceAbstract', $scope->getResource());
  43. $this->assertInstanceOf('League\Fractal\Resource\Item', $scope->getResource());
  44. }
  45. /**
  46. * @covers \League\Fractal\Scope::toArray
  47. */
  48. public function testToArray()
  49. {
  50. $manager = new Manager();
  51. $resource = new Item(['foo' => 'bar'], function ($data) {
  52. return $data;
  53. });
  54. $scope = new Scope($manager, $resource);
  55. $this->assertSame(['data' => ['foo' => 'bar']], $scope->toArray());
  56. }
  57. public function testToJson()
  58. {
  59. $data = [
  60. 'foo' => 'bar',
  61. ];
  62. $manager = new Manager();
  63. $resource = new Item($data, function ($data) {
  64. return $data;
  65. });
  66. $scope = new Scope($manager, $resource);
  67. $expected = json_encode([
  68. 'data' => $data,
  69. ]);
  70. $this->assertSame($expected, $scope->toJson());
  71. }
  72. public function testToJsonWithOption()
  73. {
  74. $data = [
  75. 'foo' => 'bar',
  76. ];
  77. $manager = new Manager();
  78. $resource = new Item($data, function ($data) {
  79. return $data;
  80. });
  81. $scope = new Scope($manager, $resource);
  82. $expected = json_encode([
  83. 'data' => $data,
  84. ], JSON_PRETTY_PRINT);
  85. $this->assertSame($expected, $scope->toJson(JSON_PRETTY_PRINT));
  86. }
  87. public function testGetCurrentScope()
  88. {
  89. $manager = new Manager();
  90. $resource = new Item(['name' => 'Larry Ullman'], function () {
  91. });
  92. $scope = new Scope($manager, $resource, 'book');
  93. $this->assertSame('book', $scope->getScopeIdentifier());
  94. $childScope = $scope->embedChildScope('author', $resource);
  95. $this->assertSame('author', $childScope->getScopeIdentifier());
  96. $grandChildScope = $childScope->embedChildScope('profile', $resource);
  97. $this->assertSame('profile', $grandChildScope->getScopeIdentifier());
  98. }
  99. public function testGetIdentifier()
  100. {
  101. $manager = new Manager();
  102. $resource = new Item(['name' => 'Larry Ullman'], function () {
  103. });
  104. $scope = new Scope($manager, $resource, 'book');
  105. $this->assertSame('book', $scope->getIdentifier());
  106. $childScope = $scope->embedChildScope('author', $resource);
  107. $this->assertSame('book.author', $childScope->getIdentifier());
  108. $grandChildScope = $childScope->embedChildScope('profile', $resource);
  109. $this->assertSame('book.author.profile', $grandChildScope->getIdentifier());
  110. }
  111. public function testGetParentScopes()
  112. {
  113. $manager = new Manager();
  114. $resource = new Item(['name' => 'Larry Ullman'], function () {
  115. });
  116. $scope = new Scope($manager, $resource, 'book');
  117. $childScope = $scope->embedChildScope('author', $resource);
  118. $this->assertSame(['book'], $childScope->getParentScopes());
  119. $grandChildScope = $childScope->embedChildScope('profile', $resource);
  120. $this->assertSame(['book', 'author'], $grandChildScope->getParentScopes());
  121. }
  122. public function testIsRequested()
  123. {
  124. $manager = new Manager();
  125. $manager->parseIncludes(['foo', 'bar', 'baz.bart']);
  126. $scope = new Scope($manager, Mockery::mock('League\Fractal\Resource\ResourceAbstract'));
  127. $this->assertTrue($scope->isRequested('foo'));
  128. $this->assertTrue($scope->isRequested('bar'));
  129. $this->assertTrue($scope->isRequested('baz'));
  130. $this->assertTrue($scope->isRequested('baz.bart'));
  131. $this->assertFalse($scope->isRequested('nope'));
  132. $childScope = $scope->embedChildScope('baz', Mockery::mock('League\Fractal\Resource\ResourceAbstract'));
  133. $this->assertTrue($childScope->isRequested('bart'));
  134. $this->assertFalse($childScope->isRequested('foo'));
  135. $this->assertFalse($childScope->isRequested('bar'));
  136. $this->assertFalse($childScope->isRequested('baz'));
  137. }
  138. public function testIsExcluded()
  139. {
  140. $manager = new Manager();
  141. $manager->parseIncludes(['foo', 'bar', 'baz.bart']);
  142. $scope = new Scope($manager, Mockery::mock('League\Fractal\Resource\ResourceAbstract'));
  143. $childScope = $scope->embedChildScope('baz', Mockery::mock('League\Fractal\Resource\ResourceAbstract'));
  144. $manager->parseExcludes('bar');
  145. $this->assertFalse($scope->isExcluded('foo'));
  146. $this->assertTrue($scope->isExcluded('bar'));
  147. $this->assertFalse($scope->isExcluded('baz.bart'));
  148. $manager->parseExcludes('baz.bart');
  149. $this->assertFalse($scope->isExcluded('baz'));
  150. $this->assertTrue($scope->isExcluded('baz.bart'));
  151. }
  152. /**
  153. * @expectedException \InvalidArgumentException
  154. */
  155. public function testScopeRequiresConcreteImplementation()
  156. {
  157. $manager = new Manager();
  158. $manager->parseIncludes('book');
  159. $resource = Mockery::mock('League\Fractal\Resource\ResourceAbstract', [
  160. ['bar' => 'baz'],
  161. function () {},
  162. ])->makePartial();
  163. $scope = new Scope($manager, $resource);
  164. $scope->toArray();
  165. }
  166. public function testToArrayWithIncludes()
  167. {
  168. $manager = new Manager();
  169. $manager->parseIncludes('book,price');
  170. $transformer = Mockery::mock('League\Fractal\TransformerAbstract')->makePartial();
  171. $transformer->shouldReceive('getAvailableIncludes')->twice()->andReturn(['book']);
  172. $transformer->shouldReceive('transform')->once()->andReturnUsing(function (array $data) {
  173. return $data;
  174. });
  175. $transformer
  176. ->shouldReceive('processIncludedResources')
  177. ->once()
  178. ->andReturn(['book' => ['yin' => 'yang'], 'price' => 99]);
  179. $resource = new Item(['bar' => 'baz'], $transformer);
  180. $scope = new Scope($manager, $resource);
  181. $this->assertSame(['data' => ['bar' => 'baz', 'book' => ['yin' => 'yang'], 'price' => 99]], $scope->toArray());
  182. }
  183. public function testToArrayWithNumericKeysPreserved()
  184. {
  185. $manager = new Manager();
  186. $manager->setSerializer(new ArraySerializer());
  187. $resource = new Item(['1' => 'First', '2' => 'Second'], function ($data) {
  188. return $data;
  189. });
  190. $scope = new Scope($manager, $resource);
  191. $this->assertSame(['1' => 'First', '2' => 'Second'], $scope->toArray());
  192. }
  193. public function testToArrayWithSideloadedIncludes()
  194. {
  195. $serializer = Mockery::mock('League\Fractal\Serializer\ArraySerializer')->makePartial();
  196. $serializer->shouldReceive('sideloadIncludes')->andReturn(true);
  197. $serializer->shouldReceive('item')->andReturnUsing(function ($key, $data) {
  198. return ['data' => $data];
  199. });
  200. $serializer->shouldReceive('includedData')->andReturnUsing(function ($key, $data) {
  201. return ['sideloaded' => array_pop($data)];
  202. });
  203. $manager = new Manager();
  204. $manager->parseIncludes('book');
  205. $manager->setSerializer($serializer);
  206. $transformer = Mockery::mock('League\Fractal\TransformerAbstract')->makePartial();
  207. $transformer->shouldReceive('getAvailableIncludes')->twice()->andReturn(['book']);
  208. $transformer->shouldReceive('transform')->once()->andReturnUsing(function (array $data) {
  209. return $data;
  210. });
  211. $transformer->shouldReceive('processIncludedResources')->once()->andReturn(['book' => ['yin' => 'yang']]);
  212. $resource = new Item(['bar' => 'baz'], $transformer);
  213. $scope = new Scope($manager, $resource);
  214. $expected = [
  215. 'data' => ['bar' => 'baz'],
  216. 'sideloaded' => ['book' => ['yin' => 'yang']],
  217. ];
  218. $this->assertSame($expected, $scope->toArray());
  219. }
  220. public function testPushParentScope()
  221. {
  222. $manager = new Manager();
  223. $resource = new Item(['name' => 'Larry Ullman'], function () {
  224. });
  225. $scope = new Scope($manager, $resource);
  226. $this->assertSame(1, $scope->pushParentScope('book'));
  227. $this->assertSame(2, $scope->pushParentScope('author'));
  228. $this->assertSame(3, $scope->pushParentScope('profile'));
  229. $this->assertSame(['book', 'author', 'profile'], $scope->getParentScopes());
  230. }
  231. public function testRunAppropriateTransformerWithPrimitive()
  232. {
  233. $manager = new Manager();
  234. $transformer = Mockery::mock('League\Fractal\TransformerAbstract');
  235. $transformer->shouldReceive('transform')->once()->andReturn('simple string');
  236. $transformer->shouldReceive('setCurrentScope')->once()->andReturn([]);
  237. $transformer->shouldNotReceive('getAvailableIncludes');
  238. $transformer->shouldNotReceive('getDefaultIncludes');
  239. $resource = new Primitive('test', $transformer);
  240. $scope = $manager->createData($resource);
  241. $this->assertSame('simple string', $scope->transformPrimitiveResource());
  242. $resource = new Primitive(10, function ($x) {return $x + 10;});
  243. $scope = $manager->createData($resource);
  244. $this->assertSame(20, $scope->transformPrimitiveResource());
  245. }
  246. public function testRunAppropriateTransformerWithItem()
  247. {
  248. $manager = new Manager();
  249. $transformer = Mockery::mock('League\Fractal\TransformerAbstract');
  250. $transformer->shouldReceive('transform')->once()->andReturn($this->simpleItem);
  251. $transformer->shouldReceive('getAvailableIncludes')->once()->andReturn([]);
  252. $transformer->shouldReceive('getDefaultIncludes')->once()->andReturn([]);
  253. $transformer->shouldReceive('setCurrentScope')->once()->andReturn([]);
  254. $resource = new Item($this->simpleItem, $transformer);
  255. $scope = $manager->createData($resource);
  256. $this->assertSame(['data' => $this->simpleItem], $scope->toArray());
  257. }
  258. public function testRunAppropriateTransformerWithCollection()
  259. {
  260. $manager = new Manager();
  261. $transformer = Mockery::mock('League\Fractal\TransformerAbstract');
  262. $transformer->shouldReceive('transform')->once()->andReturn(['foo' => 'bar']);
  263. $transformer->shouldReceive('getAvailableIncludes')->once()->andReturn([]);
  264. $transformer->shouldReceive('getDefaultIncludes')->once()->andReturn([]);
  265. $transformer->shouldReceive('setCurrentScope')->once()->andReturn([]);
  266. $resource = new Collection([['foo' => 'bar']], $transformer);
  267. $scope = $manager->createData($resource);
  268. $this->assertSame(['data' => [['foo' => 'bar']]], $scope->toArray());
  269. }
  270. /**
  271. * @covers \League\Fractal\Scope::executeResourceTransformers
  272. * @expectedException \InvalidArgumentException
  273. * @expectedExceptionMessage Argument $resource should be an instance of League\Fractal\Resource\Item or League\Fractal\Resource\Collection
  274. */
  275. public function testCreateDataWithClassFuckKnows()
  276. {
  277. $manager = new Manager();
  278. $transformer = Mockery::mock('League\Fractal\TransformerAbstract')->makePartial();
  279. $resource = Mockery::mock('League\Fractal\Resource\ResourceAbstract', [$this->simpleItem, $transformer])->makePartial();
  280. $scope = $manager->createData($resource);
  281. $scope->toArray();
  282. }
  283. public function testPaginatorOutput()
  284. {
  285. $manager = new Manager();
  286. $collection = new Collection([['foo' => 'bar', 'baz' => 'ban']], function (array $data) {
  287. return $data;
  288. });
  289. $paginator = Mockery::mock('League\Fractal\Pagination\IlluminatePaginatorAdapter')->makePartial();
  290. $total = 100;
  291. $perPage = $count = 5;
  292. $currentPage = 2;
  293. $lastPage = 20;
  294. $paginator->shouldReceive('getTotal')->once()->andReturn($total);
  295. $paginator->shouldReceive('getCount')->once()->andReturn($count);
  296. $paginator->shouldReceive('getPerPage')->once()->andReturn($perPage);
  297. $paginator->shouldReceive('getCurrentPage')->once()->andReturn($currentPage);
  298. $paginator->shouldReceive('getLastPage')->once()->andReturn($lastPage);
  299. $paginator->shouldReceive('getUrl')->times(2)->andReturnUsing(function ($page) {
  300. return 'http://example.com/foo?page='.$page;
  301. });
  302. $collection->setPaginator($paginator);
  303. $rootScope = $manager->createData($collection);
  304. $expectedOutput = [
  305. 'data' => [
  306. [
  307. 'foo' => 'bar',
  308. 'baz' => 'ban',
  309. ],
  310. ],
  311. 'meta' => [
  312. 'pagination' => [
  313. 'total' => $total,
  314. 'count' => $count,
  315. 'per_page' => $perPage,
  316. 'current_page' => $currentPage,
  317. 'total_pages' => $lastPage,
  318. 'links' => [
  319. 'previous' => 'http://example.com/foo?page=1',
  320. 'next' => 'http://example.com/foo?page=3',
  321. ],
  322. ],
  323. ],
  324. ];
  325. $this->assertSame($expectedOutput, $rootScope->toArray());
  326. }
  327. public function testCursorOutput()
  328. {
  329. $manager = new Manager();
  330. $inputData = [
  331. [
  332. 'foo' => 'bar',
  333. 'baz' => 'ban',
  334. ],
  335. ];
  336. $collection = new Collection($inputData, function (array $data) {
  337. return $data;
  338. });
  339. $cursor = new Cursor(0, 'ban', 'ban', 2);
  340. $collection->setCursor($cursor);
  341. $rootScope = $manager->createData($collection);
  342. $expectedOutput = [
  343. 'data' => $inputData,
  344. 'meta' => [
  345. 'cursor' => [
  346. 'current' => 0,
  347. 'prev' => 'ban',
  348. 'next' => 'ban',
  349. 'count' => 2,
  350. ],
  351. ],
  352. ];
  353. $this->assertSame($expectedOutput, $rootScope->toArray());
  354. }
  355. public function testDefaultIncludeSuccess()
  356. {
  357. $manager = new Manager();
  358. $manager->setSerializer(new ArraySerializer());
  359. // Send this stub junk, it has a specific format anyhow
  360. $resource = new Item([], new DefaultIncludeBookTransformer());
  361. // Try without metadata
  362. $scope = new Scope($manager, $resource);
  363. $expected = [
  364. 'a' => 'b',
  365. 'author' => [
  366. 'c' => 'd',
  367. ],
  368. ];
  369. $this->assertSame($expected, $scope->toArray());
  370. }
  371. public function testPrimitiveResourceIncludeSuccess()
  372. {
  373. $manager = new Manager();
  374. $manager->setSerializer(new ArraySerializer());
  375. $resource = new Item(['price' => '49'], new PrimitiveIncludeBookTransformer);
  376. $scope = new Scope($manager, $resource);
  377. $expected = [
  378. 'a' => 'b',
  379. 'price' => 49,
  380. ];
  381. $this->assertSame($expected, $scope->toArray());
  382. }
  383. public function testNullResourceIncludeSuccess()
  384. {
  385. $manager = new Manager();
  386. $manager->setSerializer(new ArraySerializerWithNull);
  387. // Send this stub junk, it has a specific format anyhow
  388. $resource = new Item([], new NullIncludeBookTransformer);
  389. // Try without metadata
  390. $scope = new Scope($manager, $resource);
  391. $expected = [
  392. 'a' => 'b',
  393. 'author' => null,
  394. ];
  395. $this->assertSame($expected, $scope->toArray());
  396. }
  397. /**
  398. * @covers \League\Fractal\Scope::toArray
  399. */
  400. public function testNullResourceDataAndJustMeta()
  401. {
  402. $manager = new Manager();
  403. $manager->setSerializer(new ArraySerializerWithNull);
  404. $resource = new NullResource();
  405. $resource->setMeta(['foo' => 'bar']);
  406. $scope = new Scope($manager, $resource);
  407. $this->assertSame(['meta' => ['foo' => 'bar']], $scope->toArray());
  408. }
  409. /**
  410. * @covers \League\Fractal\Scope::toArray
  411. * @dataProvider fieldsetsProvider
  412. */
  413. public function testToArrayWithFieldsets($fieldsetsToParse, $expected)
  414. {
  415. $manager = new Manager();
  416. $resource = new Item(
  417. ['foo' => 'bar', 'baz' => 'qux'],
  418. function ($data) {
  419. return $data;
  420. },
  421. 'resourceName'
  422. );
  423. $scope = new Scope($manager, $resource);
  424. $manager->parseFieldsets($fieldsetsToParse);
  425. $this->assertSame($expected, $scope->toArray());
  426. }
  427. public function fieldsetsProvider()
  428. {
  429. return [
  430. [
  431. ['resourceName' => 'foo'],
  432. ['data' => ['foo' => 'bar']]
  433. ],
  434. [
  435. ['resourceName' => 'foo,baz'],
  436. ['data' => ['foo' => 'bar', 'baz' => 'qux']]
  437. ],
  438. [
  439. ['resourceName' => 'inexistentField'],
  440. ['data' => []]
  441. ]
  442. ];
  443. }
  444. /**
  445. * @covers \League\Fractal\Scope::toArray
  446. * @dataProvider fieldsetsWithMandatorySerializerFieldsProvider
  447. */
  448. public function testToArrayWithFieldsetsAndMandatorySerializerFields($fieldsetsToParse, $expected)
  449. {
  450. $serializer = Mockery::mock('League\Fractal\Serializer\DataArraySerializer')->makePartial();
  451. $serializer->shouldReceive('getMandatoryFields')->andReturn(['foo']);
  452. $resource = new Item(
  453. ['foo' => 'bar', 'baz' => 'qux'],
  454. function ($data) {
  455. return $data;
  456. },
  457. 'resourceName'
  458. );
  459. $manager = new Manager();
  460. $manager->setSerializer($serializer);
  461. $scope = new Scope($manager, $resource);
  462. $manager->parseFieldsets($fieldsetsToParse);
  463. $this->assertSame($expected, $scope->toArray());
  464. }
  465. public function fieldsetsWithMandatorySerializerFieldsProvider()
  466. {
  467. return [
  468. //Don't request for mandatory field
  469. [
  470. ['resourceName' => 'baz'],
  471. ['data' => ['foo' => 'bar', 'baz' => 'qux']]
  472. ],
  473. //Request required field anyway
  474. [
  475. ['resourceName' => 'foo,baz'],
  476. ['data' => ['foo' => 'bar', 'baz' => 'qux']]
  477. ]
  478. ];
  479. }
  480. /**
  481. * @dataProvider fieldsetsWithIncludesProvider
  482. */
  483. public function testToArrayWithIncludesAndFieldsets($fieldsetsToParse, $expected)
  484. {
  485. $transformer = $this->createTransformerWithIncludedResource('book', ['book' => ['yin' => 'yang']]);
  486. $resource = new Item(
  487. ['foo' => 'bar', 'baz' => 'qux'],
  488. $transformer,
  489. 'resourceName'
  490. );
  491. $manager = new Manager();
  492. $scope = new Scope($manager, $resource);
  493. $manager->parseIncludes('book');
  494. $manager->parseFieldsets($fieldsetsToParse);
  495. $this->assertSame($expected, $scope->toArray());
  496. }
  497. public function fieldsetsWithIncludesProvider()
  498. {
  499. return [
  500. //Included relation was not requested
  501. [
  502. ['resourceName' => 'foo'],
  503. ['data' => ['foo' => 'bar']]
  504. ],
  505. //Included relation was requested
  506. [
  507. ['resourceName' => 'foo,book', 'book' => 'yin'],
  508. ['data' => ['foo' => 'bar', 'book' => ['yin' => 'yang']]]
  509. ]
  510. ];
  511. }
  512. /**
  513. * @covers \League\Fractal\Scope::toArray
  514. * @dataProvider fieldsetsWithSideLoadIncludesProvider
  515. */
  516. public function testToArrayWithSideloadedIncludesAndFieldsets($fieldsetsToParse, $expected)
  517. {
  518. $serializer = Mockery::mock('League\Fractal\Serializer\DataArraySerializer')->makePartial();
  519. $serializer->shouldReceive('sideloadIncludes')->andReturn(true);
  520. $serializer->shouldReceive('item')->andReturnUsing(
  521. function ($key, $data) {
  522. return ['data' => $data];
  523. }
  524. );
  525. $serializer->shouldReceive('includedData')->andReturnUsing(
  526. function ($key, $data) {
  527. $data = array_pop($data);
  528. return empty($data) ? [] : ['sideloaded' => $data];
  529. }
  530. );
  531. $manager = new Manager();
  532. $manager->parseIncludes('book');
  533. $manager->setSerializer($serializer);
  534. $transformer = $this->createTransformerWithIncludedResource('book', ['book' => ['yin' => 'yang']]);
  535. $resource = new Item(['foo' => 'bar'], $transformer, 'resourceName');
  536. $scope = new Scope($manager, $resource);
  537. $manager->parseFieldsets($fieldsetsToParse);
  538. $this->assertSame($expected, $scope->toArray());
  539. }
  540. public function fieldsetsWithSideLoadIncludesProvider()
  541. {
  542. return [
  543. //Included relation was not requested
  544. [
  545. ['resourceName' => 'foo'],
  546. ['data' => ['foo' => 'bar']]
  547. ],
  548. //Included relation was requested
  549. [
  550. ['resourceName' => 'foo,book', 'book' => 'yin'],
  551. ['data' => ['foo' => 'bar'], 'sideloaded' => ['book' => ['yin' => 'yang']]]
  552. ]
  553. ];
  554. }
  555. public function tearDown()
  556. {
  557. Mockery::close();
  558. }
  559. protected function createTransformerWithIncludedResource($resourceName, $transformResult)
  560. {
  561. $transformer = Mockery::mock('League\Fractal\TransformerAbstract')->makePartial();
  562. $transformer->shouldReceive('getAvailableIncludes')->twice()->andReturn([$resourceName]);
  563. $transformer->shouldReceive('transform')->once()->andReturnUsing(
  564. function (array $data) {
  565. return $data;
  566. }
  567. );
  568. $transformer->shouldReceive('processIncludedResources')->once()->andReturn($transformResult);
  569. return $transformer;
  570. }
  571. }