From dd8e6c3166a963eb2750d5213b9b318562c57f82 Mon Sep 17 00:00:00 2001 From: alex-chevski Date: Fri, 22 Dec 2023 15:58:23 +0300 Subject: [PATCH 1/9] fix and correct tests --- tests/Feature/Plan/CreateTest.php | 4 +--- tests/Feature/Plan/ShowTest.php | 7 +++---- tests/Feature/Recipe/CreateTest.php | 16 +++++++--------- tests/Feature/Recipe/EditFormTest.php | 6 ++---- tests/Feature/Recipe/IndexTest.php | 5 ++--- tests/Feature/Recipe/ShowTest.php | 7 +++---- 6 files changed, 18 insertions(+), 27 deletions(-) diff --git a/tests/Feature/Plan/CreateTest.php b/tests/Feature/Plan/CreateTest.php index c96d2c1..34225b3 100644 --- a/tests/Feature/Plan/CreateTest.php +++ b/tests/Feature/Plan/CreateTest.php @@ -24,7 +24,7 @@ class CreateTest extends TestCase public function setUp(): void { - parent::setUp(); + $this->refreshApplicationWithLocale('en'); Storage::fake('plans'); $this->image = UploadedFile::fake()->image('plan.png'); @@ -32,7 +32,6 @@ class CreateTest extends TestCase $this->user = User::factory()->create(['is_consultant' => true]); $this->plan = Plan::factory()->make(); - $this->refreshApplicationWithLocale('en'); } public function test_show_form_create_plan() @@ -42,7 +41,6 @@ class CreateTest extends TestCase ->get(route('plan.create')); $response->assertSuccessful(); - // ->assertRedirect(route('home')); } public function test_publish_new_plan_success(): void diff --git a/tests/Feature/Plan/ShowTest.php b/tests/Feature/Plan/ShowTest.php index aa4cbb2..7c060d3 100644 --- a/tests/Feature/Plan/ShowTest.php +++ b/tests/Feature/Plan/ShowTest.php @@ -26,20 +26,19 @@ class ShowTest extends TestCase public function setUp(): void { - parent::setUp(); + $this->refreshApplicationWithLocale('en'); Storage::fake('plans'); $this->image = UploadedFile::fake()->image('plan.png'); $this->user = User::factory()->create(['is_consultant' => true]); $this->plan = Plan::inRandomOrder()->first(); - $this->refreshApplicationWithLocale('en'); } - public function test_show_form_success() + public function test_show_page_success() { $response = $this->actingAs($this->user) - ->get(route('plan.show', ['plan' => $this->plan->id])); + ->get(route('plan.show', $this->plan, false)); $response ->assertOk(); diff --git a/tests/Feature/Recipe/CreateTest.php b/tests/Feature/Recipe/CreateTest.php index 93a0310..5979aca 100644 --- a/tests/Feature/Recipe/CreateTest.php +++ b/tests/Feature/Recipe/CreateTest.php @@ -30,7 +30,7 @@ final class CreateTest extends TestCase public function setUp(): void { - parent::setUp(); + $this->refreshApplicationWithLocale('en'); Storage::fake('recipes'); @@ -38,11 +38,9 @@ final class CreateTest extends TestCase $this->user = User::factory()->create(['is_consultant' => true]); $this->recipe = Recipe::factory()->make(); - - $this->refreshApplicationWithLocale('en'); } - public function testShowFormCreateRecipe(): void + public function test_show_page_create_recipe(): void { $response = $this ->actingAs($this->user) @@ -51,7 +49,7 @@ final class CreateTest extends TestCase $response->assertSuccessful(); } - public function testPublishNewRecipeSuccess(): void + public function test_publish_new_recipe_success(): void { $this->actingAs($this->user); @@ -82,7 +80,7 @@ final class CreateTest extends TestCase self::assertEquals($count + 1, Recipe::count()); } - public function testToDraftNewRecipeSuccess(): void + public function test_to_draft_new_recipe_success(): void { $this->actingAs($this->user); @@ -115,7 +113,7 @@ final class CreateTest extends TestCase self::assertEquals(Recipe::find($count + 1)->state, 'archived'); } - public function testToPreviewNewRecipeSuccess(): void + public function test_to_preview_new_recipe_success(): void { $this->actingAs($this->user); @@ -148,7 +146,7 @@ final class CreateTest extends TestCase self::assertEquals(Recipe::find($count + 1)->state, 'archived'); } - public function testSetEmptyData(): void + public function test_set_empty_data(): void { $this->actingAs($this->user); @@ -169,7 +167,7 @@ final class CreateTest extends TestCase self::assertEquals($count, Recipe::count()); } - public function testIncorrectVideoUrl(): void + public function test_incorrect_video_url(): void { $this->actingAs($this->user); diff --git a/tests/Feature/Recipe/EditFormTest.php b/tests/Feature/Recipe/EditFormTest.php index 3975487..20c02b3 100644 --- a/tests/Feature/Recipe/EditFormTest.php +++ b/tests/Feature/Recipe/EditFormTest.php @@ -22,16 +22,14 @@ final class EditFormTest extends TestCase public function setUp(): void { - parent::setUp(); + $this->refreshApplicationWithLocale('en'); $this->user = User::factory()->create(['is_consultant' => true]); $this->recipe = Recipe::inRandomOrder()->first(); - - $this->refreshApplicationWithLocale('en'); } - public function testShowFormEditRecipe(): void + public function test_edit_page_recipe_success(): void { $response = $this ->actingAs($this->user) diff --git a/tests/Feature/Recipe/IndexTest.php b/tests/Feature/Recipe/IndexTest.php index d4ea94a..bc85a12 100644 --- a/tests/Feature/Recipe/IndexTest.php +++ b/tests/Feature/Recipe/IndexTest.php @@ -17,14 +17,13 @@ class IndexTest extends TestCase public function setUp(): void { - parent::setUp(); + $this->refreshApplicationWithLocale('en'); $this->user = User::factory()->create(['is_consultant' => true]); $this->recipe = Recipe::inRandomOrder()->first(); - $this->refreshApplicationWithLocale('en'); } - public function test_show_form_success() + public function test_index_page_success() { $response = $this ->actingAs($this->user) diff --git a/tests/Feature/Recipe/ShowTest.php b/tests/Feature/Recipe/ShowTest.php index 7e44036..6531298 100644 --- a/tests/Feature/Recipe/ShowTest.php +++ b/tests/Feature/Recipe/ShowTest.php @@ -17,17 +17,16 @@ class ShowTest extends TestCase public function setUp(): void { - parent::setUp(); + $this->refreshApplicationWithLocale('en'); $this->user = User::factory()->create(['is_consultant' => true]); $this->recipe = Recipe::inRandomOrder()->first(); - $this->refreshApplicationWithLocale('en'); } - public function test_show_form_success() + public function test_show_page_success() { $response = $this->actingAs($this->user) - ->get(route('recipe.show', ['recipe' => $this->recipe->id])); + ->get(route('recipe.show', $this->plan, false)); $response ->assertOk(); From 24c5cfdaacad0f724a91131a0db23b9a900ba87b Mon Sep 17 00:00:00 2001 From: alex-chevski Date: Thu, 21 Dec 2023 17:16:05 +0300 Subject: [PATCH 2/9] added youtube url faker --- composer.json | 1 + composer.lock | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 51 insertions(+), 1 deletion(-) diff --git a/composer.json b/composer.json index 20adde8..7f1ada7 100644 --- a/composer.json +++ b/composer.json @@ -6,6 +6,7 @@ "license": "MIT", "require": { "php": "^8.0.2", + "aalaap/faker-youtube": "^2.0", "asantibanez/livewire-charts": "^3.0", "barryvdh/laravel-debugbar": "^3.6", "cyrildewit/eloquent-viewable": "^7.0", diff --git a/composer.lock b/composer.lock index de1d128..5015c72 100644 --- a/composer.lock +++ b/composer.lock @@ -4,8 +4,57 @@ "Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies", "This file is @generated automatically" ], - "content-hash": "cec18c9498d636695e0fcc9ba3d8ef15", + "content-hash": "f1504229d84c399a1c8aea12005e3530", "packages": [ + { + "name": "aalaap/faker-youtube", + "version": "2.0.3", + "source": { + "type": "git", + "url": "https://github.com/aalaap/faker-youtube.git", + "reference": "eff2f96ee3f9a845d23140dd1721b14103b5c4d3" + }, + "dist": { + "type": "zip", + "url": "https://api.github.com/repos/aalaap/faker-youtube/zipball/eff2f96ee3f9a845d23140dd1721b14103b5c4d3", + "reference": "eff2f96ee3f9a845d23140dd1721b14103b5c4d3", + "shasum": "" + }, + "require": { + "fakerphp/faker": "^1.10", + "php": "^7.1 || ^8.0" + }, + "require-dev": { + "phpunit/phpunit": "^7.5.20 || ^8.5.8 || ^9.4.2" + }, + "type": "library", + "autoload": { + "psr-4": { + "Faker\\Provider\\": "src/" + } + }, + "notification-url": "https://packagist.org/downloads/", + "license": [ + "WTFPL" + ], + "authors": [ + { + "name": "Aalaap Ghag", + "email": "aalaap@gmail.com" + } + ], + "description": "Faker provider for generating fake YouTube video URLs", + "keywords": [ + "faker", + "provider", + "youtube" + ], + "support": { + "issues": "https://github.com/aalaap/faker-youtube/issues", + "source": "https://github.com/aalaap/faker-youtube/tree/2.0.3" + }, + "time": "2020-11-07T06:43:34+00:00" + }, { "name": "asantibanez/livewire-charts", "version": "v3.0.1", From 1da207eb4f6c77ce7fc20652c757157fd982013f Mon Sep 17 00:00:00 2001 From: alex-chevski Date: Thu, 21 Dec 2023 14:34:42 +0300 Subject: [PATCH 3/9] added recipe factories --- database/factories/RecipeFactory.php | 85 ++++++++++++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 database/factories/RecipeFactory.php diff --git a/database/factories/RecipeFactory.php b/database/factories/RecipeFactory.php new file mode 100644 index 0000000..bc17a8f --- /dev/null +++ b/database/factories/RecipeFactory.php @@ -0,0 +1,85 @@ + + */ +class RecipeFactory extends Factory +{ + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + $faker = \Faker\Factory::create(); + $this->faker->addProvider(new Restaurant($faker)); + $this->faker->addProvider(new Youtube($faker)); + + return [ + 'title' => $this->faker->foodName(), + 'image' => $this->generateImageFile('recipes'), + 'state' => $this->faker->randomElement(['active', 'archived']), + 'cooking_time' => $this->faker->numberBetween(0, 59), + 'ingredients' => $this->createQuentityIngredients($this->faker->randomDigitNotNull()), // quentity - от 1 до 10 randomDigitNotNull + 'type' => $this->faker->randomElement(['first_meals', 'second_meals', 'salads', 'snacks', 'drinks', 'other']), + 'description' => $this->faker->text(), + 'steps' => $this->createQuentitySteps($this->faker->randomDigitNotNull()), + 'video_url' => $this->faker->youtubeRandomUri(), + ]; + } + + private function generateImageFile(string $name) + { + Storage::fake($name); + $file = UploadedFile::fake()->image($name . '.png'); + return $file->store($name, 'public'); + } + + private function generateIngredients(): array + { + return [ + 'name' => $this->faker->foodName(), + 'unit' => $this->faker->randomElement(['гр', 'ч. ложек', 'cт. ложек', 'мл', 'шт', 'зубчик', 'по вкусу']), + 'portion' => $this->faker->randomNumber(3, true), + ]; + } + + private function generateSteps(): array + { + return [ + 'image' => $this->generateImageFile('steps'), + 'description' => $this->faker->text(), + ]; + } + + private function createQuentitySteps($count) + { + $result = []; + + for ($i = 0; $i < $count; $i++) { + $result[] = $this->generateSteps(); + } + + return $result; + } + + private function createQuentityIngredients($count) + { + $result = []; + + for ($i = 0; $i < $count; $i++) { + $result[] = $this->generateIngredients(); + } + + return $result; + } +} From 15b4d45515c720fb29c03da9eb1e65893054043a Mon Sep 17 00:00:00 2001 From: alex-chevski Date: Thu, 21 Dec 2023 14:35:16 +0300 Subject: [PATCH 4/9] added recipe seeder --- database/seeders/RecipeSeeder.php | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 database/seeders/RecipeSeeder.php diff --git a/database/seeders/RecipeSeeder.php b/database/seeders/RecipeSeeder.php new file mode 100644 index 0000000..800b7df --- /dev/null +++ b/database/seeders/RecipeSeeder.php @@ -0,0 +1,17 @@ +count(10)->create(); + } +} From ae0e76589c74ea085a7f82ce54a37832df5535a3 Mon Sep 17 00:00:00 2001 From: alex-chevski Date: Thu, 21 Dec 2023 14:35:28 +0300 Subject: [PATCH 5/9] added recipe author seeder --- database/seeders/RecipeAuthor.php | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 database/seeders/RecipeAuthor.php diff --git a/database/seeders/RecipeAuthor.php b/database/seeders/RecipeAuthor.php new file mode 100644 index 0000000..58c6319 --- /dev/null +++ b/database/seeders/RecipeAuthor.php @@ -0,0 +1,25 @@ + 0 && Recipe::count() > 0) { + Recipe::each(function ($recipe) { + User::inRandomOrder() + ->first() + ->author_of_recipe() + ->attach($recipe->id); + }); + } + } +} From 182cfeb4b9396aec61843989a6da7ae704614c5d Mon Sep 17 00:00:00 2001 From: alex-chevski Date: Thu, 21 Dec 2023 14:48:25 +0300 Subject: [PATCH 6/9] added Recipe comment --- database/factories/RecipeCommentFactory.php | 26 +++++++++++++++ database/seeders/RecipeCommentSeeder.php | 37 +++++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 database/factories/RecipeCommentFactory.php create mode 100644 database/seeders/RecipeCommentSeeder.php diff --git a/database/factories/RecipeCommentFactory.php b/database/factories/RecipeCommentFactory.php new file mode 100644 index 0000000..678f38a --- /dev/null +++ b/database/factories/RecipeCommentFactory.php @@ -0,0 +1,26 @@ + + */ +class RecipeCommentFactory extends Factory +{ + /** + * Define the model's default state. + * + * @return array + */ + public function definition(): array + { + + return [ + 'grade' => $this->faker->randomDigit(), + 'body' => $this->faker->text(), + ]; + + } +} diff --git a/database/seeders/RecipeCommentSeeder.php b/database/seeders/RecipeCommentSeeder.php new file mode 100644 index 0000000..3b24f5c --- /dev/null +++ b/database/seeders/RecipeCommentSeeder.php @@ -0,0 +1,37 @@ + 0 && Recipe::count() > 0) { + $comments = RecipeComment::factory()->count(5)->make(); + + $comments->each(function ($comment) { + $comment->author()->associate( + User::inRandomOrder() + ->first() + ->id + ); + + $comment->recipe()->associate( + Recipe::inRandomOrder() + ->first() + ->id + ); + + $comment->saveOrFail(); + }); + } + } +} From abd3b72e2ea7a40db187836bf366f1a799de9001 Mon Sep 17 00:00:00 2001 From: alex-chevski Date: Thu, 21 Dec 2023 14:48:44 +0300 Subject: [PATCH 7/9] added recipe seeders to database seeder --- database/seeders/DatabaseSeeder.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/database/seeders/DatabaseSeeder.php b/database/seeders/DatabaseSeeder.php index 4a70195..0fee10b 100644 --- a/database/seeders/DatabaseSeeder.php +++ b/database/seeders/DatabaseSeeder.php @@ -17,7 +17,9 @@ class DatabaseSeeder extends Seeder $this->call(UserSeeder::class); $this->call(PlanAuthor::class); $this->call(PlanCommentSeeder::class); - // $this->call(RecipeSeeder::class); + $this->call(RecipeSeeder::class); + $this->call(RecipeAuthor::class); + $this->call(RecipeCommentSeeder::class); // \App\Models\User::factory(10)->create(); } } From 4e02272d2c285392f84eb34c6fc2ef55dd16dc15 Mon Sep 17 00:00:00 2001 From: alex-chevski Date: Thu, 21 Dec 2023 15:01:03 +0300 Subject: [PATCH 8/9] recipe seeder and factories fix code style --- database/factories/RecipeCommentFactory.php | 4 ++-- database/factories/RecipeFactory.php | 6 ++++-- database/seeders/DatabaseSeeder.php | 10 +++------- database/seeders/RecipeAuthor.php | 4 +++- database/seeders/RecipeCommentSeeder.php | 4 +++- database/seeders/RecipeSeeder.php | 4 +++- 6 files changed, 18 insertions(+), 14 deletions(-) diff --git a/database/factories/RecipeCommentFactory.php b/database/factories/RecipeCommentFactory.php index 678f38a..bea271f 100644 --- a/database/factories/RecipeCommentFactory.php +++ b/database/factories/RecipeCommentFactory.php @@ -1,5 +1,7 @@ $this->faker->randomDigit(), 'body' => $this->faker->text(), ]; - } } diff --git a/database/factories/RecipeFactory.php b/database/factories/RecipeFactory.php index bc17a8f..edf251e 100644 --- a/database/factories/RecipeFactory.php +++ b/database/factories/RecipeFactory.php @@ -1,5 +1,7 @@ generateSteps(); } @@ -76,7 +78,7 @@ class RecipeFactory extends Factory { $result = []; - for ($i = 0; $i < $count; $i++) { + for ($i = 0; $i < $count; ++$i) { $result[] = $this->generateIngredients(); } diff --git a/database/seeders/DatabaseSeeder.php b/database/seeders/DatabaseSeeder.php index 0fee10b..b4935ee 100644 --- a/database/seeders/DatabaseSeeder.php +++ b/database/seeders/DatabaseSeeder.php @@ -1,17 +1,14 @@ call(PlanSeeder::class); $this->call(UserSeeder::class); @@ -20,6 +17,5 @@ class DatabaseSeeder extends Seeder $this->call(RecipeSeeder::class); $this->call(RecipeAuthor::class); $this->call(RecipeCommentSeeder::class); - // \App\Models\User::factory(10)->create(); } } diff --git a/database/seeders/RecipeAuthor.php b/database/seeders/RecipeAuthor.php index 58c6319..5381a41 100644 --- a/database/seeders/RecipeAuthor.php +++ b/database/seeders/RecipeAuthor.php @@ -1,5 +1,7 @@ 0 && Recipe::count() > 0) { - Recipe::each(function ($recipe) { + Recipe::each(static function ($recipe): void { User::inRandomOrder() ->first() ->author_of_recipe() diff --git a/database/seeders/RecipeCommentSeeder.php b/database/seeders/RecipeCommentSeeder.php index 3b24f5c..eb60093 100644 --- a/database/seeders/RecipeCommentSeeder.php +++ b/database/seeders/RecipeCommentSeeder.php @@ -1,5 +1,7 @@ 0 && Recipe::count() > 0) { $comments = RecipeComment::factory()->count(5)->make(); - $comments->each(function ($comment) { + $comments->each(static function ($comment): void { $comment->author()->associate( User::inRandomOrder() ->first() diff --git a/database/seeders/RecipeSeeder.php b/database/seeders/RecipeSeeder.php index 800b7df..9c4588e 100644 --- a/database/seeders/RecipeSeeder.php +++ b/database/seeders/RecipeSeeder.php @@ -1,9 +1,11 @@ Date: Fri, 22 Dec 2023 16:47:55 +0300 Subject: [PATCH 9/9] fixed route tests recipe --- tests/Feature/Recipe/EditFormTest.php | 2 +- tests/Feature/Recipe/ShowTest.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/Feature/Recipe/EditFormTest.php b/tests/Feature/Recipe/EditFormTest.php index 20c02b3..67bbb9e 100644 --- a/tests/Feature/Recipe/EditFormTest.php +++ b/tests/Feature/Recipe/EditFormTest.php @@ -33,7 +33,7 @@ final class EditFormTest extends TestCase { $response = $this ->actingAs($this->user) - ->get(route('recipe.edit', ['recipe' => $this->recipe->id])); + ->get(route('recipe.edit', $this->recipe, false)); $response->assertSuccessful(); } diff --git a/tests/Feature/Recipe/ShowTest.php b/tests/Feature/Recipe/ShowTest.php index 6531298..a29cec0 100644 --- a/tests/Feature/Recipe/ShowTest.php +++ b/tests/Feature/Recipe/ShowTest.php @@ -26,7 +26,7 @@ class ShowTest extends TestCase public function test_show_page_success() { $response = $this->actingAs($this->user) - ->get(route('recipe.show', $this->plan, false)); + ->get(route('recipe.show', $this->recipe, false)); $response ->assertOk();