CollectionTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. <?php namespace League\Fractal\Test\Resource;
  2. use League\Fractal\Pagination\Cursor;
  3. use League\Fractal\Resource\Collection;
  4. use Mockery;
  5. use PHPUnit\Framework\TestCase;
  6. class CollectionTest extends TestCase
  7. {
  8. protected $simpleCollection = [
  9. ['foo' => 'bar'],
  10. ['baz' => 'ban'],
  11. ];
  12. public function testGetData()
  13. {
  14. $resource = new Collection($this->simpleCollection, function (array $data) {
  15. return $data;
  16. });
  17. $this->assertSame($resource->getData(), $this->simpleCollection);
  18. }
  19. /**
  20. * @covers \League\Fractal\Resource\Collection::setData
  21. */
  22. public function testSetData()
  23. {
  24. $collection = Mockery::mock('League\Fractal\Resource\Collection')->makePartial();
  25. $collection->setData('foo');
  26. $this->assertSame('foo', $collection->getData());
  27. }
  28. public function testGetTransformer()
  29. {
  30. $resource = new Collection($this->simpleCollection, function () {
  31. });
  32. $this->assertTrue(is_callable($resource->getTransformer()));
  33. $resource = new Collection($this->simpleCollection, 'SomeClass');
  34. $this->assertSame($resource->getTransformer(), 'SomeClass');
  35. }
  36. /**
  37. * @covers \League\Fractal\Resource\Collection::setTransformer
  38. */
  39. public function testSetTransformer()
  40. {
  41. $collection = Mockery::mock('League\Fractal\Resource\Collection')->makePartial();
  42. $collection->setTransformer('foo');
  43. $this->assertSame('foo', $collection->getTransformer());
  44. }
  45. /**
  46. * @covers \League\Fractal\Resource\Collection::setCursor
  47. */
  48. public function testSetCursor()
  49. {
  50. $cursor = Mockery::mock('League\Fractal\Pagination\Cursor');
  51. $collection = Mockery::mock('League\Fractal\Resource\Collection')->makePartial();
  52. $this->assertInstanceOf('League\Fractal\Resource\Collection', $collection->setCursor($cursor));
  53. }
  54. /**
  55. * @covers \League\Fractal\Resource\Collection::getCursor
  56. */
  57. public function testGetCursor()
  58. {
  59. $cursor = new Cursor();
  60. $collection = Mockery::mock('League\Fractal\Resource\Collection')->makePartial();
  61. $collection->setCursor($cursor);
  62. $this->assertInstanceOf('League\Fractal\Pagination\Cursor', $collection->getCursor());
  63. }
  64. /**
  65. * @covers \League\Fractal\Resource\Collection::setPaginator
  66. * @covers \League\Fractal\Resource\Collection::getPaginator
  67. */
  68. public function testGetSetPaginator()
  69. {
  70. $paginator = Mockery::mock('League\Fractal\Pagination\IlluminatePaginatorAdapter');
  71. $collection = Mockery::mock('League\Fractal\Resource\Collection')->makePartial();
  72. $this->assertInstanceOf('League\Fractal\Resource\Collection', $collection->setPaginator($paginator));
  73. $this->assertInstanceOf('League\Fractal\Pagination\PaginatorInterface', $collection->getPaginator());
  74. }
  75. /**
  76. * @covers \League\Fractal\Resource\Collection::setMetaValue
  77. * @covers \League\Fractal\Resource\Collection::getMetaValue
  78. */
  79. public function testGetSetMeta()
  80. {
  81. $collection = Mockery::mock('League\Fractal\Resource\Collection')->makePartial();
  82. $this->assertInstanceOf('League\Fractal\Resource\Collection', $collection->setMetaValue('foo', 'bar'));
  83. $this->assertSame(['foo' => 'bar'], $collection->getMeta());
  84. $this->assertSame('bar', $collection->getMetaValue('foo'));
  85. $collection->setMeta(['baz' => 'bat']);
  86. $this->assertSame(['baz' => 'bat'], $collection->getMeta());
  87. }
  88. /**
  89. * @covers \League\Fractal\Resource\Collection::setResourceKey
  90. */
  91. public function testSetResourceKey()
  92. {
  93. $collection = Mockery::mock('League\Fractal\Resource\Collection')->makePartial();
  94. $this->assertInstanceOf('League\Fractal\Resource\Collection', $collection->setResourceKey('foo'));
  95. }
  96. /**
  97. * @covers \League\Fractal\Resource\Collection::getResourceKey
  98. */
  99. public function testGetResourceKey()
  100. {
  101. $collection = Mockery::mock('League\Fractal\Resource\Collection')->makePartial();
  102. $collection->setResourceKey('foo');
  103. $this->assertSame('foo', $collection->getResourceKey());
  104. }
  105. public function tearDown()
  106. {
  107. Mockery::close();
  108. }
  109. }