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

Fabricator class #2936

Merged
merged 25 commits into from
May 18, 2020
Merged
Show file tree
Hide file tree
Changes from 2 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
13 changes: 11 additions & 2 deletions system/Test/Fabricator.php
Original file line number Diff line number Diff line change
Expand Up @@ -517,7 +517,8 @@ protected function createMock(int $count = null)
case 'date':
$datetime = date('Y-m-d');
default:
$datetime = time(); }
$datetime = time();
}

// Determine which fields we will need
$fields = [];
Expand All @@ -530,7 +531,15 @@ protected function createMock(int $count = null)

if ($this->model->useSoftDeletes)
{
$fields[$this->model->deletedField] = $datetime;
// Make a 20% chance of returning a deleted item
if (rand(1, 5) === 3)
MGatner marked this conversation as resolved.
Show resolved Hide resolved
{
$fields[$this->model->deletedField] = $datetime;
}
else
{
$fields[$this->model->deletedField] = null;
}
}

// Iterate over new entities and add the necessary fields
Expand Down
4 changes: 3 additions & 1 deletion tests/system/Test/FabricatorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,8 @@ public function testCreateMockSetsDatabaseFields()

$this->assertIsInt($result->id);
$this->assertIsInt($result->created_at);
$this->assertIsInt($result->deleted_at);
$this->assertIsInt($result->updated_at);

$this->assertObjectHasAttribute('deleted_at', $result);
}
}
53 changes: 53 additions & 0 deletions user_guide_src/source/testing/fabricator.rst
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,56 @@ the object with extra database fields above without actually touching the databa

$this->assertIsNumeric($user->id);
$this->dontSeeInDatabase('user', ['id' => $user->id]);

Specifying Test Data
====================

Generated data is great, but sometimes you may want to supply a specific field for a test without
compromising your formatters configuration. Rather then creating a new fabricator for each variant
you can use ``setOverrides()`` to specify the value for any fields::

$fabricator->setOverrides(['first' => 'Bobby']);
$bobbyUser = $fabricator->make();

Now any data generated with ``make()`` or ``create()`` will always use "Bobby" for the ``first`` field:

array(
'first' => "Bobby",
'email' => "[email protected]",
'phone' => "251-806-2169",
'avatar' => "http://lorempixel.com/800/400/",
'login' => null,
)

array(
'first' => "Bobby",
'email' => "[email protected]",
'phone' => "525-214-2656 x23546",
'avatar' => "http://lorempixel.com/800/400/",
'login' => null,
)

``setOverrides()`` can take a second parameter to indicate whether this should be a persistent
override (default) or only for a single action::
MGatner marked this conversation as resolved.
Show resolved Hide resolved

$fabricator->setOverrides(['first' => 'Bobby'], false);
$bobbyUser = $fabricator->make();
$bobbyUser = $fabricator->make();

Notice after the first return the fabricator stops using the overrides::

array(
'first' => "Bobby",
'email' => "[email protected]",
'phone' => "741-857-1933 x1351",
'avatar' => "http://lorempixel.com/800/400/",
'login' => null,
)

array(
'first' => "Hans",
'email' => "[email protected]",
'phone' => "487-235-7006",
'avatar' => "http://lorempixel.com/800/400/",
'login' => null,
)