Skip to content

Commit f5513b6

Browse files
committed
adding test database
1 parent 910259b commit f5513b6

File tree

7 files changed

+226
-1
lines changed

7 files changed

+226
-1
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/ExampleTest.php

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

33
namespace Rappasoft\LaravelLivewireTables\Tests;
44

5+
use Illuminate\Container\Container;
6+
use Illuminate\Database\Capsule\Manager as DB;
7+
use Illuminate\Support\Facades\Facade;
8+
use Rappasoft\LaravelLivewireTables\Tests\Models\Species;
9+
510
class ExampleTest extends TestCase
611
{
712
/** @test */

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: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
}

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,11 +2,97 @@
22

33
namespace Rappasoft\LaravelLivewireTables\Tests;
44

5+
use Illuminate\Container\Container;
6+
use Illuminate\Database\Capsule\Manager as DB;
7+
use Illuminate\Database\Schema\Blueprint;
8+
use Illuminate\Support\Facades\Facade;
59
use Orchestra\Testbench\TestCase as Orchestra;
610
use Rappasoft\LaravelLivewireTables\LaravelLivewireTablesServiceProvider;
11+
use Rappasoft\LaravelLivewireTables\Tests\Models\Breed;
12+
use Rappasoft\LaravelLivewireTables\Tests\Models\Pet;
13+
use Rappasoft\LaravelLivewireTables\Tests\Models\Species;
714

815
class TestCase extends Orchestra
916
{
17+
protected $db;
18+
19+
/**
20+
* Setup the test environment.
21+
*/
22+
protected function setUp(): void
23+
{
24+
parent::setUp();
25+
$this->db = $db = new DB;
26+
27+
// define database
28+
$database = __DIR__ . '/tests.sqlite';
29+
30+
// remove database
31+
file_exists($database) ? unlink($database) : null;
32+
33+
// create database
34+
touch($database);
35+
36+
// setup connection
37+
$db->addConnection([
38+
'driver' => 'sqlite',
39+
'database' => $database,
40+
]);
41+
42+
$db->setAsGlobal();
43+
44+
// setup species table
45+
$this->db->connection()->getSchemaBuilder()->create('species', function (Blueprint $table) {
46+
$table->id();
47+
$table->string('name');
48+
});
49+
50+
Species::insert([
51+
[ 'id' => 1, 'name' => 'Cat', ],
52+
[ 'id' => 2, 'name' => 'Dog', ],
53+
]);
54+
55+
// setup breeds table
56+
$this->db->connection()->getSchemaBuilder()->create('breeds', function (Blueprint $table) {
57+
$table->id();
58+
$table->string('name');
59+
$table->integer('species_id')->unsigned();
60+
$table->foreign('species_id')->references('id')->on('species');
61+
});
62+
63+
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, ],
68+
[ 'id' => 100, 'name' => 'Beagle', 'species_id' => 2, ],
69+
[ 'id' => 101, 'name' => 'Corgi', 'species_id' => 2, ],
70+
[ 'id' => 102, 'name' => 'Red Setter', 'species_id' => 2, ],
71+
]);
72+
73+
// setup user table
74+
$this->db->connection()->getSchemaBuilder()->create('pets', function (Blueprint $table) {
75+
$table->id();
76+
$table->string('name')->index();
77+
$table->string('age')->nullable();
78+
$table->date('last_visit')->nullable();
79+
$table->integer('species_id')->unsigned()->nullable();
80+
$table->integer('breed_id')->unsigned()->nullable();
81+
$table->foreign('species_id')->references('id')->on('species');
82+
$table->foreign('breed_id')->references('id')->on('breeds');
83+
});
84+
85+
Pet::insert([
86+
[ 'id' => 1, 'name' => 'Cartman', 'age' => 22, 'species_id' => 1, 'breed_id' => 4 ],
87+
[ '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 ],
89+
]);
90+
91+
$container = new Container;
92+
$container->instance('db', $db->getDatabaseManager());
93+
Facade::setFacadeApplication($container);
94+
}
95+
1096
protected function getPackageProviders($app)
1197
{
1298
return [
@@ -19,7 +105,7 @@ public function getEnvironmentSetUp($app)
19105
$app['config']->set('database.default', 'sqlite');
20106
$app['config']->set('database.connections.sqlite', [
21107
'driver' => 'sqlite',
22-
'database' => ':memory:',
108+
'database' => __DIR__ . '/tests.sqlite',
23109
'prefix' => '',
24110
]);
25111
}

tests/tests.sqlite

24 KB
Binary file not shown.

0 commit comments

Comments
 (0)