Skip to content

Commit d228c2d

Browse files
committed
adding first tests
1 parent 064299f commit d228c2d

File tree

6 files changed

+139
-23
lines changed

6 files changed

+139
-23
lines changed

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/Pet.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,20 @@ class Pet extends Model
4646
'species_id',
4747
'breed_id',
4848
];
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+
}
4965
}

tests/TestCase.php

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

33
namespace Rappasoft\LaravelLivewireTables\Tests;
44

5-
use Illuminate\Container\Container;
65
use Illuminate\Database\Capsule\Manager as DB;
76
use Illuminate\Database\Schema\Blueprint;
8-
use Illuminate\Support\Facades\Facade;
7+
use Livewire\LivewireServiceProvider;
98
use Orchestra\Testbench\TestCase as Orchestra;
109
use Rappasoft\LaravelLivewireTables\LaravelLivewireTablesServiceProvider;
1110
use Rappasoft\LaravelLivewireTables\Tests\Models\Breed;
@@ -22,6 +21,7 @@ class TestCase extends Orchestra
2221
protected function setUp(): void
2322
{
2423
parent::setUp();
24+
2525
$this->db = $db = new DB;
2626

2727
// define database
@@ -50,6 +50,8 @@ protected function setUp(): void
5050
Species::insert([
5151
[ 'id' => 1, 'name' => 'Cat', ],
5252
[ 'id' => 2, 'name' => 'Dog', ],
53+
[ 'id' => 3, 'name' => 'Horse', ],
54+
[ 'id' => 4, 'name' => 'Bird', ],
5355
]);
5456

5557
// setup breeds table
@@ -61,13 +63,16 @@ protected function setUp(): void
6163
});
6264

6365
Breed::insert([
64-
[ 'id' => 1, 'name' => 'Maine Coon', 'species_id' => 1, ],
65-
[ 'id' => 2, 'name' => 'Persian', 'species_id' => 1, ],
66-
[ 'id' => 3, 'name' => 'Norwegian Forest', 'species_id' => 1, ],
67-
[ 'id' => 4, 'name' => 'American Shorthair', 'species_id' => 1, ],
66+
[ 'id' => 1, 'name' => 'American Shorthair', 'species_id' => 1, ],
67+
[ 'id' => 2, 'name' => 'Maine Coon', 'species_id' => 1, ],
68+
[ 'id' => 3, 'name' => 'Persian', 'species_id' => 1, ],
69+
[ 'id' => 4, 'name' => 'Norwegian Forest', 'species_id' => 1, ],
6870
[ 'id' => 100, 'name' => 'Beagle', 'species_id' => 2, ],
6971
[ 'id' => 101, 'name' => 'Corgi', 'species_id' => 2, ],
7072
[ 'id' => 102, 'name' => 'Red Setter', 'species_id' => 2, ],
73+
[ 'id' => 200, 'name' => 'Arabian', 'species_id' => 3, ],
74+
[ 'id' => 201, 'name' => 'Clydesdale', 'species_id' => 3, ],
75+
[ 'id' => 202, 'name' => 'Mustang', 'species_id' => 3, ],
7176
]);
7277

7378
// setup user table
@@ -85,23 +90,23 @@ protected function setUp(): void
8590
Pet::insert([
8691
[ 'id' => 1, 'name' => 'Cartman', 'age' => 22, 'species_id' => 1, 'breed_id' => 4 ],
8792
[ 'id' => 2, 'name' => 'Tux', 'age' => 8, 'species_id' => 1, 'breed_id' => 4 ],
88-
[ 'id' => 3, 'name' => 'May', 'age' => 8, 'species_id' => 2, 'breed_id' => 102 ],
93+
[ 'id' => 3, 'name' => 'May', 'age' => 3, 'species_id' => 2, 'breed_id' => 102 ],
94+
[ 'id' => 4, 'name' => 'Ben', 'age' => 5, 'species_id' => 3, 'breed_id' => 200 ],
95+
[ 'id' => 5, 'name' => 'Chico', 'age' => 7, 'species_id' => 3, 'breed_id' => 202 ],
8996
]);
90-
91-
$container = new Container;
92-
$container->instance('db', $db->getDatabaseManager());
93-
Facade::setFacadeApplication($container);
9497
}
9598

9699
protected function getPackageProviders($app)
97100
{
98101
return [
102+
//LivewireServiceProvider::class,
99103
LaravelLivewireTablesServiceProvider::class,
100104
];
101105
}
102106

103107
public function getEnvironmentSetUp($app)
104108
{
109+
$app['config']->set('app.debug', true);
105110
$app['config']->set('database.default', 'sqlite');
106111
$app['config']->set('database.connections.sqlite', [
107112
'driver' => 'sqlite',

tests/tests.sqlite

0 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)