Passed
Pull Request — master (#3)
by Mariano
01:39
created

Phiremock::__construct()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
nop 2
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * This file is part of phiremock-codeception-module.
4
 *
5
 * phiremock-codeception-module 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-module 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-module.  If not, see <http://www.gnu.org/licenses/>.
17
 */
18
19
namespace Codeception\Module;
20
21
use Codeception\Exception\ConfigurationException;
22
use Codeception\Lib\ModuleContainer;
23
use Codeception\Module as CodeceptionModule;
24
use Codeception\TestInterface;
25
use Exception;
26
use InvalidArgumentException;
27
use Mcustiel\Phiremock\Client\Connection\Scheme;
28
use Mcustiel\Phiremock\Client\Utils\ConditionsBuilder;
29
use Mcustiel\Phiremock\Codeception\Util\Config;
30
use Mcustiel\Phiremock\Codeception\Util\ExpectationAnnotationParser;
31
use Mcustiel\Phiremock\Domain\Expectation;
32
use Psr\Http\Client\ClientExceptionInterface;
33
34
class Phiremock extends CodeceptionModule
35
{
36
    /** @var array */
37
    protected $config = Config::DEFAULT_CONFIG;
38
39
    /** @var \Mcustiel\Phiremock\Client\Phiremock */
40
    private $phiremock;
41
42
    /** @var ExpectationAnnotationParser */
43
    private $expectationsParser;
44
45
    /** @var Config */
46
    private $moduleConfig;
47
48
    /** @var Phiremock[] */
49
    private $extraConnections = [];
50
51
    public function __construct(ModuleContainer $moduleContainer, $config = null)
52
    {
53
        parent::__construct($moduleContainer, $config);
54
        $this->moduleConfig = new Config($this->config, $this->createDebugMethod());
55
        foreach ($this->moduleConfig->getExtraConnectionsConfigs() as $name => $connectionConfig) {
56
            $this->extraConnections[$name] = new self($this->moduleContainer, $connectionConfig->asArray());
57
        }
58
    }
59
60
    /**
61
     * @throws ConfigurationException
62
     * @throws Exception
63
     */
64
    public function _beforeSuite($settings = [])
65
    {
66
        $this->config = array_merge($this->moduleConfig->asArray(), $settings);
67
        $this->moduleConfig = new Config($this->config, $this->createDebugMethod());
68
69
        $this->phiremock = $this->moduleConfig->getClientFactory()->createPhiremockClient(
70
            $this->moduleConfig->getHost(),
71
            $this->moduleConfig->getPort(),
72
            new Scheme($this->moduleConfig->isSecure() ? Scheme::HTTPS: Scheme::HTTP)
73
        );
74
        $this->expectationsParser = new ExpectationAnnotationParser(
75
            $this->moduleConfig->getExpectationsPath()
76
        );
77
        foreach ($this->extraConnections as $module) {
78
            $module->_beforeSuite($settings);
79
        }
80
    }
81
82
    /** @throws ClientExceptionInterface */
83
    public function _before(TestInterface $test)
84
    {
85
        if ($this->moduleConfig->isResetBeforeEachTest()) {
86
            $this->haveACleanSetupInRemoteService();
87
        }
88
        $expectations = $this->expectationsParser->getExpectations($test);
89
        if (!empty($expectations)) {
90
            foreach ($expectations as $expectation) {
91
                $this->phiremock->createExpectationFromJson(
92
                    file_get_contents($expectation)
93
                );
94
            }
95
        }
96
        parent::_before($test);
97
    }
98
99
    public function takeConnection(string $name): Phiremock
100
    {
101
        if ($name === 'default') {
102
            return $this;
103
        }
104
        if (!isset($this->extraConnections[$name])) {
105
            throw new InvalidArgumentException(
106
                sprintf('Connection %s does not exist', $name)
107
            );
108
        }
109
        return $this->extraConnections[$name];
110
    }
111
112
    /** @throws ClientExceptionInterface */
113
    public function expectARequestToRemoteServiceWithAResponse(Expectation $expectation): void
114
    {
115
        $this->phiremock->createExpectation($expectation);
116
    }
117
118
    /** @throws ClientExceptionInterface */
119
    public function haveACleanSetupInRemoteService(): void
120
    {
121
        $this->phiremock->reset();
122
    }
123
124
    /** @throws ClientExceptionInterface */
125
    public function dontExpectRequestsInRemoteService(): void
126
    {
127
        $this->phiremock->clearExpectations();
128
        $this->phiremock->resetRequestsCounter();
129
    }
130
131
    /** @throws ClientExceptionInterface */
132
    public function haveCleanScenariosInRemoteService(): void
133
    {
134
        $this->phiremock->resetScenarios();
135
    }
136
137
    /** @deprecated Name is confusing, sounds like an assertion */
138
    public function didNotReceiveRequestsInRemoteService(): void
139
    {
140
        $this->dontHaveLoggedRequestsToRemoteService();
141
    }
142
143
    /** @throws ClientExceptionInterface */
144
    public function dontHaveLoggedRequestsToRemoteService(): void
145
    {
146
        $this->phiremock->resetRequestsCounter();
147
    }
148
149
    /**
150
     * @throws ClientExceptionInterface
151
     * @throws Exception
152
     */
153
    public function seeRemoteServiceReceived(int $times, ConditionsBuilder $builder): void
154
    {
155
        $requests = $this->phiremock->countExecutions($builder);
156
        if ($times !== $requests) {
157
            throw new Exception(
158
                "Request expected to be executed $times times, called $requests times instead"
159
            );
160
        }
161
    }
162
163
    /** @throws ClientExceptionInterface */
164
    public function grabRequestsMadeToRemoteService(ConditionsBuilder $builder): array
165
    {
166
        return $this->phiremock->listExecutions($builder);
167
    }
168
169
    /** @throws ClientExceptionInterface */
170
    public function setScenarioState(string $name, string $state): void
171
    {
172
        $this->phiremock->setScenarioState($name, $state);
173
    }
174
175
    private function createDebugMethod(): callable
176
    {
177
        return function (string $msg) : void {
178
            $this->debug($msg);
179
        };
180
    }
181
}
182