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