ArraySerializerTest.php 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. <?php namespace League\Fractal\Test\Serializer;
  2. use League\Fractal\Manager;
  3. use League\Fractal\Resource\Collection;
  4. use League\Fractal\Resource\Item;
  5. use League\Fractal\Resource\NullResource;
  6. use League\Fractal\Scope;
  7. use League\Fractal\Serializer\ArraySerializer;
  8. use League\Fractal\Test\Stub\Transformer\GenericBookTransformer;
  9. use Mockery;
  10. use PHPUnit\Framework\TestCase;
  11. class ArraySerializerTest extends TestCase
  12. {
  13. private $bookItemInput = [
  14. 'title' => 'Foo',
  15. 'year' => '1991',
  16. '_author' => [
  17. 'name' => 'Dave',
  18. ],
  19. ];
  20. private $bookCollectionInput = [
  21. [
  22. 'title' => 'Foo',
  23. 'year' => '1991',
  24. '_author' => [
  25. 'name' => 'Dave',
  26. ],
  27. ],
  28. [
  29. 'title' => 'Bar',
  30. 'year' => '1997',
  31. '_author' => [
  32. 'name' => 'Bob',
  33. ],
  34. ],
  35. ];
  36. public function testSerializingItemResource()
  37. {
  38. $manager = new Manager();
  39. $manager->parseIncludes('author');
  40. $manager->setSerializer(new ArraySerializer());
  41. $resource = new Item($this->bookItemInput, new GenericBookTransformer(), 'book');
  42. // Try without metadata
  43. $scope = new Scope($manager, $resource);
  44. $expected = [
  45. 'title' => 'Foo',
  46. 'year' => 1991,
  47. 'author' => [
  48. 'name' => 'Dave',
  49. ],
  50. ];
  51. $this->assertSame($expected, $scope->toArray());
  52. //Test single field
  53. $manager->parseFieldsets(['book' => 'title']);
  54. $expected = ['title' => 'Foo'];
  55. $this->assertSame($expected, $scope->toArray());
  56. //Test multiple field
  57. $manager->parseFieldsets(['book' => 'title,year']);
  58. $expected = [
  59. 'title' => 'Foo',
  60. 'year' => 1991
  61. ];
  62. $this->assertSame($expected, $scope->toArray());
  63. //Test with relationship field
  64. $manager->parseFieldsets(['book' => 'title,author', 'author' => 'name']);
  65. $expected = [
  66. 'title' => 'Foo',
  67. 'author' => [
  68. 'name' => 'Dave'
  69. ],
  70. ];
  71. $this->assertSame($expected, $scope->toArray());
  72. //Clear all sparse fieldsets
  73. $manager->parseFieldsets([]);
  74. //Same again with meta
  75. $resource->setMetaValue('foo', 'bar');
  76. $scope = new Scope($manager, $resource);
  77. $expected = [
  78. 'title' => 'Foo',
  79. 'year' => 1991,
  80. 'author' => [
  81. 'name' => 'Dave'
  82. ],
  83. 'meta' => [
  84. 'foo' => 'bar'
  85. ]
  86. ];
  87. $this->assertSame($expected, $scope->toArray());
  88. //Test with relationship field
  89. $manager->parseFieldsets(['book' => 'title,author', 'author' => 'name']);
  90. $expected = [
  91. 'title' => 'Foo',
  92. 'author' => [
  93. 'name' => 'Dave',
  94. ],
  95. 'meta' => [
  96. 'foo' => 'bar',
  97. ]
  98. ];
  99. $this->assertSame($expected, $scope->toArray());
  100. }
  101. public function testSerializingCollectionResource()
  102. {
  103. $manager = new Manager();
  104. $manager->parseIncludes('author');
  105. $manager->setSerializer(new ArraySerializer());
  106. $resource = new Collection($this->bookCollectionInput, new GenericBookTransformer(), 'books');
  107. // Try without metadata
  108. $scope = new Scope($manager, $resource);
  109. $expected = [
  110. 'books' => [
  111. [
  112. 'title' => 'Foo',
  113. 'year' => 1991,
  114. 'author' => [
  115. 'name' => 'Dave',
  116. ],
  117. ],
  118. [
  119. 'title' => 'Bar',
  120. 'year' => 1997,
  121. 'author' => [
  122. 'name' => 'Bob',
  123. ],
  124. ],
  125. ],
  126. ];
  127. $this->assertSame($expected, $scope->toArray());
  128. // JSON array of JSON objects
  129. $expectedJson = '{"books":[{"title":"Foo","year":1991,"author":{"name":"Dave"}},{"title":"Bar","year":1997,"author":{"name":"Bob"}}]}';
  130. $this->assertSame($expectedJson, $scope->toJson());
  131. //Test single field
  132. $manager->parseFieldsets(['books' => 'title']);
  133. $expected = [
  134. 'books' => [
  135. ['title' => 'Foo'],
  136. ['title' => 'Bar']
  137. ]
  138. ];
  139. $this->assertSame($expected, $scope->toArray());
  140. //Test multiple field
  141. $manager->parseFieldsets(['books' => 'title,year']);
  142. $expected = [
  143. 'books' => [
  144. [
  145. 'title' => 'Foo',
  146. 'year' => 1991
  147. ],
  148. [
  149. 'title' => 'Bar',
  150. 'year' => 1997
  151. ]
  152. ]
  153. ];
  154. $this->assertSame($expected, $scope->toArray());
  155. //Test with relationship field
  156. $manager->parseFieldsets(['books' => 'title,author', 'author' => 'name']);
  157. $expected = [
  158. 'books' => [
  159. [
  160. 'title' => 'Foo',
  161. 'author' => [
  162. 'name' => 'Dave'
  163. ]
  164. ],
  165. [
  166. 'title' => 'Bar',
  167. 'author' => [
  168. 'name' => 'Bob'
  169. ]
  170. ]
  171. ]
  172. ];
  173. $this->assertSame($expected, $scope->toArray());
  174. //Clear all sparse fieldsets
  175. $manager->parseFieldsets([]);
  176. // Same again with metadata
  177. $resource->setMetaValue('foo', 'bar');
  178. $scope = new Scope($manager, $resource);
  179. $expected = [
  180. 'books' => [
  181. [
  182. 'title' => 'Foo',
  183. 'year' => 1991,
  184. 'author' => [
  185. 'name' => 'Dave',
  186. ],
  187. ],
  188. [
  189. 'title' => 'Bar',
  190. 'year' => 1997,
  191. 'author' => [
  192. 'name' => 'Bob',
  193. ],
  194. ],
  195. ],
  196. 'meta' => [
  197. 'foo' => 'bar',
  198. ],
  199. ];
  200. $this->assertSame($expected, $scope->toArray());
  201. $expectedJson = '{"books":[{"title":"Foo","year":1991,"author":{"name":"Dave"}},{"title":"Bar","year":1997,"author":{"name":"Bob"}}],"meta":{"foo":"bar"}}';
  202. $this->assertSame($expectedJson, $scope->toJson());
  203. $manager->parseFieldsets(['books' => 'title,author', 'author' => 'name']);
  204. $expected = [
  205. 'books' => [
  206. [
  207. 'title' => 'Foo',
  208. 'author' => [
  209. 'name' => 'Dave'
  210. ]
  211. ],
  212. [
  213. 'title' => 'Bar',
  214. 'author' => [
  215. 'name' => 'Bob'
  216. ]
  217. ]
  218. ],
  219. 'meta' => [
  220. 'foo' => 'bar',
  221. ]
  222. ];
  223. $this->assertSame($expected, $scope->toArray());
  224. }
  225. public function testSerializingNullResource()
  226. {
  227. $manager = new Manager();
  228. $manager->parseIncludes('author');
  229. $manager->setSerializer(new ArraySerializer());
  230. $resource = new NullResource($this->bookCollectionInput, new GenericBookTransformer(), 'books');
  231. // Try without metadata
  232. $scope = new Scope($manager, $resource);
  233. $expected = [];
  234. $this->assertSame($expected, $scope->toArray());
  235. // JSON array of JSON objects
  236. $expectedJson = '[]';
  237. $this->assertSame($expectedJson, $scope->toJson());
  238. //Test single field
  239. $manager->parseFieldsets(['books' => 'title']);
  240. $this->assertSame($expected, $scope->toArray());
  241. //Test multiple fields
  242. $manager->parseFieldsets(['books' => 'title,year']);
  243. $this->assertSame($expected, $scope->toArray());
  244. //Test with relationship
  245. $manager->parseFieldsets(['books' => 'title,author', 'author' => 'name']);
  246. $this->assertSame($expected, $scope->toArray());
  247. //Clear all sparse fieldsets
  248. $manager->parseFieldsets([]);
  249. // Same again with metadata
  250. $resource->setMetaValue('foo', 'bar');
  251. $scope = new Scope($manager, $resource);
  252. $expected = [
  253. 'meta' => [
  254. 'foo' => 'bar',
  255. ],
  256. ];
  257. $this->assertSame($expected, $scope->toArray());
  258. $expectedJson = '{"meta":{"foo":"bar"}}';
  259. $this->assertSame($expectedJson, $scope->toJson());
  260. //Test with relationship
  261. $manager->parseFieldsets(['books' => 'title,author', 'author' => 'name']);
  262. $this->assertSame($expected, $scope->toArray());
  263. }
  264. public function testSerializingCollectionResourceWithoutName()
  265. {
  266. $manager = new Manager();
  267. $manager->parseIncludes('author');
  268. $manager->setSerializer(new ArraySerializer());
  269. $resource = new Collection($this->bookCollectionInput, new GenericBookTransformer());
  270. // Try without metadata
  271. $scope = new Scope($manager, $resource);
  272. // JSON array of JSON objects
  273. $expectedJson = '{"data":[{"title":"Foo","year":1991,"author":{"name":"Dave"}},{"title":"Bar","year":1997,"author":{"name":"Bob"}}]}';
  274. $this->assertSame($expectedJson, $scope->toJson());
  275. // Same again with metadata
  276. $resource->setMetaValue('foo', 'bar');
  277. $scope = new Scope($manager, $resource);
  278. $expectedJson = '{"data":[{"title":"Foo","year":1991,"author":{"name":"Dave"}},{"title":"Bar","year":1997,"author":{"name":"Bob"}}],"meta":{"foo":"bar"}}';
  279. $this->assertSame($expectedJson, $scope->toJson());
  280. }
  281. public function tearDown()
  282. {
  283. Mockery::close();
  284. }
  285. }