1
|
|
|
<?php |
2
|
|
|
declare(strict_types=1); |
3
|
|
|
|
4
|
|
|
namespace Cycle\ORM\Promise\Tests; |
5
|
|
|
|
6
|
|
|
use Cycle\ORM\ORMInterface; |
7
|
|
|
use Cycle\ORM\Promise\Factory; |
8
|
|
|
use Cycle\ORM\Promise\MaterializerInterface; |
9
|
|
|
use Cycle\ORM\Promise\Materizalizer\EvalMaterializer; |
10
|
|
|
use Cycle\ORM\Promise\Materizalizer\FileMaterializer; |
11
|
|
|
use Cycle\ORM\Promise\Tests\Fixtures\Entity; |
12
|
|
|
use Cycle\ORM\SchemaInterface; |
13
|
|
|
use PHPUnit\Framework\TestCase; |
14
|
|
|
use Spiral\Core\Container; |
15
|
|
|
|
16
|
|
|
class FactoryTest extends TestCase |
17
|
|
|
{ |
18
|
|
|
private const NS = 'Cycle\ORM\Promise\Tests\Promises'; |
19
|
|
|
|
20
|
|
|
public function setUp() |
21
|
|
|
{ |
22
|
|
|
$files = glob($this->filesDirectory() . DIRECTORY_SEPARATOR . '*'); |
23
|
|
|
foreach ($files as $file) { |
24
|
|
|
if (is_file($file)) { |
25
|
|
|
unlink($file); |
26
|
|
|
} |
27
|
|
|
} |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
private function filesDirectory(): string |
31
|
|
|
{ |
32
|
|
|
return dirname(__DIR__) . DIRECTORY_SEPARATOR . 'promises'; |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
public function testFilePromise(): void |
36
|
|
|
{ |
37
|
|
|
$role = Entity::class; |
38
|
|
|
|
39
|
|
|
$orm = \Mockery::mock(ORMInterface::class); |
40
|
|
|
$schema = \Mockery::mock(SchemaInterface::class); |
41
|
|
|
$schema->shouldReceive('define')->andReturn($role); |
|
|
|
|
42
|
|
|
$orm->shouldReceive('getSchema')->andReturn($schema); |
43
|
|
|
|
44
|
|
|
$container = new Container(); |
45
|
|
|
$container->bind(ORMInterface::class, $orm); |
46
|
|
|
|
47
|
|
|
$materializer = $container->make(FileMaterializer::class, ['directory' => dirname(__DIR__) . DIRECTORY_SEPARATOR . 'promises']); |
48
|
|
|
$container->bind(MaterializerInterface::class, $materializer); |
|
|
|
|
49
|
|
|
|
50
|
|
|
/** @var Factory $factory */ |
51
|
|
|
$factory = $container->get(Factory::class); |
52
|
|
|
|
53
|
|
|
/** @var Entity $promise */ |
54
|
|
|
$promise = $factory->promise($orm, $role, []); |
55
|
|
|
|
56
|
|
|
$this->assertInstanceOf($role, $promise); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
public function testEvalPromise(): void |
60
|
|
|
{ |
61
|
|
|
$role = Entity::class; |
62
|
|
|
|
63
|
|
|
$orm = \Mockery::mock(ORMInterface::class); |
64
|
|
|
$schema = \Mockery::mock(SchemaInterface::class); |
65
|
|
|
$schema->shouldReceive('define')->andReturn($role); |
|
|
|
|
66
|
|
|
$orm->shouldReceive('getSchema')->andReturn($schema); |
67
|
|
|
|
68
|
|
|
$container = new Container(); |
69
|
|
|
$container->bind(ORMInterface::class, $orm); |
70
|
|
|
|
71
|
|
|
$materializer = $container->get(EvalMaterializer::class); |
72
|
|
|
$container->bind(MaterializerInterface::class, $materializer); |
|
|
|
|
73
|
|
|
|
74
|
|
|
/** @var Factory $factory */ |
75
|
|
|
$factory = $container->get(Factory::class); |
76
|
|
|
|
77
|
|
|
/** @var Entity $promise */ |
78
|
|
|
$promise = $factory->promise($orm, $role, []); |
79
|
|
|
|
80
|
|
|
$this->assertInstanceOf($role, $promise); |
81
|
|
|
} |
82
|
|
|
} |
It seems like the method you are trying to call exists only in some of the possible types.
Let’s take a look at an example:
Available Fixes
Add an additional type-check:
Only allow a single type to be passed if the variable comes from a parameter: