Closed simplenotezy closed 8 years ago
@GrahamCampbell FactoryGirl has an idea of Sequences, it's maybe something we can implement:
FactoryGirl.define do
factory :user do
sequence(:email) { |n| "user#{n}@example.com"}
end
end
FactoryGirl.define do
factory :user do
sequence(:id)
end
end
Quick idea:
$fm->define('User')->setDefinitions([
'email' => $fm->sequence(function($n) {
return "user{$n}@example.com";
})
]);
$fm->define('User')->setDefinitions([
'id' => $fm->sequence()
]);
The only issue with that implementation is that the sequence is global, and not per factory.
@canfiax as a work around, you could use a combination of fakers unique()
and randomDigitNotNull()
This is already possible using our callable generator.
Just pass in something like this:
function () use ($gen) {
$gen->next();
};
Where the $gen
variable is an instance of a class that looks like this:
class Gen
{
private $last = 0;
public function next()
{
return ++$this->last;
}
}
Or, even just this:
function () {
static $last;
if ($last === null) {
return $last = 1;
}
return ++$last;
};
@GrahamCampbell when we updated factory muffin from v2.1 to v3.1
For auto-incrementing ids, we used to use the following function:
function () {
static $last;
if ($last === null) {
return $last = 1;
}
return ++$last;
}
But after upgrading the same function always started returning 1
, resulting in errors for duplication of primary key in our test database.
Any idea what could be the issue and how to resolve it?
When defining a muffin, I'd like to be able to define a value that is auto incremented. For instance; I make use of UUID, and when writing tests, it could be very useful to be able to call users like this: 1, 2, 3,4.
I.e:
Is this possible as of right now?