Skip to content

Commit 7cdc5e3

Browse files
committed
adds a working test for testing ambiguous column id when using Relation fix
Took 27 minutes
1 parent 47b22f6 commit 7cdc5e3

File tree

6 files changed

+97
-23
lines changed

6 files changed

+97
-23
lines changed

database/migrations/create_test_tables.php.stub

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,5 +35,17 @@ class CreateTestTables extends Migration
3535
$table->foreign('species_id')->references('id')->on('species');
3636
$table->foreign('breed_id')->references('id')->on('breeds');
3737
});
38+
39+
Schema::create('veterinaries', function (Blueprint $table) {
40+
$table->id();
41+
$table->string('name')->index();
42+
$table->string('phone')->index();
43+
});
44+
45+
Schema::create('pet_veterinary', function (Blueprint $table) {
46+
$table->id();
47+
$table->foreignId('pet_id')->constrained();
48+
$table->foreignId('veterinary_id')->constrained();
49+
});
3850
}
3951
}

tests/Http/Livewire/CatsTable.php renamed to tests/Http/Livewire/PetVeterinariesTable.php

Lines changed: 9 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,42 +6,36 @@
66
use Illuminate\Database\Eloquent\Builder;
77
use Illuminate\Database\Eloquent\Relations\Relation;
88
use Rappasoft\LaravelLivewireTables\DataTableComponent;
9+
use Rappasoft\LaravelLivewireTables\Tests\Models\Pet;
910
use Rappasoft\LaravelLivewireTables\Tests\Models\Species;
1011
use Rappasoft\LaravelLivewireTables\Views\Column;
1112

12-
class CatsTable extends DataTableComponent
13+
class PetVeterinariesTable extends DataTableComponent
1314
{
15+
public Pet $pet;
16+
1417
public array $bulkActions = [
15-
'feed' => 'Feed selected',
18+
'count' => 'Count selected',
1619
];
1720

1821
public function query(): Relation
1922
{
20-
/** @var Species $dogSpecimen */
21-
$dogSpecimen = Species::query()->where('name', 'Cat')->first();
22-
23-
return $dogSpecimen->pets()->with('species')->with('breed');
23+
return $this->pet->veterinaries();
2424
}
2525

2626
public function columns(): array
2727
{
2828
return [
2929
Column::make('Name', 'name')
3030
->searchable(),
31-
Column::make('Age', 'age')
31+
Column::make('Phone', 'phone')
3232
->searchable(function (Builder $query, $search) {
33-
$query->orWhere('age', '=', $search);
33+
$query->orWhere('phone', '=', $search);
3434
}),
35-
Column::make('Last Visit', 'last_visit')
36-
->searchable(),
37-
Column::make('Species', 'species.name')
38-
->searchable(),
39-
Column::make('Breed', 'breed.name')
40-
->searchable(),
4135
];
4236
}
4337

44-
public function feed(): int{
38+
public function count(): int{
4539
return $this->selectedRowsQuery()->count();
4640
}
4741
}

tests/Models/Pet.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Rappasoft\LaravelLivewireTables\Tests\Models;
44

55
use Illuminate\Database\Eloquent\Model;
6+
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
67

78
/**
89
* @property int $id
@@ -64,4 +65,8 @@ public function breed()
6465
{
6566
return $this->belongsTo(Breed::class);
6667
}
68+
69+
public function veterinaries(): BelongsToMany{
70+
return $this->belongsToMany(Veterinary::class);
71+
}
6772
}

tests/Models/Veterinary.php

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
4+
namespace Rappasoft\LaravelLivewireTables\Tests\Models;
5+
6+
7+
use Illuminate\Database\Eloquent\Model;
8+
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
9+
10+
class Veterinary extends Model
11+
{
12+
/**
13+
* The table associated with the model.
14+
*
15+
* @var string
16+
*/
17+
protected $table = 'veterinaries';
18+
19+
/**
20+
* The primary key for the model.
21+
*
22+
* @var string
23+
*/
24+
protected $primaryKey = 'id';
25+
26+
/**
27+
* @var bool
28+
*/
29+
public $timestamps = false;
30+
31+
/**
32+
* The attributes that are mass assignable.
33+
*
34+
* @var array
35+
*/
36+
protected $fillable = [
37+
'id',
38+
'name',
39+
'phone',
40+
];
41+
42+
public function pets(): BelongsToMany{
43+
return $this->belongsToMany(Pet::class);
44+
}
45+
}

tests/RelationshipDataTableComponentTest.php

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
77
use Illuminate\Support\Collection;
88
use Rappasoft\LaravelLivewireTables\DataTableComponent;
9-
use Rappasoft\LaravelLivewireTables\Tests\Http\Livewire\CatsTable;
9+
use Rappasoft\LaravelLivewireTables\Tests\Http\Livewire\PetVeterinariesTable;
10+
use Rappasoft\LaravelLivewireTables\Tests\Models\Pet;
1011

1112
class RelationshipDataTableComponentTest extends TestCase
1213
{
@@ -16,7 +17,8 @@ public function setUp(): void
1617
{
1718
parent::setUp();
1819

19-
$this->table = new CatsTable();
20+
$this->table = new PetVeterinariesTable();
21+
$this->table->pet = Pet::find(1);
2022
}
2123

2224
/** @test */
@@ -30,7 +32,7 @@ public function columns(): void
3032
{
3133
$columns = $this->table->columns();
3234

33-
$this->assertCount(5, $columns);
35+
$this->assertCount(2, $columns);
3436
}
3537

3638
/** @test */
@@ -72,36 +74,36 @@ public function pagination_disabled(): void
7274
/** @test */
7375
public function search_filter(): void
7476
{
75-
$this->table->filters['search'] = 'Cartman';
77+
$this->table->filters['search'] = 'John';
7678
$this->assertEquals(1, $this->table->getRowsProperty()->total());
7779
}
7880

7981
/** @test */
8082
public function search_filter_reset(): void
8183
{
82-
$this->table->filters['search'] = 'Cartman';
84+
$this->table->filters['search'] = 'John';
8385
$this->table->resetFilters();
8486
$this->assertEquals(1, $this->table->rows->total());
8587
}
8688

8789
/** @test */
8890
public function search_filter_remove(): void
8991
{
90-
$this->table->filters['search'] = 'Cartman';
92+
$this->table->filters['search'] = 'John';
9193
$this->table->removeFilter('search');
9294
$this->assertEquals(2, $this->table->rows->total());
9395
}
9496

9597
/** @test */
9698
public function search_filter_callback(): void
9799
{
98-
$this->table->filters['search'] = '22';
100+
$this->table->filters['search'] = '123456798';
99101
$this->assertEquals(1, $this->table->getRowsProperty()->total());
100102
}
101103

102104
/** @test */
103105
public function bulk_actions(){
104106
$this->table->selected[] = 1;
105-
$this->assertEquals(1, $this->table->feed());
107+
$this->assertEquals(1, $this->table->count());
106108
}
107109
}

tests/TestCase.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,14 @@
22

33
namespace Rappasoft\LaravelLivewireTables\Tests;
44

5+
use DB;
56
use Orchestra\Testbench\TestCase as Orchestra;
67
use Rappasoft\LaravelLivewireTables\LaravelLivewireTablesServiceProvider;
78
use Rappasoft\LaravelLivewireTables\Tests\Models\Breed;
89
use Rappasoft\LaravelLivewireTables\Tests\Models\Pet;
910
use Rappasoft\LaravelLivewireTables\Tests\Models\Species;
11+
use Rappasoft\LaravelLivewireTables\Tests\Models\Veterinary;
12+
use Symfony\Component\Console\Helper\Table;
1013

1114
class TestCase extends Orchestra
1215
{
@@ -44,6 +47,19 @@ protected function setUp(): void
4447
[ 'id' => 4, 'name' => 'Ben', 'age' => 5, 'species_id' => 3, 'breed_id' => 200 ],
4548
[ 'id' => 5, 'name' => 'Chico', 'age' => 7, 'species_id' => 3, 'breed_id' => 202 ],
4649
]);
50+
51+
Veterinary::insert([
52+
['id' => 1, 'name' => 'Dr John Smith', 'phone' => "123456798"],
53+
['id' => 2, 'name' => 'Dr Fabio Ivona', 'phone' => "789456123"],
54+
['id' => 3, 'name' => 'Dr Anthony Rappa', 'phone' => "987654321"],
55+
]);
56+
57+
DB::table('pet_veterinary')->insert([
58+
['id' => 1, 'pet_id' => 1, 'veterinary_id' => 1],
59+
['id' => 2, 'pet_id' => 1, 'veterinary_id' => 2],
60+
['id' => 3, 'pet_id' => 2, 'veterinary_id' => 1],
61+
['id' => 4, 'pet_id' => 2, 'veterinary_id' => 3],
62+
]);
4763
}
4864

4965
protected function getPackageProviders($app)

0 commit comments

Comments
 (0)