Let's say I have an order table and an order_status table. I have three order status: pending (id = 1), fulfilled (2) and cancelled (3).
When I generate a new order, I want to use those three existing status, and not to generate new ones:
// Will generate using 'pending' status by default, instead of creating new ones
$order = Phactory::order();
// Will generate with the correct status
$order = Phactory::order('pending');
$order = Phactory::order('fulfilled');
$order = Phactory::order('cancelled');
I would have something like that:
class OrderStatusPhactory
{
public function blueprint()
{
return ['id' => '#{sn}', 'description' => 'Order status #{sn}'];
}
public function pendingFixture()
{
return ['id' => 1, 'description' => 'Pending'];
}
public function fulfilledFixture()
{
return ['id' => 2, 'description' => 'Fulfilled'];
}
public function cancelledFixture()
{
return ['id' => 3, 'description' => 'Cancelled'];
}
}
class OrderPhactory
{
public function blueprint()
{
return [
'id' => '#{sn}',
'status' => Phactory::hasOne('orderStatus', 'pending'),
// ... other fields ...
]
}
public function pending()
{
return ['status' => Phactory::hasOne('orderStatus', 'pending')];
}
public function fulfilled()
{
return ['status' => Phactory::hasOne('orderStatus', 'fulfilled')];
}
public function cancelled()
{
return ['status' => Phactory::hasOne('orderStatus', 'cancelled')];
}
}
Here are my issues:
- If I have seed data in the database, it will broke on trying to create those order status fixtures
- The
'id' => '#{sn}' will fail because it will start at 1 again, despite the fixtures with ID = 1, 2 and 3
What I have done:
- Using an empty database (only schema, no data)
- I never set object ids, unless they are fixed in the database (like order status)
@rbone what would you do in such a case? Is there any way I could load an existing object from the database instead of generating it in runtime? That way I would not need to truncate my phactory test database.
Let's say I have an
ordertable and anorder_statustable. I have three order status: pending (id = 1), fulfilled (2) and cancelled (3).When I generate a new order, I want to use those three existing status, and not to generate new ones:
I would have something like that:
Here are my issues:
'id' => '#{sn}'will fail because it will start at 1 again, despite the fixtures with ID = 1, 2 and 3What I have done:
@rbone what would you do in such a case? Is there any way I could load an existing object from the database instead of generating it in runtime? That way I would not need to truncate my phactory test database.