jeudi 11 janvier 2018

Codeception DataFactory interaction between helper and other factories

I'm looking into using DataFactory's in Codeception for seeding of data, and for use in our acceptance tests. In the documentation there's mention of 2 approaches, one using the helper file and one using factories files.

We load both options using this snippet from our acceptance.suite.yml

class_name: AcceptanceTester
modules:
    enabled:
        - Db
        - WebDriver
        - \Helper\Acceptance
        - Doctrine2:
            connection_callback: getEntityManager
        - DataFactory:
            factories: tests/_support/factories
            depends: Doctrine2
        - \Helper\Factory

Both of the options seem to load correctly. As per the documentation I can then define factories like this, which will allow interaction with Doctrine.

// tests/_support/Helper/Factory.php
class Factory extends Module
{

    /**
     * @param array $settings
     * @throws \League\FactoryMuffin\Exceptions\DefinitionAlreadyDefinedException
     * @throws \Codeception\Exception\ModuleException
     */
    public function _beforeSuite($settings = [])
    {
        /** @var Module\DataFactory $factory */
        $factory = $this->getModule('DataFactory');

        /** @var EntityManager $em */
        $em = $this->getModule('Doctrine2')->_getEntityManager();

        $factory->_define(User::class,[
            // generate random user name
            'name' => Faker::name(),
        ]);
        parent::_beforeSuite($settings);
    }
}

As per the other option, I can also create factories by loading all files from within tests/_support/factories, such as below:

// tests/_support/factories/seed.php
use League\FactoryMuffin\Faker\Faker;

/** @var \League\FactoryMuffin\FactoryMuffin $fm */
$user = $fm->create(User::class);
dd($user);

However, the seed.php version cannot seem to share the Factory, and errors with:

The model definition 'User' is undefined.

I wondered if maybe this could be solved by moving the Factory.php logic into the initialize() method but this seems to be called before FactoryMuffin has been initiliazed.

The documentation for this with codeception seems a bit sparse, and the FactoryMuffin docs, while better, don't cover Codeception integration. Just trying to work out if i'm missing something, or I just need to repeat the code in each place if I want to use both files/methods.

Aucun commentaire:

Enregistrer un commentaire