Test Failed
Branch scrutinizer_new_php_analysis (b739aa)
by Donald
02:21
created

Phake::strictMock()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
c 0
b 0
f 0
rs 10
cc 1
eloc 1
nc 1
nop 1
1
<?php namespace Chekote\Phake;
2
3
use Chekote\Phake\Proxies\VisibilityProxy;
4
use Chekote\Phake\Stubber\Answers\UnMockedResponseExceptionAnswer;
5
use Phake as BasePhake;
6
use Phake_CallRecorder_Recorder;
7
use Phake_ClassGenerator_MockClass;
8
use Phake_IMock;
9
use Phake_Stubber_AnswerCollection;
10
11
/**
12
 * Extends Phake to add strict mock functionality.
13
 *
14
 * A strict mock will throw an Exception stating the class and method name of any method that is called without
15
 * its response being mocked. This is to assist in strict London style TDD, whereby only explicitly mocked out
16
 * methods are allowed to be invoked.
17
 */
18
abstract class Phake extends BasePhake
19
{
20
    /**
21
     * Increases allows calling private and protected instance methods on the given mock.
22
     *
23
     * @param  Phake_IMock     $mock
24
     * @return VisibilityProxy $mock
25
     */
26
    public static function makeVisible(Phake_IMock $mock)
27
    {
28
        return new VisibilityProxy($mock);
29
    }
30
31
    /**
32
     * Creates a strict mock.
33
     *
34
     * @param  string $className the name of the class to mock.
35
     * @return mixed  the mocked class instance.
36
     */
37
    public static function strictMock(string $className)
38
    {
39
        return self::mock($className, new Phake_Stubber_AnswerCollection(new UnMockedResponseExceptionAnswer()));
40
    }
41
42
    /**
43
     * Creates a strict mock and calls the real classes constructor.
44
     *
45
     * This method creates a mock that is somewhere between what Phake::mock and Phake::partialMock would create. The
46
     * returned mock behaves exactly the same as what Phake::mock would return, except that it calls the constructor
47
     * of the mocked class (as Phake::partialMock does). However, the returned mock will NOT thenCallParent() for every
48
     * mocked method in the way that a partial mock would.
49
     *
50
     * @param  string $className the name of the class to mock.
51
     * @param  array  ...$args   arguments for the classes constructor.
52
     * @return mixed  the mocked class instance.
53
     */
54
    public static function strictMockWithConstructor(string $className, ...$args)
55
    {
56
        return self::getPhake()->mock(
57
            $className,
58
            new Phake_ClassGenerator_MockClass(self::getMockLoader()),
59
            new Phake_CallRecorder_Recorder(),
60
            new UnMockedResponseExceptionAnswer(),
61
            $args
62
        );
63
    }
64
}
65