mockery

2025-12-10 0 538

Mockery

Mockery is a simple yet flexible PHP mock object framework for use in unit testing
with PHPUnit, PHPSpec or any other testing framework. Its core goal is to offer a
test double framework with a succinct API capable of clearly defining all possible
object operations and interactions using a human readable Domain Specific Language
(DSL). Designed as a drop in alternative to PHPUnit\’s phpunit-mock-objects library,
Mockery is easy to integrate with PHPUnit and can operate alongside
phpunit-mock-objects without the World ending.

Mockery is released under a New BSD License.

Installation

To install Mockery, run the command below and you will get the latest
version

composer require --dev mockery/mockery

Documentation

In older versions, this README file was the documentation for Mockery. Over time
we have improved this, and have created an extensive documentation for you. Please
use this README file as a starting point for Mockery, but do read the documentation
to learn how to use Mockery.

The current version can be seen at docs.mockery.io.

PHPUnit Integration

Mockery ships with some helpers if you are using PHPUnit. You can extend the
Mockery\\Adapter\\Phpunit\\MockeryTestCase
class instead of PHPUnit\\Framework\\TestCase, or if you are already using a
custom base class for your tests, take a look at the traits available in the
Mockery\\Adapter\\Phpunit namespace.

Test Doubles

Test doubles (often called mocks) simulate the behaviour of real objects. They are
commonly utilised to offer test isolation, to stand in for objects which do not
yet exist, or to allow for the exploratory design of class APIs without
requiring actual implementation up front.

The benefits of a test double framework are to allow for the flexible generation
and configuration of test doubles. They allow the setting of expected method calls
and/or return values using a flexible API which is capable of capturing every
possible real object behaviour in way that is stated as close as possible to a
natural language description. Use the Mockery::mock method to create a test
double.

$double = Mockery::mock();

If you need Mockery to create a test double to satisfy a particular type hint,
you can pass the type to the mock method.

class Book {}

interface BookRepository {
    function find($id): Book;
    function findAll(): array;
    function add(Book $book): void;
}

$double = Mockery::mock(BookRepository::class);

A detailed explanation of creating and working with test doubles is given in the
documentation, Creating test doubles
section.

Method Stubs ?

A method stub is a mechanism for having your test double return canned responses
to certain method calls. With stubs, you don\’t care how many times, if at all,
the method is called. Stubs are used to provide indirect input to the system
under test.

$double->allows()->find(123)->andReturns(new Book());

$book = $double->find(123);

If you have used Mockery before, you might see something new in the example
above — we created a method stub using allows, instead of the \”old\”
shouldReceive syntax. This is a new feature of Mockery v1, but fear not,
the trusty ol\’ shouldReceive is still here.

For new users of Mockery, the above example can also be written as:

$double->shouldReceive(\'find\')->with(123)->andReturn(new Book());
$book = $double->find(123);

If your stub doesn\’t require specific arguments, you can also use this shortcut
for setting up multiple calls at once:

$double->allows([
    \"findAll\" => [new Book(), new Book()],
]);

or

$double->shouldReceive(\'findAll\')
    ->andReturn([new Book(), new Book()]);

You can also use this shortcut, which creates a double and sets up some stubs in
one call:

$double = Mockery::mock(BookRepository::class, [
    \"findAll\" => [new Book(), new Book()],
]);

Method Call Expectations ?

A Method call expectation is a mechanism to allow you to verify that a
particular method has been called. You can specify the parameters and you can
also specify how many times you expect it to be called. Method call expectations
are used to verify indirect output of the system under test.

$book = new Book();

$double = Mockery::mock(BookRepository::class);
$double->expects()->add($book);

During the test, Mockery accept calls to the add method as prescribed.
After you have finished exercising the system under test, you need to
tell Mockery to check that the method was called as expected, using the
Mockery::close method. One way to do that is to add it to your tearDown
method in PHPUnit.

public function tearDown()
{
    Mockery::close();
}

The expects() method automatically sets up an expectation that the method call
(and matching parameters) is called once and once only. You can choose to change
this if you are expecting more calls.

$double->expects()->add($book)->twice();

If you have used Mockery before, you might see something new in the example
above — we created a method expectation using expects, instead of the \”old\”
shouldReceive syntax. This is a new feature of Mockery v1, but same as with
allows in the previous section, it can be written in the \”old\” style.

For new users of Mockery, the above example can also be written as:

$double->shouldReceive(\'find\')
    ->with(123)
    ->once()
    ->andReturn(new Book());
$book = $double->find(123);

A detailed explanation of declaring expectations on method calls, please
read the documentation, the Expectation declarations
section. After that, you can also learn about the new allows and expects methods
in the Alternative shouldReceive syntax
section.

It is worth mentioning that one way of setting up expectations is no better or worse
than the other. Under the hood, allows and expects are doing the same thing as
shouldReceive, at times in \”less words\”, and as such it comes to a personal preference
of the programmer which way to use.

Test Spies

By default, all test doubles created with the Mockery::mock method will only
accept calls that they have been configured to allow or expect (or in other words,
calls that they shouldReceive). Sometimes we don\’t necessarily care about all of the
calls that are going to be made to an object. To facilitate this, we can tell Mockery
to ignore any calls it has not been told to expect or allow. To do so, we can tell a
test double shouldIgnoreMissing, or we can create the double using the Mocker::spy
shortcut.

// $double = Mockery::mock()->shouldIgnoreMissing();
$double = Mockery::spy();

$double->foo(); // null
$double->bar(); // null

Further to this, sometimes we want to have the object accept any call during the test execution
and then verify the calls afterwards. For these purposes, we need our test
double to act as a Spy. All mockery test doubles record the calls that are made
to them for verification afterwards by default:

$double->baz(123);

$double->shouldHaveReceived()->baz(123); // null
$double->shouldHaveReceived()->baz(12345); // Uncaught Exception Mockery\\Exception\\InvalidCountException...

Please refer to the Spies section
of the documentation to learn more about the spies.

Utilities ?

Global Helpers

Mockery ships with a handful of global helper methods, you just need to ask
Mockery to declare them.

Mockery::globalHelpers();

$mock = mock(Some::class);
$spy = spy(Some::class);

$spy->shouldHaveReceived()
    ->foo(anyArgs());

All of the global helpers are wrapped in a !function_exists call to avoid
conflicts. So if you already have a global function called spy, Mockery will
silently skip the declaring its own spy function.

Testing Traits

As Mockery ships with code generation capabilities, it was trivial to add
functionality allowing users to create objects on the fly that use particular
traits. Any abstract methods defined by the trait will be created and can have
expectations or stubs configured like normal Test Doubles.

trait Foo {
    function foo() {
        return $this->doFoo();
    }

    abstract function doFoo();
}

$double = Mockery::mock(Foo::class);
$double->allows()->doFoo()->andReturns(123);
$double->foo(); // int(123)

Versioning

The Mockery team attempts to adhere to Semantic Versioning,
however, some of Mockery\’s internals are considered private and will be open to
change at any time. Just because a class isn\’t final, or a method isn\’t marked
private, does not mean it constitutes part of the API we guarantee under the
versioning scheme.

Alternative Runtimes

Mockery 1.3 was the last version to support HHVM 3 and PHP 5. There is no support for HHVM 4+.

A new home for Mockery

️️ Update your remotes! Mockery has transferred to a new location. While it was once
at padraic/mockery, it is now at mockery/mockery. While your
existing repositories will redirect transparently for any operations, take some
time to transition to the new URL.

$ git remote set-url upstream https://githu*b.*co*m/mockery/mockery.git

Replace upstream with the name of the remote you use locally; upstream is commonly
used but you may be using something else. Run git remote -v to see what you\’re actually
using.

下载源码

通过命令行克隆项目:

git clone https://github.com/mockery/mockery.git

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

申明:本文由第三方发布,内容仅代表作者观点,与本网站无关。对本文以及其中全部或者部分内容的真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。本网发布或转载文章出于传递更多信息之目的,并不意味着赞同其观点或证实其描述,也不代表本网对其真实性负责。

左子网 编程相关 mockery https://www.zuozi.net/32994.html

TabNine
上一篇: TabNine
Mobile Detect
下一篇: Mobile Detect
常见问题
  • 1、自动:拍下后,点击(下载)链接即可下载;2、手动:拍下后,联系卖家发放即可或者联系官方找开发者发货。
查看详情
  • 1、源码默认交易周期:手动发货商品为1-3天,并且用户付款金额将会进入平台担保直到交易完成或者3-7天即可发放,如遇纠纷无限期延长收款金额直至纠纷解决或者退款!;
查看详情
  • 1、描述:源码描述(含标题)与实际源码不一致的(例:货不对板); 2、演示:有演示站时,与实际源码小于95%一致的(但描述中有”不保证完全一样、有变化的可能性”类似显著声明的除外); 3、发货:不发货可无理由退款; 4、安装:免费提供安装服务的源码但卖家不履行的; 5、收费:价格虚标,额外收取其他费用的(但描述中有显著声明或双方交易前有商定的除外); 6、其他:如质量方面的硬性常规问题BUG等。 注:经核实符合上述任一,均支持退款,但卖家予以积极解决问题则除外。
查看详情
  • 1、左子会对双方交易的过程及交易商品的快照进行永久存档,以确保交易的真实、有效、安全! 2、左子无法对如“永久包更新”、“永久技术支持”等类似交易之后的商家承诺做担保,请买家自行鉴别; 3、在源码同时有网站演示与图片演示,且站演与图演不一致时,默认按图演作为纠纷评判依据(特别声明或有商定除外); 4、在没有”无任何正当退款依据”的前提下,商品写有”一旦售出,概不支持退款”等类似的声明,视为无效声明; 5、在未拍下前,双方在QQ上所商定的交易内容,亦可成为纠纷评判依据(商定与描述冲突时,商定为准); 6、因聊天记录可作为纠纷评判依据,故双方联系时,只与对方在左子上所留的QQ、手机号沟通,以防对方不承认自我承诺。 7、虽然交易产生纠纷的几率很小,但一定要保留如聊天记录、手机短信等这样的重要信息,以防产生纠纷时便于左子介入快速处理。
查看详情

相关文章

猜你喜欢
发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务