承接 sitphp/doubles 相关项目开发

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

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

sitphp/doubles

Composer 安装命令:

composer require --dev sitphp/doubles

包简介

A simple yet powerful library to make class doubles for testing

README 文档

README

Build Status

The "sitphp/doubles" library can help you to test your PHP classes by generating doubles that look like the original classes but can be manipulated and tested (sort of a copy of a class). These doubles then can then be used instead of the original classes for your test. This library can create doubles of any kind of class, interface or trait.

See full documentation : here

Requirements

The "sitphp/doubles" library requires at least PhpUnit 6.5 and at least PHP 7.0. It should be installed from composer which will make sure your configuration matches requirements.

{.note .info} Note : You can get composer here : https://getcomposer.org.

Installation

Add the line "sitphp/doubles": "2.4.*" in the "require-dev" section of your composer.json file :

{
    "require-dev": {
        "sitphp/doubles": "2.4.*"
    }
}

And run the following command :

composer update

This will install the latest version of the "sitphp/doubles" library with the required PhpUnit package.

Creating a double

A double is called a "dummy" when all the methods of the original class are overwritten to return null. To get a " dummy" double instance, use the dummy method :

// Get a double instance of type "dummy" for class "MyClass"
$my_double = Double::dummy(MyClass::class)->getInstance();

A double is called a "mock" when all the methods of the original class are overwritten to behave the same as in the original class. To get a "mock" double instance, use the mock method :

// Get a double instance of type "mock" for class "MyClass"
$my_double = Double::mock(MyClass::class)->getInstance();

For more details : Read the doc on creating doubles

Testing a double

To test how many times a double method is called, use the count method :

// Test that the method "myMethod" is called a least one time
$double::_method('myMethod')->count('>=1');

To test the values of the arguments passed to a double method, use the args method :

// Test that the arguments passed to method "myMethod" are "value1" and "value2"
$double::_method('myMethod')->args(['value1', 'value2']);

To change the return value of a method, use the stub method. :

// Make method "myMethod" return "hello"
$my_double::_method('myMethod')->return('hello');

For more details : Read the doc on testing doubles

Configuration

You define the configuration for a specific double using the 2nd argument of the dummy and mock methods :

// Get double instance with config
$my_double = Double::dummy(MyClass::class, [
    'allow_final_doubles' => true,
    'allow_non_existent_classes' => true
])->getInstance();

Here is a list of all available config parameters :

  • allow_final_doubles : Set this parameter to false to stop trying to make doubles of final classes/methods.
  • allow_protected_methods : Set this parameter to false to disallow testing protected methods.
  • allow_non_existent_classes : Set this parameter to false to disallow alias doubles of non existent classes.
  • test_unexpected_methods : Set this parameter to true to automatically receive an assertion error whenever an unexpected method is called.

For more details : Read the doc on configuration

About

License

This library is licensed under the MIT license.

Author

Alexandre Geiswiller - alexandre.geiswiller@gmail.com.

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2019-06-04

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固