ControllerTest.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. <?php
  2. // Copyright 2019 Hackware SpA <human@hackware.cl>
  3. // "Hackware Web Services Core" is released under the MIT License terms.
  4. namespace Hawese\Tests;
  5. use Illuminate\Http\Request;
  6. use Laravel\Lumen\Testing\DatabaseTransactions;
  7. class ControllerTest extends TestCase
  8. {
  9. use DatabaseTransactions;
  10. public function setUp(): void
  11. {
  12. parent::setUp();
  13. $this->dumbController = new DumbController();
  14. $this->request = new Request();
  15. }
  16. public function testSingleResponse()
  17. {
  18. $obj = (object) ['any' => 'thing'];
  19. $this->assertEqualsCanonicalizing(
  20. $obj,
  21. $this->dumbController->singleResponse($obj)
  22. );
  23. }
  24. public function testCollectionResponsePage()
  25. {
  26. for ($i = 0; $i < 30; $i++) {
  27. (new DumbTableModel([
  28. 'attr1' => bin2hex(random_bytes(12))
  29. ]))->insert();
  30. }
  31. $this->request->query->set('page', 2);
  32. $response = $this->dumbController->collectionResponse(
  33. $this->request,
  34. DumbTableModel::class
  35. );
  36. $this->assertSame(3, $response['meta']['total_pages']);
  37. $this->assertSame(10, count($response['data']));
  38. }
  39. public function testCollectionResponseLimit()
  40. {
  41. for ($i = 0; $i < 30; $i++) {
  42. (new DumbTableModel([
  43. 'attr1' => bin2hex(random_bytes(12))
  44. ]))->insert();
  45. }
  46. $this->request->query->set('limit', 5);
  47. $response = $this->dumbController->collectionResponse(
  48. $this->request,
  49. DumbTableModel::class
  50. );
  51. $this->assertSame(6, $response['meta']['total_pages']);
  52. $this->assertSame(5, count($response['data']));
  53. }
  54. public function testCollectionResponseSort()
  55. {
  56. for ($i = 0; $i < 15; $i++) {
  57. (new DumbTableModel([
  58. 'attr1' => bin2hex(random_bytes(12)),
  59. 'attr2' => chr(65 + $i)
  60. ]))->insert();
  61. }
  62. $this->request->query->set('sort', 'attr2');
  63. $response = $this->dumbController->collectionResponse(
  64. $this->request,
  65. DumbTableModel::class
  66. );
  67. $this->assertSame('A', $response['data'][0]['attr2']);
  68. $this->request->query->set('sort', '-attr2');
  69. $response = $this->dumbController->collectionResponse(
  70. $this->request,
  71. DumbTableModel::class
  72. );
  73. $this->assertSame('O', $response['data'][0]['attr2']);
  74. }
  75. public function testCollectionResponseInclude()
  76. {
  77. (new DumbTableModel([
  78. 'attr1' => bin2hex(random_bytes(12)),
  79. 'foreign_id' => (new ForeignTableModel())->insert(),
  80. 'other_foreign_id' => (new ForeignTableModel())->insert()
  81. ]))->insert();
  82. $this->request->query->set('include', 'foreign');
  83. $response = $this->dumbController->collectionResponse(
  84. $this->request,
  85. DumbTableModel::class
  86. );
  87. $this->assertArrayHasKey('foreign', $response['data'][0]);
  88. $this->assertArrayNotHasKey('other_foreign', $response['data'][0]);
  89. $this->request->query->set('include', 'foreign,other_foreign');
  90. $response = $this->dumbController->collectionResponse(
  91. $this->request,
  92. DumbTableModel::class
  93. );
  94. $this->assertArrayHasKey('other_foreign', $response['data'][0]);
  95. }
  96. public function testCollectionResponseFiltersCallback()
  97. {
  98. for ($i = 0; $i < 5; $i++) {
  99. (new DumbTableModel([
  100. 'attr1' => bin2hex(random_bytes(12))
  101. ]))->insert();
  102. }
  103. $attr1 = DumbTableModel::select()->inRandomOrder()->first()->attr1;
  104. $response = $this->dumbController->collectionResponse(
  105. $this->request,
  106. DumbTableModel::class,
  107. function ($query) use ($attr1) {
  108. return $query->where('attr1', $attr1);
  109. }
  110. );
  111. $this->assertSame($attr1, $response['data'][0]['attr1']);
  112. }
  113. }