Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions src/Controllers/CypressController.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -40,7 +41,7 @@ public function login(Request $request)
->where($attributes)
->first();

if (!$user) {
if (! $user) {
$user = $this->factoryBuilder(
$this->userClassName(),
$request->input('state', [])
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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([
Expand All @@ -121,6 +122,7 @@ protected function userClassName()

protected function factoryBuilder($model, $states = [])
{
$model = Relation::getMorphedModel($model) ?? $model;
$factory = $model::factory();

$states = Arr::wrap($states);
Expand Down
41 changes: 32 additions & 9 deletions tests/CypressControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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');

Expand Down Expand Up @@ -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']);
Expand Down Expand Up @@ -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']);
Expand All @@ -136,15 +159,15 @@ 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']);

// 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']);
Expand All @@ -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,
Expand All @@ -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,
Expand Down