Skip to content

Commit

Permalink
Make all model attributes visible to Cypress response
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffreyWay committed Jan 18, 2021
1 parent 6912bb4 commit e4fa34b
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/Controllers/CypressController.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,16 @@ public function logout()

public function factory(Request $request)
{
$builder = $this->factoryBuilder($request->input('model'));
$times = intval($request->input('times', 1));

if ($times > 1) {
$builder->times($times);
}
$collection = $this->factoryBuilder($request->input('model'))
->times($times)
->create($request->input('attributes'))
->each(function ($model) {
$model->setHidden([]);
});

return $builder->create($request->input('attributes'));
return $collection->count() === 1 ? $collection->first() : $collection;
}

public function artisan(Request $request)
Expand Down
32 changes: 32 additions & 0 deletions tests/CypressControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,38 @@ public function it_generates_a_collection_of_eloquent_model_using_a_factory()
$this->assertEquals('John Doe', $response->json()[0]['name']);
}

/** @test */
function it_makes_model_attributes_visible()
{
$response = $this->post(route('cypress.factory'), [
'model' => TestUser::class,
'attributes' => [
'name' => 'John Doe',
],
]);

// The TestUser model sets the "plan" field to hidden. But
// when we fetch it with Cypress, it should be visible.
$this->assertEquals('monthly', $response->json()['plan']);
}

/** @test */
function it_makes_collection_model_attributes_visible()
{
$response = $this->post(route('cypress.factory'), [
'model' => TestUser::class,
'times' => 2,
'attributes' => [
'name' => 'John Doe',
],
]);

// The TestUser model sets the "plan" field to hidden. But
// when we fetch it with Cypress, it should be visible.
$this->assertEquals('monthly', $response->json()[0]['plan']);
$this->assertEquals('monthly', $response->json()[1]['plan']);
}

/** @test */
public function it_runs_an_artisan_command()
{
Expand Down
1 change: 1 addition & 0 deletions tests/support/TestUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
class TestUser extends Authenticatable
{
protected $table = 'users';
protected $hidden = ['plan'];
}
1 change: 1 addition & 0 deletions tests/support/factories/UserFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
return [
'name' => $faker->name,
'email' => $faker->unique()->safeEmail,
'plan' => 'monthly',
'password' => 'foopassword',
];
});

0 comments on commit e4fa34b

Please sign in to comment.