@@ -18,6 +18,7 @@ abstract class ModelCrudTestCase extends CIUnitTestCase
|
||||
protected function assertModelCanInsertAndRetrieve(string $modelClass): void
|
||||
{
|
||||
$model = $this->newModel($modelClass);
|
||||
$this->assertModelFieldsExist($model);
|
||||
$record = $this->fakeModelRecord($model);
|
||||
$id = $this->primaryKeyValue($record, $model->primaryKey);
|
||||
|
||||
@@ -32,6 +33,7 @@ abstract class ModelCrudTestCase extends CIUnitTestCase
|
||||
protected function assertModelCanUpdate(string $modelClass): void
|
||||
{
|
||||
$model = $this->newModel($modelClass);
|
||||
$this->assertModelFieldsExist($model);
|
||||
$record = $this->fakeModelRecord($model);
|
||||
$id = $this->primaryKeyValue($record, $model->primaryKey);
|
||||
$updateData = $this->updateData($model, $record);
|
||||
@@ -42,6 +44,7 @@ abstract class ModelCrudTestCase extends CIUnitTestCase
|
||||
protected function assertModelCanDelete(string $modelClass): void
|
||||
{
|
||||
$model = $this->newModel($modelClass);
|
||||
$this->assertModelFieldsExist($model);
|
||||
$record = $this->fakeModelRecord($model);
|
||||
$id = $this->primaryKeyValue($record, $model->primaryKey);
|
||||
|
||||
@@ -89,13 +92,34 @@ abstract class ModelCrudTestCase extends CIUnitTestCase
|
||||
|
||||
protected function fakeModelRecord(Model $model): array|object
|
||||
{
|
||||
if (! Database::connect('tests')->tableExists($model->table)) {
|
||||
$this->markTestSkipped(sprintf('Table %s is not present in the migrated test schema.', $model->table));
|
||||
}
|
||||
|
||||
return fake($model, $this->fabricatorOverrides($model));
|
||||
}
|
||||
|
||||
protected function assertModelFieldsExist(Model $model): void
|
||||
{
|
||||
$db = Database::connect('tests');
|
||||
|
||||
$this->assertTrue(
|
||||
$db->tableExists($model->table, false),
|
||||
sprintf('%s maps to table "%s", but that table is not present in the migrated test schema.', $model::class, $model->table)
|
||||
);
|
||||
|
||||
$databaseFields = $db->getFieldNames($model->table);
|
||||
|
||||
foreach ($model->allowedFields as $field) {
|
||||
$this->assertContains(
|
||||
$field,
|
||||
$databaseFields,
|
||||
sprintf(
|
||||
'%s allows field "%s", but table "%s" does not contain it.',
|
||||
$model::class,
|
||||
$field,
|
||||
$model->table
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
protected function fabricatorOverrides(Model $model): array
|
||||
{
|
||||
$overrides = [];
|
||||
@@ -184,9 +208,7 @@ abstract class ModelCrudTestCase extends CIUnitTestCase
|
||||
|
||||
private function newModel(string $modelClass): Model
|
||||
{
|
||||
if (! class_exists($modelClass)) {
|
||||
$this->markTestSkipped(sprintf('Model %s is not part of the application.', $modelClass));
|
||||
}
|
||||
$this->assertTrue(class_exists($modelClass), sprintf('Model %s is not part of the application.', $modelClass));
|
||||
|
||||
$model = new $modelClass();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user