File: MockDelegateFunctionTest.php

package info (click to toggle)
php-mock-integration 3.0.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 152 kB
  • sloc: php: 183; makefile: 18; sh: 7; xml: 7
file content (86 lines) | stat: -rw-r--r-- 2,406 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
<?php

namespace phpmock\integration;

use PHPUnit\Framework\TestCase;

/**
 * Tests MockDelegateFunction.
 *
 * @author Markus Malkusch <markus@malkusch.de>
 * @link bitcoin:1335STSwu9hST4vcMRppEPgENMHD2r1REK Donations
 * @license http://www.wtfpl.net/txt/copying/ WTFPL
 * @see MockDelegateFunction
 */
class MockDelegateFunctionTest extends TestCase
{
    use TestCaseTrait;
    
    /**
     * @var string The class name of a generated class.
     */
    private $className;
    
    protected function setUpCompat()
    {
        parent::setUp();
        
        $builder = new MockDelegateFunctionBuilder();
        $builder->build();
        $this->className = $builder->getFullyQualifiedClassName();
    }

    /**
     * Tests delegate() returns the mock's result.
     *
     * @test
     */
    #[\PHPUnit\Framework\Attributes\Test]
    public function testDelegateReturnsMockResult()
    {
        $expected    = 3;
        $mockBuilder = $this->getMockBuilder($this->className);

        // `setMethods` is gone from phpunit 10, alternative is `onlyMethods`
        if (method_exists($mockBuilder, 'onlyMethods')) {
            $mockBuilder->onlyMethods([MockDelegateFunctionBuilder::METHOD]);
        } else {
            $mockBuilder->setMethods([MockDelegateFunctionBuilder::METHOD]);
        }

        $mock = $mockBuilder->getMock();

        $mock->expects($this->once())
             ->method(MockDelegateFunctionBuilder::METHOD)
             ->willReturn($expected);
        
        $result = call_user_func($mock->getCallable());
        $this->assertEquals($expected, $result);
    }

    /**
     * Tests delegate() forwards the arguments.
     *
     * @test
     */
    #[\PHPUnit\Framework\Attributes\Test]
    public function testDelegateForwardsArguments()
    {
        $mockBuilder = $this->getMockBuilder($this->className);

        // `setMethods` is gone from phpunit 10, alternative is `onlyMethods`
        if (method_exists($mockBuilder, 'onlyMethods')) {
            $mockBuilder->onlyMethods([MockDelegateFunctionBuilder::METHOD]);
        } else {
            $mockBuilder->setMethods([MockDelegateFunctionBuilder::METHOD]);
        }

        $mock = $mockBuilder->getMock();

        $mock->expects($this->once())
             ->method(MockDelegateFunctionBuilder::METHOD)
             ->with(1, 2);

        call_user_func($mock->getCallable(), 1, 2);
    }
}