1- <?php namespace CodeIgniter \ Database ;
1+ <?php
22
3+ namespace CodeIgniter \Database ;
4+
5+ use CodeIgniter \Database \Exceptions \DatabaseException ;
6+ use CodeIgniter \Test \CIUnitTestCase ;
37use CodeIgniter \Test \Mock \MockConnection ;
8+ use Throwable ;
49
5- class BaseConnectionTest extends \ CodeIgniter \ Test \ CIUnitTestCase
10+ class BaseConnectionTest extends CIUnitTestCase
611{
712 protected $ options = [
813 'DSN ' => '' ,
@@ -42,8 +47,6 @@ class BaseConnectionTest extends \CodeIgniter\Test\CIUnitTestCase
4247 'failover ' => [],
4348 ];
4449
45- //--------------------------------------------------------------------
46-
4750 public function testSavesConfigOptions ()
4851 {
4952 $ db = new MockConnection ($ this ->options );
@@ -64,94 +67,75 @@ public function testSavesConfigOptions()
6467 $ this ->assertSame ([], $ db ->failover );
6568 }
6669
67- //--------------------------------------------------------------------
68-
6970 public function testConnectionThrowExceptionWhenCannotConnect ()
7071 {
71- $ db = new MockConnection ($ this ->options );
72-
73- $ this ->expectException ('\CodeIgniter\Database\Exceptions\DatabaseException ' );
74- $ this ->expectExceptionMessage ('Unable to connect to the database. ' );
75-
76- $ db ->shouldReturn ('connect ' , false )
77- ->initialize ();
72+ try
73+ {
74+ $ db = new MockConnection ($ this ->options );
75+ $ db ->shouldReturn ('connect ' , false )->initialize ();
76+ }
77+ catch (Throwable $ e )
78+ {
79+ $ this ->assertInstanceOf (DatabaseException::class, $ e );
80+ $ this ->assertStringContainsString ('Unable to connect to the database. ' , $ e ->getMessage ());
81+ }
7882 }
7983
80- //--------------------------------------------------------------------
81-
8284 public function testCanConnectAndStoreConnection ()
8385 {
8486 $ db = new MockConnection ($ this ->options );
85-
86- $ db ->shouldReturn ('connect ' , 123 )
87- ->initialize ();
87+ $ db ->shouldReturn ('connect ' , 123 )->initialize ();
8888
8989 $ this ->assertSame (123 , $ db ->getConnection ());
9090 }
9191
92- //--------------------------------------------------------------------
93-
9492 /**
95- * @throws \CodeIgniter\Database\Exceptions\DatabaseException
96- * @group single
93+ * @group single
9794 */
9895 public function testCanConnectToFailoverWhenNoConnectionAvailable ()
9996 {
10097 $ options = $ this ->options ;
10198 $ options ['failover ' ] = [$ this ->failoverOptions ];
10299
103100 $ db = new MockConnection ($ options );
104-
105- $ db ->shouldReturn ('connect ' , [false , 345 ])
106- ->initialize ();
101+ $ db ->shouldReturn ('connect ' , [false , 345 ])->initialize ();
107102
108103 $ this ->assertSame (345 , $ db ->getConnection ());
109104 $ this ->assertSame ('failover ' , $ db ->username );
110105 }
111106
112- //--------------------------------------------------------------------
113-
114107 public function testStoresConnectionTimings ()
115108 {
116109 $ start = microtime (true );
117110
118111 $ db = new MockConnection ($ this ->options );
119-
120112 $ db ->initialize ();
121113
122114 $ this ->assertGreaterThan ($ start , $ db ->getConnectStart ());
123115 $ this ->assertGreaterThan (0.0 , $ db ->getConnectDuration ());
124116 }
125117
126- //--------------------------------------------------------------------
127-
128118 public function testMagicIssetTrue ()
129119 {
130120 $ db = new MockConnection ($ this ->options );
131121
132122 $ this ->assertTrue (isset ($ db ->charset ));
133123 }
134124
135- //--------------------------------------------------------------------
136-
137125 public function testMagicIssetFalse ()
138126 {
139127 $ db = new MockConnection ($ this ->options );
140128
141129 $ this ->assertFalse (isset ($ db ->foobar ));
142130 }
143131
144- //--------------------------------------------------------------------
145-
146132 public function testMagicGet ()
147133 {
148134 $ db = new MockConnection ($ this ->options );
149135
150- $ this ->assertEquals ('utf8 ' , $ db ->charset );
136+ $ this ->assertSame ('utf8 ' , $ db ->charset );
151137 }
152138
153- //--------------------------------------------------------------------
154-
155139 public function testMagicGetMissing ()
156140 {
157141 $ db = new MockConnection ($ this ->options );
0 commit comments