DataArraySerializerTest.php 10 KB

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