承接 folsh/phpunit-dynamic-fixture 相关项目开发

从需求分析到上线部署,全程专人跟进,保证项目质量与交付效率

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

folsh/phpunit-dynamic-fixture

最新稳定版本:0.2.0

Composer 安装命令:

composer require folsh/phpunit-dynamic-fixture

包简介

Dynamic setUp to create an appropriate context in your tests

README 文档

README

SensioLabsInsight Build Status Scrutinizer Quality Score Code Coverage

DynamicFixture

Thanks to annotations, this library allows you to call dynamic/custom "setUp" methods before each one of your test. It eases the understanding of your test because you explicitly set which context/variables you will use. It also can speed up your tests as you only initialize what they need.

Installation

Composer

Simply add this to your composer.json file:

"require": {
    "folsh/phpunit-dynamic-fixture": "dev-master"
}

Then run php composer.phar install

PhpUnit configuration

To activate the plugin. Add the listener to your phpunit.xml(.dist) file:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit>
    ...
    <listeners>
        <listener class="folsh\DynamicFixture\DynamicFixtureListener" file="vendor/folsh/phpunit-dynamic-fixture/src/DynamicFixtureListener.php" />
    </listeners>
</phpunit>

Usage

Use the annotation "@setUpContext" to call the specified method(s) just before the test. Of course, you can add as many annotations as you want.

class MyTest extends PHPUnit_Framework_TestCase {

    private $name;

    /**
     * Must be public
     */
    public function setUpName()
    {
        $this->name = 'Nicolas';
    }

    public function initContext()
    {
        //...
    }

    /**
     * @setUpContext setUpName
     * @setUpContext initContext
     * @setUpContext ...
     *
     */
    public function testSetUpName()
    {
        //$this->name is "Nicolas"
    }
}

Params

If you want to add some parameters in your function, add them between brackets. Warning: at this time, you can only add simple types (see exemples) Types accepted:

  • string
  • json
  • array (single level)
  • numeric (convert in string !)
class MyTest extends PHPUnit_Framework_TestCase {

    private $name1;
    private $name2;
    private $list;

    /**
     * Must be public
     */
    public function setUpNames($name1, $name2)
    {
        $this->name1 = $name1;
        $this->name2 = $name2;
    }

    public function initContext()
    {
        //...
    }

    /**
     * @setUpContext setUpNames("Nicolas", "Cedric")
     */
    public function testSetUpNames()
    {
        //$this->name1 is "Nicolas"
        //$this->name2 is "Cedric"
    }
}
class MyTest extends PHPUnit_Framework_TestCase {

    private $list;

    /**
     * Must be public
     */
    public function setUpArray(array $list)
    {
        $this->list = $list;
    }

    public function initContext()
    {
        //...
    }

    /**
     * @setUpContext setUpArray(["Nicolas", "Cedric"])
     */
    public function testSetUpArray()
    {
        //$this->list is array("Nicolas", "Cedric")
    }
}

Customize

If you don't like the name of the annotation, you can change it by passing a new one in the constructor:

 <listener class="folsh\DynamicFixture\DynamicFixtureListener" file="vendor/folsh/phpunit-dynamic-fixture/src/DynamicFixtureListener.php">
    <arguments>
        <string>myCustomSetUp</string>
    </arguments>
</listener>

统计信息

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

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2015-06-11