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