Fix bug in seeder with missing optional param (#1724)
* Fix bug in seeder with optional missing param which was causing seeds to be skipped * Style fix * gitignore file path * Additional test case * Split up test cases
This commit is contained in:
parent
655f150570
commit
ca565fc563
1
app/Database/seeds/.gitignore
vendored
Normal file
1
app/Database/seeds/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
vmsacars.yml
|
||||||
@ -29,22 +29,21 @@ class Database
|
|||||||
public static function seed_from_yaml_file($yaml_file, bool $ignore_errors = false): array
|
public static function seed_from_yaml_file($yaml_file, bool $ignore_errors = false): array
|
||||||
{
|
{
|
||||||
$yml = file_get_contents($yaml_file);
|
$yml = file_get_contents($yaml_file);
|
||||||
|
$yml = Yaml::parse($yml);
|
||||||
|
|
||||||
return static::seed_from_yaml($yml, $ignore_errors);
|
return static::seed_from_yaml($yml, $ignore_errors);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @param $yml
|
* @param mixed $yml
|
||||||
* @param bool $ignore_errors
|
* @param bool $ignore_errors
|
||||||
*
|
|
||||||
* @throws \Exception
|
|
||||||
*
|
*
|
||||||
* @return array
|
* @return array
|
||||||
*/
|
*/
|
||||||
public static function seed_from_yaml($yml, bool $ignore_errors = false): array
|
public static function seed_from_yaml(mixed $yml, bool $ignore_errors = false): array
|
||||||
{
|
{
|
||||||
$imported = [];
|
$imported = [];
|
||||||
$yml = Yaml::parse($yml);
|
|
||||||
if (empty($yml)) {
|
if (empty($yml)) {
|
||||||
return $imported;
|
return $imported;
|
||||||
}
|
}
|
||||||
@ -62,7 +61,7 @@ class Database
|
|||||||
$ignore_on_update = $data['ignore_on_update'];
|
$ignore_on_update = $data['ignore_on_update'];
|
||||||
}
|
}
|
||||||
|
|
||||||
$ignore_if_exists = true;
|
$ignore_if_exists = false;
|
||||||
if (array_key_exists('ignore_if_exists', $data)) {
|
if (array_key_exists('ignore_if_exists', $data)) {
|
||||||
$ignore_if_exists = $data['ignore_if_exists'];
|
$ignore_if_exists = $data['ignore_if_exists'];
|
||||||
}
|
}
|
||||||
@ -75,7 +74,14 @@ class Database
|
|||||||
|
|
||||||
foreach ($rows as $row) {
|
foreach ($rows as $row) {
|
||||||
try {
|
try {
|
||||||
static::insert_row($table, $row, $id_column, $ignore_on_update, $ignore_if_exists);
|
static::insert_row(
|
||||||
|
$table,
|
||||||
|
$row,
|
||||||
|
$id_column,
|
||||||
|
$ignore_on_update,
|
||||||
|
true,
|
||||||
|
$ignore_if_exists
|
||||||
|
);
|
||||||
} catch (QueryException $e) {
|
} catch (QueryException $e) {
|
||||||
if ($ignore_errors) {
|
if ($ignore_errors) {
|
||||||
continue;
|
continue;
|
||||||
|
|||||||
@ -154,7 +154,9 @@ class DatabaseActivator implements ActivatorInterface
|
|||||||
{
|
{
|
||||||
$module = $this->getModuleByName($module->getName());
|
$module = $this->getModuleByName($module->getName());
|
||||||
if (!$module) {
|
if (!$module) {
|
||||||
return;
|
$module = \App\Models\Module::create([
|
||||||
|
'name' => $module->name,
|
||||||
|
]);
|
||||||
}
|
}
|
||||||
|
|
||||||
$module->enabled = $active;
|
$module->enabled = $active;
|
||||||
|
|||||||
78
tests/DatabaseTest.php
Normal file
78
tests/DatabaseTest.php
Normal file
@ -0,0 +1,78 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace Tests;
|
||||||
|
|
||||||
|
use App\Services\DatabaseService;
|
||||||
|
use App\Support\Database;
|
||||||
|
use Symfony\Component\Yaml\Yaml;
|
||||||
|
|
||||||
|
class DatabaseTest extends TestCase
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Make sure the seeder works correctly
|
||||||
|
*/
|
||||||
|
public function testSeeder()
|
||||||
|
{
|
||||||
|
/** @var DatabaseService $dbSvc */
|
||||||
|
$file = file_get_contents(base_path('tests/data/seed.yml'));
|
||||||
|
$yml = Yaml::parse($file);
|
||||||
|
|
||||||
|
Database::seed_from_yaml($yml);
|
||||||
|
$value = setting('test.setting');
|
||||||
|
$this->assertEquals('default', $value);
|
||||||
|
|
||||||
|
// Try updating the value now
|
||||||
|
$yml['settings']['data'][0]['value'] = 'changed';
|
||||||
|
|
||||||
|
// The value shouldn't change here
|
||||||
|
Database::seed_from_yaml($yml);
|
||||||
|
$value = setting('test.setting');
|
||||||
|
$this->assertEquals('default', $value);
|
||||||
|
|
||||||
|
// Now the value should change
|
||||||
|
$yml['settings']['ignore_on_update'] = [];
|
||||||
|
Database::seed_from_yaml($yml);
|
||||||
|
$value = setting('test.setting');
|
||||||
|
$this->assertEquals('changed', $value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testSeederValueIgnoreValue()
|
||||||
|
{
|
||||||
|
/** @var DatabaseService $dbSvc */
|
||||||
|
$file = file_get_contents(base_path('tests/data/seed.yml'));
|
||||||
|
$yml = Yaml::parse($file);
|
||||||
|
|
||||||
|
Database::seed_from_yaml($yml);
|
||||||
|
$value = setting('test.setting');
|
||||||
|
$this->assertEquals('default', $value);
|
||||||
|
|
||||||
|
// Try updating the value now
|
||||||
|
$yml['settings']['data'][0]['value'] = 'changed';
|
||||||
|
|
||||||
|
// The value shouldn't change here
|
||||||
|
Database::seed_from_yaml($yml);
|
||||||
|
$value = setting('test.setting');
|
||||||
|
$this->assertEquals('default', $value);
|
||||||
|
}
|
||||||
|
|
||||||
|
public function testSeederDontIgnoreValue()
|
||||||
|
{
|
||||||
|
/** @var DatabaseService $dbSvc */
|
||||||
|
$file = file_get_contents(base_path('tests/data/seed.yml'));
|
||||||
|
$yml = Yaml::parse($file);
|
||||||
|
|
||||||
|
$yml['settings']['ignore_on_update'] = [];
|
||||||
|
|
||||||
|
Database::seed_from_yaml($yml);
|
||||||
|
$value = setting('test.setting');
|
||||||
|
$this->assertEquals('default', $value);
|
||||||
|
|
||||||
|
// Change the value
|
||||||
|
$yml['settings']['data'][0]['value'] = 'changed';
|
||||||
|
|
||||||
|
// Now the value should change
|
||||||
|
Database::seed_from_yaml($yml);
|
||||||
|
$value = setting('test.setting');
|
||||||
|
$this->assertEquals('changed', $value);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -5,8 +5,10 @@ namespace Tests;
|
|||||||
use App\Contracts\Factory;
|
use App\Contracts\Factory;
|
||||||
use App\Contracts\Unit;
|
use App\Contracts\Unit;
|
||||||
use App\Exceptions\Handler;
|
use App\Exceptions\Handler;
|
||||||
|
use App\Models\User;
|
||||||
use App\Repositories\SettingRepository;
|
use App\Repositories\SettingRepository;
|
||||||
use App\Services\DatabaseService;
|
use App\Services\DatabaseService;
|
||||||
|
use App\Services\ModuleService;
|
||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
use DateTimeImmutable;
|
use DateTimeImmutable;
|
||||||
use Exception;
|
use Exception;
|
||||||
@ -20,6 +22,7 @@ use Illuminate\Support\Facades\Artisan;
|
|||||||
use Illuminate\Support\Facades\Log;
|
use Illuminate\Support\Facades\Log;
|
||||||
use Illuminate\Support\Facades\Notification;
|
use Illuminate\Support\Facades\Notification;
|
||||||
use Illuminate\Testing\TestResponse;
|
use Illuminate\Testing\TestResponse;
|
||||||
|
use Nwidart\Modules\Facades\Module;
|
||||||
use ReflectionClass;
|
use ReflectionClass;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -33,19 +36,21 @@ abstract class TestCase extends \Illuminate\Foundation\Testing\TestCase
|
|||||||
/**
|
/**
|
||||||
* The base URL to use while testing the application.
|
* The base URL to use while testing the application.
|
||||||
*/
|
*/
|
||||||
public static $prefix = '/api';
|
public static string $prefix = '/api';
|
||||||
|
|
||||||
protected $app;
|
protected $app;
|
||||||
protected $baseUrl = 'http://localhost';
|
protected string $baseUrl = 'http://localhost';
|
||||||
protected $connectionsToTransact = ['test'];
|
protected array $connectionsToTransact = ['test'];
|
||||||
|
|
||||||
/** @var \App\Models\User */
|
/** @var User */
|
||||||
protected $user;
|
protected $user;
|
||||||
|
|
||||||
protected static $auth_headers = [
|
protected static array $auth_headers = [
|
||||||
'x-api-key' => 'testadminapikey',
|
'x-api-key' => 'testadminapikey',
|
||||||
];
|
];
|
||||||
|
|
||||||
|
private Client $client;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
@ -61,6 +66,16 @@ abstract class TestCase extends \Illuminate\Foundation\Testing\TestCase
|
|||||||
Artisan::call('database:create', ['--reset' => true]);
|
Artisan::call('database:create', ['--reset' => true]);
|
||||||
Artisan::call('migrate', ['--env' => 'testing', '--force' => true]);
|
Artisan::call('migrate', ['--env' => 'testing', '--force' => true]);
|
||||||
|
|
||||||
|
/** @var ModuleService $moduleSvc */
|
||||||
|
$moduleSvc = app(ModuleService::class);
|
||||||
|
$modules = Module::all();
|
||||||
|
|
||||||
|
// Call migrate on all modules
|
||||||
|
/** @var \Nwidart\Modules\Module[] $modules */
|
||||||
|
foreach ($modules as $module) {
|
||||||
|
$moduleSvc->addModule($module->getName());
|
||||||
|
}
|
||||||
|
|
||||||
Notification::fake();
|
Notification::fake();
|
||||||
// $this->disableExceptionHandling();
|
// $this->disableExceptionHandling();
|
||||||
|
|
||||||
|
|||||||
1
tests/data/.gitignore
vendored
Normal file
1
tests/data/.gitignore
vendored
Normal file
@ -0,0 +1 @@
|
|||||||
|
vmsacars.yml
|
||||||
12
tests/data/seed.yml
Normal file
12
tests/data/seed.yml
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
settings:
|
||||||
|
ignore_on_update:
|
||||||
|
- value
|
||||||
|
data:
|
||||||
|
- id: test_setting
|
||||||
|
key: test.setting
|
||||||
|
name: 'A test setting'
|
||||||
|
group: general
|
||||||
|
value: 'default'
|
||||||
|
options: ''
|
||||||
|
type: select
|
||||||
|
description: 'a test setting'
|
||||||
Loading…
Reference in New Issue
Block a user