File: SpyTest.php

package info (click to toggle)
php-mock 2.5.0-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 332 kB
  • sloc: php: 1,703; makefile: 18; xml: 17; sh: 7
file content (170 lines) | stat: -rw-r--r-- 4,988 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php

namespace phpmock\spy;

use phpmock\Mock;
use phpmock\AbstractMockTestCase;

/**
 * Tests the Spy.
 *
 * @author Markus Malkusch <markus@malkusch.de>
 * @link bitcoin:1335STSwu9hST4vcMRppEPgENMHD2r1REK Donations
 * @license http://www.wtfpl.net/txt/copying/ WTFPL
 * @see Spy
 */
class SpyTest extends AbstractMockTestCase
{
    protected function defineFunction($namespace, $functionName)
    {
        $mock = new Spy($namespace, $functionName, function () {
        });
        $mock->define();
    }

    protected function mockFunction($namespace, $functionName, callable $function)
    {
        $mock = new Spy($namespace, $functionName, $function);
        $mock->enable();
    }

    protected function disableMocks()
    {
        Mock::disableAll();
    }

    /**
     * Tests spying.
     *
     * @param array    $expected
     * @param string   $name
     * @param callable $invocations
     * @dataProvider provideTestGetInvocations
     */
    public function testGetInvocations(array $expected, $name, callable $invocations)
    {
        $spy = new Spy(__NAMESPACE__, $name);
        $spy->enable();
        call_user_func($invocations);
        $this->assertEquals($expected, $spy->getInvocations());
    }

    /**
     * Returns test cases for testGetInvocations().
     *
     * @return array Test cases for testGetInvocations.
     */
    public static function provideTestGetInvocations()
    {
        eval("function testGetInvocations_noParameters() { return 123; }");
        eval("function testGetInvocations_oneParameter(\$a) { return \$a + 1; }");
        eval("function testGetInvocations_twoParameters(\$a, \$b) { return \$a + \$b; }");
        eval("function testGetInvocations_optionalParameter(\$a = null) { return \$a; }");

        return [
            [
                [],
                "testGetInvocations_noParameters",
                function () {
                }
            ],
            [
                [new Invocation([], 123)],
                "testGetInvocations_noParameters",
                function () {
                    testGetInvocations_noParameters();
                }
            ],
            [
                [
                    new Invocation([], 123),
                    new Invocation([], 123),
                ],
                "testGetInvocations_noParameters",
                function () {
                    testGetInvocations_noParameters();
                    testGetInvocations_noParameters();
                }
            ],
            [
                [new Invocation([1], 2)],
                "testGetInvocations_oneParameter",
                function () {
                    testGetInvocations_oneParameter(1);
                }
            ],
            [
                [new Invocation([1, 2], 3)],
                "testGetInvocations_twoParameters",
                function () {
                    testGetInvocations_twoParameters(1, 2);
                }
            ],
            [
                [new Invocation([], null)],
                "testGetInvocations_optionalParameter",
                function () {
                    testGetInvocations_optionalParameter();
                }
            ],
            [
                [new Invocation([123], 123)],
                "testGetInvocations_optionalParameter",
                function () {
                    testGetInvocations_optionalParameter(123);
                }
            ],
        ];
    }

    /**
     * Tests the default function.
     */
    public function testDefaultFunction()
    {
        eval("function testDefaultFunction() { return 123; }");
        $spy = new Spy(__NAMESPACE__, "testDefaultFunction");
        $spy->enable();

        $result = testDefaultFunction();
        $this->assertEquals(123, $result);
    }

    /**
     * An exception should still be recorded.
     */
    public function testException()
    {
        eval('function testException($foo) { throw new \Exception(); }');
        $spy = new Spy(__NAMESPACE__, "testException");
        $spy->enable();

        try {
            testException("foo");
            $this->fail("Expected exception");
        } catch (\Exception $e) {
            $invocation = $spy->getInvocations()[0];

            $this->assertEquals(["foo"], $invocation->getArguments());
            $this->assertNull($invocation->getReturn());
            $this->assertTrue($invocation->isExceptionThrown());
            $this->assertEquals($e, $invocation->getException());
        }
    }

    /**
     * Test the invocation of a none exception call.
     */
    public function testNoException()
    {
        eval("function testNoException() { }");
        $spy = new Spy(__NAMESPACE__, "testNoException");
        $spy->enable();

        testNoException();

        $invocation = $spy->getInvocations()[0];
        $this->assertFalse($invocation->isExceptionThrown());
        $this->assertNull($invocation->getException());
    }
}