nateritter's avatar

Lumen + sqlite + TestDummy relationships

When using Lumen, sqlite, and laracasts/TestDummy, I'm finding that relationships in the Fakers aren't being built out.

For instance, in my factories.php I have the following:

<?php

$factory('App\Approval', [
    'id' => $faker->randomDigitNotNull,
    'person_id' => 'faker:App\Person',
    'service_id' => 'faker:App\Service',
    'processed_at' => $faker->dateTime,
]);

$factory('App\Attrib', [
    'id' => $faker->randomDigitNotNull,
    'person_id' => 'faker:App\Person',
    'service_id' => 'faker:App\Service',
    'k' => $faker->word,
    'v' => $faker->sentence(6),
]);

$factory('App\Person', [
    'id' => $faker->randomDigitNotNull,
]);

$factory('App\Service', [
    'id' => $faker->randomDigitNotNull,
    'service_name' => $faker->word,
    'group' => $faker->randomDigitNotNull,
]);

Then in my PersonControllerTest.php I have:


    public function testGetPeople()
    {
        // Arrange
        Factory::times(3)->create('App\Attrib');

        // Act
        $response = $this->call('GET', '/people');
        dd($response->getContent());

        // Assert
        $this->assertInstanceOf('App\Person', $people[0]);
        $this->assertResponseOk();
    }

When I run the test, I get the following in the sqlite table:

sqlite> select * from attributes;
2|faker:App\Person|faker:App\Service|quasi|Quia iste optio molestiae quis quia quia exercitationem.||2015-06-17 04:06:12|2015-06-17 04:06:12
5|faker:App\Person|faker:App\Service|aut|Voluptatem eveniet ut quis sit sequi sed consequuntur reiciendis.||2015-06-17 04:06:12|2015-06-17 04:06:12
9|faker:App\Person|faker:App\Service|minima|Et iste eveniet provident exercitationem explicabo magni magni.||2015-06-17 04:06:12|2015-06-17 04:06:12
sqlite> select * from people;
sqlite> 

So, instead of creating the associated Person record, it simply throws in the faker:App\Person string.

Anyone figure out how to get this to work properly and create the associated record, and then insert the appropriate id?

0 likes
3 replies
nateritter's avatar

I had factory::App\Person, etc, in some places (note 2 colons) in the factories.php file... I've changed it to factory:App\Person (one colon), and am now getting the following, more respectable error:

Illuminate\Database\QueryException: SQLSTATE[23000]: Integrity constraint violation: 19 UNIQUE constraint failed: attributes.id (SQL: insert into "attributes" ("id", "person_id", "service_id", "k", "v", "updated_at", "created_at") values (1, faker:App\Person, faker:App\Service, ipsa, Exercitationem doloribus sint modi et., 2015-06-17 04:44:28, 2015-06-17 04:44:28))

This makes more sense because the person_id should not be able to take a string.

That said, I'm still in the same place. The relationships aren't being created. Really, it should be trying to save an integer (the id of the associated Person).

Any help would be appreciated.

nateritter's avatar
nateritter
OP
Best Answer
Level 3

Ok, it's either the beer in me (or lack of beer earlier) which made me use faker:... instead of factory:... in my factories.php file.

I've since changed my factories.php to this, to also not conflict with my seeds:

<?php

$factory('App\Approval', [
    'id' => $faker->numberBetween(10000, 90000),
    'person_id' => 'factory:App\Person',
    'service_id' => 'factory:App\Service',
    'processed_at' => $faker->dateTime,
]);

$factory('App\Attrib', [
    'id' => $faker->numberBetween(10000, 90000),
    'person_id' => 'factory:App\Person',
    'service_id' => 'factory:App\Service',
    'k' => $faker->word,
    'v' => $faker->sentence(6),
]);

$factory('App\Person', [
    'id' => $faker->numberBetween(10000, 90000),
]);

$factory('App\Service', [
    'id' => $faker->numberBetween(10000, 90000),
    'service_name' => $faker->word,
    'group' => $faker->randomDigitNotNull,
]);

Please or to participate in this conversation.