diff --git a/system/Helpers/test_helper.php b/system/Helpers/test_helper.php index 46d0d14302d7..c6f9bebe7577 100644 --- a/system/Helpers/test_helper.php +++ b/system/Helpers/test_helper.php @@ -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); @@ -37,6 +37,11 @@ function fake($model, array $overrides = null) $fabricator->setOverrides($overrides); } - return $fabricator->create(); + if ($persist) + { + return $fabricator->create(); + } + + return $fabricator->make(); } } diff --git a/tests/system/Database/Live/FabricatorLiveTest.php b/tests/system/Database/Live/FabricatorLiveTest.php index 60ebb9beec60..35f675af9ec3 100644 --- a/tests/system/Database/Live/FabricatorLiveTest.php +++ b/tests/system/Database/Live/FabricatorLiveTest.php @@ -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']); + } } diff --git a/user_guide_src/source/testing/fabricator.rst b/user_guide_src/source/testing/fabricator.rst index aa96c5cd3a19..933db982b2ee 100644 --- a/user_guide_src/source/testing/fabricator.rst +++ b/user_guide_src/source/testing/fabricator.rst @@ -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']); @@ -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 ============