Skip to content

Commit 8b4ba58

Browse files
committed
Add updateBatch test
1 parent 8f401de commit 8b4ba58

File tree

1 file changed

+91
-0
lines changed

1 file changed

+91
-0
lines changed

tests/system/Database/Live/UpdateTest.php

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace CodeIgniter\Database\Live;
1313

1414
use CodeIgniter\Database\Exceptions\DatabaseException;
15+
use CodeIgniter\Database\Forge;
1516
use CodeIgniter\Database\RawSql;
1617
use CodeIgniter\Test\CIUnitTestCase;
1718
use CodeIgniter\Test\DatabaseTestTrait;
@@ -27,6 +28,11 @@ final class UpdateTest extends CIUnitTestCase
2728
{
2829
use DatabaseTestTrait;
2930

31+
/**
32+
* @var Forge|mixed
33+
*/
34+
public $forge;
35+
3036
protected $refresh = true;
3137
protected $seed = CITestSeeder::class;
3238

@@ -496,4 +502,89 @@ public function testNoConstraintFound()
496502
$this->db->table('job')
497503
->updateBatch($jobData);
498504
}
505+
506+
public function testUpdateBatchWithQuery()
507+
{
508+
$this->forge = Database::forge($this->DBGroup);
509+
510+
$this->forge->dropTable('user2', true);
511+
512+
$this->forge->addField([
513+
'id' => ['type' => 'INTEGER', 'constraint' => 3, 'auto_increment' => true],
514+
'name' => ['type' => 'VARCHAR', 'constraint' => 80],
515+
'email' => ['type' => 'VARCHAR', 'constraint' => 100],
516+
'country' => ['type' => 'VARCHAR', 'constraint' => 40],
517+
'created_at' => ['type' => 'DATETIME', 'null' => true],
518+
'updated_at' => ['type' => 'DATETIME', 'null' => true],
519+
'deleted_at' => ['type' => 'DATETIME', 'null' => true],
520+
'last_loggin' => ['type' => 'DATETIME', 'null' => true],
521+
])->addKey('id', true)->addUniqueKey('email')->addKey('country')->createTable('user2', true);
522+
523+
$data = [
524+
[
525+
'name' => 'Derek Jones user2',
526+
'email' => '[email protected]',
527+
'country' => 'France',
528+
],
529+
[
530+
'name' => 'Ahmadinejad user2',
531+
'email' => '[email protected]',
532+
'country' => 'Greece',
533+
],
534+
[
535+
'name' => 'Richard A Causey user2',
536+
'email' => '[email protected]',
537+
'country' => 'France',
538+
],
539+
[
540+
'name' => 'Chris Martin user2',
541+
'email' => '[email protected]',
542+
'country' => 'Greece',
543+
],
544+
[
545+
'name' => 'New User user2',
546+
'email' => '[email protected]',
547+
'country' => 'US',
548+
],
549+
[
550+
'name' => 'New User2 user2',
551+
'email' => '[email protected]',
552+
'country' => 'US',
553+
],
554+
];
555+
$this->db->table('user2')->insertBatch($data);
556+
557+
if ($this->db->DBDriver === 'SQLite3' && ! (version_compare($this->db->getVersion(), '3.33.0') >= 0)) {
558+
$this->markTestSkipped('Only SQLite 3.33 and newer can complete this test.');
559+
}
560+
561+
$updateFields = ['country', 'updated_at' => new RawSql('CURRENT_TIMESTAMP')];
562+
563+
$subQuery = $this->db->table('user2')
564+
->select('email, country')
565+
->where('country', 'France');
566+
567+
$affectedRows = $this->db->table('user')
568+
->updateFields($updateFields, true)
569+
->updateBatch($subQuery, 'email');
570+
571+
$this->assertSame(2, (int) $affectedRows);
572+
573+
$this->seeInDatabase('user', ['name' => 'Derek Jones', 'country' => 'France']);
574+
$this->seeInDatabase('user', ['name' => 'Ahmadinejad', 'country' => 'Iran']);
575+
$this->seeInDatabase('user', ['name' => 'Richard A Causey', 'country' => 'France']);
576+
$this->seeInDatabase('user', ['name' => 'Chris Martin', 'country' => 'UK']);
577+
578+
$result = $this->db->table('user')->get()->getResultArray();
579+
580+
foreach ($result as $row) {
581+
if ($row['email'] === '[email protected]' || $row['email'] === '[email protected]') {
582+
$this->assertNotNull($row['updated_at']);
583+
} else {
584+
$this->assertNull($row['updated_at']);
585+
}
586+
}
587+
588+
$this->forge->dropTable('user2', true);
589+
}
499590
}

0 commit comments

Comments
 (0)