-
Notifications
You must be signed in to change notification settings - Fork 42
/
Copy pathMock.php
103 lines (92 loc) · 2.49 KB
/
Mock.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
declare(strict_types=1);
namespace EcEuropa\Toolkit;
use Symfony\Component\Process\Process;
/**
* Toolkit mock class.
*/
final class Mock
{
/**
* The default mock tag to use to download and local directory.
*
* @var string
*/
private static string $defaultTag = '0.0.31';
/**
* The directory to download the mock to.
*
* @var string
*/
private static string $directory = '.toolkit-mock';
/**
* Downloads the mock from the repo.
*
* @throws \Exception
* If missing env var TOOLKIT_MOCK_REPO.
*/
public static function download(): bool
{
$tag = self::tag();
$mockDir = self::$directory . '/' . $tag;
if (file_exists($mockDir)) {
return true;
}
if (!Toolkit::isCiCd()) {
return false;
}
$repo = self::repo();
$command = "git clone --depth 1 --branch $tag $repo $mockDir";
$process = Process::fromShellCommandline($command);
$process->run();
if ($process->getExitCode()) {
throw new \Exception($process->getErrorOutput());
}
return file_exists($mockDir);
}
/**
* Returns the content of the endpoint from the mock.
*
* @param string $endpoint
* The endpoint to return the content from.
*
* @throws \Exception
* If the mock or endpoint file is not found.
*/
public static function getEndpointContent(string $endpoint)
{
$tag = self::tag();
$mockDir = self::$directory . '/' . $tag;
if (!file_exists($mockDir)) {
throw new \Exception("Mock not found at '$mockDir'.");
}
$endpointFile = "$mockDir/$endpoint.json";
if (!file_exists($endpointFile)) {
throw new \Exception("No file found for endpoint '$endpoint'.");
}
return file_get_contents($endpointFile);
}
/**
* Returns the repository url.
*
* @throws \Exception
* If missing env var TOOLKIT_MOCK_REPO.
*/
public static function repo(): string
{
if (empty($repo = getenv('TOOLKIT_MOCK_REPO'))) {
throw new \Exception('Missing env var TOOLKIT_MOCK_REPO.');
}
return (string) $repo;
}
/**
* Returns the tag to use.
*/
public static function tag(): string
{
if (!empty($tag = getenv('TOOLKIT_MOCK_TAG'))) {
return (string) $tag;
}
return self::$defaultTag;
}
}