|
| 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\HumanUser; |
| 8 | +use Yajra\DataTables\Tests\Models\User; |
| 9 | +use Yajra\DataTables\Tests\TestCase; |
| 10 | + |
| 11 | +/** |
| 12 | + * Class MorphToRelationTest. |
| 13 | + */ |
| 14 | +class MorphToRelationTest extends TestCase |
| 15 | +{ |
| 16 | + use DatabaseTransactions; |
| 17 | + |
| 18 | + /** @test */ |
| 19 | + public function it_returns_all_records_with_the_relation_when_called_without_parameters() |
| 20 | + { |
| 21 | + $response = $this->call('GET', '/relations/morphTo'); |
| 22 | + $response->assertJson([ |
| 23 | + 'draw' => 0, |
| 24 | + 'recordsTotal' => 20, |
| 25 | + 'recordsFiltered' => 20, |
| 26 | + ]); |
| 27 | + |
| 28 | + $this->assertArrayHasKey('user', $response->json()['data'][0]); |
| 29 | + $this->assertCount(20, $response->json()['data']); |
| 30 | + } |
| 31 | + |
| 32 | + /** @test */ |
| 33 | + public function it_returns_all_records_with_the_deleted_relation_when_called_with_withtrashed_parameter() |
| 34 | + { |
| 35 | + HumanUser::find(1)->delete(); |
| 36 | + |
| 37 | + $response = $this->call('GET', '/relations/morphToWithTrashed'); |
| 38 | + $response->assertJson([ |
| 39 | + 'draw' => 0, |
| 40 | + 'recordsTotal' => 20, |
| 41 | + 'recordsFiltered' => 20, |
| 42 | + ]); |
| 43 | + |
| 44 | + $this->assertArrayHasKey('user', $response->json()['data'][0]); |
| 45 | + $this->assertArrayHasKey('user', $response->json()['data'][1]); |
| 46 | + $this->assertNotEmpty($response->json()['data'][0]['user']); |
| 47 | + $this->assertNotEmpty($response->json()['data'][1]['user']); |
| 48 | + } |
| 49 | + |
| 50 | + /** @test */ |
| 51 | + public function it_returns_all_records_with_the_only_deleted_relation_when_called_with_onlytrashed_parameter() |
| 52 | + { |
| 53 | + HumanUser::find(1)->delete(); |
| 54 | + |
| 55 | + $response = $this->call('GET', '/relations/morphToOnlyTrashed'); |
| 56 | + $response->assertJson([ |
| 57 | + 'draw' => 0, |
| 58 | + 'recordsTotal' => 20, |
| 59 | + 'recordsFiltered' => 20, |
| 60 | + ]); |
| 61 | + |
| 62 | + $this->assertArrayHasKey('user', $response->json()['data'][0]); |
| 63 | + $this->assertArrayHasKey('user', $response->json()['data'][1]); |
| 64 | + $this->assertNotEmpty($response->json()['data'][0]['user']); |
| 65 | + $this->assertEmpty($response->json()['data'][1]['user']); |
| 66 | + } |
| 67 | + |
| 68 | + /** @test */ |
| 69 | + public function it_can_perform_global_search_on_the_relation() |
| 70 | + { |
| 71 | + $response = $this->getJsonResponse([ |
| 72 | + 'search' => ['value' => 'Animal'], |
| 73 | + ]); |
| 74 | + |
| 75 | + $response->assertJson([ |
| 76 | + 'draw' => 0, |
| 77 | + 'recordsTotal' => 20, |
| 78 | + 'recordsFiltered' => 10, |
| 79 | + ]); |
| 80 | + |
| 81 | + $this->assertCount(10, $response->json()['data']); |
| 82 | + } |
| 83 | + |
| 84 | + protected function getJsonResponse(array $params = []) |
| 85 | + { |
| 86 | + $data = [ |
| 87 | + 'columns' => [ |
| 88 | + ['data' => 'user.name', 'name' => 'user.name', 'searchable' => 'true', 'orderable' => 'false'], |
| 89 | + ['data' => 'name', 'name' => 'name', 'searchable' => 'true', 'orderable' => 'true'], |
| 90 | + ['data' => 'email', 'name' => 'email', 'searchable' => 'true', 'orderable' => 'true'], |
| 91 | + ], |
| 92 | + ]; |
| 93 | + |
| 94 | + return $this->call('GET', '/relations/morphTo', array_merge($data, $params)); |
| 95 | + } |
| 96 | + |
| 97 | + protected function setUp(): void |
| 98 | + { |
| 99 | + parent::setUp(); |
| 100 | + |
| 101 | + $this->app['router']->get('/relations/morphTo', function (DataTables $datatables) { |
| 102 | + return $datatables->eloquent(User::with('user')->select('users.*'))->toJson(); |
| 103 | + }); |
| 104 | + |
| 105 | + $this->app['router']->get('/relations/morphToWithTrashed', function (DataTables $datatables) { |
| 106 | + return $datatables->eloquent(User::with(['user' => function ($query) { |
| 107 | + $query->withTrashed(); |
| 108 | + }])->select('users.*'))->toJson(); |
| 109 | + }); |
| 110 | + |
| 111 | + $this->app['router']->get('/relations/morphToOnlyTrashed', function (DataTables $datatables) { |
| 112 | + return $datatables->eloquent(User::with(['user' => function ($query) { |
| 113 | + $query->onlyTrashed(); |
| 114 | + }])->select('users.*'))->toJson(); |
| 115 | + }); |
| 116 | + } |
| 117 | +} |
0 commit comments