|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Moka\Strategy; |
|
5
|
|
|
|
|
6
|
|
|
use Mockery\MockInterface; |
|
7
|
|
|
use Moka\Exception\InvalidArgumentException; |
|
8
|
|
|
use Moka\Exception\MockNotCreatedException; |
|
9
|
|
|
use Moka\Stub\Stub; |
|
10
|
|
|
use Moka\Stub\StubSet; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class MockeryMockingStrategy |
|
14
|
|
|
* @package Moka\Strategy |
|
15
|
|
|
*/ |
|
16
|
|
|
class MockeryMockingStrategy extends AbstractMockingStrategy |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* MockeryMockingStrategy constructor. |
|
20
|
|
|
*/ |
|
21
|
1 |
|
public function __construct() |
|
22
|
|
|
{ |
|
23
|
1 |
|
$this->setMockType(MockInterface::class); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param string $fqcn |
|
28
|
|
|
* @return MockInterface |
|
29
|
|
|
* |
|
30
|
|
|
* @throws MockNotCreatedException |
|
31
|
|
|
*/ |
|
32
|
5 |
|
public function build(string $fqcn) |
|
33
|
|
|
{ |
|
34
|
|
|
try { |
|
35
|
5 |
|
return \Mockery::mock($fqcn)->shouldIgnoreMissing(); |
|
36
|
2 |
|
} catch (\Throwable $exception) { |
|
37
|
2 |
|
throw new MockNotCreatedException( |
|
38
|
2 |
|
sprintf( |
|
39
|
2 |
|
'Unable to create mock object for FQCN %s: %s', |
|
40
|
2 |
|
$fqcn, |
|
41
|
2 |
|
$exception->getMessage() |
|
42
|
|
|
) |
|
43
|
|
|
); |
|
44
|
|
|
} |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @param MockInterface $mock |
|
49
|
|
|
* @param StubSet $stubs |
|
50
|
|
|
* @return void |
|
51
|
|
|
* |
|
52
|
|
|
* @throws InvalidArgumentException |
|
53
|
|
|
*/ |
|
54
|
2 |
View Code Duplication |
public function decorate($mock, StubSet $stubs) |
|
55
|
|
|
{ |
|
56
|
2 |
|
$this->checkMockType($mock); |
|
57
|
|
|
|
|
58
|
|
|
/** @var Stub $stub */ |
|
59
|
1 |
|
foreach ($stubs as $stub) { |
|
60
|
1 |
|
$methodValue = $stub->getMethodValue(); |
|
61
|
|
|
|
|
62
|
1 |
|
$partial = $mock->shouldReceive($stub->getMethodName())->zeroOrMoreTimes(); |
|
63
|
1 |
|
$methodValue instanceof \Throwable |
|
64
|
1 |
|
? $partial->andThrow($methodValue) |
|
65
|
1 |
|
: $partial->andReturn($methodValue); |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* @param MockInterface $mock |
|
71
|
|
|
* @return MockInterface |
|
72
|
|
|
* |
|
73
|
|
|
* @throws InvalidArgumentException |
|
74
|
|
|
*/ |
|
75
|
3 |
|
public function get($mock) |
|
76
|
|
|
{ |
|
77
|
3 |
|
$this->checkMockType($mock); |
|
78
|
|
|
|
|
79
|
2 |
|
return $mock; |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|