15
15
use CodeIgniter \Database \Query ;
16
16
use CodeIgniter \Test \CIUnitTestCase ;
17
17
use CodeIgniter \Test \DatabaseTestTrait ;
18
+ use Tests \Support \Database \Seeds \CITestSeeder ;
18
19
19
20
/**
20
21
* @group DatabaseLive
@@ -25,19 +26,38 @@ final class PreparedQueryTest extends CIUnitTestCase
25
26
{
26
27
use DatabaseTestTrait;
27
28
28
- protected $ refresh = true ;
29
- protected $ seed = 'Tests\Support\Database\Seeds\CITestSeeder ' ;
29
+ protected $ seed = CITestSeeder::class;
30
+
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
+ }
30
50
31
51
public function testPrepareReturnsPreparedQuery ()
32
52
{
33
- $ query = $ this ->db ->prepare (static function ($ db ) {
53
+ $ this -> query = $ this ->db ->prepare (static function ($ db ) {
34
54
return $ db ->table ('user ' )->insert ([
35
55
'name ' => 'a ' ,
36
56
37
57
]);
38
58
});
39
59
40
- $ this ->assertInstanceOf (BasePreparedQuery::class, $ query );
60
+ $ this ->assertInstanceOf (BasePreparedQuery::class, $ this -> query );
41
61
42
62
$ ec = $ this ->db ->escapeChar ;
43
63
$ pre = $ this ->db ->DBPrefix ;
@@ -54,20 +74,19 @@ public function testPrepareReturnsPreparedQuery()
54
74
} else {
55
75
$ expected = "INSERT INTO {$ ec }{$ pre }user {$ ec } ( {$ ec }name {$ ec }, {$ ec }email {$ ec }) VALUES ( {$ placeholders }) " ;
56
76
}
57
- $ this ->assertSame ($ expected , $ query ->getQueryString ());
58
77
59
- $ query ->close ( );
78
+ $ this -> assertSame ( $ expected , $ this -> query ->getQueryString () );
60
79
}
61
80
62
81
public function testPrepareReturnsManualPreparedQuery ()
63
82
{
64
- $ query = $ this ->db ->prepare (static function ($ db ) {
83
+ $ this -> query = $ this ->db ->prepare (static function ($ db ) {
65
84
$ sql = "INSERT INTO {$ db ->DBPrefix }user (name, email, country) VALUES (?, ?, ?) " ;
66
85
67
86
return (new Query ($ db ))->setQuery ($ sql );
68
87
});
69
88
70
- $ this ->assertInstanceOf (BasePreparedQuery::class, $ query );
89
+ $ this ->assertInstanceOf (BasePreparedQuery::class, $ this -> query );
71
90
72
91
$ pre = $ this ->db ->DBPrefix ;
73
92
@@ -78,33 +97,29 @@ public function testPrepareReturnsManualPreparedQuery()
78
97
}
79
98
80
99
$ 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 ());
84
101
}
85
102
86
103
public function testExecuteRunsQueryAndReturnsResultObject ()
87
104
{
88
- $ query = $ this ->db ->prepare (static function ($ db ) {
105
+ $ this -> query = $ this ->db ->prepare (static function ($ db ) {
89
106
return $ db ->table ('user ' )->insert ([
90
107
'name ' => 'a ' ,
91
108
92
109
'country ' => 'x ' ,
93
110
]);
94
111
});
95
112
96
- $ query->
execute (
'foo ' ,
'[email protected] ' ,
'US ' );
97
- $ query->
execute (
'bar ' ,
'[email protected] ' ,
'GB ' );
113
+ $ this -> query ->
execute (
'foo ' ,
'[email protected] ' ,
'US ' );
114
+ $ this -> query ->
execute (
'bar ' ,
'[email protected] ' ,
'GB ' );
98
115
99
116
$ this ->
seeInDatabase (
$ this ->
db ->
DBPrefix .
'user ' , [
'name ' =>
'foo ' ,
'email ' =>
'[email protected] ' ]);
100
117
$ this ->
seeInDatabase (
$ this ->
db ->
DBPrefix .
'user ' , [
'name ' =>
'bar ' ,
'email ' =>
'[email protected] ' ]);
101
-
102
- $ query ->close ();
103
118
}
104
119
105
120
public function testExecuteRunsQueryAndReturnsManualResultObject ()
106
121
{
107
- $ query = $ this ->db ->prepare (static function ($ db ) {
122
+ $ this -> query = $ this ->db ->prepare (static function ($ db ) {
108
123
$ sql = "INSERT INTO {$ db ->DBPrefix }user (name, email, country) VALUES (?, ?, ?) " ;
109
124
110
125
if ($ db ->DBDriver === 'SQLSRV ' ) {
@@ -114,12 +129,10 @@ public function testExecuteRunsQueryAndReturnsManualResultObject()
114
129
return (new Query ($ db ))->setQuery ($ sql );
115
130
});
116
131
117
- $ query->
execute (
'foo ' ,
'[email protected] ' ,
'' );
118
- $ query->
execute (
'bar ' ,
'[email protected] ' ,
'' );
132
+ $ this -> query ->
execute (
'foo ' ,
'[email protected] ' ,
'' );
133
+ $ this -> query ->
execute (
'bar ' ,
'[email protected] ' ,
'' );
119
134
120
135
$ this ->
seeInDatabase (
$ this ->
db ->
DBPrefix .
'user ' , [
'name ' =>
'foo ' ,
'email ' =>
'[email protected] ' ]);
121
136
$ this ->
seeInDatabase (
$ this ->
db ->
DBPrefix .
'user ' , [
'name ' =>
'bar ' ,
'email ' =>
'[email protected] ' ]);
122
-
123
- $ query ->close ();
124
137
}
125
138
}
0 commit comments