From 32888533c1574eaa1c83f8dd2e13b273bac47280 Mon Sep 17 00:00:00 2001 From: Deon George Date: Fri, 16 Jul 2021 16:55:26 +1000 Subject: [PATCH] Testing needs to have the database seeded --- .gitlab-test.yml | 1 + app/Providers/AppServiceProvider.php | 39 +++---------------- app/Traits/SingleOrFail.php | 51 +++++++++++++++++++++++++ database/seeders/DatabaseSeeder.php | 21 +++++----- database/seeders/InitialSetupSeeder.php | 8 ++-- 5 files changed, 74 insertions(+), 46 deletions(-) create mode 100644 app/Traits/SingleOrFail.php diff --git a/.gitlab-test.yml b/.gitlab-test.yml index 886b9b1..41d65bb 100644 --- a/.gitlab-test.yml +++ b/.gitlab-test.yml @@ -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 diff --git a/app/Providers/AppServiceProvider.php b/app/Providers/AppServiceProvider.php index b560169..9ebae38 100644 --- a/app/Providers/AppServiceProvider.php +++ b/app/Providers/AppServiceProvider.php @@ -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(); } } diff --git a/app/Traits/SingleOrFail.php b/app/Traits/SingleOrFail.php new file mode 100644 index 0000000..571761d --- /dev/null +++ b/app/Traits/SingleOrFail.php @@ -0,0 +1,51 @@ +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); + }); + } +} diff --git a/database/seeders/DatabaseSeeder.php b/database/seeders/DatabaseSeeder.php index 91cb6d1..b74d615 100644 --- a/database/seeders/DatabaseSeeder.php +++ b/database/seeders/DatabaseSeeder.php @@ -1,16 +1,19 @@ call(UsersTableSeeder::class); - } + /** + * Seed the application's database. + * + * @return void + */ + public function run() + { + //$this->call(InitialSetupSeeder::class); + $this->call(NodeHierarchy::class); + } } diff --git a/database/seeders/InitialSetupSeeder.php b/database/seeders/InitialSetupSeeder.php index d9aba02..d745a1c 100644 --- a/database/seeders/InitialSetupSeeder.php +++ b/database/seeders/InitialSetupSeeder.php @@ -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([