Skip to content

Commit ac69958

Browse files
committed
Create RawSqlTest.php
1 parent e5095fc commit ac69958

File tree

1 file changed

+201
-0
lines changed

1 file changed

+201
-0
lines changed
Lines changed: 201 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,201 @@
1+
<?php
2+
3+
/**
4+
* This file is part of CodeIgniter 4 framework.
5+
*
6+
* (c) CodeIgniter Foundation <[email protected]>
7+
*
8+
* For the full copyright and license information, please view
9+
* the LICENSE file that was distributed with this source code.
10+
*/
11+
12+
namespace CodeIgniter\Database\Live\MySQLi;
13+
14+
use CodeIgniter\Database\RawSql;
15+
use CodeIgniter\Test\CIUnitTestCase;
16+
use CodeIgniter\Test\DatabaseTestTrait;
17+
use stdclass;
18+
use Tests\Support\Database\Seeds\CITestSeeder;
19+
20+
/**
21+
* @group DatabaseLive
22+
*
23+
* @internal
24+
*/
25+
final class RawSqlTest extends CIUnitTestCase
26+
{
27+
use DatabaseTestTrait;
28+
29+
protected $refresh = true;
30+
protected $seed = CITestSeeder::class;
31+
32+
protected function setUp(): void
33+
{
34+
parent::setUp();
35+
36+
if ($this->db->DBDriver !== 'MySQLi') {
37+
$this->markTestSkipped('Only MySQLi has its own implementation.');
38+
} else {
39+
$this->addSqlFunction();
40+
}
41+
}
42+
43+
protected function addSqlFunction()
44+
{
45+
$this->db->query('DROP FUNCTION IF EXISTS setDateTime');
46+
47+
$sql = "CREATE FUNCTION setDateTime ( setDate varchar(20) )
48+
RETURNS DATETIME
49+
BEGIN
50+
RETURN CONVERT(CONCAT(setDate,' ','01:01:11'), DATETIME);
51+
END;";
52+
53+
$this->db->query($sql);
54+
}
55+
56+
public function testRawSqlUpdateObject()
57+
{
58+
$data = [];
59+
60+
$row = new stdclass();
61+
$row->email = '[email protected]';
62+
$row->created_at = new RawSql("setDateTime('2022-01-01')");
63+
$data[] = $row;
64+
65+
$row = new stdclass();
66+
$row->email = '[email protected]';
67+
$row->created_at = new RawSql("setDateTime('2022-01-01')");
68+
$data[] = $row;
69+
70+
$this->db->table('user')->updateBatch($data, 'email');
71+
72+
$row->created_at = new RawSql("setDateTime('2022-01-11')");
73+
74+
$this->db->table('user')->update($row, "email = '[email protected]'");
75+
76+
$this->seeInDatabase('user', ['email' => '[email protected]', 'created_at' => '2022-01-01 01:01:11']);
77+
$this->seeInDatabase('user', ['email' => '[email protected]', 'created_at' => '2022-01-11 01:01:11']);
78+
}
79+
80+
public function testRawSqlSetUpdateObject()
81+
{
82+
$data = [];
83+
84+
$row = new stdclass();
85+
$row->email = '[email protected]';
86+
$row->created_at = new RawSql("setDateTime('2022-02-01')");
87+
$data[] = $row;
88+
89+
$row = new stdclass();
90+
$row->email = '[email protected]';
91+
$row->created_at = new RawSql("setDateTime('2022-02-01')");
92+
$data[] = $row;
93+
94+
$this->db->table('user')->setUpdateBatch($data, 'email')->updateBatch(null, 'email');
95+
96+
$row->created_at = new RawSql("setDateTime('2022-02-11')");
97+
98+
$this->db->table('user')->set($row)->update(null, "email = '[email protected]'");
99+
100+
$this->seeInDatabase('user', ['email' => '[email protected]', 'created_at' => '2022-02-01 01:01:11']);
101+
$this->seeInDatabase('user', ['email' => '[email protected]', 'created_at' => '2022-02-11 01:01:11']);
102+
}
103+
104+
public function testRawSqlUpdateArray()
105+
{
106+
$data = [
107+
['email' => '[email protected]', 'created_at' => new RawSql("setDateTime('2022-03-01')")],
108+
['email' => '[email protected]', 'created_at' => new RawSql("setDateTime('2022-03-01')")],
109+
];
110+
111+
$this->db->table('user')->updateBatch($data, 'email');
112+
113+
$this->seeInDatabase('user', ['email' => '[email protected]', 'created_at' => '2022-03-01 01:01:11']);
114+
$this->seeInDatabase('user', ['email' => '[email protected]', 'created_at' => '2022-03-01 01:01:11']);
115+
116+
$data = ['email' => '[email protected]', 'created_at' => new RawSql("setDateTime('2022-03-11')")];
117+
118+
$this->db->table('user')->update($data, "email = '[email protected]'");
119+
120+
$this->seeInDatabase('user', ['email' => '[email protected]', 'created_at' => '2022-03-11 01:01:11']);
121+
}
122+
123+
public function testRawSqlInsertArray()
124+
{
125+
$data = [
126+
['email' => '[email protected]', 'created_at' => new RawSql("setDateTime('2022-04-01')")],
127+
['email' => '[email protected]', 'created_at' => new RawSql("setDateTime('2022-04-01')")],
128+
];
129+
130+
$this->db->table('user')->insertBatch($data);
131+
132+
$this->seeInDatabase('user', ['email' => '[email protected]', 'created_at' => '2022-04-01 01:01:11']);
133+
$this->seeInDatabase('user', ['email' => '[email protected]', 'created_at' => '2022-04-01 01:01:11']);
134+
135+
$data = ['email' => '[email protected]', 'created_at' => new RawSql("setDateTime('2022-04-11')")];
136+
137+
$this->db->table('user')->insert($data);
138+
139+
$this->seeInDatabase('user', ['email' => '[email protected]', 'created_at' => '2022-04-11 01:01:11']);
140+
}
141+
142+
public function testRawSqlInsertObject()
143+
{
144+
$data = [];
145+
146+
$row = new stdclass();
147+
$row->email = '[email protected]';
148+
$row->created_at = new RawSql("setDateTime('2022-05-01')");
149+
$data[] = $row;
150+
151+
$row = new stdclass();
152+
$row->email = '[email protected]';
153+
$row->created_at = new RawSql("setDateTime('2022-05-01')");
154+
$data[] = $row;
155+
156+
$this->db->table('user')->insertBatch($data);
157+
158+
$row->email = '[email protected]';
159+
$row->created_at = new RawSql("setDateTime('2022-05-11')");
160+
161+
$this->db->table('user')->insert($row);
162+
163+
$this->seeInDatabase('user', ['email' => '[email protected]', 'created_at' => '2022-05-01 01:01:11']);
164+
$this->seeInDatabase('user', ['email' => '[email protected]', 'created_at' => '2022-05-01 01:01:11']);
165+
$this->seeInDatabase('user', ['email' => '[email protected]', 'created_at' => '2022-05-11 01:01:11']);
166+
}
167+
168+
public function testRawSqlSetInsertObject()
169+
{
170+
$data = [];
171+
172+
$row = new stdclass();
173+
$row->email = '[email protected]';
174+
$row->created_at = new RawSql("setDateTime('2022-06-01')");
175+
$data[] = $row;
176+
177+
$row = new stdclass();
178+
$row->email = '[email protected]';
179+
$row->created_at = new RawSql("setDateTime('2022-06-01')");
180+
$data[] = $row;
181+
182+
$this->db->table('user')->setInsertBatch($data)->insertBatch();
183+
184+
$this->seeInDatabase('user', ['email' => '[email protected]', 'created_at' => '2022-06-01 01:01:11']);
185+
$this->seeInDatabase('user', ['email' => '[email protected]', 'created_at' => '2022-06-01 01:01:11']);
186+
187+
$row->email = '[email protected]';
188+
$row->created_at = new RawSql("setDateTime('2022-06-11')");
189+
190+
$this->db->table('user')->set($row)->insert();
191+
192+
$this->seeInDatabase('user', ['email' => '[email protected]', 'created_at' => '2022-06-11 01:01:11']);
193+
194+
$this->db->table('user')
195+
->set('email', '[email protected]')
196+
->set('created_at', new RawSql("setDateTime('2022-06-13')"))
197+
->insert();
198+
199+
$this->seeInDatabase('user', ['email' => '[email protected]', 'created_at' => '2022-06-13 01:01:11']);
200+
}
201+
}

0 commit comments

Comments
 (0)