From 32a21d2c05bdf7997d394ddba39af26c27205f4e Mon Sep 17 00:00:00 2001 From: PovilasKorop Date: Tue, 9 Nov 2021 10:29:27 +0200 Subject: [PATCH] Task 4 - Parent child delete --- README.md | 17 ++++++++++ app/Models/Category.php | 11 ++++++ app/Models/Product.php | 11 ++++++ database/factories/CategoryFactory.php | 20 +++++++++++ database/factories/ProductFactory.php | 21 ++++++++++++ ...1_11_09_082155_create_categories_table.php | 32 +++++++++++++++++ ...021_11_09_082205_create_products_table.php | 34 +++++++++++++++++++ tests/Feature/MigrationsTest.php | 14 ++++++++ 8 files changed, 160 insertions(+) create mode 100644 app/Models/Category.php create mode 100644 app/Models/Product.php create mode 100644 database/factories/CategoryFactory.php create mode 100644 database/factories/ProductFactory.php create mode 100644 database/migrations/task4/2021_11_09_082155_create_categories_table.php create mode 100644 database/migrations/task4/2021_11_09_082205_create_products_table.php diff --git a/README.md b/README.md index dee6bf7..93a5bb7 100644 --- a/README.md +++ b/README.md @@ -8,6 +8,15 @@ In the very beginning, if you run `php artisan test`, or `vendor/bin/phpunit`, a Your task is to make those tests pass. +## IMPORTANT NOTICE - TESTING DATABASE IS MYSQL + +**TESTS ARE CONFIGURED TO RUN ON A LOCAL MYSQL DATABASE (NOT SQLITE) WHICH SHOULD BE CALLED "mysql_testing"** + +**DON'T FORGET TO CREATE THAT DATABASE** + +**ALSO, THAT DB WILL BE WIPED A LOT WITHIN TESTS BY "migrate:fresh"** + + ## How to Submit Your Solution If you want to submit your solution, you should make a Pull Request to the `main` branch. @@ -52,3 +61,11 @@ Test method `test_soft_deletes()`. --- +## Task 4. Auto-Delete Related Records + +Folder `database/migrations/task4` contains migrations for category and products tables. You need to modify the products migration, so that deleting the category would auto-delete its products, instead of throwing an error. + +Test method `test_delete_parent_child_record()`. + +--- + diff --git a/app/Models/Category.php b/app/Models/Category.php new file mode 100644 index 0000000..c9d6222 --- /dev/null +++ b/app/Models/Category.php @@ -0,0 +1,11 @@ + $this->faker->text(20), + ]; + } +} diff --git a/database/factories/ProductFactory.php b/database/factories/ProductFactory.php new file mode 100644 index 0000000..fe0e7d2 --- /dev/null +++ b/database/factories/ProductFactory.php @@ -0,0 +1,21 @@ + 1, + 'name' => $this->faker->text(20), + ]; + } +} diff --git a/database/migrations/task4/2021_11_09_082155_create_categories_table.php b/database/migrations/task4/2021_11_09_082155_create_categories_table.php new file mode 100644 index 0000000..5789eab --- /dev/null +++ b/database/migrations/task4/2021_11_09_082155_create_categories_table.php @@ -0,0 +1,32 @@ +id(); + $table->string('name'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('categories'); + } +} diff --git a/database/migrations/task4/2021_11_09_082205_create_products_table.php b/database/migrations/task4/2021_11_09_082205_create_products_table.php new file mode 100644 index 0000000..7863601 --- /dev/null +++ b/database/migrations/task4/2021_11_09_082205_create_products_table.php @@ -0,0 +1,34 @@ +id(); + $table->foreignId('category_id')->constrained(); + $table->string('name'); + $table->timestamps(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::dropIfExists('products'); + } +} diff --git a/tests/Feature/MigrationsTest.php b/tests/Feature/MigrationsTest.php index 403a284..e76f1c1 100644 --- a/tests/Feature/MigrationsTest.php +++ b/tests/Feature/MigrationsTest.php @@ -2,6 +2,8 @@ namespace Tests\Feature; +use App\Models\Category; +use App\Models\Product; use App\Models\Project; use App\Models\User; use Illuminate\Support\Facades\Artisan; @@ -51,4 +53,16 @@ class MigrationsTest extends TestCase $project = Project::factory()->create(); $project->delete(); } + + public function test_delete_parent_child_record() + { + // We just test if the test succeeds or throws an exception + $this->expectNotToPerformAssertions(); + + Artisan::call('migrate:fresh', ['--path' => '/database/migrations/task4']); + + $category = Category::factory()->create(); + Product::factory()->create(); + $category->delete(); + } }