1414
1515class TypeRegistryTest extends TestCase
1616{
17- private const TEST_TYPE_NAME = 'test ' ;
18- private const TEST_TYPE_CLASS = BlobType::class;
19- private const OTHER_TYPE_NAME = 'other ' ;
20- private const OTHER_TYPE_CLASS = BinaryType::class;
17+ private const TEST_TYPE_NAME = 'test ' ;
18+ private const OTHER_TEST_TYPE_NAME = 'other ' ;
2119
2220 /** @var TypeRegistry */
2321 private $ registry ;
2422
23+ /** @var BlobType */
24+ private $ testType ;
25+
26+ /** @var BinaryType */
27+ private $ otherTestType ;
28+
2529 protected function setUp () : void
2630 {
31+ $ this ->testType = new BlobType ();
32+ $ this ->otherTestType = new BinaryType ();
33+
2734 $ this ->registry = new TypeRegistry ();
28- $ this ->registry ->register (self ::OTHER_TYPE_NAME , self :: OTHER_TYPE_CLASS );
29- $ this ->registry ->register (self ::TEST_TYPE_NAME , self :: TEST_TYPE_CLASS );
35+ $ this ->registry ->register (self ::TEST_TYPE_NAME , $ this -> testType );
36+ $ this ->registry ->register (self ::OTHER_TEST_TYPE_NAME , $ this -> otherTestType );
3037 }
3138
3239 public function testGet () : void
3340 {
34- self ::assertInstanceOf (self ::TEST_TYPE_CLASS , $ this ->registry ->get (self ::TEST_TYPE_NAME ));
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 ));
3543
3644 $ this ->expectException (DBALException::class);
3745 $ this ->registry ->get ('unknown ' );
@@ -49,7 +57,11 @@ public function testLookupName() : void
4957 {
5058 self ::assertSame (
5159 self ::TEST_TYPE_NAME ,
52- $ this ->registry ->lookupName ($ this ->registry ->get (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 )
5365 );
5466
5567 $ this ->expectException (DBALException::class);
@@ -59,40 +71,84 @@ public function testLookupName() : void
5971 public function testHas () : void
6072 {
6173 self ::assertTrue ($ this ->registry ->has (self ::TEST_TYPE_NAME ));
74+ self ::assertTrue ($ this ->registry ->has (self ::OTHER_TEST_TYPE_NAME ));
6275 self ::assertFalse ($ this ->registry ->has ('unknown ' ));
6376 }
6477
6578 public function testRegister () : void
6679 {
67- $ this ->registry ->register ('some ' , TextType::class);
80+ $ newType = new TextType ();
81+
82+ $ this ->registry ->register ('some ' , $ newType );
6883
6984 self ::assertTrue ($ this ->registry ->has ('some ' ));
70- self ::assertInstanceOf (TextType::class, $ this ->registry ->get ('some ' ));
85+ self ::assertSame ($ newType , $ this ->registry ->get ('some ' ));
86+ }
87+
88+ public function testRegisterWithAlradyRegisteredName () : void
89+ {
90+ $ this ->registry ->register ('some ' , new TextType ());
7191
7292 $ this ->expectException (DBALException::class);
73- $ this ->registry ->register ('some ' , TextType::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 );
74104 }
75105
76106 public function testOverride () : void
77107 {
78- $ this -> registry -> register ( ' some ' , TextType::class );
79- $ this -> registry -> override ( ' some ' , StringType::class );
108+ $ baseType = new TextType ( );
109+ $ overrideType = new StringType ( );
80110
81- self ::assertTrue ($ this ->registry ->has ('some ' ));
82- self ::assertInstanceOf (StringType::class, $ this ->registry ->get ('some ' ));
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 ());
83133
84134 $ this ->expectException (DBALException::class);
85- $ this ->registry ->override ('unknown ' , StringType::class );
135+ $ this ->registry ->override ('second ' , $ newType );
86136 }
87137
88- public function testGetTypeClasses () : void
138+ public function testOverrideWithUnknownType () : void
89139 {
90- self ::assertSame (
91- [
92- self ::OTHER_TYPE_NAME => self ::OTHER_TYPE_CLASS ,
93- self ::TEST_TYPE_NAME => self ::TEST_TYPE_CLASS ,
94- ],
95- $ this ->registry ->getTypeClasses ()
96- );
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 ]);
97153 }
98154}
0 commit comments