davesweb/mockery-helper

A set of helpers to create useful unit tests that use the Mockery library

1.1.1 2018-02-17 00:04 UTC

This package is auto-updated.

Last update: 2024-04-12 23:35:47 UTC


README

A library to help you use Mockery in the best way possible.

Build Status Latest Stable Version Latest Unstable Version License composer.lock

Current stable version: 1.1.0

Installation

Via composer:

composer require davesweb/mockery-helper

Use the --dev option if you only require your test dependencies in developer mode.

Via composer.json file:

You can also add this package directly to your composer.json file. Add the following line to your require block, or require-dev block:

"davesweb/mockery-helper": "^1.1"

Then run:

composer update davesweb/mockery-helper

Usage

Once you installed the package, you can use the provided trait in your tests:

<?php

namespace My\Tests;

use Davesweb\MockeryHelper\UsesMockery;
use PHPUnit\Framework\TestCase;

class UsesMockeryTest extends TestCase
{
    use UsesMockery;
}

This traits ensures that your Mockery expectations are enforced, because it expands on the Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration trait. So if you use this trait, calling \Mockery::close() is no longer needed.

Methods

This trait provides the following methods.

method

Signature:

public function method(callable $callable): string

If you need to pass a method name to a Mockery expectation, use this method to do so. This method accepts a callable as its argument, assuring that the method name you want to pass is recognised as such by an IDE. That way, when you refactor your code, the method name is also refactored in your Unit tests.

This method also checks to see if the method actually exists, so you don't make any typos in your method name.

The callable can be passed in the form of 'ClassName::method', [ClassName::class, 'method'] or [$object, 'method'].

The method method returns the name of the method.

Example usage:

<?php

namespace My\Tests;

use Davesweb\MockeryHelper\UsesMockery;
use PHPUnit\Framework\TestCase;
use Some\Package\MyDependency;

class UsesMockeryTest extends TestCase
{
    use UsesMockery;
    
    public function setUp()
    {
        $mockedDependency = \Mockery::mock(MyDependency::class);
        
        $mockedDependency
            ->shouldReceive($this->method([MyDependency::class, 'methodItShouldReceive']))
            ->with('param 1', 'param 2')
            ->once()
            ->andReturn('My return value');
        
        //...
    }
}

mock

Signature:

public function mock(...$args): MockInterface

This is an alias for the Mockery::mock(...$args): MockerInterface call. Check the Mockery documentation on how to use this method.

spy

Signature:

public function spy(...$args): MockInterface

This is an alias for the Mockery::spy(...$args): MockerInterface call. Check the Mockery documentation on how to use this method.

namedMock

Signature:

public function namedMock(...$args): MockInterface

This is an alias for the Mockery::namedMock(...$args): MockerInterface call. Check the Mockery documentation on how to use this method.

Skipping the check if a method exists.

Sometimes you want to be able to skip the check if a method is actually defined on a class, while still keeping the refactor options. This can happen for instance if you use the magic __call() method to handle some method calls.

In that case you can disable the check by overriding the allowNonExistingMethods property, or setting it in your test:

class UsesMockeryTest extends TestCase
{
    use UsesMockery;
    
    /**
     * {@inheritdoc}
     */
    protected $allowNonExistingMethods = true;
    
    public function test_something()
    {
        // This entire test case won't check if methods exist.
    }
}
class UsesMockeryTest extends TestCase
{
    use UsesMockery;
    
    public function test_something()
    {
        $this->allowNonExistingMethods = true;
        
        // Only this test won't check if methods exist.
    }
}

You can also set the check per method call by adding a second parameter to the method call. Set it to true to enforce the check, even if the global setting is to skip to check. Or set it to false to skip the check just for that method call.

class UsesMockeryTest extends TestCase
{
    use UsesMockery;
    
    public function test_something()
    {        
        $method = $this->method([MyDependency::class, 'someNonExistingMethod'], false);
        
        // Only the above call skips the check if the method exists
    }
}

Contributing

Thank you for taking the time to improve this package! If you want to contribute to this package, please read to following file first: Contributing.