1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Moka\Generator; |
5
|
|
|
|
6
|
|
|
use Moka\Exception\InvalidArgumentException; |
7
|
|
|
use Moka\Exception\MockNotCreatedException; |
8
|
|
|
use Moka\Generator\Template\ClassTemplate; |
9
|
|
|
use Moka\Proxy\ProxyInterface; |
10
|
|
|
use Moka\Proxy\ProxyTrait; |
11
|
|
|
use Moka\Strategy\MockingStrategyInterface; |
12
|
|
|
|
13
|
|
|
/** |
14
|
|
|
* Class ProxyGenerator |
15
|
|
|
* @package Moka\Generator |
16
|
|
|
*/ |
17
|
|
|
class ProxyGenerator |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* @var MockingStrategyInterface |
21
|
|
|
*/ |
22
|
|
|
private $mockingStrategy; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* ProxyGenerator constructor. |
26
|
|
|
* @param MockingStrategyInterface $mockingStrategy |
27
|
|
|
*/ |
28
|
4 |
|
public function __construct(MockingStrategyInterface $mockingStrategy) |
29
|
|
|
{ |
30
|
4 |
|
$this->mockingStrategy = $mockingStrategy; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param string $fqcn |
35
|
|
|
* @return ProxyInterface |
36
|
|
|
* |
37
|
|
|
* @throws MockNotCreatedException |
38
|
|
|
* @throws InvalidArgumentException |
39
|
|
|
* @throws \ReflectionException |
40
|
|
|
*/ |
41
|
10 |
|
public function get(string $fqcn): ProxyInterface |
42
|
|
|
{ |
43
|
10 |
|
$mock = $this->mockingStrategy->build($fqcn); |
44
|
10 |
|
$mockFQCN = \get_class($this->mockingStrategy->get($mock)); |
45
|
10 |
|
$mockClass = new \ReflectionClass($mockFQCN); |
46
|
|
|
|
47
|
10 |
|
$proxyCode = ClassTemplate::generate($mockClass); |
48
|
10 |
|
$proxyFQCN = eval($proxyCode); |
49
|
|
|
|
50
|
10 |
|
return $this->getInstance($proxyFQCN) |
51
|
10 |
|
->__moka_setMock($mock) |
52
|
10 |
|
->__moka_setMockingStrategy($this->mockingStrategy); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param string $proxyFQCN |
57
|
|
|
* @return ProxyInterface|ProxyTrait |
58
|
|
|
* @throws \ReflectionException |
59
|
|
|
*/ |
60
|
10 |
|
protected function getInstance(string $proxyFQCN): ProxyInterface |
61
|
|
|
{ |
62
|
10 |
|
$proxyClass = new \ReflectionClass($proxyFQCN); |
63
|
|
|
|
64
|
|
|
/** @var ProxyInterface|ProxyTrait $proxy */ |
65
|
10 |
|
$proxy = $proxyClass->newInstance(); |
66
|
|
|
|
67
|
10 |
|
return $proxy; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|