TransformerAbstractTest.php 12 KB

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