File: TestCase.php

package info (click to toggle)
php-react-promise 3.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 832 kB
  • sloc: php: 3,457; makefile: 22
file content (53 lines) | stat: -rw-r--r-- 1,458 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<?php

namespace React\Promise;

use PHPUnit\Framework\MockObject\MockBuilder;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase as BaseTestCase;

class TestCase extends BaseTestCase
{
    public function expectCallableExactly(int $amount): callable
    {
        $mock = $this->createCallableMock();
        $mock->expects(self::exactly($amount))->method('__invoke');
        assert(is_callable($mock));

        return $mock;
    }

    public function expectCallableOnce(): callable
    {
        $mock = $this->createCallableMock();
        $mock->expects(self::once())->method('__invoke');
        assert(is_callable($mock));

        return $mock;
    }

    public function expectCallableNever(): callable
    {
        $mock = $this->createCallableMock();
        $mock->expects(self::never())->method('__invoke');
        assert(is_callable($mock));

        return $mock;
    }

    /** @return MockObject&callable */
    protected function createCallableMock(): MockObject
    {
        $builder = $this->getMockBuilder(\stdClass::class);
        if (method_exists($builder, 'addMethods')) {
            // PHPUnit 9+
            $mock = $builder->addMethods(['__invoke'])->getMock();
        } else {
            // legacy PHPUnit 4 - PHPUnit 9
            $mock = $builder->setMethods(['__invoke'])->getMock();
        }
        assert($mock instanceof MockObject && is_callable($mock));

        return $mock;
    }
}