Skip to content

Commit

Permalink
Merge pull request #4607 from caswell-wc/non_persistent_fake
Browse files Browse the repository at this point in the history
Non-persistent fake
  • Loading branch information
paulbalandan authored Apr 24, 2021
2 parents 17ddb98 + 388803c commit 0879a35
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 3 deletions.
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

0 comments on commit 0879a35

Please sign in to comment.