icanhazstring/phpunit-faker-extension 问题修复 & 功能扩展

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

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

icanhazstring/phpunit-faker-extension

最新稳定版本:0.1

Composer 安装命令:

composer require icanhazstring/phpunit-faker-extension

包简介

PHPUnit extension to add faker support

README 文档

README

Build Min php version Min phpunit version

Using this extension you can use fzaninotto/faker with your phpunit tests. Every test will be seeded so you will be able run the same test again if an error occurs.

phpunit-faker-extension-screenshot

Installation

You can install this extension by using Composer. This package should be added as a require-dev dependency:

composer require --dev icanhazstring/phpunit-faker-extension

Usage

Enable with all defaults by adding the following code to your project's phpunit.xml file:

<phpunit bootstrap="vendor/autoload.php">
...
    <listeners>
        <listener class="PHPUnitFaker\FakerTestListener" />
    </listeners>
</phpunit>

Now run the test suite as normal. As soon as all tests are completed, you will see a seed that was used to generate the faker data:

Tests done with seed: XXX

Using Fake inside a test

To use faker inside your tests, you will need to implement the FakerAwareTest interface and the FakerTrait. Since not all tests will need faker, the interface makes sure only tests using faker, will get the needed data.

To actually invoke you only need to do the following:

class AwesomeTest extends TestCase implements FakerAwareTest
{
    use FakerTrait;
    
    public function testAwesomeStuff(): void
    {
        $this->assertSame($this->fake()->name, $this->fake()->name);
    }
}

See faker documentation for more information about formatters: https://github.com/fzaninotto/faker#formatters

Running tests with a given seed

To run tests with a given seed, simple set the PHPUNIT_SEED environment variable before running phpunit:

$ PHPUNIT_SEED=XXX vendor/bin/phpunit

Configuration

This extension has three configurable parameters:

  • locale - The locale that faker should use (Default: en_GB)
  • fakerProviderProvider - (Silly name I know) A single invokable class returning a list of providers (string or instance)

These configuration parameters are set in phpunit.xml when adding the listener:

<phpunit bootstrap="vendor/autoload.php">
    <!-- ... other suite configuration here ... -->

    <listeners>
        <listener class="PHPUnitFaker\FakerTestListener">
            <arguments>
                <array>
                    <element key="locale">
                        <string>de_DE</string>
                    </element>
                    <element key="fakerProviderProvider">
                        <string>Your\FakerProviderProvider</string>
                    </element>
                </array>
            </arguments>
        </listener>
    </listeners>
</phpunit>

License

phpunit-faker-extension is available under the MIT License.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-01-25