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:
Nabeel S 2023-12-16 17:23:01 -05:00 committed by GitHub
parent 655f150570
commit ca565fc563
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 129 additions and 14 deletions

1
app/Database/seeds/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
vmsacars.yml

View File

@ -29,22 +29,21 @@ class Database
public static function seed_from_yaml_file($yaml_file, bool $ignore_errors = false): array
{
$yml = file_get_contents($yaml_file);
$yml = Yaml::parse($yml);
return static::seed_from_yaml($yml, $ignore_errors);
}
/**
* @param $yml
* @param bool $ignore_errors
*
* @throws \Exception
* @param mixed $yml
* @param bool $ignore_errors
*
* @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 = [];
$yml = Yaml::parse($yml);
if (empty($yml)) {
return $imported;
}
@ -62,7 +61,7 @@ class Database
$ignore_on_update = $data['ignore_on_update'];
}
$ignore_if_exists = true;
$ignore_if_exists = false;
if (array_key_exists('ignore_if_exists', $data)) {
$ignore_if_exists = $data['ignore_if_exists'];
}
@ -75,7 +74,14 @@ class Database
foreach ($rows as $row) {
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) {
if ($ignore_errors) {
continue;

View File

@ -154,7 +154,9 @@ class DatabaseActivator implements ActivatorInterface
{
$module = $this->getModuleByName($module->getName());
if (!$module) {
return;
$module = \App\Models\Module::create([
'name' => $module->name,
]);
}
$module->enabled = $active;

78
tests/DatabaseTest.php Normal file
View 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);
}
}

View File

@ -5,8 +5,10 @@ namespace Tests;
use App\Contracts\Factory;
use App\Contracts\Unit;
use App\Exceptions\Handler;
use App\Models\User;
use App\Repositories\SettingRepository;
use App\Services\DatabaseService;
use App\Services\ModuleService;
use Carbon\Carbon;
use DateTimeImmutable;
use Exception;
@ -20,6 +22,7 @@ use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Notification;
use Illuminate\Testing\TestResponse;
use Nwidart\Modules\Facades\Module;
use ReflectionClass;
/**
@ -33,19 +36,21 @@ abstract class TestCase extends \Illuminate\Foundation\Testing\TestCase
/**
* The base URL to use while testing the application.
*/
public static $prefix = '/api';
public static string $prefix = '/api';
protected $app;
protected $baseUrl = 'http://localhost';
protected $connectionsToTransact = ['test'];
protected string $baseUrl = 'http://localhost';
protected array $connectionsToTransact = ['test'];
/** @var \App\Models\User */
/** @var User */
protected $user;
protected static $auth_headers = [
protected static array $auth_headers = [
'x-api-key' => 'testadminapikey',
];
private Client $client;
/**
* @throws Exception
*/
@ -61,6 +66,16 @@ abstract class TestCase extends \Illuminate\Foundation\Testing\TestCase
Artisan::call('database:create', ['--reset' => 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();
// $this->disableExceptionHandling();

1
tests/data/.gitignore vendored Normal file
View File

@ -0,0 +1 @@
vmsacars.yml

12
tests/data/seed.yml Normal file
View 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'