cheprasov/php-extra-mocks 问题修复 & 功能扩展

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

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

cheprasov/php-extra-mocks

最新稳定版本:1.0.0

Composer 安装命令:

composer require cheprasov/php-extra-mocks

包简介

ExtraMocks are a tools that give extra functionality for Mocks.

README 文档

README

MIT license

ExtraMocks v1.0.0 for PHP >= 5.5

About

ExtraMocks are a tools that give extra functionality for Mocks.

Main features

  • Allow to redefine global function in namespaces.

Usage

\ExtraMocks\Mocks :: mockGlobalFunction ( string $fullName , mixed|callable $result [, int|null $count = null ] )

Redefine global function.

Method Pameters
  1. string $fullName - name with namespace of function for redefine
  2. mixed|callable $result - new function or result
  3. int|null $count, default = null. Count of mocked calls

\ExtraMocks\Mocks :: getCountCalls ( string $fullName )

Get count of mocked calls

Method Pameters
  1. string $fullName - fullname of redefined function

Examples

namespace A;

class A
{
    public static function string_length($str)
    {
        return strlen($str);
    }
}
namespace B;

class B {

    public static function string_length($str)
    {
        return strlen($str);
    }
}
namespace Example;

require (__DIR__ . '/../src/autoloader.php');

use ExtraMocks\Mocks;

// 1. Redefine Global Function by Function

\ExtraMocks\Mocks::mockGlobalFunction(
    '\A\strlen',
    function($s) {
        return strlen($s) * 5;
    }
);

echo \A\A::string_length('foo') . PHP_EOL; // 15
echo \B\B::string_length('foo') . PHP_EOL; // 3;

// 2. Redefine Global Function by Result

\ExtraMocks\Mocks::mockGlobalFunction('\A\strlen', 42);

echo \A\A::string_length('foo') . PHP_EOL; // 42;
echo \B\B::string_length('foo') . PHP_EOL; // 3;

// 3. Redefine Global Function by Result once

\ExtraMocks\Mocks::mockGlobalFunction('\A\strlen', 42, 1);

echo \A\A::string_length('foo') . PHP_EOL; // 42;
echo \A\A::string_length('foo') . PHP_EOL; // 3;
echo \B\B::string_length('foo') . PHP_EOL; // 3;

// 3. Get count of calls mocked function

\ExtraMocks\Mocks::mockGlobalFunction('\A\strlen', 42);

echo Mocks::getCountCalls('\A\strlen') . PHP_EOL; // 0
echo \A\A::string_length('foo') . PHP_EOL;        // 42;
echo Mocks::getCountCalls('\A\strlen') . PHP_EOL; // 1
echo \A\A::string_length('foo') . PHP_EOL;        // 42;
echo Mocks::getCountCalls('\A\strlen') . PHP_EOL; // 2

Installation

Composer

Download composer:

wget -nc http://getcomposer.org/composer.phar

and add dependency to your project:

php composer.phar require cheprasov/php-extra-mocks

Running tests

  1. To run tests type in console:

    ./vendor/bin/phpunit

Something doesn't work

Feel free to fork project, fix bugs and finally request for pull

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2017-01-28