@@ -28,16 +28,36 @@ final class PreparedQueryTest extends CIUnitTestCase
2828 protected $ refresh = true ;
2929 protected $ seed = 'Tests\Support\Database\Seeds\CITestSeeder ' ;
3030
31+ /**
32+ * @var BasePreparedQuery|null
33+ */
34+ private $ query ;
35+
36+ protected function setUp (): void
37+ {
38+ parent ::setUp ();
39+ $ this ->query = null ;
40+ }
41+
42+ protected function tearDown (): void
43+ {
44+ parent ::tearDown ();
45+
46+ if ($ this ->query !== null ) {
47+ $ this ->query ->close ();
48+ }
49+ }
50+
3151 public function testPrepareReturnsPreparedQuery ()
3252 {
33- $ query = $ this ->db ->prepare (static function ($ db ) {
53+ $ this -> query = $ this ->db ->prepare (static function ($ db ) {
3454 return $ db ->table ('user ' )->insert ([
3555 'name ' => 'a ' ,
3656 'email ' => 'b@example.com ' ,
3757 ]);
3858 });
3959
40- $ this ->assertInstanceOf (BasePreparedQuery::class, $ query );
60+ $ this ->assertInstanceOf (BasePreparedQuery::class, $ this -> query );
4161
4262 $ ec = $ this ->db ->escapeChar ;
4363 $ pre = $ this ->db ->DBPrefix ;
@@ -54,20 +74,19 @@ public function testPrepareReturnsPreparedQuery()
5474 } else {
5575 $ expected = "INSERT INTO {$ ec }{$ pre }user {$ ec } ( {$ ec }name {$ ec }, {$ ec }email {$ ec }) VALUES ( {$ placeholders }) " ;
5676 }
57- $ this ->assertSame ($ expected , $ query ->getQueryString ());
5877
59- $ query ->close ( );
78+ $ this -> assertSame ( $ expected , $ this -> query ->getQueryString () );
6079 }
6180
6281 public function testPrepareReturnsManualPreparedQuery ()
6382 {
64- $ query = $ this ->db ->prepare (static function ($ db ) {
83+ $ this -> query = $ this ->db ->prepare (static function ($ db ) {
6584 $ sql = "INSERT INTO {$ db ->DBPrefix }user (name, email, country) VALUES (?, ?, ?) " ;
6685
6786 return (new Query ($ db ))->setQuery ($ sql );
6887 });
6988
70- $ this ->assertInstanceOf (BasePreparedQuery::class, $ query );
89+ $ this ->assertInstanceOf (BasePreparedQuery::class, $ this -> query );
7190
7291 $ pre = $ this ->db ->DBPrefix ;
7392
@@ -78,33 +97,29 @@ public function testPrepareReturnsManualPreparedQuery()
7897 }
7998
8099 $ expected = "INSERT INTO {$ pre }user (name, email, country) VALUES ( {$ placeholders }) " ;
81- $ this ->assertSame ($ expected , $ query ->getQueryString ());
82-
83- $ query ->close ();
100+ $ this ->assertSame ($ expected , $ this ->query ->getQueryString ());
84101 }
85102
86103 public function testExecuteRunsQueryAndReturnsResultObject ()
87104 {
88- $ query = $ this ->db ->prepare (static function ($ db ) {
105+ $ this -> query = $ this ->db ->prepare (static function ($ db ) {
89106 return $ db ->table ('user ' )->insert ([
90107 'name ' => 'a ' ,
91108 'email ' => 'b@example.com ' ,
92109 'country ' => 'x ' ,
93110 ]);
94111 });
95112
96- $ query ->execute ('foo ' , 'foo@example.com ' , 'US ' );
97- $ query ->execute ('bar ' , 'bar@example.com ' , 'GB ' );
113+ $ this -> query ->execute ('foo ' , 'foo@example.com ' , 'US ' );
114+ $ this -> query ->execute ('bar ' , 'bar@example.com ' , 'GB ' );
98115
99116 $ this ->seeInDatabase ($ this ->db ->DBPrefix . 'user ' , ['name ' => 'foo ' , 'email ' => 'foo@example.com ' ]);
100117 $ this ->seeInDatabase ($ this ->db ->DBPrefix . 'user ' , ['name ' => 'bar ' , 'email ' => 'bar@example.com ' ]);
101-
102- $ query ->close ();
103118 }
104119
105120 public function testExecuteRunsQueryAndReturnsManualResultObject ()
106121 {
107- $ query = $ this ->db ->prepare (static function ($ db ) {
122+ $ this -> query = $ this ->db ->prepare (static function ($ db ) {
108123 $ sql = "INSERT INTO {$ db ->DBPrefix }user (name, email, country) VALUES (?, ?, ?) " ;
109124
110125 if ($ db ->DBDriver === 'SQLSRV ' ) {
@@ -114,12 +129,10 @@ public function testExecuteRunsQueryAndReturnsManualResultObject()
114129 return (new Query ($ db ))->setQuery ($ sql );
115130 });
116131
117- $ query ->execute ('foo ' , 'foo@example.com ' , '' );
118- $ query ->execute ('bar ' , 'bar@example.com ' , '' );
132+ $ this -> query ->execute ('foo ' , 'foo@example.com ' , '' );
133+ $ this -> query ->execute ('bar ' , 'bar@example.com ' , '' );
119134
120135 $ this ->seeInDatabase ($ this ->db ->DBPrefix . 'user ' , ['name ' => 'foo ' , 'email ' => 'foo@example.com ' ]);
121136 $ this ->seeInDatabase ($ this ->db ->DBPrefix . 'user ' , ['name ' => 'bar ' , 'email ' => 'bar@example.com ' ]);
122-
123- $ query ->close ();
124137 }
125138}
0 commit comments