1 | <?php |
||
17 | class FactoryTest extends BaseTest |
||
18 | { |
||
19 | public const DRIVER = 'sqlite'; |
||
20 | |||
21 | /** @var \Spiral\Core\Container */ |
||
22 | private $container; |
||
23 | |||
24 | public function setUp() |
||
25 | { |
||
26 | self::$config = [ |
||
27 | 'debug' => false, |
||
28 | 'strict' => true, |
||
29 | 'benchmark' => false, |
||
30 | 'sqlite' => [ |
||
31 | 'driver' => SQLiteDriver::class, |
||
32 | 'check' => function () { |
||
33 | return !in_array('sqlite', \PDO::getAvailableDrivers()); |
||
34 | }, |
||
35 | 'conn' => 'sqlite::memory:', |
||
36 | 'user' => 'sqlite', |
||
37 | 'pass' => '' |
||
38 | ], |
||
39 | ]; |
||
40 | |||
41 | parent::setUp(); |
||
42 | |||
43 | $files = glob($this->filesDirectory() . DIRECTORY_SEPARATOR . '*'); |
||
44 | foreach ($files as $file) { |
||
45 | if (is_file($file)) { |
||
46 | unlink($file); |
||
47 | } |
||
48 | } |
||
49 | |||
50 | $this->container = new Container(); |
||
51 | $this->container->bind(ORMInterface::class, $this->orm()); |
||
52 | } |
||
53 | |||
54 | private function filesDirectory(): string |
||
55 | { |
||
56 | return dirname(__DIR__) . DIRECTORY_SEPARATOR . 'fixtures' . DIRECTORY_SEPARATOR . 'promises'; |
||
57 | } |
||
58 | |||
59 | public function testFilePromise(): void |
||
60 | { |
||
61 | $role = SchematicEntity::class; |
||
62 | $scope = []; |
||
63 | |||
64 | $this->bindMaterializer($this->container->make(FileMaterializer::class, ['directory' => $this->filesDirectory()])); |
||
65 | |||
66 | /** @var SchematicEntity $promise */ |
||
67 | $promise = $this->factory()->promise($this->orm(), $role, $scope); |
||
68 | |||
69 | $this->assertInstanceOf($role, $promise); |
||
70 | |||
71 | // $promise->setName('my name'); |
||
72 | // $this->assertSame('my name', $promise->getName()); |
||
73 | } |
||
74 | |||
75 | public function testEvalPromise(): void |
||
76 | { |
||
77 | $role = SchematicEntity::class; |
||
78 | $scope = []; |
||
79 | |||
80 | $this->bindMaterializer($this->container->get(EvalMaterializer::class)); |
||
81 | |||
82 | /** @var SchematicEntity $promise */ |
||
83 | $promise = $this->factory()->promise($this->orm(), $role, $scope); |
||
84 | |||
85 | $this->assertInstanceOf($role, $promise); |
||
86 | |||
87 | // $promise->setName('my name'); |
||
88 | // $this->assertSame('my name', $promise->getName()); |
||
89 | } |
||
90 | |||
91 | private function orm(): ORMInterface |
||
92 | { |
||
93 | $schema = (new Schema\Compiler())->compile(new Schema\Registry($this->dbal), [ |
||
94 | new Entities($this->locator), |
||
95 | new Schema\Generator\ResetTables(), |
||
96 | new Schema\Generator\GenerateRelations(), |
||
97 | new Schema\Generator\ValidateEntities(), |
||
98 | new Schema\Generator\RenderTables(), |
||
99 | new Schema\Generator\RenderRelations(), |
||
100 | new Schema\Generator\SyncTables(), |
||
101 | new Schema\Generator\GenerateTypecast(), |
||
102 | ]); |
||
103 | |||
104 | return $this->withSchema(new \Cycle\ORM\Schema($schema)); |
||
105 | } |
||
106 | |||
107 | private function factory(): Factory |
||
108 | { |
||
109 | return $this->container->get(Factory::class); |
||
110 | } |
||
111 | |||
112 | private function bindMaterializer(MaterializerInterface $materializer) |
||
113 | { |
||
114 | $this->container->bind(MaterializerInterface::class, $materializer); |
||
115 | } |
||
116 | } |