chubbyphp/chubbyphp-mock

Chubbyphp Mock

1.4.2 2019-10-21 07:17 UTC

This package is auto-updated.

Last update: 2019-12-11 19:29:00 UTC


README

Build Status Coverage Status Total Downloads Monthly Downloads Latest Stable Version Latest Unstable Version

Description

A helper trait simplify mocking within phpunit.

Requirements

  • php: ~7.0
  • phpunit/phpunit: ^6.5|^7.0|^8.0

Installation

Through Composer as chubbyphp/chubbyphp-mock.

composer require chubbyphp/chubbyphp-mock "~1.4" --dev

Usage

<?php

declare(strict_types=1);

namespace MyProject\Tests;

use Chubbyphp\Mock\Argument\ArgumentCallback;
use Chubbyphp\Mock\Argument\ArgumentInstanceOf;
use Chubbyphp\Mock\Call;
use Chubbyphp\Mock\MockByCallsTrait;
use MyProject\Services\DateTimeService;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

class MyTest extends TestCase
{
    use MockByCallsTrait;

    public function testExecute()
    {
        /** @var DateTimeService|MockObject $dateTimeService */
        $dateTimeService = $this->getMockByCalls(DateTimeService::class, [
            Call::create('format')
                ->with(new ArgumentInstanceOf(\DateTime::class), 'c'),
                ->willReturn('2004-02-12T15:19:21+00:00')
            Call::create('format')
                ->with(
                    new ArgumentCallback(function ($dateTime) {
                        self::assertInstanceOf(\DateTime::class, $dateTime);
                    }),
                    'c'
                )
                ->willReturn('2008-05-23T08:12:55+00:00')
        ]);

        self::assertSame('2004-02-12T15:19:21+00:00' , $dateTimeService->format(new \DateTime(), 'c'));
        self::assertSame('2008-05-23T08:12:55+00:00' , $dateTimeService->format(new \DateTime(), 'c'));
    }
}

FAQ

Expectation failed for method name is anything when invoked <1...> time(s).

There is a mock with $calls given, but no method get called on the mock.

/** @var User|MockObject $user */
$user = $this->getMockByCalls(Job::class, [
    Call::create('getId')->with()->willReturn('a656cca7-7363-4ba7-875d-74bb0fd4f543'),
]);

// uncomment and test will be green
//$job->getId();

Copyright

Dominik Zogg 2018