Skip to content

Commit 1b665ab

Browse files
committed
Add InsertBatch() test
1 parent afede47 commit 1b665ab

File tree

1 file changed

+65
-0
lines changed

1 file changed

+65
-0
lines changed

tests/system/Database/Live/InsertTest.php

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use CodeIgniter\Test\CIUnitTestCase;
1515
use CodeIgniter\Test\DatabaseTestTrait;
16+
use Config\Database;
1617
use Tests\Support\Database\Seeds\CITestSeeder;
1718

1819
/**
@@ -116,4 +117,68 @@ public function testInsertPasswordHash()
116117

117118
$this->seeInDatabase('misc', ['value' => $hash]);
118119
}
120+
121+
public function testInsertWithQuery()
122+
{
123+
$this->forge = Database::forge($this->DBGroup);
124+
125+
$this->forge->dropTable('user2', true);
126+
127+
$this->forge->addField([
128+
'id' => ['type' => 'INTEGER', 'constraint' => 3, 'auto_increment' => true],
129+
'name' => ['type' => 'VARCHAR', 'constraint' => 80],
130+
'email' => ['type' => 'VARCHAR', 'constraint' => 100],
131+
'country' => ['type' => 'VARCHAR', 'constraint' => 40],
132+
'created_at' => ['type' => 'DATETIME', 'null' => true],
133+
'updated_at' => ['type' => 'DATETIME', 'null' => true],
134+
'deleted_at' => ['type' => 'DATETIME', 'null' => true],
135+
'last_loggin' => ['type' => 'DATETIME', 'null' => true],
136+
])->addKey('id', true)->addUniqueKey('email')->addKey('country')->createTable('user2', true);
137+
138+
$data = [
139+
[
140+
'name' => 'Derek Jones user2',
141+
'email' => '[email protected]',
142+
'country' => 'France',
143+
],
144+
[
145+
'name' => 'Ahmadinejad user2',
146+
'email' => '[email protected]',
147+
'country' => 'Greece',
148+
],
149+
[
150+
'name' => 'Richard A Causey user2',
151+
'email' => '[email protected]',
152+
'country' => 'France',
153+
],
154+
[
155+
'name' => 'Chris Martin user2',
156+
'email' => '[email protected]',
157+
'country' => 'Greece',
158+
],
159+
[
160+
'name' => 'New User user2',
161+
'email' => '[email protected]',
162+
'country' => 'US',
163+
],
164+
[
165+
'name' => 'New User2 user2',
166+
'email' => '[email protected]',
167+
'country' => 'US',
168+
],
169+
];
170+
$this->db->table('user2')->insertBatch($data);
171+
172+
$subQuery = $this->db->table('user2')
173+
->select('user2.name, user2.email, user2.country')
174+
->join('user', 'user.email = user2.email', 'left')
175+
->where('user.email IS NULL');
176+
177+
$this->db->table('user')->insertBatch($subQuery);
178+
179+
$this->seeInDatabase('user', ['name' => 'New User user2']);
180+
$this->seeInDatabase('user', ['name' => 'New User2 user2']);
181+
182+
$this->forge->dropTable('user2', true);
183+
}
119184
}

0 commit comments

Comments
 (0)