Completed
Push — master ( 02745e...0cb812 )
by Valentin
07:17
created

FactoryTest::orm()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.7666
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
declare(strict_types=1);
3
4
namespace Cycle\ORM\Promise\Tests;
5
6
use Cycle\Annotated\Entities;
7
use Cycle\ORM\ORMInterface;
8
use Cycle\ORM\Promise\Factory;
9
use Cycle\ORM\Promise\MaterializerInterface;
10
use Cycle\ORM\Promise\Materizalizer\EvalMaterializer;
11
use Cycle\ORM\Promise\Materizalizer\FileMaterializer;
12
use Cycle\ORM\Promise\Tests\Fixtures\SchematicEntity;
13
use Cycle\Schema;
14
use Spiral\Core\Container;
15
use Spiral\Database\Driver\SQLite\SQLiteDriver;
16
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());
1 ignored issue
show
Documentation introduced by
$this->orm() is of type object<Cycle\ORM\ORMInterface>, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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);
1 ignored issue
show
Documentation introduced by
$materializer is of type object<Cycle\ORM\Promise\MaterializerInterface>, but the function expects a callable.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
115
    }
116
}