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.
- php artisan key:generate
- php artisan migrate
- php artisan db:seed
script:
# run laravel tests

View File

@ -2,12 +2,15 @@
namespace App\Providers;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Support\ServiceProvider;
use Laravel\Passport\Passport;
use App\Traits\SingleOrFail;
class AppServiceProvider extends ServiceProvider
{
use SingleOrFail;
/**
* Register any application services.
*
@ -25,38 +28,6 @@ class AppServiceProvider extends ServiceProvider
*/
public function boot()
{
// 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();
}
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);
});
static::bootSingleOrFail();
}
}

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