Skip to content

Commit d8d7919

Browse files
committed
Merge branch 'feature-testing' of https://github.com/DeltaSystems/laravel-livewire-tables into DeltaSystems-feature-testing
2 parents 1b3fe44 + 8405c6a commit d8d7919

File tree

10 files changed

+344
-13
lines changed

10 files changed

+344
-13
lines changed

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",

tests.sqlite

24 KB
Binary file not shown.

tests/DataTableComponentTest.php

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

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: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,110 @@
22

33
namespace Rappasoft\LaravelLivewireTables\Tests;
44

5+
use Illuminate\Database\Capsule\Manager as DB;
6+
use Illuminate\Database\Schema\Blueprint;
7+
use Livewire\LivewireServiceProvider;
58
use Orchestra\Testbench\TestCase as Orchestra;
69
use Rappasoft\LaravelLivewireTables\LaravelLivewireTablesServiceProvider;
10+
use Rappasoft\LaravelLivewireTables\Tests\Models\Breed;
11+
use Rappasoft\LaravelLivewireTables\Tests\Models\Pet;
12+
use Rappasoft\LaravelLivewireTables\Tests\Models\Species;
713

814
class TestCase extends Orchestra
915
{
16+
protected $db;
17+
18+
/**
19+
* Setup the test environment.
20+
*/
21+
protected function setUp(): void
22+
{
23+
parent::setUp();
24+
25+
$this->db = $db = new DB;
26+
27+
// grab database
28+
$database = $this->app['config']['database']['connections']['sqlite']['database'];
29+
30+
// setup connection
31+
$db->addConnection([
32+
'driver' => 'sqlite',
33+
'database' => $database,
34+
]);
35+
36+
$db->setAsGlobal();
37+
38+
// setup species table
39+
$this->db->connection()->getSchemaBuilder()->create('species', function (Blueprint $table) {
40+
$table->id();
41+
$table->string('name');
42+
});
43+
44+
Species::insert([
45+
[ 'id' => 1, 'name' => 'Cat', ],
46+
[ 'id' => 2, 'name' => 'Dog', ],
47+
[ 'id' => 3, 'name' => 'Horse', ],
48+
[ 'id' => 4, 'name' => 'Bird', ],
49+
]);
50+
51+
// setup breeds table
52+
$this->db->connection()->getSchemaBuilder()->create('breeds', function (Blueprint $table) {
53+
$table->id();
54+
$table->string('name');
55+
$table->integer('species_id')->unsigned();
56+
$table->foreign('species_id')->references('id')->on('species');
57+
});
58+
59+
Breed::insert([
60+
[ 'id' => 1, 'name' => 'American Shorthair', 'species_id' => 1, ],
61+
[ 'id' => 2, 'name' => 'Maine Coon', 'species_id' => 1, ],
62+
[ 'id' => 3, 'name' => 'Persian', 'species_id' => 1, ],
63+
[ 'id' => 4, 'name' => 'Norwegian Forest', 'species_id' => 1, ],
64+
[ 'id' => 100, 'name' => 'Beagle', 'species_id' => 2, ],
65+
[ 'id' => 101, 'name' => 'Corgi', 'species_id' => 2, ],
66+
[ 'id' => 102, 'name' => 'Red Setter', 'species_id' => 2, ],
67+
[ 'id' => 200, 'name' => 'Arabian', 'species_id' => 3, ],
68+
[ 'id' => 201, 'name' => 'Clydesdale', 'species_id' => 3, ],
69+
[ 'id' => 202, 'name' => 'Mustang', 'species_id' => 3, ],
70+
]);
71+
72+
// setup user table
73+
$this->db->connection()->getSchemaBuilder()->create('pets', function (Blueprint $table) {
74+
$table->id();
75+
$table->string('name')->index();
76+
$table->string('age')->nullable();
77+
$table->date('last_visit')->nullable();
78+
$table->integer('species_id')->unsigned()->nullable();
79+
$table->integer('breed_id')->unsigned()->nullable();
80+
$table->foreign('species_id')->references('id')->on('species');
81+
$table->foreign('breed_id')->references('id')->on('breeds');
82+
});
83+
84+
Pet::insert([
85+
[ 'id' => 1, 'name' => 'Cartman', 'age' => 22, 'species_id' => 1, 'breed_id' => 4 ],
86+
[ 'id' => 2, 'name' => 'Tux', 'age' => 8, 'species_id' => 1, 'breed_id' => 4 ],
87+
[ 'id' => 3, 'name' => 'May', 'age' => 3, 'species_id' => 2, 'breed_id' => 102 ],
88+
[ 'id' => 4, 'name' => 'Ben', 'age' => 5, 'species_id' => 3, 'breed_id' => 200 ],
89+
[ 'id' => 5, 'name' => 'Chico', 'age' => 7, 'species_id' => 3, 'breed_id' => 202 ],
90+
]);
91+
}
92+
1093
protected function getPackageProviders($app)
1194
{
1295
return [
96+
//LivewireServiceProvider::class,
1397
LaravelLivewireTablesServiceProvider::class,
1498
];
1599
}
16100

17101
public function getEnvironmentSetUp($app)
18102
{
103+
$app['config']->set('app.debug', true);
19104
$app['config']->set('database.default', 'sqlite');
20105
$app['config']->set('database.connections.sqlite', [
21106
'driver' => 'sqlite',
22-
'database' => ':memory:',
107+
//'database' => 'tests/tests.sqlite',
108+
'database' => tempnam(sys_get_temp_dir(), 'LaravelLivewireTables'),
23109
'prefix' => '',
24110
]);
25111
}

tests/tests.sqlite

24 KB
Binary file not shown.

0 commit comments

Comments
 (0)