|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Moka; |
|
5
|
|
|
|
|
6
|
|
|
use Moka\Exception\InvalidIdentifierException; |
|
7
|
|
|
use Moka\Exception\MockNotCreatedException; |
|
8
|
|
|
use Moka\Exception\NotImplementedException; |
|
9
|
|
|
use Moka\Exception\PluginNotRegisteredException; |
|
10
|
|
|
use Moka\Factory\ProxyBuilderFactory; |
|
11
|
|
|
use Moka\Plugin\PHPUnit\PHPUnitMockingStrategy; |
|
12
|
|
|
use Moka\Plugin\PluginHelper; |
|
13
|
|
|
use Moka\Plugin\PluginInterface; |
|
14
|
|
|
use Moka\Proxy\Proxy; |
|
15
|
|
|
use Moka\Strategy\MockingStrategyInterface; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Class Moka |
|
19
|
|
|
* @package Moka |
|
20
|
|
|
* |
|
21
|
|
|
* @method static Proxy mockery(string $fqcn, string $alias = null) |
|
22
|
|
|
* @method static Proxy phake(string $fqcn, string $alias = null) |
|
23
|
|
|
* @method static Proxy phpunit(string $fqcn, string $alias = null) |
|
24
|
|
|
* @method static Proxy prophecy(string $fqcn, string $alias = null) |
|
25
|
|
|
*/ |
|
26
|
|
|
class Moka |
|
27
|
|
|
{ |
|
28
|
|
|
/** |
|
29
|
|
|
* @var array|MockingStrategyInterface[] |
|
30
|
|
|
*/ |
|
31
|
|
|
private static $mockingStrategies = []; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param string $name |
|
35
|
|
|
* @param array $arguments |
|
36
|
|
|
* @return Proxy |
|
37
|
|
|
* |
|
38
|
|
|
* @throws NotImplementedException |
|
39
|
|
|
* @throws InvalidIdentifierException |
|
40
|
|
|
* @throws MockNotCreatedException |
|
41
|
|
|
* @throws PluginNotRegisteredException |
|
42
|
|
|
*/ |
|
43
|
2 |
|
public static function __callStatic($name, $arguments) |
|
44
|
|
|
{ |
|
45
|
2 |
|
if (!isset(self::$mockingStrategies[$name])) { |
|
46
|
|
|
/** @var PluginInterface $pluginFQCN */ |
|
47
|
2 |
|
$pluginFQCN = PluginHelper::load($name); |
|
48
|
|
|
|
|
49
|
1 |
|
self::$mockingStrategies[$name] = $pluginFQCN::getStrategy(); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
1 |
|
$mockFQCN = $arguments[0]; |
|
53
|
1 |
|
$alias = $arguments[1] ?? null; |
|
54
|
1 |
|
$mockingStrategy = self::$mockingStrategies[$name]; |
|
55
|
|
|
|
|
56
|
1 |
|
return static::brew($mockFQCN, $alias, $mockingStrategy); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param string $fqcn |
|
61
|
|
|
* @param string|null $alias |
|
62
|
|
|
* @param MockingStrategyInterface|null $mockingStrategy |
|
63
|
|
|
* @return Proxy |
|
64
|
|
|
* |
|
65
|
|
|
* @throws MockNotCreatedException |
|
66
|
|
|
* @throws InvalidIdentifierException |
|
67
|
|
|
*/ |
|
68
|
3 |
|
public static function brew(string $fqcn, string $alias = null, MockingStrategyInterface $mockingStrategy = null): Proxy |
|
69
|
|
|
{ |
|
70
|
3 |
|
if (!$mockingStrategy instanceof MockingStrategyInterface) { |
|
71
|
1 |
|
$mockingStrategy = new PHPUnitMockingStrategy(); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
3 |
|
return ProxyBuilderFactory::get($mockingStrategy)->getProxy($fqcn, $alias); |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @return void |
|
79
|
|
|
*/ |
|
80
|
1 |
|
public static function clean() |
|
81
|
|
|
{ |
|
82
|
1 |
|
ProxyBuilderFactory::reset(); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|