承接 thanos-kontos/amock 相关项目开发

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

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

thanos-kontos/amock

Composer 安装命令:

composer require thanos-kontos/amock

包简介

PHPunit mock fixtures

README 文档

README

Latest Stable Version Build Status Code Coverage Scrutinizer Code Quality

Release notes

Still not 100% stable. Use with caution

At a glance

Quoting from phpunit:

Sometimes it is just plain hard to test the system under test (SUT) because it depends on other components that cannot be used in the test environment. This could be because they aren't available, they will not return the results needed for the test or because executing them would have undesirable side effects. In other cases, our test strategy requires us to have more control or visibility of the internal behavior of the SUT.

When we are writing a test in which we cannot (or chose not to) use a real depended-on component (DOC), we can replace it with a Test Double. The Test Double doesn't have to behave exactly like the real DOC; it merely has to provide the same API as the real one so that the SUT thinks it is the real one!

It is almost impossible to write good tests without stubbing objects, because we often need to imitate writing to a database or calling an external service.

Amock lets you create these stub objects in yaml instead of "polluting" your tests with irrelevant code.

Instructions

composer require --dev thanos-kontos/amock

Add a directory to store your fixtures e.g. tests/mock_fixtures and as as many yaml files as you want.

# tests/mock_fixtures/repositories.yml

MyProject\Library\UserRepository:
  mockUserRepository:
    disableConstructor: true
    mockMethods:
      insert: '@null'
      update: '@null'
MyProject\Library\ProductRepository:
  mockProductRepository:
    disableConstructor: true
    mockMethods:
      insert: '@null'
      update: '@null'
# tests/mock_fixtures/gateways.yml

MyProject\Library\SomeApiGateway:
  mockSuccessResponse:
    disableConstructor: true
    mockMethods:
      getHelloReponse: '@string:{"hello":"world"}'
  mock404Response:
    disableConstructor: true
    mockMethods:
      getHelloReponse: '@string:{"error": "404","message":"Not found"}'
      sampleSetter: '@self'
      methodThatReturnsDifferentValueOnConsecutiveCalls:
        - '@string:{"hello":"world"}'
        - '@integer:123'
        - '@boolean:false'
      methodThatReturnsArray: '@array:{"111":"aaa","222":"bbb"}'
      methodThatReturnsBoolean: '@boolean:false'
      methodThatReturnsInteger: '@integer:123'
      methodThatReturnsNull: '@null'
  mockExceptionResponse:
    disableConstructor: true
    mockMethods:
      getHelloReponse: '@exception:\MyProject\Library\Exception\ApiException'

On your test setUp method

$config = new \Amock\Configuration('yaml', 'dir', '/path/to/mock_fixtures');
$this->amock = \Amock\Amock::create($config, $testCase);

or

$config = new \Amock\Configuration('yaml', 'file', '/path/to/mock_fixtures/somefile.yml');
$this->amock = \Amock\Amock::create($config, $testCase);

Then you can use the configured mocks in your tests:

$stub = $this->amock->get('mock404Response');

Example

There is an dummy project here that you can use as a reference.

License

Amock is released under the MIT License.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-10-31

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固