Skip to content

Commit

Permalink
Merge branch 'feature-testing' of https://github.com/DeltaSystems/lar…
Browse files Browse the repository at this point in the history
…avel-livewire-tables into DeltaSystems-feature-testing
  • Loading branch information
rappasoft committed Apr 21, 2021
2 parents 1b3fe44 + 8405c6a commit d8d7919
Show file tree
Hide file tree
Showing 10 changed files with 344 additions and 13 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"illuminate/contracts": "^8.0"
},
"require-dev": {
"ext-sqlite3": "*",
"orchestra/testbench": "^6.13",
"phpunit/phpunit": "^9.3",
"spatie/laravel-ray": "^1.9",
Expand Down
Binary file added tests.sqlite
Binary file not shown.
75 changes: 75 additions & 0 deletions tests/DataTableComponentTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

namespace Rappasoft\LaravelLivewireTables\Tests;

use Illuminate\Contracts\Pagination\LengthAwarePaginator;
use Illuminate\Database\Eloquent\Builder;
use Rappasoft\LaravelLivewireTables\DataTableComponent;
use Rappasoft\LaravelLivewireTables\Tests\Http\Livewire\PetsTable;

class DataTableComponentTest extends TestCase
{
protected DataTableComponent $table;

public function setUp(): void
{
parent::setUp();

$this->table = $table = new PetsTable();
}

/** @test */
public function bootstrap_test_datatable()
{
$this->assertInstanceOf(DataTableComponent::class, $this->table);
}

/** @test */
public function test_query()
{
$query = $this->table->query();

$this->assertInstanceOf(Builder::class, $query);
}

/** @test */
public function test_columns()
{
$columns = $this->table->columns();

$this->assertIsArray($columns);
$this->assertCount(5, $columns);
}

/** @test */
public function test_rows()
{
$rows = $this->table->rows;

$this->assertInstanceOf(LengthAwarePaginator::class, $rows);
$this->assertEquals(5, $this->table->getRowsProperty()->total());
}

/** @test */
public function test_search_filter()
{
$this->table->filters['search'] = 'Cartman';
$this->assertEquals(1, $this->table->getRowsProperty()->total());
}

/** @test */
public function test_search_filter_reset()
{
$this->table->filters['search'] = 'Cartman';
$this->table->resetFilters();
$this->assertEquals(1, $this->table->rows->total());
}

/** @test */
public function test_search_filter_remove()
{
$this->table->filters['search'] = 'Cartman';
$this->table->removeFilter('search');
$this->assertEquals(5, $this->table->rows->total());
}
}
12 changes: 0 additions & 12 deletions tests/ExampleTest.php

This file was deleted.

32 changes: 32 additions & 0 deletions tests/Http/Livewire/PetsTable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace Rappasoft\LaravelLivewireTables\Tests\Http\Livewire;

use Illuminate\Database\Eloquent\Builder;
use Rappasoft\LaravelLivewireTables\DataTableComponent;
use Rappasoft\LaravelLivewireTables\Tests\Models\Pet;
use Rappasoft\LaravelLivewireTables\Views\Column;

class PetsTable extends DataTableComponent
{
/**
* @return Builder
*/
public function query() : Builder
{
return Pet::query()->when($this->getFilter('search'), function (Builder $query, $search) {
$query->where('name', 'like', '%'.$search.'%');
});
}

public function columns(): array
{
return [
Column::make('Name', 'name'),
Column::make('Age', 'age'),
Column::make('Last Visit', 'last_visit'),
Column::make('Species', 'species.name'),
Column::make('Breed', 'breed.name'),
];
}
}
43 changes: 43 additions & 0 deletions tests/Models/Breed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace Rappasoft\LaravelLivewireTables\Tests\Models;

use Illuminate\Database\Eloquent\Model;

/**
* @property int $id
* @property string $name
* @property int $species_id
*/
class Breed extends Model
{
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'breeds';

/**
* The primary key for the model.
*
* @var string
*/
protected $primaryKey = 'id';

/**
* @var bool
*/
public $timestamps = false;

/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'id',
'name',
'species_id',
];
}
65 changes: 65 additions & 0 deletions tests/Models/Pet.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

namespace Rappasoft\LaravelLivewireTables\Tests\Models;

use Illuminate\Database\Eloquent\Model;

/**
* @property int $id
* @property string $name
* @property string $age
* @property string $last_visit
* @property int $species_id
* @property int $breed_id
*/
class Pet extends Model
{
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'pets';

/**
* The primary key for the model.
*
* @var string
*/
protected $primaryKey = 'id';

/**
* @var bool
*/
public $timestamps = false;

/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'id',
'name',
'age',
'last_visit',
'species_id',
'breed_id',
];

/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo|Species[]
*/
public function species()
{
return $this->belongsTo(Species::class);
}

/**
* @return \Illuminate\Database\Eloquent\Relations\BelongsTo|Breed[]
*/
public function breeds()
{
return $this->belongsTo(Breed::class);
}
}
41 changes: 41 additions & 0 deletions tests/Models/Species.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Rappasoft\LaravelLivewireTables\Tests\Models;

use Illuminate\Database\Eloquent\Model;

/**
* @property int $id
* @property string $name
*/
class Species extends Model
{
/**
* The table associated with the model.
*
* @var string
*/
protected $table = 'species';

/**
* The primary key for the model.
*
* @var string
*/
protected $primaryKey = 'id';

/**
* @var bool
*/
public $timestamps = false;

/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = [
'id',
'name',
];
}
88 changes: 87 additions & 1 deletion tests/TestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,110 @@

namespace Rappasoft\LaravelLivewireTables\Tests;

use Illuminate\Database\Capsule\Manager as DB;
use Illuminate\Database\Schema\Blueprint;
use Livewire\LivewireServiceProvider;
use Orchestra\Testbench\TestCase as Orchestra;
use Rappasoft\LaravelLivewireTables\LaravelLivewireTablesServiceProvider;
use Rappasoft\LaravelLivewireTables\Tests\Models\Breed;
use Rappasoft\LaravelLivewireTables\Tests\Models\Pet;
use Rappasoft\LaravelLivewireTables\Tests\Models\Species;

class TestCase extends Orchestra
{
protected $db;

/**
* Setup the test environment.
*/
protected function setUp(): void
{
parent::setUp();

$this->db = $db = new DB;

// grab database
$database = $this->app['config']['database']['connections']['sqlite']['database'];

// setup connection
$db->addConnection([
'driver' => 'sqlite',
'database' => $database,
]);

$db->setAsGlobal();

// setup species table
$this->db->connection()->getSchemaBuilder()->create('species', function (Blueprint $table) {
$table->id();
$table->string('name');
});

Species::insert([
[ 'id' => 1, 'name' => 'Cat', ],
[ 'id' => 2, 'name' => 'Dog', ],
[ 'id' => 3, 'name' => 'Horse', ],
[ 'id' => 4, 'name' => 'Bird', ],
]);

// setup breeds table
$this->db->connection()->getSchemaBuilder()->create('breeds', function (Blueprint $table) {
$table->id();
$table->string('name');
$table->integer('species_id')->unsigned();
$table->foreign('species_id')->references('id')->on('species');
});

Breed::insert([
[ 'id' => 1, 'name' => 'American Shorthair', 'species_id' => 1, ],
[ 'id' => 2, 'name' => 'Maine Coon', 'species_id' => 1, ],
[ 'id' => 3, 'name' => 'Persian', 'species_id' => 1, ],
[ 'id' => 4, 'name' => 'Norwegian Forest', 'species_id' => 1, ],
[ 'id' => 100, 'name' => 'Beagle', 'species_id' => 2, ],
[ 'id' => 101, 'name' => 'Corgi', 'species_id' => 2, ],
[ 'id' => 102, 'name' => 'Red Setter', 'species_id' => 2, ],
[ 'id' => 200, 'name' => 'Arabian', 'species_id' => 3, ],
[ 'id' => 201, 'name' => 'Clydesdale', 'species_id' => 3, ],
[ 'id' => 202, 'name' => 'Mustang', 'species_id' => 3, ],
]);

// setup user table
$this->db->connection()->getSchemaBuilder()->create('pets', function (Blueprint $table) {
$table->id();
$table->string('name')->index();
$table->string('age')->nullable();
$table->date('last_visit')->nullable();
$table->integer('species_id')->unsigned()->nullable();
$table->integer('breed_id')->unsigned()->nullable();
$table->foreign('species_id')->references('id')->on('species');
$table->foreign('breed_id')->references('id')->on('breeds');
});

Pet::insert([
[ 'id' => 1, 'name' => 'Cartman', 'age' => 22, 'species_id' => 1, 'breed_id' => 4 ],
[ 'id' => 2, 'name' => 'Tux', 'age' => 8, 'species_id' => 1, 'breed_id' => 4 ],
[ 'id' => 3, 'name' => 'May', 'age' => 3, 'species_id' => 2, 'breed_id' => 102 ],
[ 'id' => 4, 'name' => 'Ben', 'age' => 5, 'species_id' => 3, 'breed_id' => 200 ],
[ 'id' => 5, 'name' => 'Chico', 'age' => 7, 'species_id' => 3, 'breed_id' => 202 ],
]);
}

protected function getPackageProviders($app)
{
return [
//LivewireServiceProvider::class,
LaravelLivewireTablesServiceProvider::class,
];
}

public function getEnvironmentSetUp($app)
{
$app['config']->set('app.debug', true);
$app['config']->set('database.default', 'sqlite');
$app['config']->set('database.connections.sqlite', [
'driver' => 'sqlite',
'database' => ':memory:',
//'database' => 'tests/tests.sqlite',
'database' => tempnam(sys_get_temp_dir(), 'LaravelLivewireTables'),
'prefix' => '',
]);
}
Expand Down
Binary file added tests/tests.sqlite
Binary file not shown.

0 comments on commit d8d7919

Please sign in to comment.