1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace AHJ\ApprovalTests; |
6
|
|
|
|
7
|
|
|
use PHPUnit\Framework\Assert; |
8
|
|
|
|
9
|
|
|
final class Approvals |
10
|
|
|
{ |
11
|
|
|
private FileHandler $fileHandler; |
12
|
|
|
|
13
|
|
|
private FilePathResolver $filePathResolver; |
14
|
|
|
|
15
|
|
|
private ReceivedMap $receiveMap; |
16
|
|
|
|
17
|
2 |
|
public static function create(): self |
18
|
|
|
{ |
19
|
2 |
|
return new self( |
20
|
2 |
|
new FileHandler(), |
21
|
2 |
|
new FilePathResolver(), |
22
|
2 |
|
new ReceivedMap() |
23
|
|
|
); |
24
|
|
|
} |
25
|
|
|
|
26
|
2 |
|
public function __construct( |
27
|
|
|
FileHandler $fileHandler, |
28
|
|
|
FilePathResolver $filePathResolver, |
29
|
|
|
ReceivedMap $receiveMap |
30
|
|
|
) { |
31
|
2 |
|
$this->fileHandler = $fileHandler; |
32
|
2 |
|
$this->filePathResolver = $filePathResolver; |
33
|
2 |
|
$this->receiveMap = $receiveMap; |
34
|
2 |
|
} |
35
|
|
|
|
36
|
2 |
|
public function verifyList(array $input, array $output, bool $plain = false): void |
37
|
|
|
{ |
38
|
2 |
|
$filePathResolved = $this->filePathResolver->resolve(); |
39
|
2 |
|
$received = $this->receiveMap->create($input, $output, $plain); |
40
|
|
|
|
41
|
|
|
# always create the received file |
42
|
2 |
|
$this->fileHandler->placeFile( |
43
|
2 |
|
$filePathResolved->getDirPath(), |
44
|
2 |
|
$filePathResolved->getReceivedName(), |
45
|
|
|
$received |
46
|
|
|
); |
47
|
|
|
|
48
|
|
|
# only create the approved file if it does not exist |
49
|
2 |
|
if (!file_exists($filePathResolved->getApprovedFile())) { |
50
|
|
|
$this->fileHandler->placeFile( |
51
|
|
|
$filePathResolved->getDirPath(), |
52
|
|
|
$filePathResolved->getApprovedName(), |
53
|
|
|
null |
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
2 |
|
$this->verify($received, $filePathResolved); |
58
|
2 |
|
} |
59
|
|
|
|
60
|
2 |
|
private function verify(string $received, FilePathResolverResult $filePathResolved): void |
61
|
|
|
{ |
62
|
2 |
|
$approved = file_get_contents($filePathResolved->getApprovedFile()); |
63
|
|
|
|
64
|
2 |
|
Assert::assertEquals( |
65
|
2 |
|
trim($approved), |
66
|
2 |
|
trim($received), |
67
|
2 |
|
'To approve run: mv ' . $filePathResolved->getReceivedFile() . ' ' . $filePathResolved->getApprovedFile() |
68
|
|
|
); |
69
|
|
|
|
70
|
|
|
# delete the received file here because we know it is equal to the approved file |
71
|
2 |
|
$this->fileHandler->deleteReceived($filePathResolved); |
72
|
2 |
|
} |
73
|
|
|
} |
74
|
|
|
|