|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* This file is part of Phiremock. |
|
4
|
|
|
* |
|
5
|
|
|
* Phiremock 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 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. If not, see <http://www.gnu.org/licenses/>. |
|
17
|
|
|
*/ |
|
18
|
|
|
|
|
19
|
|
|
namespace Mcustiel\Phiremock\Server\Utils; |
|
20
|
|
|
|
|
21
|
|
|
use Exception; |
|
22
|
|
|
use Mcustiel\Phiremock\Common\Utils\ArrayToExpectationConverterLocator; |
|
23
|
|
|
use Mcustiel\Phiremock\Server\Model\ExpectationStorage; |
|
24
|
|
|
use Mcustiel\Phiremock\Server\Utils\Traits\ExpectationValidator; |
|
25
|
|
|
use Psr\Log\LoggerInterface; |
|
26
|
|
|
|
|
27
|
|
|
class FileExpectationsLoader |
|
28
|
|
|
{ |
|
29
|
|
|
use ExpectationValidator; |
|
30
|
|
|
|
|
31
|
|
|
/** @var ArrayToExpectationConverterLocator */ |
|
32
|
|
|
private $converterLocator; |
|
33
|
|
|
/** @var ExpectationStorage */ |
|
34
|
|
|
private $storage; |
|
35
|
|
|
/** @var ExpectationStorage */ |
|
36
|
|
|
private $backup; |
|
37
|
|
|
/** @var \Psr\Log\LoggerInterface */ |
|
38
|
|
|
private $logger; |
|
39
|
|
|
|
|
40
|
|
|
public function __construct( |
|
41
|
|
|
ArrayToExpectationConverterLocator $converterLocator, |
|
42
|
|
|
ExpectationStorage $storage, |
|
43
|
|
|
ExpectationStorage $backup, |
|
44
|
|
|
LoggerInterface $logger |
|
45
|
|
|
) { |
|
46
|
|
|
$this->converterLocator = $converterLocator; |
|
47
|
|
|
$this->storage = $storage; |
|
48
|
|
|
$this->backup = $backup; |
|
49
|
|
|
$this->logger = $logger; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** @throws Exception */ |
|
53
|
|
|
public function loadExpectationFromFile(string $fileName): void |
|
54
|
|
|
{ |
|
55
|
|
|
$this->logger->debug("Loading expectation file $fileName"); |
|
56
|
|
|
$content = file_get_contents($fileName); |
|
57
|
|
|
$data = @json_decode($content, true); |
|
58
|
|
|
if (\JSON_ERROR_NONE !== json_last_error()) { |
|
59
|
|
|
throw new Exception(json_last_error_msg()); |
|
60
|
|
|
} |
|
61
|
|
|
$expectation = $this->converterLocator->locate($data)->convert($data); |
|
62
|
|
|
$this->validateExpectationOrThrowException($expectation, $this->logger); |
|
63
|
|
|
|
|
64
|
|
|
$this->logger->debug('Parsed expectation: ' . var_export($expectation, true)); |
|
65
|
|
|
$this->storage->addExpectation($expectation); |
|
66
|
|
|
// As we have no API to modify expectation, parsed the same object could be used for backup. |
|
67
|
|
|
// On futher changes when $expectation modifications are possible something like deep-copy |
|
68
|
|
|
// should be used to clone expectation. |
|
69
|
|
|
$this->backup->addExpectation($expectation); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** @throws Exception */ |
|
73
|
|
|
public function loadExpectationsFromDirectory(string $directory): void |
|
74
|
|
|
{ |
|
75
|
|
|
$this->logger->info("Loading expectations from directory $directory"); |
|
76
|
|
|
$iterator = new \RecursiveDirectoryIterator( |
|
77
|
|
|
$directory, |
|
78
|
|
|
\RecursiveDirectoryIterator::FOLLOW_SYMLINKS |
|
79
|
|
|
); |
|
80
|
|
|
|
|
81
|
|
|
$iterator = new \RecursiveIteratorIterator($iterator); |
|
82
|
|
|
foreach ($iterator as $fileInfo) { |
|
83
|
|
|
if ($fileInfo->isFile()) { |
|
84
|
|
|
$filePath = $fileInfo->getRealPath(); |
|
85
|
|
|
if (preg_match('/\.json$/i', $filePath)) { |
|
86
|
|
|
$this->loadExpectationFromFile($filePath); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
} |
|
91
|
|
|
} |
|
92
|
|
|
|