ManagerTest.php 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. <?php namespace League\Fractal\Test;
  2. use League\Fractal\Manager;
  3. use League\Fractal\ParamBag;
  4. use League\Fractal\Resource\Collection;
  5. use League\Fractal\Resource\Item;
  6. use Mockery;
  7. use PHPUnit\Framework\TestCase;
  8. class ManagerTest extends TestCase
  9. {
  10. public function testParseIncludeSelfie()
  11. {
  12. $manager = new Manager();
  13. // Test that some includes provided returns self
  14. $this->assertInstanceOf(get_class($manager), $manager->parseIncludes(['foo']));
  15. }
  16. /**
  17. * @expectedException \InvalidArgumentException
  18. * @expectedExceptionMessage The parseIncludes() method expects a string or an array. NULL given
  19. */
  20. public function testInvalidParseInclude()
  21. {
  22. $manager = new Manager();
  23. $manager->parseIncludes(null);
  24. }
  25. /**
  26. * @expectedException \InvalidArgumentException
  27. * @expectedExceptionMessage The parseIncludes() method expects a string or an array. integer given
  28. */
  29. public function testIceTParseInclude()
  30. {
  31. $manager = new Manager();
  32. $manager->parseIncludes(99);
  33. }
  34. public function testParseIncludes()
  35. {
  36. $manager = new Manager();
  37. // Does a CSV string work
  38. $manager->parseIncludes('foo,bar');
  39. $this->assertSame(['foo', 'bar'], $manager->getRequestedIncludes());
  40. // Does a big array of stuff work
  41. $manager->parseIncludes(['foo', 'bar', 'bar.baz']);
  42. $this->assertSame(['foo', 'bar', 'bar.baz'], $manager->getRequestedIncludes());
  43. // Are repeated things stripped
  44. $manager->parseIncludes(['foo', 'foo', 'bar']);
  45. $this->assertSame(['foo', 'bar'], $manager->getRequestedIncludes());
  46. // Do requests for `baz.bart` also request `baz`?
  47. $manager->parseIncludes(['foo.bar']);
  48. $this->assertSame(['foo', 'foo.bar'], $manager->getRequestedIncludes());
  49. // See if fancy syntax works
  50. $manager->parseIncludes('foo:limit(5|1):order(-something):anotherparam');
  51. $params = $manager->getIncludeParams('foo');
  52. $this->assertInstanceOf('League\Fractal\ParamBag', $params);
  53. $this->assertSame(['5', '1'], $params['limit']);
  54. $this->assertSame(['-something'], $params['order']);
  55. $this->assertSame([''], $params['anotherparam']);
  56. $this->assertNull($params['totallymadeup']);
  57. }
  58. public function testParseExcludeSelfie()
  59. {
  60. $manager = new Manager();
  61. // Test that some excludes provided returns self
  62. $this->assertInstanceOf(get_class($manager), $manager->parseExcludes(['foo']));
  63. }
  64. /**
  65. * @expectedException \InvalidArgumentException
  66. * @expectedExceptionMessage The parseExcludes() method expects a string or an array. NULL given
  67. */
  68. public function testInvalidParseExclude()
  69. {
  70. $manager = new Manager();
  71. $manager->parseExcludes(null);
  72. }
  73. /**
  74. * @expectedException \InvalidArgumentException
  75. * @expectedExceptionMessage The parseExcludes() method expects a string or an array. integer given
  76. */
  77. public function testIceTParseExclude()
  78. {
  79. $manager = new Manager();
  80. $manager->parseExcludes(99);
  81. }
  82. public function testParseExcludes()
  83. {
  84. $manager = new Manager();
  85. // Does a CSV string work
  86. $manager->parseExcludes('foo,bar');
  87. $this->assertSame(['foo', 'bar'], $manager->getRequestedExcludes());
  88. // Does a big array of stuff work
  89. $manager->parseExcludes(['foo', 'bar', 'bar.baz']);
  90. $this->assertSame(['foo', 'bar', 'bar.baz'], $manager->getRequestedExcludes());
  91. // Are repeated things stripped
  92. $manager->parseExcludes(['foo', 'foo', 'bar']);
  93. $this->assertSame(['foo', 'bar'], $manager->getRequestedExcludes());
  94. // Do requests for `baz.bart` also request `baz`?
  95. $manager->parseExcludes(['foo.bar']);
  96. $this->assertSame(['foo.bar'], $manager->getRequestedExcludes());
  97. }
  98. public function testRecursionLimiting()
  99. {
  100. $manager = new Manager();
  101. // Should limit to 10 by default
  102. $manager->parseIncludes('a.b.c.d.e.f.g.h.i.j.NEVER');
  103. $this->assertSame(
  104. [
  105. 'a',
  106. 'a.b',
  107. 'a.b.c',
  108. 'a.b.c.d',
  109. 'a.b.c.d.e',
  110. 'a.b.c.d.e.f',
  111. 'a.b.c.d.e.f.g',
  112. 'a.b.c.d.e.f.g.h',
  113. 'a.b.c.d.e.f.g.h.i',
  114. 'a.b.c.d.e.f.g.h.i.j',
  115. ],
  116. $manager->getRequestedIncludes()
  117. );
  118. // Try setting to 3 and see what happens
  119. $manager->setRecursionLimit(3);
  120. $manager->parseIncludes('a.b.c.NEVER');
  121. $this->assertSame(
  122. [
  123. 'a',
  124. 'a.b',
  125. 'a.b.c',
  126. ],
  127. $manager->getRequestedIncludes()
  128. );
  129. }
  130. public function testCreateDataWithCallback()
  131. {
  132. $manager = new Manager();
  133. // Item
  134. $resource = new Item(['foo' => 'bar'], function (array $data) {
  135. return $data;
  136. });
  137. $rootScope = $manager->createData($resource);
  138. $this->assertInstanceOf('League\Fractal\Scope', $rootScope);
  139. $this->assertSame(['data' => ['foo' => 'bar']], $rootScope->toArray());
  140. $this->assertSame('{"data":{"foo":"bar"}}', $rootScope->toJson());
  141. // Collection
  142. $resource = new Collection([['foo' => 'bar']], function (array $data) {
  143. return $data;
  144. });
  145. $rootScope = $manager->createData($resource);
  146. $this->assertInstanceOf('League\Fractal\Scope', $rootScope);
  147. $this->assertSame(['data' => [['foo' => 'bar']]], $rootScope->toArray());
  148. $this->assertSame('{"data":[{"foo":"bar"}]}', $rootScope->toJson());
  149. }
  150. public function testParseFieldsets()
  151. {
  152. $manager = new Manager();
  153. $fields = [
  154. 'articles' => 'title,body',
  155. 'people' => 'name'
  156. ];
  157. $expectedFieldset = [
  158. 'articles' => ['title' , 'body'],
  159. 'people' => ['name']
  160. ];
  161. $manager->parseFieldsets($fields);
  162. $this->assertSame($expectedFieldset, $manager->getRequestedFieldsets());
  163. $paramBag = new ParamBag($expectedFieldset['articles']);
  164. $this->assertEquals($paramBag, $manager->getFieldset('articles'));
  165. // Are repeated fields stripped
  166. $manager->parseFieldsets(['foo' => 'bar,baz,bar']);
  167. $this->assertSame(['foo' => ['bar', 'baz']], $manager->getRequestedFieldsets());
  168. // Are empty fields stripped
  169. $manager->parseFieldsets(['foo' => 'bar,']);
  170. $this->assertSame(['foo' => ['bar']], $manager->getRequestedFieldsets());
  171. // Verify you can send in arrays directly
  172. $manager->parseFieldsets(['foo' => ['bar', 'baz']]);
  173. $this->assertSame(['foo' => ['bar', 'baz']], $manager->getRequestedFieldsets());
  174. $this->assertSame(null, $manager->getFieldset('inexistent'));
  175. }
  176. public function tearDown()
  177. {
  178. Mockery::close();
  179. }
  180. }