englishdom/unitarum 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

englishdom/unitarum

最新稳定版本:1.0.5

Composer 安装命令:

composer require englishdom/unitarum

包简介

The PHPUnit library is providing and flexible changing fixtures to database

README 文档

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
        ...
    }
}

统计信息

  • 总下载量: 8.08k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 2
  • Forks: 3
  • 开发语言: PHP

其他信息

  • 授权协议: Apache-2.0
  • 更新时间: 2018-01-19