|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Yajra\DataTables\Tests\Integration; |
| 4 | + |
| 5 | +use Illuminate\Foundation\Testing\DatabaseTransactions; |
| 6 | +use Yajra\DataTables\DataTables; |
| 7 | +use Yajra\DataTables\Tests\Models\User; |
| 8 | +use Yajra\DataTables\Tests\TestCase; |
| 9 | + |
| 10 | +class IgnoreGettersTest extends TestCase |
| 11 | +{ |
| 12 | + use DatabaseTransactions; |
| 13 | + |
| 14 | + /** @test */ |
| 15 | + public function it_return_the_default_value_when_attribute_is_null() |
| 16 | + { |
| 17 | + $user = User::create([ |
| 18 | + 'name' => 'foo', |
| 19 | + |
| 20 | + 'color' => null |
| 21 | + ]); |
| 22 | + |
| 23 | + $this->assertEquals('#000000', $user->color); |
| 24 | + $this->assertEquals('#000000', $user->refresh()->toArray()['color']); |
| 25 | + } |
| 26 | + |
| 27 | + /** @test */ |
| 28 | + public function it_return_the_getter_value_without_ignore_getters() |
| 29 | + { |
| 30 | + $this->app['router']->get('/ignore-getters', function (DataTables $datatables) { |
| 31 | + return $datatables->eloquent(User::with('posts.user')->select('users.*'))->toJson(); |
| 32 | + }); |
| 33 | + |
| 34 | + $response = $this->call('GET', '/ignore-getters'); |
| 35 | + $response->assertJson([ |
| 36 | + 'draw' => 0, |
| 37 | + 'recordsTotal' => 20, |
| 38 | + 'recordsFiltered' => 20, |
| 39 | + ]); |
| 40 | + |
| 41 | + $this->assertNotNull($response->json()['data'][0]['posts']); |
| 42 | + // Assert the getter color is call on primary Model |
| 43 | + $this->assertNotNull($response->json()['data'][0]['color']); |
| 44 | + // Assert the getter color is call on relationships |
| 45 | + $this->assertNotNull($response->json()['data'][0]['posts'][0]['user']['color']); |
| 46 | + $this->assertCount(20, $response->json()['data']); |
| 47 | + } |
| 48 | + |
| 49 | + /** @test */ |
| 50 | + public function it_ignore_the_getter_value_with_ignore_getters() |
| 51 | + { |
| 52 | + $this->app['router']->get('/ignore-getters', function (DataTables $datatables) { |
| 53 | + return $datatables->eloquent(User::with('posts.user')->select('users.*'))->ignoreGetters()->toJson(); |
| 54 | + }); |
| 55 | + |
| 56 | + $response = $this->call('GET', '/ignore-getters'); |
| 57 | + $response->assertJson([ |
| 58 | + 'draw' => 0, |
| 59 | + 'recordsTotal' => 20, |
| 60 | + 'recordsFiltered' => 20, |
| 61 | + ]); |
| 62 | + |
| 63 | + $this->assertNotNull($response->json()['data'][0]['posts']); |
| 64 | + // Assert the getter color is not call on primary Model |
| 65 | + $this->assertNull($response->json()['data'][0]['color']); |
| 66 | + // Assert the getter color is not call on relationships |
| 67 | + $this->assertNull($response->json()['data'][0]['posts'][0]['user']['color']); |
| 68 | + $this->assertCount(20, $response->json()['data']); |
| 69 | + } |
| 70 | +} |
0 commit comments