Skip to content

Using seed data #10

Description

@alanwillms

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions