Passed
Push — master ( 32f4fa...9a9d10 )
by Pavel
02:45
created

TestDiType   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 24
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 3

Importance

Changes 0
Metric Value
wmc 3
lcom 0
cbo 3
dl 0
loc 24
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Bankiru\Doctrine\DiType;
4
5
use Bankiru\Doctrine\DiType\Test\ContainerTestTrait;
6
use Doctrine\Bundle\DoctrineBundle\DoctrineBundle;
7
use Doctrine\Common\Persistence\ManagerRegistry;
8
use Doctrine\DBAL\Platforms\AbstractPlatform;
9
use Doctrine\DBAL\Types\Type;
10
use PHPUnit\Framework\Assert;
11
use PHPUnit\Framework\TestCase;
12
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
13
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
14
use Symfony\Component\DependencyInjection\ContainerBuilder;
15
use Symfony\Component\DependencyInjection\ContainerInterface;
16
use Symfony\Component\DependencyInjection\Reference;
17
18
final class DiTypeTest extends TestCase
19
{
20
    use ContainerTestTrait;
21
22
    public function testDiTypeIsRegistered()
23
    {
24
        $container = $this->getContainer();
25
26
        $container->register('test_type', TestDiType::class)
27
                  ->addMethodCall('setContainer', [new Reference('service_container')])
28
                  ->addTag('doctrine_di_type', ['type' => 'test']);
29
30
        $this->compile($container);
31
32
        $container->get('doctrine');
33
34
        $types = Type::getTypesMap();
35
36
        self::assertArrayHasKey('test', $types);
37
38
        /** @var TestDiType $type */
39
        $type = Type::getType('test');
40
41
        self::assertInstanceOf(TestDiType::class, $type);
42
        self::assertSame($container, $type->getContainer());
43
    }
44
45
    /**
46
     * @expectedException \LogicException
47
     */
48
    public function testDiRequiresTypeTagAttribute()
49
    {
50
        $container = $this->getContainer();
51
52
        $container->register('test_type', TestDiType::class)
53
                  ->addMethodCall('setContainer', [new Reference('service_container')])
54
                  ->addTag('doctrine_di_type');
55
56
        $this->compile($container);
57
    }
58
59
    public function testTypesAreNotRegisteredUntilRequired()
60
    {
61
        $container = $this->buildContainer(
62
            [
63
                new DoctrineBundle(),
64
                new BankiruDoctrineDiTypeBundle(),
65
            ],
66
            [
67
                'doctrine_di_types' => [
68
                    'init_on_boot' => false,
69
                ],
70
                'doctrine'          => [
71
                    'dbal' => [
72
                        'driver' => 'pdo_sqlite',
73
                        'memory' => true,
74
                    ],
75
                ],
76
77
            ],
78
            false
79
        );
80
81
        $container->register('test_type', TestDiType::class)
82
                  ->addMethodCall('setContainer', [new Reference('service_container')])
83
                  ->addTag('doctrine_di_type', ['type' => 'test']);
84
85
        $this->compile($container);
86
87
        self::assertArrayNotHasKey('test', Type::getTypesMap());
88
89
        $container->get('doctrine');
90
91
        self::assertArrayHasKey('test', Type::getTypesMap());
92
    }
93
94
    public function testExtensionShouldBeEnabled()
95
    {
96
        $container = $this->buildContainer(
97
            [
98
                new DoctrineBundle(),
99
                new BankiruDoctrineDiTypeBundle(),
100
            ],
101
            [
102
                'doctrine' => [
103
                    'dbal' => [
104
                        'driver' => 'pdo_sqlite',
105
                        'memory' => true,
106
                    ],
107
                ],
108
109
            ],
110
            false
111
        );
112
113
        $this->compile($container);
114
115
        self::assertFalse($container->has('bankiru_doctrine_di_types.type_factory'));
116
    }
117
118
    public function testBundlePreservesPreviousConfigurator()
119
    {
120
        $container = $this->getContainer();
121
122
        $container->getDefinition('doctrine')
123
                  ->setConfigurator([Configurator::class, 'configure']);
124
125
        $this->compile($container);
126
    }
127
128
    /**
129
     * @return ContainerBuilder
130
     */
131
    protected function getContainer()
132
    {
133
        $container = $this->buildContainer(
134
            [
135
                new DoctrineBundle(),
136
                new BankiruDoctrineDiTypeBundle(),
137
            ],
138
            [
139
                'doctrine_di_types' => null,
140
                'doctrine'          => [
141
                    'dbal' => [
142
                        'driver' => 'pdo_sqlite',
143
                        'memory' => true,
144
                    ],
145
                ],
146
147
            ],
148
            false
149
        );
150
151
        return $container;
152
    }
153
}
154
155
final class Configurator
156
{
157
    public static function configure($instance)
158
    {
159
        Assert::assertInstanceOf(ManagerRegistry::class, $instance);
160
    }
161
}
162
163
final class TestDiType extends AbstractDiType implements ContainerAwareInterface
164
{
165
    use ContainerAwareTrait;
166
167
    /** {@inheritdoc} */
168
    public function getSQLDeclaration(array $fieldDeclaration, AbstractPlatform $platform)
169
    {
170
        return $platform->getIntegerTypeDeclarationSQL($fieldDeclaration);
171
    }
172
173
    /** {@inheritdoc} */
174
    public function getName()
175
    {
176
        return 'test';
177
    }
178
179
    /**
180
     * @return ContainerInterface
181
     */
182
    public function getContainer()
183
    {
184
        return $this->getOriginal()->container;
185
    }
186
}
187