1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace ApiClients\Tools\ResourceTestUtilities; |
5
|
|
|
|
6
|
|
|
use ApiClients\Foundation\Hydrator\Factory; |
7
|
|
|
use ApiClients\Foundation\Hydrator\Options; |
8
|
|
|
use ApiClients\Tools\TestUtilities\TestCase as BaseTestCase; |
9
|
|
|
|
10
|
|
|
abstract class TestCase extends BaseTestCase |
11
|
|
|
{ |
12
|
|
|
const DEFAULT_GENERATED_CLASS_NAMESPACE = 'GHGC_%s'; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
private $tmpDir; |
|
|
|
|
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var string |
21
|
|
|
*/ |
22
|
|
|
private $tmpNamespace; |
|
|
|
|
23
|
|
|
|
24
|
6 |
|
public function setUp() |
25
|
|
|
{ |
26
|
6 |
|
parent::setUp(); |
27
|
6 |
|
$crc32 = crc32(get_class($this)); |
28
|
6 |
|
$this->tmpDir = sys_get_temp_dir() . DIRECTORY_SEPARATOR . uniqid('act-' . $crc32, true) . DIRECTORY_SEPARATOR; |
29
|
6 |
|
mkdir($this->tmpDir, 0777, true); |
30
|
6 |
|
$this->tmpNamespace = sprintf( |
31
|
6 |
|
static::DEFAULT_GENERATED_CLASS_NAMESPACE, |
32
|
6 |
|
crc32(uniqid((string)$crc32, true)) |
33
|
|
|
); |
34
|
6 |
|
} |
35
|
|
|
|
36
|
6 |
|
public function tearDown() |
37
|
|
|
{ |
38
|
6 |
|
parent::tearDown(); |
39
|
6 |
|
$this->rmdir($this->tmpDir); |
40
|
6 |
|
} |
41
|
|
|
|
42
|
6 |
|
protected function rmdir($dir) |
43
|
|
|
{ |
44
|
6 |
|
$directory = dir($dir); |
45
|
6 |
|
while (false !== ($entry = $directory->read())) { |
46
|
6 |
|
if (in_array($entry, ['.', '..'])) { |
47
|
6 |
|
continue; |
48
|
|
|
} |
49
|
|
|
|
50
|
6 |
|
if (is_dir($dir . $entry)) { |
51
|
6 |
|
$this->rmdir($dir . $entry . DIRECTORY_SEPARATOR); |
52
|
6 |
|
continue; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
if (is_file($dir . $entry)) { |
56
|
|
|
unlink($dir . $entry); |
57
|
|
|
continue; |
58
|
|
|
} |
59
|
|
|
} |
60
|
6 |
|
$directory->close(); |
61
|
6 |
|
rmdir($dir); |
62
|
6 |
|
} |
63
|
|
|
|
64
|
|
|
protected function getTmpDir(): string |
65
|
|
|
{ |
66
|
|
|
return $this->tmpDir; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
protected function getRandomNameSpace(): string |
70
|
|
|
{ |
71
|
|
|
return $this->tmpNamespace; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function hydrate($class, $json, $namespace) |
75
|
|
|
{ |
76
|
|
|
return Factory::create([ |
|
|
|
|
77
|
|
|
Options::NAMESPACE => $namespace, |
78
|
|
|
])->hydrateFQCN($class, $json); |
79
|
|
|
} |
80
|
|
|
} |
81
|
|
|
|