Testing needs to have the database seeded

This commit is contained in:
Deon George 2021-07-16 16:55:26 +10:00
parent a0d3c8d8ab
commit 32888533c1
5 changed files with 74 additions and 46 deletions

View File

@ -28,6 +28,7 @@ test:
# Generate an application key. Re-cache. # Generate an application key. Re-cache.
- php artisan key:generate - php artisan key:generate
- php artisan migrate - php artisan migrate
- php artisan db:seed
script: script:
# run laravel tests # run laravel tests

View File

@ -2,12 +2,15 @@
namespace App\Providers; namespace App\Providers;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\ServiceProvider; use Illuminate\Support\ServiceProvider;
use Laravel\Passport\Passport; use Laravel\Passport\Passport;
use App\Traits\SingleOrFail;
class AppServiceProvider extends ServiceProvider class AppServiceProvider extends ServiceProvider
{ {
use SingleOrFail;
/** /**
* Register any application services. * Register any application services.
* *
@ -25,38 +28,6 @@ class AppServiceProvider extends ServiceProvider
*/ */
public function boot() public function boot()
{ {
// When a query should return 1 object, or FAIL if it doesnt static::bootSingleOrFail();
Builder::macro('singleOrFail',function () {
$result = $this->get();
if (($x=$result->count()) == 1) {
return $result->first();
}
throw new \Exception(sprintf('Query brings back %d record(s) called for singleOrFail()',$x));
});
// When a query should return 1 object, or NULL if it doesnt
Builder::macro('single',function () {
$result = $this->get();
if ($result->count() == 1) {
return $result->first();
}
return NULL;
});
// When a query should return 1 object, or NULL if it doesnt
Builder::macro('singleOrNew',function ($args) {
//dd(['func'=>func_get_args(),'args'=>$args,'this'=>$this]);
$result = $this->where($args)->get();
if ($result->count() == 1) {
return $result->first();
}
return $this->newModelInstance($args);
});
} }
} }

View File

@ -0,0 +1,51 @@
<?php
/**
* Add eloquent queries single(), singleOrFail(), singleOrNew()
*/
namespace App\Traits;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\ModelNotFoundException;
trait SingleOrFail
{
private static function bootSingleOrFail()
{
// When a query should return 1 object, or FAIL if it doesnt
Builder::macro('singleOrFail',function () {
$result = $this->get();
if (($x=$result->count()) == 1) {
return $result->first();
}
if ($x == 0)
throw new ModelNotFoundException('Query brings back 0 record(s) called for singleOrFail()');
else
throw new \Exception(sprintf('Query brings back %d record(s) called for singleOrFail()',$x));
});
// When a query should return 1 object, or NULL if it doesnt
Builder::macro('single',function () {
$result = $this->get();
if ($result->count() == 1) {
return $result->first();
}
return NULL;
});
// When a query should return 1 object, or NULL if it doesnt
Builder::macro('singleOrNew',function ($args) {
$result = $this->where($args)->get();
if ($result->count() == 1) {
return $result->first();
}
return $this->newModelInstance($args);
});
}
}

View File

@ -1,16 +1,19 @@
<?php <?php
namespace Database\Seeders;
use Illuminate\Database\Seeder; use Illuminate\Database\Seeder;
class DatabaseSeeder extends Seeder class DatabaseSeeder extends Seeder
{ {
/** /**
* Seed the application's database. * Seed the application's database.
* *
* @return void * @return void
*/ */
public function run() public function run()
{ {
// $this->call(UsersTableSeeder::class); //$this->call(InitialSetupSeeder::class);
} $this->call(NodeHierarchy::class);
}
} }

View File

@ -5,6 +5,8 @@ namespace Database\Seeders;
use Illuminate\Database\Seeder; use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
use App\Models\Software;
class InitialSetupSeeder extends Seeder class InitialSetupSeeder extends Seeder
{ {
/** /**
@ -27,21 +29,21 @@ class InitialSetupSeeder extends Seeder
DB::table('software')->insert([ DB::table('software')->insert([
'name'=>'Custom', 'name'=>'Custom',
'active'=>TRUE, 'active'=>TRUE,
'type'=>Software::SOFTWARE_MAILER,
]); ]);
DB::table('domains')->insert([ DB::table('domains')->insert([
'name'=>'private', 'name'=>'private',
'default'=>TRUE, 'default'=>TRUE,
'active'=>TRUE, 'active'=>TRUE,
'public'=>TRUE,
'notes'=>'PrivateNet: Internal Testing Network'
]); ]);
DB::table('zones')->insert([ DB::table('zones')->insert([
'zone_id'=>'10', 'zone_id'=>'10',
'domain_id'=>1, 'domain_id'=>1,
'public'=>TRUE,
'active'=>TRUE, 'active'=>TRUE,
'name'=>'private',
'description'=>'PrivateNet: Internal Testing Network'
]); ]);
DB::table('nodes')->insert([ DB::table('nodes')->insert([