|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Binaryk\LaravelRestify\Tests\Feature; |
| 4 | + |
| 5 | +use Binaryk\LaravelRestify\Tests\Fixtures\User\User; |
| 6 | +use Binaryk\LaravelRestify\Tests\Fixtures\User\UserRepository; |
| 7 | +use Binaryk\LaravelRestify\Tests\IntegrationTestCase; |
| 8 | + |
| 9 | +class RepositoryGroupByTest extends IntegrationTestCase |
| 10 | +{ |
| 11 | + public function test_it_can_group_by_the_results(): void |
| 12 | + { |
| 13 | + User::factory(4)->create([ |
| 14 | + 'name' => 'John Doe', |
| 15 | + ]); |
| 16 | + |
| 17 | + User::factory(4)->create([ |
| 18 | + 'name' => 'Second John Doe', |
| 19 | + ]); |
| 20 | + |
| 21 | + UserRepository::$groupBy = ['name']; |
| 22 | + |
| 23 | + $this->getJson(UserRepository::route(query: ['group_by' => 'name']))->assertJsonCount(2, 'data'); |
| 24 | + } |
| 25 | + |
| 26 | + public function test_it_can_group_by_the_results_multiple_columns(): void |
| 27 | + { |
| 28 | + User::factory(3)->create([ |
| 29 | + 'name' => 'John Doe', |
| 30 | + 'avatar' => 'image.jpg', |
| 31 | + ]); |
| 32 | + |
| 33 | + User::factory(1)->create([ |
| 34 | + 'name' => 'Another John Doe', |
| 35 | + 'avatar' => 'image.jpg', |
| 36 | + ]); |
| 37 | + |
| 38 | + UserRepository::$groupBy = ['name', 'avatar']; |
| 39 | + |
| 40 | + $this->getJson(UserRepository::route(query: ['group_by' => 'name,avatar']))->assertJsonCount(2, 'data'); |
| 41 | + } |
| 42 | + |
| 43 | + public function test_it_can_not_group_by_the_results_because_wrong_column(): void |
| 44 | + { |
| 45 | + User::factory(4)->create([ |
| 46 | + 'name' => 'John Doe', |
| 47 | + ]); |
| 48 | + |
| 49 | + User::factory(4)->create([ |
| 50 | + 'name' => 'Second John Doe', |
| 51 | + ]); |
| 52 | + |
| 53 | + UserRepository::$groupBy = ['email']; |
| 54 | + |
| 55 | + $this->getJson(UserRepository::route(query: ['group_by' => 'name']))->assertUnprocessable(); |
| 56 | + } |
| 57 | +} |
0 commit comments