diff --git a/README.md b/README.md index f4551d6..d1fe8ed 100644 --- a/README.md +++ b/README.md @@ -79,3 +79,11 @@ Test method `test_repeating_column_table()`. --- +## Task 6. Duplicate Column Value + +Folder `database/migrations/task6` contains a migration for companies table. Edit that migration, so that it would be impossible to create multiple companies with the same name. + +Test method `test_duplicate_name()`. + +--- + diff --git a/app/Models/Company.php b/app/Models/Company.php new file mode 100644 index 0000000..6eda071 --- /dev/null +++ b/app/Models/Company.php @@ -0,0 +1,13 @@ +id(); + $table->string('name'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('companies'); + } +} diff --git a/tests/Feature/MigrationsTest.php b/tests/Feature/MigrationsTest.php index b6f0a56..aaafe29 100644 --- a/tests/Feature/MigrationsTest.php +++ b/tests/Feature/MigrationsTest.php @@ -3,9 +3,11 @@ namespace Tests\Feature; use App\Models\Category; +use App\Models\Company; use App\Models\Product; use App\Models\Project; use App\Models\User; +use Illuminate\Database\QueryException; use Illuminate\Support\Facades\Artisan; use Tests\TestCase; @@ -72,4 +74,16 @@ class MigrationsTest extends TestCase Artisan::call('migrate:fresh', ['--path' => '/database/migrations/task5']); } + + public function test_duplicate_name() + { + // We expect that the second Company::create() would throw an exception like + // "Integrity constraint violation: 1062 Duplicate entry 'Company One'" + $this->expectException(QueryException::class); + + Artisan::call('migrate:fresh', ['--path' => '/database/migrations/task6']); + + Company::create(['name' => 'Company One']); + Company::create(['name' => 'Company One']); + } }