ManagerTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  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 Mockery;
  6. class ManagerTest extends \PHPUnit_Framework_TestCase
  7. {
  8. public function testParseIncludeSelfie()
  9. {
  10. $manager = new Manager();
  11. // Test that some includes provided returns self
  12. $this->assertInstanceOf(get_class($manager), $manager->parseIncludes(array('foo')));
  13. }
  14. /**
  15. * @expectedException InvalidArgumentException
  16. * @expectedExceptionMessage The parseIncludes() method expects a string or an array. NULL given
  17. */
  18. public function testInvalidParseInclude()
  19. {
  20. $manager = new Manager();
  21. $manager->parseIncludes(null);
  22. }
  23. /**
  24. * @expectedException InvalidArgumentException
  25. * @expectedExceptionMessage The parseIncludes() method expects a string or an array. integer given
  26. */
  27. public function testIceTParseInclude()
  28. {
  29. $manager = new Manager();
  30. $manager->parseIncludes(99);
  31. }
  32. public function testParseIncludes()
  33. {
  34. $manager = new Manager();
  35. // Does a CSV string work
  36. $manager->parseIncludes('foo,bar');
  37. $this->assertEquals(array('foo', 'bar'), $manager->getRequestedIncludes());
  38. // Does a big array of stuff work
  39. $manager->parseIncludes(array('foo', 'bar', 'bar.baz'));
  40. $this->assertEquals(array('foo', 'bar', 'bar.baz'), $manager->getRequestedIncludes());
  41. // Are repeated things stripped
  42. $manager->parseIncludes(array('foo', 'foo', 'bar'));
  43. $this->assertEquals(array('foo', 'bar'), $manager->getRequestedIncludes());
  44. // Do requests for `baz.bart` also request `baz`?
  45. $manager->parseIncludes(array('foo.bar'));
  46. $this->assertEquals(array('foo', 'foo.bar'), $manager->getRequestedIncludes());
  47. // See if fancy syntax works
  48. $manager->parseIncludes('foo:limit(5|1):order(-something)');
  49. $params = $manager->getIncludeParams('foo');
  50. $this->assertInstanceOf('League\Fractal\ParamBag', $params);
  51. $this->assertEquals(array('5', '1'), $params['limit']);
  52. $this->assertEquals(array('-something'), $params['order']);
  53. $this->assertNull($params['totallymadeup']);
  54. }
  55. public function testRecursionLimiting()
  56. {
  57. $manager = new Manager();
  58. // Should limit to 10 by default
  59. $manager->parseIncludes('a.b.c.d.e.f.g.h.i.j.NEVER');
  60. $this->assertEquals(
  61. array(
  62. 'a',
  63. 'a.b',
  64. 'a.b.c',
  65. 'a.b.c.d',
  66. 'a.b.c.d.e',
  67. 'a.b.c.d.e.f',
  68. 'a.b.c.d.e.f.g',
  69. 'a.b.c.d.e.f.g.h',
  70. 'a.b.c.d.e.f.g.h.i',
  71. 'a.b.c.d.e.f.g.h.i.j',
  72. ),
  73. $manager->getRequestedIncludes()
  74. );
  75. // Try setting to 3 and see what happens
  76. $manager->setRecursionLimit(3);
  77. $manager->parseIncludes('a.b.c.NEVER');
  78. $this->assertEquals(
  79. array(
  80. 'a',
  81. 'a.b',
  82. 'a.b.c',
  83. ),
  84. $manager->getRequestedIncludes()
  85. );
  86. }
  87. public function testCreateDataWithCallback()
  88. {
  89. $manager = new Manager();
  90. // Item
  91. $resource = new Item(array('foo' => 'bar'), function (array $data) {
  92. return $data;
  93. });
  94. $rootScope = $manager->createData($resource);
  95. $this->assertInstanceOf('League\Fractal\Scope', $rootScope);
  96. $this->assertEquals(array('data' => array('foo' => 'bar')), $rootScope->toArray());
  97. $this->assertEquals('{"data":{"foo":"bar"}}', $rootScope->toJson());
  98. // Collection
  99. $resource = new Collection(array(array('foo' => 'bar')), function (array $data) {
  100. return $data;
  101. });
  102. $rootScope = $manager->createData($resource);
  103. $this->assertInstanceOf('League\Fractal\Scope', $rootScope);
  104. $this->assertEquals(array('data' => array(array('foo' => 'bar'))), $rootScope->toArray());
  105. $this->assertEquals('{"data":[{"foo":"bar"}]}', $rootScope->toJson());
  106. }
  107. public function tearDown()
  108. {
  109. Mockery::close();
  110. }
  111. }