File: PHPMockTest.php

package info (click to toggle)
php-mock-phpunit 2.13.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 248 kB
  • sloc: php: 1,066; makefile: 20; xml: 7; sh: 7
file content (126 lines) | stat: -rw-r--r-- 3,029 bytes parent folder | download
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
<?php

namespace phpmock\phpunit;

use phpmock\AbstractMockTestCase;
use phpmock\Deactivatable;
use PHPUnit\Framework\ExpectationFailedException;

/**
 * Tests PHPMock.
 *
 * @author Markus Malkusch <markus@malkusch.de>
 * @link bitcoin:1335STSwu9hST4vcMRppEPgENMHD2r1REK Donations
 * @license http://www.wtfpl.net/txt/copying/ WTFPL
 * @see PHPMock
 */
class PHPMockTest extends AbstractMockTestCase
{

    use PHPMock;

    protected function defineFunction($namespace, $functionName)
    {
        self::defineFunctionMock($namespace, $functionName);
    }

    protected function mockFunction($namespace, $functionName, callable $function)
    {
        $mock = $this->getFunctionMock($namespace, $functionName);
        $mock->expects($this->any())->willReturnCallback($function);
    }

    protected function disableMocks()
    {
    }

    /**
     * Tests building a mock with arguments.
     *
     * @test
     */
    public function testFunctionMockWithArguments()
    {
        $time = $this->getFunctionMock(__NAMESPACE__, "sqrt");
        $time->expects($this->once())->with(9)->willReturn(2);

        $this->assertEquals(2, sqrt(9));
    }

    /**
     * Tests failing an expectation.
     *
     * @test
     */
    public function testFunctionMockFailsExpectation()
    {
        try {
            $time = $this->getFunctionMock(__NAMESPACE__, "time");
            $time->expects($this->once());

            $time->__phpunit_verify();
            $this->fail("Expectation should fail");
        } catch (ExpectationFailedException $e) {
            time(); // satisfy the expectation
        }
    }

    /**
     * Register a Deactivatable for a tear down.
     *
     * @test
     */
    public function testRegisterForTearDownRegistered()
    {
        $obj = new \stdClass();
        $obj->count = 0;

        $class = new class ($obj) implements Deactivatable
        {
            private $obj;

            public function __construct($obj)
            {
                $this->obj = $obj;
            }

            public function disable()
            {
                ++$this->obj->count;
            }
        };
        $this->registerForTearDown($class);

        self::assertSame(0, $obj->count);

        return $obj;
    }

    /**
     * Check the Deactivatable was executed on a tear down of dependent test.
     *
     * @test
     *
     * @depends testRegisterForTearDownRegistered
     */
    #[\PHPUnit\Framework\Attributes\Depends('testRegisterForTearDownRegistered')]
    public function testRegisterForTearDownExecuted($obj)
    {
        self::assertSame(1, $obj->count);

        return $obj;
    }

    /**
     * Check the Deactivatable was unregistered after executing, so it is not executed again.
     *
     * @test
     *
     * @depends testRegisterForTearDownExecuted
     */
    #[\PHPUnit\Framework\Attributes\Depends('testRegisterForTearDownExecuted')]
    public function testRegisterForTearDownRemoved($obj)
    {
        self::assertSame(1, $obj->count);
    }
}