1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Cycle\ORM\Promise\Tests; |
5
|
|
|
|
6
|
|
|
use Cycle\ORM\Promise\Materizalizer\EvalMaterializer; |
7
|
|
|
use Cycle\ORM\Promise\Materizalizer\FileMaterializer; |
8
|
|
|
use Cycle\ORM\Promise\Tests\Fixtures\Entity; |
9
|
|
|
use PHPUnit\Framework\TestCase; |
10
|
|
|
use Spiral\Core\Container; |
11
|
|
|
|
12
|
|
|
class MaterializerTest extends TestCase |
13
|
|
|
{ |
14
|
|
|
private const NS = 'Cycle\ORM\Promise\Tests\Promises'; |
15
|
|
|
|
16
|
|
|
public function setUp() |
17
|
|
|
{ |
18
|
|
|
$files = glob($this->filesDirectory() . DIRECTORY_SEPARATOR . '*'); |
19
|
|
|
foreach ($files as $file) { |
20
|
|
|
if (is_file($file)) { |
21
|
|
|
unlink($file); |
22
|
|
|
} |
23
|
|
|
} |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @dataProvider evalDataProvider |
28
|
|
|
* |
29
|
|
|
* @param string $className |
30
|
|
|
* @param string $code |
31
|
|
|
* |
32
|
|
|
* @throws \ReflectionException |
33
|
|
|
*/ |
34
|
|
|
public function testEvalMaterialize(string $className, string $code): void |
35
|
|
|
{ |
36
|
|
|
$this->assertFalse(class_exists($className)); |
37
|
|
|
|
38
|
|
|
$materializer = new EvalMaterializer(); |
39
|
|
|
$materializer->materialize($code, $className, new \ReflectionClass(Entity::class)); |
40
|
|
|
|
41
|
|
|
$this->assertTrue(class_exists($className)); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function evalDataProvider(): array |
45
|
|
|
{ |
46
|
|
|
return [ |
47
|
|
|
['EvalTestOne', '<?php class EvalTestOne {}'], |
48
|
|
|
['EvalTestTwo', '<? class EvalTestTwo {}'], |
49
|
|
|
['EvalTestThree', 'class EvalTestThree {}'], |
50
|
|
|
]; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @dataProvider fileDataProvider |
55
|
|
|
* |
56
|
|
|
* @param string $className |
57
|
|
|
* @param string $code |
58
|
|
|
* |
59
|
|
|
* @throws \ReflectionException |
60
|
|
|
*/ |
61
|
|
|
public function testFileMaterializer(string $className, string $code): void |
62
|
|
|
{ |
63
|
|
|
$fullClassName = '\\' . self::NS . '\\' . $className; |
64
|
|
|
$this->assertFalse(class_exists($fullClassName)); |
65
|
|
|
|
66
|
|
|
$container = new Container(); |
67
|
|
|
/** @var FileMaterializer $materializer */ |
68
|
|
|
$materializer = $container->make(FileMaterializer::class, ['directory' => $this->filesDirectory()]); |
69
|
|
|
$materializer->materialize($code, $className, new \ReflectionClass(Entity::class)); |
70
|
|
|
|
71
|
|
|
$this->assertTrue(class_exists($fullClassName)); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
public function fileDataProvider(): array |
75
|
|
|
{ |
76
|
|
|
$ns = self::NS; |
77
|
|
|
|
78
|
|
|
$output = []; |
79
|
|
|
$classes = ['FileTestOne', 'FileTestTwo', 'FileTestThree']; |
80
|
|
|
$codePrefixes = ['<?php', '<?', '']; |
81
|
|
|
foreach ($classes as $i => $class) { |
82
|
|
|
$output[] = [ |
83
|
|
|
$class, |
84
|
|
|
"{$codePrefixes[$i]} namespace $ns; class $class {}" |
85
|
|
|
]; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
return $output; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
private function filesDirectory(): string |
92
|
|
|
{ |
93
|
|
|
return dirname(__DIR__) . DIRECTORY_SEPARATOR . 'promises'; |
94
|
|
|
} |
95
|
|
|
} |