InstanceMockPass::apply()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 8
ccs 5
cts 5
cp 1
rs 9.4285
cc 2
eloc 4
nc 2
nop 2
crap 2
1
<?php
2
/**
3
 * Mockery
4
 *
5
 * LICENSE
6
 *
7
 * This source file is subject to the new BSD license that is bundled
8
 * with this package in the file LICENSE.txt.
9
 * It is also available through the world-wide-web at this URL:
10
 * http://github.com/padraic/mockery/blob/master/LICENSE
11
 * If you did not receive a copy of the license and are unable to
12
 * obtain it through the world-wide-web, please send an email
13
 * to [email protected] so we can send you a copy immediately.
14
 *
15
 * @category   Mockery
16
 * @package    Mockery
17
 * @copyright  Copyright (c) 2010 Pádraic Brady (http://blog.astrumfutura.com)
18
 * @license    http://github.com/padraic/mockery/blob/master/LICENSE New BSD License
19
 */
20
21
namespace Mockery\Generator\StringManipulation\Pass;
22
23
use Mockery\Generator\MockConfiguration;
24
25
class InstanceMockPass
26
{
27
    const INSTANCE_MOCK_CODE = <<<MOCK
28
29
    protected \$_mockery_ignoreVerification = true;
30
31
    public function __construct()
32
    {
33
        \$this->_mockery_ignoreVerification = false;
34
        \$associatedRealObject = \Mockery::fetchMock(__CLASS__);
35
36
        foreach (get_object_vars(\$this) as \$attr => \$val) {
37
            if (\$attr !== "_mockery_ignoreVerification" && \$attr !== "_mockery_expectations") {
38
                \$this->\$attr = \$associatedRealObject->\$attr;
39
            }
40
        }
41
42
        \$directors = \$associatedRealObject->mockery_getExpectations();
43
        foreach (\$directors as \$method=>\$director) {
44
            // get the director method needed
45
            \$existingDirector = \$this->mockery_getExpectationsFor(\$method);
46
            if (!\$existingDirector) {
47
                \$existingDirector = new \Mockery\ExpectationDirector(\$method, \$this);
48
                \$this->mockery_setExpectationsFor(\$method, \$existingDirector);
49
            }
50
            \$expectations = \$director->getExpectations();
51
            foreach (\$expectations as \$expectation) {
52
                \$clonedExpectation = clone \$expectation;
53
                \$existingDirector->addExpectation(\$clonedExpectation);
54
            }
55
            \$defaultExpectations = \$director->getDefaultExpectations();
56
            foreach (array_reverse(\$defaultExpectations) as \$expectation) {
57
                \$clonedExpectation = clone \$expectation;
58
                \$existingDirector->addExpectation(\$clonedExpectation);
59
                \$existingDirector->makeExpectationDefault(\$clonedExpectation);
60
            }
61
        }
62
        \Mockery::getContainer()->rememberMock(\$this);
63
    }
64
MOCK;
65
66 398
    public function apply($code, MockConfiguration $config)
67
    {
68 398
        if ($config->isInstanceMock()) {
69 12
            $code = $this->appendToClass($code, static::INSTANCE_MOCK_CODE);
70 12
        }
71
72 398
        return $code;
73
    }
74
75 12
    protected function appendToClass($class, $code)
76
    {
77 12
        $lastBrace = strrpos($class, "}");
78 12
        $class = substr($class, 0, $lastBrace) . $code . "\n    }\n";
79 12
        return $class;
80
    }
81
}
82