|
1
|
|
|
<?php |
|
2
|
|
|
declare(strict_types=1); |
|
3
|
|
|
|
|
4
|
|
|
namespace Moka\Plugin\Mockery; |
|
5
|
|
|
|
|
6
|
|
|
use Mockery; |
|
7
|
|
|
use Mockery\MockInterface; |
|
8
|
|
|
use Moka\Exception\MissingDependencyException; |
|
9
|
|
|
use Moka\Strategy\AbstractMockingStrategy; |
|
10
|
|
|
use Moka\Stub\MethodStub; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class MockeryMockingStrategy |
|
14
|
|
|
* @package Moka\Strategy |
|
15
|
|
|
*/ |
|
16
|
|
|
class MockeryMockingStrategy extends AbstractMockingStrategy |
|
17
|
|
|
{ |
|
18
|
|
|
private const CLASS_NAME = Mockery::class; |
|
19
|
|
|
private const PACKAGE_NAME = 'mockery/mockery'; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* MockeryMockingStrategy constructor. |
|
23
|
|
|
* |
|
24
|
|
|
* @throws MissingDependencyException |
|
25
|
|
|
*/ |
|
26
|
2 |
|
public function __construct() |
|
27
|
|
|
{ |
|
28
|
2 |
|
self::checkDependencies(self::CLASS_NAME, self::PACKAGE_NAME); |
|
29
|
|
|
|
|
30
|
2 |
|
$this->setMockType(MockInterface::class); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @param string $fqcn |
|
35
|
|
|
* @return MockInterface |
|
36
|
|
|
*/ |
|
37
|
24 |
|
protected function doBuild(string $fqcn): MockInterface |
|
38
|
|
|
{ |
|
39
|
24 |
|
return Mockery::mock($fqcn); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* @param MockInterface $mock |
|
44
|
|
|
* @param MethodStub $stub |
|
45
|
|
|
* @return void |
|
46
|
|
|
*/ |
|
47
|
23 |
|
protected function doDecorateWithMethod($mock, MethodStub $stub): void |
|
48
|
|
|
{ |
|
49
|
23 |
|
$methodName = $stub->getName(); |
|
50
|
23 |
|
$methodValue = $stub->getValue(); |
|
51
|
|
|
|
|
52
|
|
|
/** @noinspection PhpMethodParametersCountMismatchInspection */ |
|
53
|
23 |
|
$partial = $mock->shouldReceive($methodName)->zeroOrMoreTimes(); |
|
54
|
23 |
|
$methodValue instanceof \Throwable |
|
55
|
23 |
|
? $partial->andThrow($methodValue) |
|
56
|
23 |
|
: $partial->andReturn($methodValue); |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @param MockInterface $mock |
|
61
|
|
|
* @return MockInterface |
|
62
|
|
|
*/ |
|
63
|
19 |
|
protected function doGet($mock): MockInterface |
|
64
|
|
|
{ |
|
65
|
19 |
|
return $mock; |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|