From 888d5d87c2d1ce0dfe8480c8f0d8038a6270c91f Mon Sep 17 00:00:00 2001 From: PovilasKorop Date: Tue, 9 Nov 2021 10:44:29 +0200 Subject: [PATCH] Task 6 - duplicate values --- README.md | 8 +++++ app/Models/Company.php | 13 ++++++++ ...21_11_09_083843_create_companies_table.php | 33 +++++++++++++++++++ tests/Feature/MigrationsTest.php | 14 ++++++++ 4 files changed, 68 insertions(+) create mode 100644 app/Models/Company.php create mode 100644 database/migrations/task6/2021_11_09_083843_create_companies_table.php 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']); + } }