|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Binaryk\LaravelRestify\Tests\Controllers; |
| 4 | + |
| 5 | +use Binaryk\LaravelRestify\Tests\Fixtures\Post\ActiveBooleanFilter; |
| 6 | +use Binaryk\LaravelRestify\Tests\Fixtures\Post\CreatedAfterDateFilter; |
| 7 | +use Binaryk\LaravelRestify\Tests\Fixtures\Post\Post; |
| 8 | +use Binaryk\LaravelRestify\Tests\Fixtures\Post\SelectCategoryFilter; |
| 9 | +use Binaryk\LaravelRestify\Tests\Fixtures\User\UserRepository; |
| 10 | +use Binaryk\LaravelRestify\Tests\IntegrationTest; |
| 11 | + |
| 12 | +class RepositoryFilterControllerTest extends IntegrationTest |
| 13 | +{ |
| 14 | + public function test_can_get_available_filters() |
| 15 | + { |
| 16 | + $response = $this |
| 17 | + ->withoutExceptionHandling() |
| 18 | + ->getJson('restify-api/posts/filters') |
| 19 | + ->dump() |
| 20 | + ->assertStatus(200); |
| 21 | + |
| 22 | + $this->assertCount(3, $response->json('data')); |
| 23 | + } |
| 24 | + |
| 25 | + public function test_the_boolean_filter_is_applied() |
| 26 | + { |
| 27 | + factory(Post::class)->create(['is_active' => false]); |
| 28 | + factory(Post::class)->create(['is_active' => true]); |
| 29 | + |
| 30 | + $filters = base64_encode(json_encode([ |
| 31 | + [ |
| 32 | + 'class' => ActiveBooleanFilter::class, |
| 33 | + 'value' => [ |
| 34 | + 'is_active' => false, |
| 35 | + ], |
| 36 | + ], |
| 37 | + ])); |
| 38 | + |
| 39 | + $response = $this |
| 40 | + ->withoutExceptionHandling() |
| 41 | + ->getJson('restify-api/posts?filters='.$filters) |
| 42 | + ->dump() |
| 43 | + ->assertStatus(200); |
| 44 | + |
| 45 | + $this->assertCount(1, $response->json('data')); |
| 46 | + } |
| 47 | + |
| 48 | + public function test_the_select_filter_is_applied() |
| 49 | + { |
| 50 | + factory(Post::class)->create(['category' => 'movie']); |
| 51 | + factory(Post::class)->create(['category' => 'article']); |
| 52 | + |
| 53 | + $filters = base64_encode(json_encode([ |
| 54 | + [ |
| 55 | + 'class' => SelectCategoryFilter::class, |
| 56 | + 'value' => 'article', |
| 57 | + ], |
| 58 | + ])); |
| 59 | + |
| 60 | + $response = $this |
| 61 | + ->withExceptionHandling() |
| 62 | + ->getJson('restify-api/posts?filters='.$filters) |
| 63 | + ->assertStatus(200); |
| 64 | + |
| 65 | + $this->assertCount(1, $response->json('data')); |
| 66 | + } |
| 67 | + |
| 68 | + public function test_the_timestamp_filter_is_applied() |
| 69 | + { |
| 70 | + factory(Post::class)->create(['created_at' => now()->addYear()]); |
| 71 | + factory(Post::class)->create(['created_at' => now()->subYear()]); |
| 72 | + |
| 73 | + $filters = base64_encode(json_encode([ |
| 74 | + [ |
| 75 | + 'class' => UserRepository::class, |
| 76 | + 'value' => now()->addWeek()->timestamp, |
| 77 | + ], |
| 78 | + [ |
| 79 | + 'class' => CreatedAfterDateFilter::class, |
| 80 | + 'value' => now()->addWeek()->timestamp, |
| 81 | + ], |
| 82 | + ])); |
| 83 | + |
| 84 | + $response = $this |
| 85 | + ->withExceptionHandling() |
| 86 | + ->getJson('restify-api/posts?filters='.$filters) |
| 87 | + ->assertStatus(200); |
| 88 | + |
| 89 | + $this->assertCount(1, $response->json('data')); |
| 90 | + } |
| 91 | +} |
0 commit comments