1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of phiremock-codeception-extension. |
4
|
|
|
* |
5
|
|
|
* phiremock-codeception-extension is free software: you can redistribute it and/or modify |
6
|
|
|
* it under the terms of the GNU Lesser General Public License as published by |
7
|
|
|
* the Free Software Foundation, either version 3 of the License, or |
8
|
|
|
* (at your option) any later version. |
9
|
|
|
* |
10
|
|
|
* phiremock-codeception-extension is distributed in the hope that it will be useful, |
11
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
12
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13
|
|
|
* GNU General Public License for more details. |
14
|
|
|
* |
15
|
|
|
* You should have received a copy of the GNU General Public License |
16
|
|
|
* along with phiremock-codeception-extension. If not, see <http://www.gnu.org/licenses/>. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace Codeception\Module; |
20
|
|
|
|
21
|
|
|
use Codeception\Module as CodeceptionModule; |
22
|
|
|
use Codeception\TestInterface; |
23
|
|
|
use Mcustiel\Phiremock\Client\Phiremock as PhiremockClient; |
24
|
|
|
use Mcustiel\Phiremock\Client\Utils\RequestBuilder; |
25
|
|
|
use Mcustiel\Phiremock\Domain\Expectation; |
26
|
|
|
|
27
|
|
|
class Phiremock extends CodeceptionModule |
28
|
|
|
{ |
29
|
|
|
/** |
30
|
|
|
* @var array |
31
|
|
|
*/ |
32
|
|
|
protected $config = [ |
33
|
|
|
'host' => 'localhost', |
34
|
|
|
'port' => '8086', |
35
|
|
|
'resetBeforeEachTest' => false, |
36
|
|
|
]; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @var \Mcustiel\Phiremock\Client\Phiremock |
40
|
|
|
*/ |
41
|
|
|
private $phiremock; |
42
|
|
|
|
43
|
|
|
public function _beforeSuite($settings = []) |
44
|
|
|
{ |
45
|
|
|
$this->config = array_merge($this->config, $settings); |
46
|
|
|
$this->phiremock = new PhiremockClient($this->config['host'], $this->config['port']); |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
public function _before(TestInterface $test) |
50
|
|
|
{ |
51
|
|
|
if ($this->config['resetBeforeEachTest']) { |
52
|
|
|
$this->haveACleanSetupInRemoteService(); |
53
|
|
|
} |
54
|
|
|
parent::_before($test); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function expectARequestToRemoteServiceWithAResponse(Expectation $expectation) |
58
|
|
|
{ |
59
|
|
|
$this->phiremock->createExpectation($expectation); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
public function haveACleanSetupInRemoteService() |
63
|
|
|
{ |
64
|
|
|
$this->phiremock->reset(); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
public function dontExpectRequestsInRemoteService() |
68
|
|
|
{ |
69
|
|
|
$this->phiremock->clearExpectations(); |
70
|
|
|
$this->phiremock->resetRequestsCounter(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
public function haveCleanScenariosInRemoteService() |
74
|
|
|
{ |
75
|
|
|
$this->phiremock->resetScenarios(); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
public function didNotReceiveRequestsInRemoteService() |
79
|
|
|
{ |
80
|
|
|
$this->phiremock->resetRequestsCounter(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @param int $times |
85
|
|
|
* @param RequestBuilder $builder |
86
|
|
|
* |
87
|
|
|
* @throws \Exception |
88
|
|
|
*/ |
89
|
|
|
public function seeRemoteServiceReceived($times, RequestBuilder $builder) |
90
|
|
|
{ |
91
|
|
|
$requests = $this->phiremock->countExecutions($builder); |
92
|
|
|
if ($times !== $requests) { |
93
|
|
|
throw new \Exception( |
94
|
|
|
"Request expected to be executed $times times, called $requests times instead" |
95
|
|
|
); |
96
|
|
|
} |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|