From a069d128919f9bc47b7783127171855692ad6e75 Mon Sep 17 00:00:00 2001 From: Prashank Abhishek Date: Sat, 23 Apr 2022 00:12:21 +0530 Subject: [PATCH 1/3] allow building factory by model morph name Signed-off-by: Prashank Abhishek --- src/Controllers/CypressController.php | 2 ++ tests/CypressControllerTest.php | 23 +++++++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/src/Controllers/CypressController.php b/src/Controllers/CypressController.php index 2b25bc8..1bcd35e 100644 --- a/src/Controllers/CypressController.php +++ b/src/Controllers/CypressController.php @@ -2,6 +2,7 @@ namespace Laracasts\Cypress\Controllers; +use Illuminate\Database\Eloquent\Relations\Relation; use Illuminate\Http\Request; use Illuminate\Support\Facades\Artisan; use Illuminate\Support\Facades\Route; @@ -121,6 +122,7 @@ protected function userClassName() protected function factoryBuilder($model, $states = []) { + $model = Relation::getMorphedModel($model) ?? $model; $factory = $model::factory(); $states = Arr::wrap($states); diff --git a/tests/CypressControllerTest.php b/tests/CypressControllerTest.php index 731f96e..47c41b8 100644 --- a/tests/CypressControllerTest.php +++ b/tests/CypressControllerTest.php @@ -2,6 +2,7 @@ namespace Laracasts\Cypress\Tests; +use Illuminate\Database\Eloquent\Relations\Relation; use Illuminate\Support\Facades\Artisan; use Illuminate\Support\Facades\Route; use Laracasts\Cypress\CypressServiceProvider; @@ -131,6 +132,28 @@ public function it_builds_a_model_factory() $this->assertEquals('guest', $response->json()['plan']); } + /** @test */ + public function it_builds_a_model_factory_by_its_morph_name() + { + Relation::morphMap([ + 'test_user' => TestUser::class, + ]); + + $response = $this->post(route('cypress.factory'), [ + 'model' => 'test_user', + 'attributes' => [ + 'name' => 'John Doe', + ], + 'load' => ['profile'], + 'state' => ['guest'], + ]); + + $this->assertDatabaseHas('users', ['name' => 'John Doe']); + $this->assertEquals('John Doe', $response->json()['name']); + $this->assertEquals('USA', $response->json()['profile']['location']); + $this->assertEquals('guest', $response->json()['plan']); + } + /** @test */ public function it_accepts_arguments_to_model_factory_states() { From 184e852557460e230d6ebc6363f8879af29ccd15 Mon Sep 17 00:00:00 2001 From: Prashank Abhishek Date: Fri, 30 Sep 2022 17:05:40 +0530 Subject: [PATCH 2/3] styleci fixes Signed-off-by: Prashank Abhishek --- src/Controllers/CypressController.php | 10 +++++----- tests/CypressControllerTest.php | 16 ++++++++-------- 2 files changed, 13 insertions(+), 13 deletions(-) diff --git a/src/Controllers/CypressController.php b/src/Controllers/CypressController.php index 1bcd35e..e0974a2 100644 --- a/src/Controllers/CypressController.php +++ b/src/Controllers/CypressController.php @@ -4,10 +4,10 @@ use Illuminate\Database\Eloquent\Relations\Relation; use Illuminate\Http\Request; +use Illuminate\Support\Arr; use Illuminate\Support\Facades\Artisan; use Illuminate\Support\Facades\Route; use Illuminate\Support\Str; -use Illuminate\Support\Arr; class CypressController { @@ -41,7 +41,7 @@ public function login(Request $request) ->where($attributes) ->first(); - if (!$user) { + if (! $user) { $user = $this->factoryBuilder( $this->userClassName(), $request->input('state', []) @@ -76,7 +76,7 @@ public function factory(Request $request) ) ->count(intval($request->input('count', 1))) ->create($request->input('attributes')) - ->each(fn($model) => $model->setHidden([])->setVisible([])) + ->each(fn ($model) => $model->setHidden([])->setVisible([])) ->load($request->input('load', [])) ->pipe(function ($collection) { return $collection->count() > 1 @@ -106,8 +106,8 @@ public function runPhp(Request $request) $code .= ';'; } - if (!Str::contains($code, 'return')) { - $code = 'return ' . $code; + if (! Str::contains($code, 'return')) { + $code = 'return '.$code; } return response()->json([ diff --git a/tests/CypressControllerTest.php b/tests/CypressControllerTest.php index 47c41b8..f8a90d8 100644 --- a/tests/CypressControllerTest.php +++ b/tests/CypressControllerTest.php @@ -21,7 +21,7 @@ protected function setUp(): void { parent::setUp(); - $this->loadMigrationsFrom(__DIR__ . '/database/migrations'); + $this->loadMigrationsFrom(__DIR__.'/database/migrations'); config(['auth.providers.users.model' => TestUser::class]); } @@ -80,7 +80,7 @@ public function it_logs_a_user_in() { $this->post(route('cypress.login'), [ 'attributes' => ['name' => 'Frank'], - 'state' => ['guest'] + 'state' => ['guest'], ]); $this->assertDatabaseHas('users', ['name' => 'Frank']); @@ -123,7 +123,7 @@ public function it_builds_a_model_factory() 'name' => 'John Doe', ], 'load' => ['profile'], - 'state' => ['guest'] + 'state' => ['guest'], ]); $this->assertDatabaseHas('users', ['name' => 'John Doe']); @@ -159,7 +159,7 @@ public function it_accepts_arguments_to_model_factory_states() { $response = $this->post(route('cypress.factory'), [ 'model' => TestUser::class, - 'state' => ['guest' => 'forum'] + 'state' => ['guest' => 'forum'], ]); $this->assertEquals('forum', $response->json()['plan']); @@ -167,7 +167,7 @@ public function it_accepts_arguments_to_model_factory_states() // When passing an array of arguments. $response = $this->post(route('cypress.factory'), [ 'model' => TestUser::class, - 'state' => ['guest' => ['forum']] + 'state' => ['guest' => ['forum']], ]); $this->assertEquals('forum', $response->json()['plan']); @@ -178,14 +178,14 @@ public function it_builds_a_collection_of_model_factories() { $response = $this->post(route('cypress.factory'), [ 'model' => TestUser::class, - 'count' => 2 + 'count' => 2, ]); $this->assertCount(2, $response->json()); } /** @test */ - function it_makes_model_attributes_visible() + public function it_makes_model_attributes_visible() { $response = $this->post(route('cypress.factory'), [ 'model' => TestUser::class, @@ -200,7 +200,7 @@ function it_makes_model_attributes_visible() } /** @test */ - function it_makes_collection_model_attributes_visible() + public function it_makes_collection_model_attributes_visible() { $response = $this->post(route('cypress.factory'), [ 'model' => TestUser::class, From 8ecf3ec13fce8627bf085d4c9649f5fe832e2d59 Mon Sep 17 00:00:00 2001 From: Prashank Abhishek Date: Fri, 30 Sep 2022 17:06:41 +0530 Subject: [PATCH 3/3] missed a styleci fix Signed-off-by: Prashank Abhishek --- tests/CypressControllerTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/CypressControllerTest.php b/tests/CypressControllerTest.php index f8a90d8..9944aa6 100644 --- a/tests/CypressControllerTest.php +++ b/tests/CypressControllerTest.php @@ -27,7 +27,7 @@ protected function setUp(): void } /** @test */ - function it_fetches_a_collection_of_named_routes() + public function it_fetches_a_collection_of_named_routes() { Route::get('foo')->name('home');