Completed
Pull Request — master (#17)
by Angelo
05:07
created

AbstractMockingStrategy::doBuild()

Size

Total Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 1
ccs 0
cts 0
cp 0
c 0
b 0
f 0
nc 1
1
<?php
2
declare(strict_types=1);
3
4
namespace Moka\Strategy;
5
6
use Moka\Exception\InvalidArgumentException;
7
use Moka\Exception\MissingDependencyException;
8
use Moka\Exception\MockNotCreatedException;
9
use Moka\Exception\NotImplementedException;
10
use Moka\Stub\Stub;
11
use Moka\Stub\StubSet;
12
13
/**
14
 * Class AbstractMockingStrategy
15
 * @package Moka\Strategy
16
 */
17
abstract class AbstractMockingStrategy implements MockingStrategyInterface
18
{
19
    /**
20
     * @var string
21
     */
22
    private $mockType;
23
24
    /**
25
     * @param string $dependencyClassName
26
     * @param string $dependencyPackageName
27
     *
28
     * @throws MissingDependencyException
29
     */
30 6
    final protected static function checkDependencies(string $dependencyClassName, string $dependencyPackageName)
31
    {
32 6
        if (!class_exists($dependencyClassName)) {
33 1
            throw new MissingDependencyException(
34 1
                sprintf(
35 1
                    'Class "%s" does not exist, please install package "%s"',
36 1
                    $dependencyClassName,
37 1
                    $dependencyPackageName
38
                )
39
            );
40
        }
41
    }
42
43
    /**
44
     * @param string $fqcn
45
     * @return object
46
     *
47
     * @throws MockNotCreatedException
48
     */
49 89
    public function build(string $fqcn)
50
    {
51
        try {
52 89
            return $this->doBuild($fqcn);
53 11
        } catch (\Throwable $exception) {
54 11
            throw new MockNotCreatedException(
55 11
                sprintf(
56 11
                    'Cannot create mock object for FQCN "%s": %s',
57 11
                    $fqcn,
58 11
                    $exception->getMessage()
59
                )
60
            );
61
        }
62
    }
63
64
    /**
65
     * @param object $mock
66
     * @param StubSet $stubs
67
     * @return void
68
     *
69
     * @throws NotImplementedException
70
     * @throws InvalidArgumentException
71
     */
72 89
    public function decorate($mock, StubSet $stubs)
73
    {
74 89
        $this->checkMockType($mock);
75
76
        /** @var Stub $stub */
77 89
        foreach ($stubs as $stub) {
78 89
            $this->doDecorate($mock, $stub);
79
        }
80
    }
81
82
    /**
83
     * @param object $mock
84
     * @return object
85
     *
86
     * @throws NotImplementedException
87
     * @throws InvalidArgumentException
88
     */
89 50
    public function get($mock)
90
    {
91 50
        $this->checkMockType($mock);
92
93 46
        return $this->doGet($mock);
94
    }
95
96
    /**
97
     * @return string
98
     *
99
     * @throws NotImplementedException
100
     */
101 23
    public function getMockType(): string
102
    {
103 23
        $this->verifyMockType();
104
105 22
        return $this->mockType;
106
    }
107
108
    /**
109
     * @param string $fqcn
110
     */
111 5
    final protected function setMockType(string $fqcn)
112
    {
113 5
        $this->mockType = $fqcn;
114
    }
115
116
    /**
117
     * @param object $mock
118
     *
119
     * @throws NotImplementedException
120
     * @throws InvalidArgumentException
121
     */
122 89
    final protected function checkMockType($mock)
123
    {
124 89
        $this->verifyMockType();
125
126 89
        if (!is_a($mock, $this->mockType)) {
127 8
            throw new InvalidArgumentException(
128 8
                sprintf(
129 8
                    'Mock object must be of type "%s", "%s" given',
130 8
                    $this->mockType,
131 8
                    gettype($mock)
132
                )
133
            );
134
        }
135
    }
136
137
    /**
138
     * @param string $fqcn
139
     * @return object
140
     */
141
    abstract protected function doBuild(string $fqcn);
142
143
    /**
144
     * @param object $mock
145
     * @param Stub $stub
146
     * @return void
147
     */
148
    abstract protected function doDecorate($mock, Stub $stub);
149
150
    /**
151
     * @param object $mock
152
     * @return object
153
     */
154
    abstract protected function doGet($mock);
155
156
    /**
157
     * @return void
158
     *
159
     * @throws NotImplementedException
160
     */
161 90
    private function verifyMockType()
162
    {
163 90
        if (!$this->mockType) {
164 1
            throw new NotImplementedException('Mock type was not defined');
165
        }
166
    }
167
}
168