sebastianknott/test-utils 问题修复 & 功能扩展

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

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

sebastianknott/test-utils

最新稳定版本:0.2

Composer 安装命令:

composer require sebastianknott/test-utils

包简介

Some classes i like to use for testing

README 文档

README

Some classes i like to use for testing purposes

System Under Test (SUT) Factory

I don't like to do tedious work, so I created a factory that creates the System Under Test (SUT) for me. The factory creates a new instance of the SUT and mocks its constructor dependencies as good as it can.

It returns a Bundle object that contains the SUT and the mocks.

class SomeClass
{
    public function __construct(Dependency $myDependency)
    {
        // ...
    }
}

class Dependency{
    public function someMethod()
    {
        // ...
    }
}

$facade = new BundleFacade();
$bundle = $facade->build(SomeClass::class);

// Get the SUT
$sut = $bundle->sut;
// Access the mocked dependencies by parameter name
$bundle['myDependency']->expects()->someMethod()->once();

Type of Mocks

It supports multiple types of mocking libraries (Mockery, Phake, Prophecy). Just use the provided build method. The default mocking library is Mockery.

[...]
$factory->build(SomeClass::class); // Returns MockeryBundle
$factory->buildMockeryBundle(SomeClass::class); // Returns MockeryBundle
$factory->buildPhakeBundle(SomeClass::class); // Returns PhakeBundle
$factory->buildProphecyBundle(SomeClass::class); // Returns ProphecyBundle
[...]

Prebuild Parameters

If the SUT has dependencies that are not mockable, you can pass them as a prebuild parameter to the factory. The prebuild parameter is an associative array where the key is the name of the parameter in the suts constructor and the value is the instance.

[...]
$prebuildParameters = [
    'myDependency' => new Dependency()
];
$bundel = $factory->build(
    SomeClass::class,
    prebuildParameters: $prebuildParameters
);
[...]

TestToolsCase

I found myself writing the same boilerplate code over and over again. So I condensed it into a base class that I can extend from.

It does the following:

  • Make a sut factory available self::$bundleFactory
  • Make faker available self::$faker
  • Require hamcrest and Mockery

So instead of writing this:

class SomeTest extends \PHPUnit\Framework\TestCase
{
    private $bundleFactory;
    private $faker;
    private $mockedDependency;

    public function setUp()
    {
        $this->bundleFactory = new BundleFactory();
        $this->faker = \Faker\Factory::create();
        $this->faker->seed(9876543255);
        $this->mockedDependency = \Mockery::mock(Dependency::class);
        $this->subject = new SomeClass($this->mockedDependency);
    }

    public function testSomething()
{
        $this->mockedDependency->expects()
            ->someMethod(Matchers::stringValue())
            ->andReturn($this->faker->word());
        $sut->run();
    }

    public function tearDown()
    {
        \Mockery::close();
    }
}

I write that:

class SomeTest extends TestToolsCase
{
    public function testSomething()
    {
        $bundle = self::$bundleFactory->build(SomeClass::class);
        $bundle['myDependency']->expects()
            ->someMethod(stringValue())->andReturn(self::$faker->word());

        $bundle->sut->run();
    }
}

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: GPL-3.0-only
  • 更新时间: 2024-05-21

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固