Skip to content

Commit e8d77a4

Browse files
committed
Merge branch 'DeltaSystems-feature-testing' into develop
2 parents f282095 + 97d21de commit e8d77a4

File tree

10 files changed

+336
-14
lines changed

10 files changed

+336
-14
lines changed

README.md

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -622,11 +622,17 @@ The final result would look like:
622622

623623
- [x] Bootstrap 4 Template
624624
- [x] Bootstrap 5 Template
625-
- [ ] Sorting By Relationships
626-
- [ ] Test Suite
625+
- [x] Sorting By Relationships
626+
- [ ] Test Suite (WIP)
627627
- [ ] Column Search
628628
- [ ] Greater Configurability
629629

630+
## Testing
631+
632+
```bash
633+
composer test
634+
```
635+
630636
## Changelog
631637

632638
Please see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
"illuminate/contracts": "^8.0"
2525
},
2626
"require-dev": {
27+
"ext-sqlite3": "*",
2728
"orchestra/testbench": "^6.13",
2829
"phpunit/phpunit": "^9.3",
2930
"spatie/laravel-ray": "^1.9",
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class CreateTestTables extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('species', function (Blueprint $table) {
17+
$table->id();
18+
$table->string('name');
19+
});
20+
21+
Schema::create('breeds', function (Blueprint $table) {
22+
$table->id();
23+
$table->string('name');
24+
$table->integer('species_id')->unsigned();
25+
$table->foreign('species_id')->references('id')->on('species');
26+
});
27+
28+
Schema::create('pets', function (Blueprint $table) {
29+
$table->id();
30+
$table->string('name')->index();
31+
$table->string('age')->nullable();
32+
$table->date('last_visit')->nullable();
33+
$table->integer('species_id')->unsigned()->nullable();
34+
$table->integer('breed_id')->unsigned()->nullable();
35+
$table->foreign('species_id')->references('id')->on('species');
36+
$table->foreign('breed_id')->references('id')->on('breeds');
37+
});
38+
}
39+
}

tests/DataTableComponentTest.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
namespace Rappasoft\LaravelLivewireTables\Tests;
4+
5+
use Illuminate\Contracts\Pagination\LengthAwarePaginator;
6+
use Rappasoft\LaravelLivewireTables\DataTableComponent;
7+
use Rappasoft\LaravelLivewireTables\Tests\Http\Livewire\PetsTable;
8+
9+
class DataTableComponentTest extends TestCase
10+
{
11+
protected DataTableComponent $table;
12+
13+
public function setUp(): void
14+
{
15+
parent::setUp();
16+
17+
$this->table = new PetsTable();
18+
}
19+
20+
/** @test */
21+
public function bootstrap_test_datatable(): void
22+
{
23+
$this->assertInstanceOf(DataTableComponent::class, $this->table);
24+
}
25+
26+
/** @test */
27+
public function test_columns(): void
28+
{
29+
$columns = $this->table->columns();
30+
31+
$this->assertCount(5, $columns);
32+
}
33+
34+
/** @test */
35+
public function test_rows(): void
36+
{
37+
$rows = $this->table->rows;
38+
39+
$this->assertInstanceOf(LengthAwarePaginator::class, $rows);
40+
$this->assertEquals(5, $this->table->getRowsProperty()->total());
41+
}
42+
43+
/** @test */
44+
public function test_search_filter(): void
45+
{
46+
$this->table->filters['search'] = 'Cartman';
47+
$this->assertEquals(1, $this->table->getRowsProperty()->total());
48+
}
49+
50+
/** @test */
51+
public function test_search_filter_reset(): void
52+
{
53+
$this->table->filters['search'] = 'Cartman';
54+
$this->table->resetFilters();
55+
$this->assertEquals(1, $this->table->rows->total());
56+
}
57+
58+
/** @test */
59+
public function test_search_filter_remove(): void
60+
{
61+
$this->table->filters['search'] = 'Cartman';
62+
$this->table->removeFilter('search');
63+
$this->assertEquals(5, $this->table->rows->total());
64+
}
65+
}

tests/ExampleTest.php

Lines changed: 0 additions & 12 deletions
This file was deleted.

tests/Http/Livewire/PetsTable.php

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<?php
2+
3+
namespace Rappasoft\LaravelLivewireTables\Tests\Http\Livewire;
4+
5+
use Illuminate\Database\Eloquent\Builder;
6+
use Rappasoft\LaravelLivewireTables\DataTableComponent;
7+
use Rappasoft\LaravelLivewireTables\Tests\Models\Pet;
8+
use Rappasoft\LaravelLivewireTables\Views\Column;
9+
10+
class PetsTable extends DataTableComponent
11+
{
12+
/**
13+
* @return Builder
14+
*/
15+
public function query() : Builder
16+
{
17+
return Pet::query()->when($this->getFilter('search'), function (Builder $query, $search) {
18+
$query->where('name', 'like', '%'.$search.'%');
19+
});
20+
}
21+
22+
public function columns(): array
23+
{
24+
return [
25+
Column::make('Name', 'name'),
26+
Column::make('Age', 'age'),
27+
Column::make('Last Visit', 'last_visit'),
28+
Column::make('Species', 'species.name'),
29+
Column::make('Breed', 'breed.name'),
30+
];
31+
}
32+
}

tests/Models/Breed.php

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

tests/Models/Pet.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
<?php
2+
3+
namespace Rappasoft\LaravelLivewireTables\Tests\Models;
4+
5+
use Illuminate\Database\Eloquent\Model;
6+
7+
/**
8+
* @property int $id
9+
* @property string $name
10+
* @property string $age
11+
* @property string $last_visit
12+
* @property int $species_id
13+
* @property int $breed_id
14+
*/
15+
class Pet extends Model
16+
{
17+
/**
18+
* The table associated with the model.
19+
*
20+
* @var string
21+
*/
22+
protected $table = 'pets';
23+
24+
/**
25+
* The primary key for the model.
26+
*
27+
* @var string
28+
*/
29+
protected $primaryKey = 'id';
30+
31+
/**
32+
* @var bool
33+
*/
34+
public $timestamps = false;
35+
36+
/**
37+
* The attributes that are mass assignable.
38+
*
39+
* @var array
40+
*/
41+
protected $fillable = [
42+
'id',
43+
'name',
44+
'age',
45+
'last_visit',
46+
'species_id',
47+
'breed_id',
48+
];
49+
50+
/**
51+
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo|Species[]
52+
*/
53+
public function species()
54+
{
55+
return $this->belongsTo(Species::class);
56+
}
57+
58+
/**
59+
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo|Breed[]
60+
*/
61+
public function breeds()
62+
{
63+
return $this->belongsTo(Breed::class);
64+
}
65+
}

tests/Models/Species.php

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

tests/TestCase.php

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,48 @@
44

55
use Orchestra\Testbench\TestCase as Orchestra;
66
use Rappasoft\LaravelLivewireTables\LaravelLivewireTablesServiceProvider;
7+
use Rappasoft\LaravelLivewireTables\Tests\Models\Breed;
8+
use Rappasoft\LaravelLivewireTables\Tests\Models\Pet;
9+
use Rappasoft\LaravelLivewireTables\Tests\Models\Species;
710

811
class TestCase extends Orchestra
912
{
13+
/**
14+
* Setup the test environment.
15+
*/
16+
protected function setUp(): void
17+
{
18+
parent::setUp();
19+
20+
Species::insert([
21+
[ 'id' => 1, 'name' => 'Cat', ],
22+
[ 'id' => 2, 'name' => 'Dog', ],
23+
[ 'id' => 3, 'name' => 'Horse', ],
24+
[ 'id' => 4, 'name' => 'Bird', ],
25+
]);
26+
27+
Breed::insert([
28+
[ 'id' => 1, 'name' => 'American Shorthair', 'species_id' => 1, ],
29+
[ 'id' => 2, 'name' => 'Maine Coon', 'species_id' => 1, ],
30+
[ 'id' => 3, 'name' => 'Persian', 'species_id' => 1, ],
31+
[ 'id' => 4, 'name' => 'Norwegian Forest', 'species_id' => 1, ],
32+
[ 'id' => 100, 'name' => 'Beagle', 'species_id' => 2, ],
33+
[ 'id' => 101, 'name' => 'Corgi', 'species_id' => 2, ],
34+
[ 'id' => 102, 'name' => 'Red Setter', 'species_id' => 2, ],
35+
[ 'id' => 200, 'name' => 'Arabian', 'species_id' => 3, ],
36+
[ 'id' => 201, 'name' => 'Clydesdale', 'species_id' => 3, ],
37+
[ 'id' => 202, 'name' => 'Mustang', 'species_id' => 3, ],
38+
]);
39+
40+
Pet::insert([
41+
[ 'id' => 1, 'name' => 'Cartman', 'age' => 22, 'species_id' => 1, 'breed_id' => 4 ],
42+
[ 'id' => 2, 'name' => 'Tux', 'age' => 8, 'species_id' => 1, 'breed_id' => 4 ],
43+
[ 'id' => 3, 'name' => 'May', 'age' => 3, 'species_id' => 2, 'breed_id' => 102 ],
44+
[ 'id' => 4, 'name' => 'Ben', 'age' => 5, 'species_id' => 3, 'breed_id' => 200 ],
45+
[ 'id' => 5, 'name' => 'Chico', 'age' => 7, 'species_id' => 3, 'breed_id' => 202 ],
46+
]);
47+
}
48+
1049
protected function getPackageProviders($app)
1150
{
1251
return [
@@ -22,5 +61,8 @@ public function getEnvironmentSetUp($app)
2261
'database' => ':memory:',
2362
'prefix' => '',
2463
]);
64+
65+
include_once __DIR__.'/../database/migrations/create_test_tables.php.stub';
66+
(new \CreateTestTables())->up();
2567
}
2668
}

0 commit comments

Comments
 (0)