TransformerAbstractTest.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. <?php namespace League\Fractal\Test;
  2. use League\Fractal\Manager;
  3. use League\Fractal\Resource\Collection;
  4. use League\Fractal\Resource\Item;
  5. use League\Fractal\Scope;
  6. use Mockery as m;
  7. use PHPUnit\Framework\TestCase;
  8. class TransformerAbstractTest extends TestCase
  9. {
  10. /**
  11. * @covers \League\Fractal\TransformerAbstract::setAvailableIncludes
  12. */
  13. public function testSetAvailableIncludes()
  14. {
  15. $transformer = m::mock('League\Fractal\TransformerAbstract')->makePartial();
  16. $this->assertInstanceOf('League\Fractal\TransformerAbstract', $transformer->setAvailableIncludes(['foo']));
  17. }
  18. /**
  19. * @covers \League\Fractal\TransformerAbstract::getAvailableIncludes
  20. */
  21. public function testGetAvailableIncludes()
  22. {
  23. $transformer = m::mock('League\Fractal\TransformerAbstract')->makePartial();
  24. $transformer->setAvailableIncludes(['foo', 'bar']);
  25. $this->assertSame(['foo', 'bar'], $transformer->getAvailableIncludes());
  26. }
  27. /**
  28. * @covers \League\Fractal\TransformerAbstract::setDefaultIncludes
  29. */
  30. public function testSetDefaultIncludes()
  31. {
  32. $transformer = m::mock('League\Fractal\TransformerAbstract')->makePartial();
  33. $this->assertInstanceOf('League\Fractal\TransformerAbstract', $transformer->setDefaultIncludes(['foo']));
  34. }
  35. /**
  36. * @covers \League\Fractal\TransformerAbstract::getDefaultIncludes
  37. */
  38. public function testGetDefaultIncludes()
  39. {
  40. $transformer = m::mock('League\Fractal\TransformerAbstract')->makePartial();
  41. $transformer->setDefaultIncludes(['foo', 'bar']);
  42. $this->assertSame(['foo', 'bar'], $transformer->getDefaultIncludes());
  43. }
  44. /**
  45. * @covers \League\Fractal\TransformerAbstract::setCurrentScope
  46. */
  47. public function testSetCurrentScope()
  48. {
  49. $transformer = $this->getMockForAbstractClass('League\Fractal\TransformerAbstract');
  50. $manager = new Manager();
  51. $scope = new Scope($manager, m::mock('League\Fractal\Resource\ResourceAbstract'));
  52. $this->assertInstanceOf('League\Fractal\TransformerAbstract', $transformer->setCurrentScope($scope));
  53. }
  54. /**
  55. * @covers \League\Fractal\TransformerAbstract::getCurrentScope
  56. */
  57. public function testGetCurrentScope()
  58. {
  59. $transformer = m::mock('League\Fractal\TransformerAbstract')->makePartial();
  60. $manager = new Manager();
  61. $scope = new Scope($manager, m::mock('League\Fractal\Resource\ResourceAbstract'));
  62. $transformer->setCurrentScope($scope);
  63. $this->assertSame($transformer->getCurrentScope(), $scope);
  64. }
  65. public function testProcessEmbeddedResourcesNoAvailableIncludes()
  66. {
  67. $transformer = m::mock('League\Fractal\TransformerAbstract')->makePartial();
  68. $manager = new Manager();
  69. $manager->parseIncludes('foo');
  70. $scope = new Scope($manager, m::mock('League\Fractal\Resource\ResourceAbstract'));
  71. $this->assertFalse($transformer->processIncludedResources($scope, ['some' => 'data']));
  72. }
  73. public function testProcessEmbeddedResourcesNoDefaultIncludes()
  74. {
  75. $transformer = m::mock('League\Fractal\TransformerAbstract')->makePartial();
  76. $manager = new Manager();
  77. $manager->parseIncludes('foo');
  78. $scope = new Scope($manager, m::mock('League\Fractal\Resource\ResourceAbstract'));
  79. $this->assertFalse($transformer->processIncludedResources($scope, ['some' => 'data']));
  80. }
  81. /**
  82. * @covers \League\Fractal\TransformerAbstract::processIncludedResources
  83. * @covers \League\Fractal\TransformerAbstract::callIncludeMethod
  84. * @expectedException \BadMethodCallException
  85. */
  86. public function testProcessEmbeddedResourcesInvalidAvailableEmbed()
  87. {
  88. $transformer = m::mock('League\Fractal\TransformerAbstract')->makePartial();
  89. $manager = new Manager();
  90. $manager->parseIncludes('book');
  91. $scope = new Scope($manager, m::mock('League\Fractal\Resource\ResourceAbstract'));
  92. $transformer->setCurrentScope($scope);
  93. $transformer->setAvailableIncludes(['book']);
  94. $transformer->processIncludedResources($scope, []);
  95. }
  96. /**
  97. * @covers \League\Fractal\TransformerAbstract::processIncludedResources
  98. * @covers \League\Fractal\TransformerAbstract::callIncludeMethod
  99. * @expectedException \BadMethodCallException
  100. */
  101. public function testProcessEmbeddedResourcesInvalidDefaultEmbed()
  102. {
  103. $transformer = m::mock('League\Fractal\TransformerAbstract')->makePartial();
  104. $manager = new Manager();
  105. $scope = new Scope($manager, m::mock('League\Fractal\Resource\ResourceAbstract'));
  106. $transformer->setDefaultIncludes(['book']);
  107. $transformer->processIncludedResources($scope, []);
  108. }
  109. /**
  110. * @covers \League\Fractal\TransformerAbstract::processIncludedResources
  111. * @covers \League\Fractal\TransformerAbstract::callIncludeMethod
  112. */
  113. public function testProcessIncludedAvailableResources()
  114. {
  115. $manager = new Manager();
  116. $manager->parseIncludes('book');
  117. $transformer = m::mock('League\Fractal\TransformerAbstract[transform]');
  118. $transformer->shouldReceive('includeBook')->once()->andReturnUsing(function ($data) {
  119. return new Item(['included' => 'thing'], function ($data) {
  120. return $data;
  121. });
  122. });
  123. $transformer->setAvailableIncludes(['book', 'publisher']);
  124. $scope = new Scope($manager, new Item([], $transformer));
  125. $included = $transformer->processIncludedResources($scope, ['meh']);
  126. $this->assertSame(['book' => ['data' => ['included' => 'thing']]], $included);
  127. }
  128. /**
  129. * @covers \League\Fractal\TransformerAbstract::processIncludedResources
  130. * @covers \League\Fractal\TransformerAbstract::figureOutWhichIncludes
  131. */
  132. public function testProcessExcludedAvailableResources()
  133. {
  134. $manager = new Manager();
  135. $transformer = m::mock('League\Fractal\TransformerAbstract[transform]');
  136. $scope = new Scope($manager, new Item([], $transformer));
  137. $transformer->shouldReceive('includeBook')->never();
  138. $transformer->shouldReceive('includePublisher')->once()->andReturnUsing(function ($data) {
  139. return new Item(['another' => 'thing'], function ($data) {
  140. return $data;
  141. });
  142. });
  143. // available includes that have been requested are excluded
  144. $manager->parseIncludes('book,publisher');
  145. $manager->parseExcludes('book');
  146. $transformer->setAvailableIncludes(['book', 'publisher']);
  147. $included = $transformer->processIncludedResources($scope, ['meh']);
  148. $this->assertSame(['publisher' => ['data' => ['another' => 'thing']]], $included);
  149. }
  150. /**
  151. * @covers \League\Fractal\TransformerAbstract::processIncludedResources
  152. * @covers \League\Fractal\TransformerAbstract::figureOutWhichIncludes
  153. */
  154. public function testProcessExcludedDefaultResources()
  155. {
  156. $manager = new Manager();
  157. $transformer = m::mock('League\Fractal\TransformerAbstract[transform]');
  158. $scope = new Scope($manager, new Item([], $transformer));
  159. $transformer->shouldReceive('includeBook')->never();
  160. $transformer->shouldReceive('includePublisher')->once()->andReturnUsing(function ($data) {
  161. return new Item(['another' => 'thing'], function ($data) {
  162. return $data;
  163. });
  164. });
  165. $manager->parseIncludes('book,publisher');
  166. $manager->parseExcludes('book');
  167. $transformer->setDefaultIncludes(['book', 'publisher']);
  168. $included = $transformer->processIncludedResources($scope, ['meh']);
  169. $this->assertSame(['publisher' => ['data' => ['another' => 'thing']]], $included);
  170. }
  171. /**
  172. * @covers \League\Fractal\TransformerAbstract::processIncludedResources
  173. * @covers \League\Fractal\TransformerAbstract::callIncludeMethod
  174. */
  175. public function testProcessIncludedAvailableResourcesEmptyEmbed()
  176. {
  177. $manager = new Manager();
  178. $manager->parseIncludes(['book']);
  179. $transformer = m::mock('League\Fractal\TransformerAbstract[transform]');
  180. $transformer->shouldReceive('includeBook')->once()->andReturn(null);
  181. $transformer->setAvailableIncludes(['book']);
  182. $scope = new Scope($manager, new Item([], $transformer));
  183. $included = $transformer->processIncludedResources($scope, ['meh']);
  184. $this->assertFalse($included);
  185. }
  186. /**
  187. * @covers \League\Fractal\TransformerAbstract::callIncludeMethod
  188. * @expectedException \Exception
  189. * @expectedExceptionMessage Invalid return value from League\Fractal\TransformerAbstract::includeBook().
  190. */
  191. public function testCallEmbedMethodReturnsCrap()
  192. {
  193. $manager = new Manager();
  194. $manager->parseIncludes('book');
  195. $transformer = m::mock('League\Fractal\TransformerAbstract[transform]');
  196. $transformer->shouldReceive('includeBook')->once()->andReturn(new \stdClass());
  197. $transformer->setAvailableIncludes(['book']);
  198. $scope = new Scope($manager, new Item([], $transformer));
  199. $transformer->processIncludedResources($scope, ['meh']);
  200. }
  201. /**
  202. * @covers \League\Fractal\TransformerAbstract::processIncludedResources
  203. * @covers \League\Fractal\TransformerAbstract::callIncludeMethod
  204. */
  205. public function testProcessEmbeddedDefaultResources()
  206. {
  207. $manager = new Manager();
  208. $transformer = m::mock('League\Fractal\TransformerAbstract[transform]');
  209. $transformer->shouldReceive('includeBook')->once()->andReturnUsing(function ($data) {
  210. return new Item(['included' => 'thing'], function ($data) {
  211. return $data;
  212. });
  213. });
  214. $transformer->setDefaultIncludes(['book']);
  215. $scope = new Scope($manager, new Item([], $transformer));
  216. $included = $transformer->processIncludedResources($scope, ['meh']);
  217. $this->assertSame(['book' => ['data' => ['included' => 'thing']]], $included);
  218. }
  219. /**
  220. * @covers \League\Fractal\TransformerAbstract::processIncludedResources
  221. * @covers \League\Fractal\TransformerAbstract::callIncludeMethod
  222. */
  223. public function testIncludedItem()
  224. {
  225. $manager = new Manager();
  226. $manager->parseIncludes('book');
  227. $transformer = m::mock('League\Fractal\TransformerAbstract[transform]');
  228. $transformer->shouldReceive('includeBook')->once()->andReturnUsing(function ($data) {
  229. return new Item(['included' => 'thing'], function ($data) {
  230. return $data;
  231. });
  232. });
  233. $transformer->setAvailableIncludes(['book']);
  234. $scope = new Scope($manager, new Item([], $transformer));
  235. $included = $transformer->processIncludedResources($scope, ['meh']);
  236. $this->assertSame(['book' => ['data' => ['included' => 'thing']]], $included);
  237. }
  238. public function testParamBagIsProvidedForIncludes()
  239. {
  240. $manager = new Manager();
  241. $manager->parseIncludes('book:foo(bar)');
  242. $transformer = m::mock('League\Fractal\TransformerAbstract')->makePartial();
  243. $transformer->shouldReceive('includeBook')
  244. ->with(m::any(), m::type('\League\Fractal\ParamBag'))
  245. ->once();
  246. $transformer->setAvailableIncludes(['book']);
  247. $scope = new Scope($manager, new Item([], $transformer));
  248. $included = $transformer->processIncludedResources($scope, []);
  249. }
  250. /**
  251. * @covers \League\Fractal\TransformerAbstract::processIncludedResources
  252. * @covers \League\Fractal\TransformerAbstract::callIncludeMethod
  253. */
  254. public function testIncludedCollection()
  255. {
  256. $manager = new Manager();
  257. $manager->parseIncludes('book');
  258. $collectionData = [
  259. ['included' => 'thing'],
  260. ['another' => 'thing'],
  261. ];
  262. $transformer = m::mock('League\Fractal\TransformerAbstract[transform]');
  263. $transformer->shouldReceive('includeBook')->once()->andReturnUsing(function ($data) use ($collectionData) {
  264. return new Collection($collectionData, function ($data) {
  265. return $data;
  266. });
  267. });
  268. $transformer->setAvailableIncludes(['book']);
  269. $scope = new Scope($manager, new Collection([], $transformer));
  270. $included = $transformer->processIncludedResources($scope, ['meh']);
  271. $this->assertSame(['book' => ['data' => $collectionData]], $included);
  272. }
  273. /**
  274. * @covers \League\Fractal\TransformerAbstract::processIncludedResources
  275. * @covers \League\Fractal\TransformerAbstract::callIncludeMethod
  276. */
  277. public function testProcessEmbeddedDefaultResourcesEmptyEmbed()
  278. {
  279. $transformer = m::mock('League\Fractal\TransformerAbstract[transform]');
  280. $transformer->shouldReceive('includeBook')->once()->andReturn(null);
  281. $transformer->setDefaultIncludes(['book']);
  282. $scope = new Scope(new Manager(), new Item([], $transformer));
  283. $included = $transformer->processIncludedResources($scope, ['meh']);
  284. $this->assertFalse($included);
  285. }
  286. /**
  287. * @covers \League\Fractal\TransformerAbstract::item
  288. */
  289. public function testItem()
  290. {
  291. $mock = m::mock('League\Fractal\TransformerAbstract');
  292. $item = $mock->item([], function () {
  293. });
  294. $this->assertInstanceOf('League\Fractal\Resource\Item', $item);
  295. }
  296. /**
  297. * @covers \League\Fractal\TransformerAbstract::collection
  298. */
  299. public function testCollection()
  300. {
  301. $mock = m::mock('League\Fractal\TransformerAbstract');
  302. $collection = $mock->collection([], function () {
  303. });
  304. $this->assertInstanceOf('League\Fractal\Resource\Collection', $collection);
  305. }
  306. public function tearDown()
  307. {
  308. m::close();
  309. }
  310. }