Passed
Push — type-registry ( c25a28...f90912 )
by Michael
23:20
created

TypeRegistryTest::testOverrideWithUnknownType()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Doctrine\Tests\DBAL\Types;
6
7
use Doctrine\DBAL\DBALException;
8
use Doctrine\DBAL\Types\BinaryType;
9
use Doctrine\DBAL\Types\BlobType;
10
use Doctrine\DBAL\Types\StringType;
11
use Doctrine\DBAL\Types\TextType;
12
use Doctrine\DBAL\Types\TypeRegistry;
13
use PHPUnit\Framework\TestCase;
14
15
class TypeRegistryTest extends TestCase
16
{
17
    private const TEST_TYPE_NAME       = 'test';
18
    private const OTHER_TEST_TYPE_NAME = 'other';
19
20
    /** @var TypeRegistry */
21
    private $registry;
22
23
    /** @var BlobType */
24
    private $testType;
25
26
    /** @var BinaryType */
27
    private $otherTestType;
28
29
    protected function setUp() : void
30
    {
31
        $this->testType      = new BlobType();
32
        $this->otherTestType = new BinaryType();
33
34
        $this->registry = new TypeRegistry();
35
        $this->registry->register(self::TEST_TYPE_NAME, $this->testType);
36
        $this->registry->register(self::OTHER_TEST_TYPE_NAME, $this->otherTestType);
37
    }
38
39
    public function testGet() : void
40
    {
41
        self::assertSame($this->testType, $this->registry->get(self::TEST_TYPE_NAME));
42
        self::assertSame($this->otherTestType, $this->registry->get(self::OTHER_TEST_TYPE_NAME));
43
44
        $this->expectException(DBALException::class);
45
        $this->registry->get('unknown');
46
    }
47
48
    public function testGetReturnsSameInstances() : void
49
    {
50
        self::assertSame(
51
            $this->registry->get(self::TEST_TYPE_NAME),
52
            $this->registry->get(self::TEST_TYPE_NAME)
53
        );
54
    }
55
56
    public function testLookupName() : void
57
    {
58
        self::assertSame(
59
            self::TEST_TYPE_NAME,
60
            $this->registry->lookupName($this->testType)
61
        );
62
        self::assertSame(
63
            self::OTHER_TEST_TYPE_NAME,
64
            $this->registry->lookupName($this->otherTestType)
65
        );
66
67
        $this->expectException(DBALException::class);
68
        $this->registry->lookupName(new TextType());
69
    }
70
71
    public function testHas() : void
72
    {
73
        self::assertTrue($this->registry->has(self::TEST_TYPE_NAME));
74
        self::assertTrue($this->registry->has(self::OTHER_TEST_TYPE_NAME));
75
        self::assertFalse($this->registry->has('unknown'));
76
    }
77
78
    public function testRegister() : void
79
    {
80
        $newType = new TextType();
81
82
        $this->registry->register('some', $newType);
83
84
        self::assertTrue($this->registry->has('some'));
85
        self::assertSame($newType, $this->registry->get('some'));
86
    }
87
88
    public function testRegisterWithAlradyRegisteredName() : void
89
    {
90
        $this->registry->register('some', new TextType());
91
92
        $this->expectException(DBALException::class);
93
        $this->registry->register('some', new TextType());
94
    }
95
96
    public function testRegisterWithAlreadyRegisteredInstance() : void
97
    {
98
        $newType = new TextType();
99
100
        $this->registry->register('some', $newType);
101
102
        $this->expectException(DBALException::class);
103
        $this->registry->register('some', $newType);
104
    }
105
106
    public function testOverride() : void
107
    {
108
        $baseType     = new TextType();
109
        $overrideType = new StringType();
110
111
        $this->registry->register('some', $baseType);
112
        $this->registry->override('some', $overrideType);
113
114
        self::assertSame($overrideType, $this->registry->get('some'));
115
    }
116
117
    public function testOverrideAllowsExistingInstance() : void
118
    {
119
        $type = new TextType();
120
121
        $this->registry->register('some', $type);
122
        $this->registry->override('some', $type);
123
124
        self::assertSame($type, $this->registry->get('some'));
125
    }
126
127
    public function testOverrideWithAlreadyRegisteredInstance() : void
128
    {
129
        $newType = new TextType();
130
131
        $this->registry->register('first', $newType);
132
        $this->registry->register('second', new StringType());
133
134
        $this->expectException(DBALException::class);
135
        $this->registry->override('second', $newType);
136
    }
137
138
    public function testOverrideWithUnknownType() : void
139
    {
140
        $this->expectException(DBALException::class);
141
        $this->registry->override('unknown', new TextType());
142
    }
143
144
    public function testAll() : void
145
    {
146
        $registeredTypes = $this->registry->all();
147
148
        self::assertCount(2, $registeredTypes);
149
        self::assertArrayHasKey(self::TEST_TYPE_NAME, $registeredTypes);
150
        self::assertArrayHasKey(self::OTHER_TEST_TYPE_NAME, $registeredTypes);
151
        self::assertSame($this->testType, $registeredTypes[self::TEST_TYPE_NAME]);
152
        self::assertSame($this->otherTestType, $registeredTypes[self::OTHER_TEST_TYPE_NAME]);
153
    }
154
}
155