chmeldax/phpmocks 问题修复 & 功能扩展

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

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

chmeldax/phpmocks

Composer 安装命令:

composer require --dev chmeldax/phpmocks

包简介

Enhanced mocking of PHP classes for testing purposes

README 文档

README

Making mocking a pleasure!

Library to be used for testing classes.

Features

  • Stubbing and mocking of all public class methods
  • Supports abstract classes and interfaces
  • Mocking of static methods
  • Different behaviors of the mock based on the method call parameters
  • Strict mocking by default (calls to non-mocked method are disabled by default)
  • Fluent intuitive interface

Requirements

  • PHP 5.6+
  • Composer
  • allowed eval language construct

Installation

composer require chmeldax/phpmocks

Usage

Stubs

When you want to replace default behavior of the class, but you are not testing the method calls themselves, then use allowMethodCall().

$doubleBuilder = new \Chmeldax\PhpMocks\Double\Builder($className);
$doubleBuilder
    ->allowMethodCall('methodToBeStubbed')
    ->with(new Constraints\Anything, new Constraints\Anything, null) // Specify allowed parameters
    ->andReturn('return_value_1');
$double = $doubleBuilder->build(); // This is the stub

Specifying return values

$doubleBuilder
    ->allowMethodCall('methodToBeStubbed')
    ->with(new Constraints\Anything, new Constraints\Anything, null) // Specify allowed parameters
    ->andReturn('return_value_1', 'return_value_2'); // Returns different values on #1 and #2 call
$doubleBuilder
    ->allowMethodCall('methodToBeStubbed')
    ->with(new Constraints\Anything, new Constraints\Anything, null) // Specify allowed parameters
    ->andInvoke(function ($a, $b, $c) { // specify your own callback
        return 'return_value'
      });
$doubleBuilder = new \Chmeldax\PhpMocks\Double\Builder($instance);
$doubleBuilder
    ->allowMethodCall('methodToBeStubbed')
    ->with(new Constraints\Anything, new Constraints\Anything, null) // Specify allowed parameters
    ->andCallOriginal(); // Calls the original method from $instance

Multiple with blocks

$doubleBuilder
    ->allowMethodCall('methodToBeStubbed')
    ->with(new Constraints\Anything, new Constraints\Anything, 'first')
    ->andReturn('return_value_1'); // Returns different values on #1 and #2 call

$doubleBuilder
    ->allowMethodCall('methodToBeStubbed')
    ->with(new Constraints\Anything, new Constraints\Anything, 'second') // Different parameter value
    ->andReturn('return_value_2');

$double = $doubleBuilder->build();
$double->methodToBeStubbed(1, 2, 'first');  // returns 'return_value_1'
$double->methodToBeStubbed(1, 2, 'second'); // returns 'return_value_2'

See tests/DoubleTest.php for more examples.

Mocks

When you are testing the interaction with class, use expectMethodCall. It will allow you to specify expected calls.

$doubleBuilder
    ->expectMethodCall('methodToBeMocked')
    ->with(new Constraints\Anything, new Constraints\Anything, null)
    ->times(10) // Expectation
    ->andReturn('return_value_1');
$doubleBuilder
    ->expectMethodCall('methodToBeMocked')
    ->with(new Constraints\Anything, new Constraints\Anything, 'first')
    ->atCalls(1, 3) // Expectation for 1st and 3rd call (counted globally for all "with" blocks)
    ->andReturn('return_value_1');

$doubleBuilder
    ->expectMethodCall('methodToBeMocked')
    ->with(new Constraints\Anything, new Constraints\Anything, 'second')
    ->atCall(2) // Expectation for 2nd call (counted globally for all "with" blocks)
    ->andReturn('return_value_2');

Checking expectations

$doubleBuilder->checkExpectations() // Throws exception if any expectation is not met

It is advised to use tearDown() (or similar method) in your testing framework.

See tests/ExpectedMethodTest.php for more examples.

Things to implement

  • Support for PHP 7 (mainly return values, scalar types)
  • Implement better Constraints
  • Clean the code
  • Introduce true unit tests

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-03-05

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固