Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Non-persistent fake #4607

Merged
merged 1 commit into from
Apr 24, 2021
Merged
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
9 changes: 7 additions & 2 deletions system/Helpers/test_helper.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
*
* @return object|array
*/
function fake($model, array $overrides = null)
function fake($model, array $overrides = null, $persist = true)
{
// Get a model-appropriate Fabricator instance
$fabricator = new Fabricator($model);
Expand All @@ -37,6 +37,11 @@ function fake($model, array $overrides = null)
$fabricator->setOverrides($overrides);
}

return $fabricator->create();
if ($persist)
{
return $fabricator->create();
}

return $fabricator->make();
}
}
8 changes: 8 additions & 0 deletions tests/system/Database/Live/FabricatorLiveTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,4 +79,12 @@ public function testCreateThrowsOnFailure()

fake(ValidModel::class, ['name' => 'eh']);
}

public function testHelperDoesNotPersist()
{
helper('test');
$result = fake(UserModel::class, ['name' => 'Derek'], false);
$this->assertEquals('Derek', $result->name);
$this->dontSeeInDatabase('user', ['name' => 'Derek']);
}
}
4 changes: 3 additions & 1 deletion user_guide_src/source/testing/fabricator.rst
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ Test Helper
===========

Often all you will need is a one-and-done fake object for testing. The Test Helper provides
the ``fake($model, $overrides)`` function to do just this::
the ``fake($model, $overrides, $persist = true)`` function to do just this::

helper('test');
$user = fake('App\Models\UserModel', ['name' => 'Gerry']);
Expand All @@ -228,6 +228,8 @@ This is equivalent to::
$fabricator->setOverrides(['name' => 'Gerry']);
$user = $fabricator->create();

If you just need a fake object without saving it to the database you can pass false into the persist parameter.

Table Counts
============

Expand Down