englishdom/unitarum

The PHPUnit library is providing and flexible changing fixtures to database

1.0.5 2024-02-23 16:24 UTC

This package is auto-updated.

Last update: 2024-04-23 16:57:43 UTC


README

The library for providing and flexible changing fixtures for PHPUnit

Default fixtures

Fixtures use entities. Entities must provide methods getId and setId. The library propose interface EntityIdentifierInterface with this two methods if it need.

An entity's example

class User
{
    protected $id;
    protected $name;
    protected $email;

    public function getId()
    {
        return $this->id;
    }

    public function setId($id)
    {
        $this->id = $id;
    }

    public function getName()
    {
        return $this->name;
    }

    public function setName($name)
    {
        $this->name = $name;
    }

    public function getEmail()
    {
        return $this->email;
    }

    public function setEmail($email)
    {
        $this->email = $email;
    }
}

A fixture's example:

$entity = new Entity\User();
$entity->setId(1);
$entity->setName('Test');
$entity->setEmail('test@test.no');

return ['table_name' => $entity];

PhpUnit test

In the test's start need initialize the Unitarum object with two parameters. The first parameter is path to folder with fixtures. The second parameter is dsn connection string to sqlite.

Test example

use PHPUnit\Framework\TestCase;

class DatabaseTest extends TestCase
{
    protected static $unitarum;

    public static function setUpBeforeClass()
    {
        self::$unitarum = new Unitarum([
            OptionsInterface::FIXTURE_FOLDER_OPTION => '../data',
            OptionsInterface::DSN_OPTION => 'sqlite:data/sqlite.db',
        ]);
    }
}

In the every test's cases need to start and rollback a transaction.

Test example

class DatabaseTest extends TestCase
{
    protected static $unitarum;

    public static function setUpBeforeClass()
    {
        ...
    }
    
    public function setUp()
    {
        self::$unitarum->getDataBase()->startTransaction();
        // or notning
    }
    
    public function tearDown()
    {
        self::$unitarum->getDataBase()->rollbackTransaction();
        //or
        self::$unitarun->truncate();
    }
}

If you don't want to use transaction, you can use TRUNCATE data.

Setup fixtures

In the test you can apply fixtures and change any data from it. You can call methods from the initialized unitarum object. Called method name must be equals name of fixture file. A method can get one parameter, is it Entity. An entity can rewrite default data from fixture.

Test example

class DatabaseTest extends TestCase
{
    ...
    
    public function testApplication()
    {
        $user = new Entity\User();
        $user->setEmail('newemail@email.no');
        
        $role = new Entity\Role();
        $role->setRole('viewer');
        $role->setUserId($user->getId());
        
        self::$unitarum->user($user)->role($role);
        
        // test
        ...
    }
}