Skip to content

Commit

Permalink
Change testAssertTypeReturning to testReturning; remove dynamic $asse…
Browse files Browse the repository at this point in the history
…rtType and instead use if statement
  • Loading branch information
markconnellypro committed Mar 4, 2024
1 parent d2c6162 commit b0dd905
Showing 1 changed file with 79 additions and 53 deletions.
132 changes: 79 additions & 53 deletions tests/system/Database/Live/WriteTypeQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,17 @@ final class WriteTypeQueryTest extends CIUnitTestCase
protected $seed = CITestSeeder::class;

/**
* Whether CodeIgniter ignores RETURNING for isWriteType.
* Whether CodeIgniter considers RETURNING in isWriteType.
*
* Currently, only Postgre is supported by CodeIgniter.
* This method should be updated if support for RETURNING
* is expanded to other databases.
* is expanded for other databases.
*
* @param string $dbDriver
*/
private function testAssertTypeReturning($dbDriver): string
private function testReturning($dbDriver): bool
{
if ($dbDriver === 'Postgre') {
return 'assertFalse';
}

return 'assertTrue';
return $dbDriver === 'Postgre';
}

public function testSet(): void
Expand Down Expand Up @@ -114,9 +110,11 @@ public function testInsertOneReturning(): void
{
$sql = "INSERT INTO my_table (col1, col2) VALUES ('Joe', 'Cool') RETURNING id;";

$assertType = $this->testAssertTypeReturning($this->db->DBDriver);

$this->{$assertType}($this->db->isWriteType($sql));
if ($this->testReturning($this->db->DBDriver)) {
$this->assertFalse($this->db->isWriteType($sql));
} else {
$this->assertTrue($this->db->isWriteType($sql));
}
}

public function testInsertMultiReturning(): void
Expand All @@ -127,27 +125,33 @@ public function testInsertMultiReturning(): void
RETURNING id;
SQL;

$assertType = $this->testAssertTypeReturning($this->db->DBDriver);

$this->{$assertType}($this->db->isWriteType($sql));
if ($this->testReturning($this->db->DBDriver)) {
$this->assertFalse($this->db->isWriteType($sql));
} else {
$this->assertTrue($this->db->isWriteType($sql));
}
}

public function testInsertWithOneReturning(): void
{
$sql = "WITH seqvals AS (SELECT '3' AS seqval) INSERT INTO my_table (col1, col2) SELECT 'Joe', seqval FROM seqvals RETURNING id;";

$assertType = $this->testAssertTypeReturning($this->db->DBDriver);

$this->{$assertType}($this->db->isWriteType($sql));
if ($this->testReturning($this->db->DBDriver)) {
$this->assertFalse($this->db->isWriteType($sql));
} else {
$this->assertTrue($this->db->isWriteType($sql));
}
}

public function testInsertWithOneReturningNoSpace(): void
{
$sql = "WITH seqvals AS (SELECT '3' AS seqval)INSERT INTO my_table (col1, col2) SELECT 'Joe', seqval FROM seqvals RETURNING id;";

$assertType = $this->testAssertTypeReturning($this->db->DBDriver);

$this->{$assertType}($this->db->isWriteType($sql));
if ($this->testReturning($this->db->DBDriver)) {
$this->assertFalse($this->db->isWriteType($sql));
} else {
$this->assertTrue($this->db->isWriteType($sql));
}
}

public function testInsertWithMultiReturning(): void
Expand All @@ -160,9 +164,11 @@ public function testInsertWithMultiReturning(): void
RETURNING id;
SQL;

$assertType = $this->testAssertTypeReturning($this->db->DBDriver);

$this->{$assertType}($this->db->isWriteType($sql));
if ($this->testReturning($this->db->DBDriver)) {
$this->assertFalse($this->db->isWriteType($sql));
} else {
$this->assertTrue($this->db->isWriteType($sql));
}
}

public function testUpdateBuilder(): void
Expand Down Expand Up @@ -223,9 +229,11 @@ public function testUpdateOneReturning(): void
{
$sql = "UPDATE my_table SET col1 = 'foo' WHERE id = 2 RETURNING *;";

$assertType = $this->testAssertTypeReturning($this->db->DBDriver);

$this->{$assertType}($this->db->isWriteType($sql));
if ($this->testReturning($this->db->DBDriver)) {
$this->assertFalse($this->db->isWriteType($sql));
} else {
$this->assertTrue($this->db->isWriteType($sql));
}
}

public function testUpdateMultiReturning(): void
Expand All @@ -237,27 +245,33 @@ public function testUpdateMultiReturning(): void
RETURNING *;
SQL;

$assertType = $this->testAssertTypeReturning($this->db->DBDriver);

$this->{$assertType}($this->db->isWriteType($sql));
if ($this->testReturning($this->db->DBDriver)) {
$this->assertFalse($this->db->isWriteType($sql));
} else {
$this->assertTrue($this->db->isWriteType($sql));
}
}

public function testUpdateWithOneReturning(): void
{
$sql = "WITH seqvals AS (SELECT '3' AS seqval) UPDATE my_table SET col1 = seqval FROM seqvals WHERE id = 2 RETURNING *;";

$assertType = $this->testAssertTypeReturning($this->db->DBDriver);

$this->{$assertType}($this->db->isWriteType($sql));
if ($this->testReturning($this->db->DBDriver)) {
$this->assertFalse($this->db->isWriteType($sql));
} else {
$this->assertTrue($this->db->isWriteType($sql));
}
}

public function testUpdateWithOneReturningNoSpace(): void
{
$sql = "WITH seqvals AS (SELECT '3' AS seqval)UPDATE my_table SET col1 = seqval FROM seqvals WHERE id = 2 RETURNING *;";

$assertType = $this->testAssertTypeReturning($this->db->DBDriver);

$this->{$assertType}($this->db->isWriteType($sql));
if ($this->testReturning($this->db->DBDriver)) {
$this->assertFalse($this->db->isWriteType($sql));
} else {
$this->assertTrue($this->db->isWriteType($sql));
}
}

public function testUpdateWithMultiReturning(): void
Expand All @@ -271,9 +285,11 @@ public function testUpdateWithMultiReturning(): void
RETURNING *;
SQL;

$assertType = $this->testAssertTypeReturning($this->db->DBDriver);

$this->{$assertType}($this->db->isWriteType($sql));
if ($this->testReturning($this->db->DBDriver)) {
$this->assertFalse($this->db->isWriteType($sql));
} else {
$this->assertTrue($this->db->isWriteType($sql));
}
}

public function testDeleteBuilder(): void
Expand Down Expand Up @@ -332,9 +348,11 @@ public function testDeleteOneReturning(): void
{
$sql = 'DELETE FROM my_table WHERE id = 2 RETURNING *;';

$assertType = $this->testAssertTypeReturning($this->db->DBDriver);

$this->{$assertType}($this->db->isWriteType($sql));
if ($this->testReturning($this->db->DBDriver)) {
$this->assertFalse($this->db->isWriteType($sql));
} else {
$this->assertTrue($this->db->isWriteType($sql));
}
}

public function testDeleteMultiReturning(): void
Expand All @@ -345,27 +363,33 @@ public function testDeleteMultiReturning(): void
RETURNING *;
SQL;

$assertType = $this->testAssertTypeReturning($this->db->DBDriver);

$this->{$assertType}($this->db->isWriteType($sql));
if ($this->testReturning($this->db->DBDriver)) {
$this->assertFalse($this->db->isWriteType($sql));
} else {
$this->assertTrue($this->db->isWriteType($sql));
}
}

public function testDeleteWithOneReturning(): void
{
$sql = "WITH seqvals AS (SELECT '3' AS seqval) DELETE FROM my_table JOIN seqvals ON col1 = seqval RETURNING *;";

$assertType = $this->testAssertTypeReturning($this->db->DBDriver);

$this->{$assertType}($this->db->isWriteType($sql));
if ($this->testReturning($this->db->DBDriver)) {
$this->assertFalse($this->db->isWriteType($sql));
} else {
$this->assertTrue($this->db->isWriteType($sql));
}
}

public function testDeleteWithOneReturningNoSpace(): void
{
$sql = "WITH seqvals AS (SELECT '3' AS seqval)DELETE FROM my_table JOIN seqvals ON col1 = seqval RETURNING *;";

$assertType = $this->testAssertTypeReturning($this->db->DBDriver);

$this->{$assertType}($this->db->isWriteType($sql));
if ($this->testReturning($this->db->DBDriver)) {
$this->assertFalse($this->db->isWriteType($sql));
} else {
$this->assertTrue($this->db->isWriteType($sql));
}
}

public function testDeleteWithMultiReturning(): void
Expand All @@ -379,9 +403,11 @@ public function testDeleteWithMultiReturning(): void
RETURNING *;
SQL;

$assertType = $this->testAssertTypeReturning($this->db->DBDriver);

$this->{$assertType}($this->db->isWriteType($sql));
if ($this->testReturning($this->db->DBDriver)) {
$this->assertFalse($this->db->isWriteType($sql));
} else {
$this->assertTrue($this->db->isWriteType($sql));
}
}

public function testReplace(): void
Expand Down

0 comments on commit b0dd905

Please sign in to comment.