13

I would like to use Laravel's Model Factory in some PHPUnit tests. The only thing I want to do is make a Model instance without saving it to database.

Why the Model Factory needs connection to database? These tests must pass on CI environment without configured database.

When I create Model manually by new App\Model($dataArray), tests pass and the connection is not needed.

I am using Model Factory in other places, so I would like to reuse it in that tests, to avoid code duplication.

I am using MongoDB and jenssegers/laravel-mongodb library, but I am guessing that it has no matter - in pure Eloquent and e.g. MySQL database, the issue would be the same.

Test that works without database:

class ModelTransformerTest extends TestCase
{
    public function testTransformMinimalModelData()
    {
        $data = [
            '_id' => $faker->md5,
            'email' => $faker->email,
        ];

        $model = new App\Model($data);
        // […];
    }
}

My Model Factory

$factory->defineAs(Model::class, 'base', function ($faker) {
    return [
        '_id' => $faker->md5,
        'email' => $faker->email,
    ];
});

Test that needs database connection:

class ModelTransformerTest extends TestCase
{
    public function testTransformMinimalModelData()
    {
        $model = factory(App\Model::class, 'base')->make();
        // […];
    }
}

Full stack trace:

Error: Class 'MongoDB\Driver\Manager' not found

app\vendor\mongodb\mongodb\src\Client.php:56
app\vendor\jenssegers\mongodb\src\Jenssegers\Mongodb\Connection.php:147
app\vendor\jenssegers\mongodb\src\Jenssegers\Mongodb\Connection.php:37
app\vendor\jenssegers\mongodb\src\Jenssegers\Mongodb\MongodbServiceProvider.php:27
app\vendor\illuminate\database\DatabaseManager.php:173
app\vendor\illuminate\database\DatabaseManager.php:68
app\vendor\illuminate\database\Eloquent\Model.php:3282
app\vendor\illuminate\database\Eloquent\Model.php:3248
app\vendor\jenssegers\mongodb\src\Jenssegers\Mongodb\Eloquent\Model.php:523
app\vendor\jenssegers\mongodb\src\Jenssegers\Mongodb\Eloquent\Model.php:284
app\vendor\illuminate\database\Eloquent\Model.php:443
app\vendor\illuminate\database\Eloquent\Model.php:281
app\vendor\illuminate\database\Eloquent\FactoryBuilder.php:142
app\vendor\illuminate\database\Eloquent\Model.php:2286
app\vendor\illuminate\database\Eloquent\FactoryBuilder.php:143
app\vendor\illuminate\database\Eloquent\FactoryBuilder.php:106
app\tests\phpunit\Transformers\ModelTransformerTest.php:25
\php\pear\PHPUnit\TextUI\Command.php:176
\php\pear\PHPUnit\TextUI\Command.php:129
user1292810
  • 1,702
  • 7
  • 19
  • 38
  • You can use SQLite for this. Laravel provides an option to create a SQLite DB in memory. So you technically have a database but it won't populate your MongoDB or MySQL etc. As soon as the tests are complete the DB is removed from memory – user3425867 Feb 23 '18 at 20:56

3 Answers3

1

I don't think this is currently possible. The make() method calls newModelInstance() which tries to set a database connection.

You can review the source code yourself.

Camilo
  • 6,504
  • 4
  • 39
  • 60
1

Do you try using DatabaseTransactions?

For example

use Illuminate\Foundation\Testing\DatabaseTransactions;
class ModelTransformerTest extends TestCase
{
    use DatabaseTransactions;
}
David L
  • 703
  • 6
  • 22
0

I normally go for creating a similar test DB and run all the tests on that DB.

It ensures that your test DB type is same as production and you don't touch the production DB for running the tests.

Zeshan
  • 2,496
  • 3
  • 21
  • 26