mnapoli/mockup 问题修复 & 功能扩展

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

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

mnapoli/mockup

Composer 安装命令:

composer require mnapoli/mockup

包简介

Concise mock library for PHP tests

README 文档

README

Concise mock library for PHP tests.

Build Status

Why?

This mock library is meant to be a simple yet powerful alternative to existing solutions.

  • Mockup
$mock = mock(Foo::class, [
    'foo' => 'Hello',
]);
  • PHPUnit
$mock = $this->getMock(Foo::class, [], [], '', false);
$mock->expect($this->any())
    ->method('foo')
    ->willReturn('Hello');
  • Prophecy
$prophet = new \Prophecy\Prophet();
$mock = $prophet->prophesize(Foo::class);
$mock->foo()->willReturn('Hello');
$mock = $mock->reveal();
  • Mockery
$mock = Mockery::mock(Foo::class);
$mock->shouldReceive('foo')
    ->andReturn('Hello');

Additionally, Mockup doesn't include assertions. Instead of forcing you to learn a specific assertion syntax, complex enough to cover all cases, it lets you use the assertions you already know (PHPUnit, phpspec, …). Here is an example in a PHPUnit test:

// The mock method was called once
$this->assertEquals(1, inspect($mock)->foo()->invokationCount());

Read more about spying method calls below.

Installation

composer require --dev mnapoli/mockup

Usage

Mocks

You can mock a class or an interface:

use function Mockup\mock;

interface Foo
{
    public function foo($bar);
}

$mock = mock(Foo::class);
$mock->foo();

All its methods will do nothing and return null (null object pattern). The mock will implement the interface or extend the class given, as such it will work fine with any type-hint.

You can make some methods return values other than null:

$mock = mock(Foo::class, [
    'foo' => 'hello',
]);

$mock->foo('john'); // hello

You can also use a closure to define the new method's body:

$mock = mock(Foo::class, [
    'foo' => function ($bar) {
        return strtoupper('hello ' . $bar);
    }
]);

$mock->foo('john'); // HELLO JOHN

Spies

You can spy calls to an object:

use function Mockup\{spy, inspect};

$spy = spy($cache);
$foo->doSomething($spy);

inspect($spy)->set()->invokationCount(); // number of calls to $spy->set()
inspect($spy)->set()->parameters(0); // parameters provided to the first call to $spy->set()
inspect($spy)->set()->returnValue(0); // value returned by the first call to $spy->set()

The difference with a mock is that you are spying real calls to a real object. A mock is a null object.

Mockup does not provide assertions or expectations so that you can use the assertion library you prefer.

Every mock object is also a spy, so you can create a mock and spy its method calls:

use function Mockup\{mock, inspect};

$mock = mock(CacheInterface::class);
$foo->doSomething($mock);

inspect($spy)->set()->invokationCount();
inspect($spy)->set()->parameters(0);
inspect($spy)->set()->returnValue(0);

统计信息

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

GitHub 信息

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

其他信息

  • 授权协议: MIT
  • 更新时间: 2016-03-31

承接程序开发

PHP开发

VUE

Vue开发

前端开发

小程序开发

公众号开发

系统定制

数据库设计

云部署

网站建设

安全加固