PHP code example of breerly / factory-girl-php

1. Go to this page and download the library: Download breerly/factory-girl-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

breerly / factory-girl-php example snippets




use FactoryGirl\Provider\Doctrine\FieldDef;
use FactoryGirl\Provider\Doctrine\FixtureFactory;
use PHPUnit\Framework;

abstract class TestCase extends Framework\TestCase
{
    protected $factory;

    protected function setUp(): void
    {
        // ... (set up a blank database and $this->entityManager) ...

        $this->factory = new FixtureFactory($this->entityManager);
        $this->factory->setEntityNamespace('What\Ever'); // If applicable

        // Define that users have names like user_1, user_2, etc.,
        // that they are not administrators by default and
        // that they point to a Group entity.
        $this->factory->defineEntity('User', [
            'username' => FieldDef::sequence("user_%d"),
            'administrator' => false,
            'group' => FieldDef::reference('Group')
        ]);

        // Define a Group to just have a unique name as above.
        // The order of the definitions does not matter.
        $this->factory->defineEntity('Group', [
            'name' => FieldDef::sequence("group_%d")
        ]);


        // If you want your created entities to be saved by default
        // then do the following. You can selectively re-enable or disable
        // this behavior in each test as well.
        // It's recommended to only enable this in tests that need it.
        // In any case, you'll need to call flush() yourself.
        //$this->factory->persistOnGet();
    }
}



class UserServiceTest extends TestCase
{
    // ...

    public function testChangingPasswords(): void
    {
        $user = $this->factory->get('User', [
            'name' => 'John'
        ]);
        $this->service->changePassword($user, 'xoo');
        $this->assertSame($user, $this->service->authenticateUser('john', 'xoo'));
    }
}



class SomeTest extends TestCase
{
    protected function setUp(): void
    {
        parent::setUp();
        $this->org = $this->factory->getAsSingleton('Organization');
    }

    public function testSomething(): void
    {
        $user1 = $this->factory->get('User');
        $user2 = $this->factory->get('User');

        // now $user1->getOrganization() === $user2->getOrganization() ...
    }
}



$factory->defineEntity(
    'User', 
    [
        'username' => FieldDef::sequence("user_%d"),
    ], 
    [
    'afterCreate' => function(User $user, array $fieldValues) {
        $user->__construct($fieldValues['username']);
    }
]);



// Defining entities
$factory->defineEntity(
    'EntityName', 
    [
        'simpleField' => 'constantValue',
    
        'generatedField' => function($factory) { return ...; },
    
        'sequenceField1' => FieldDef::sequence('name-%d'), // name-1, name-2, ...
        'sequenceField2' => FieldDef::sequence('name-'),   // the same
        'sequenceField3' => FieldDef::sequence(function($n) { return "name-$n"; }),
    
        'referenceField' => FieldDef::reference('OtherEntity')
    ], 
    [
        'afterCreate' => function($entity, $fieldValues) {
            // ...
        }
    ]
);

// Getting an entity (new or singleton)
$factory->get('EntityName', ['field' => 'value']);

// Getting an array of entities
$numberOfEntities = 15;
$factory->getList('EntityName', ['field' => 'value'], $numberOfEntities);

// Singletons
$factory->getAsSingleton('EntityName', ['field' => 'value']);
$factory->setSingleton('EntityName', $entity);
$factory->unsetSingleton('EntityName');

// Configuration
$this->factory->setEntityNamespace('What\Ever');  // Default: empty
$this->factory->persistOnGet();                   // Default: don't persist
$this->factory->persistOnGet(false);