diff --git a/src/Controllers/CypressController.php b/src/Controllers/CypressController.php index 2b25bc8..e0974a2 100644 --- a/src/Controllers/CypressController.php +++ b/src/Controllers/CypressController.php @@ -2,11 +2,12 @@ namespace Laracasts\Cypress\Controllers; +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 { @@ -40,7 +41,7 @@ public function login(Request $request) ->where($attributes) ->first(); - if (!$user) { + if (! $user) { $user = $this->factoryBuilder( $this->userClassName(), $request->input('state', []) @@ -75,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 @@ -105,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([ @@ -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..9944aa6 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; @@ -20,13 +21,13 @@ protected function setUp(): void { parent::setUp(); - $this->loadMigrationsFrom(__DIR__ . '/database/migrations'); + $this->loadMigrationsFrom(__DIR__.'/database/migrations'); config(['auth.providers.users.model' => TestUser::class]); } /** @test */ - function it_fetches_a_collection_of_named_routes() + public function it_fetches_a_collection_of_named_routes() { Route::get('foo')->name('home'); @@ -79,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']); @@ -122,7 +123,29 @@ public function it_builds_a_model_factory() 'name' => 'John Doe', ], 'load' => ['profile'], - 'state' => ['guest'] + '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_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']); @@ -136,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']); @@ -144,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']); @@ -155,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, @@ -177,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,