承接 mvkasatkin/mocker 相关项目开发

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

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

mvkasatkin/mocker

Composer 安装命令:

composer require mvkasatkin/mocker

包简介

Mocking helper for PHPUnit

README 文档

README

Build Status Coverage Status Latest Stable Version License

Helper for convenient work with PHPUnit mocks.
It uses the library phpunit/phpunit-mock-objects supplied with PHPUnit.

Install

composer require mvkasatkin/mocker

Initialize

    public function setUp()
    {
        parent::setUp();
        Mocker::init($this);
    }

Simple test-double

Works with regular classes, abstract classes and interfaces.

        $mock = Mocker::create(SomeClass::class);
        $this->assertInstanceOf(SomeClass::class, $mock);

Test-double with methods

        $mock = Mocker::create(SomeClass::class, [
            Mocker::method('firstMethod')->returns(true),
            Mocker::method('secondMethod', 1)->returns(true),
            Mocker::method('thirdMethod', 1, [$param1, $param2])->returns($value),
            Mocker::method('fourthMethod', 1)->with([$param1, $param2])->returns($value),
        ]);

In this example:

$mock->firstMethod - can be called any number of times with any parameters and returns true
$mock->secondMethod - must be called once and returns true
$mock->thirdMethod - must be called once with the parameters $param1, $param2 and returns $value
$mock->fourthMethod - must be called once with the parameters $param1, $param2 and returns $value

Test-double with/return map:

        /** @var SomeClass $mock */
        $mock = Mocker::create(SomeClass::class, [
            Mocker::method('checkMap', 5)->returnsMap([
                ['arg1', 'arg2', 'arg3', 'A'],
                ['arg1', 'arg2', null, 'B'],
                ['arg1', null, null, 'C'],
                [null, null, null, 'D'],
            ]),
        ]);
        $this->assertEquals('A', $mock->checkMap('arg1', 'arg2', 'arg3'));
        $this->assertEquals('B', $mock->checkMap('arg1', 'arg2'));
        $this->assertEquals('C', $mock->checkMap('arg1'));
        $this->assertEquals('D', $mock->checkMap());
        $this->assertEquals(null, $mock->checkMap('a', 'b', 'c'));

Mocking of private methods is impossible.
Mocking of protected methods is possible, but it's not a «best practice».

Test-double with constructor call

        $mock = Mocker::create(SomeClass::class, [...], [$arg1, $arg2]);
        $this->assertInstanceOf(SomeClass::class, $mock);

Constructor of the SomeClass will be invoked with args: $arg1, $arg2

Protected properties and methods

Despite the fact that testing internal implementation of classes is not the best practice, sometimes it is still necessary to set or verify a protected property, or to call a protected method.

It works both with protected properties and methods, and with private:

        $o = new SomeClass();
        Mocker::setProperty($o, 'protectedProperty', 'a');
        Mocker::setProperty($o, 'privateProperty', 'b');
        $this->assertEquals('a', Mocker::getProperty($o, 'protectedProperty'));
        $this->assertEquals('b', Mocker::getProperty($o, 'privateProperty'));
        $this->assertEquals('aZ', Mocker::invoke($o, 'privateMethod', ['a']));

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-09-20

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固