Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions tests/TestCase/Db/Adapter/MysqlAdapterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1126,6 +1126,7 @@ public static function columnsProvider()
['column6', 'float', []],
['column7', 'decimal', []],
['decimal_precision_scale', 'decimal', ['precision' => 10, 'scale' => 2]],
['decimal_precision_scale_zero', 'decimal', ['precision' => 65, 'scale' => 0]],
['decimal_limit', 'decimal', ['limit' => 10]],
['decimal_precision', 'decimal', ['precision' => 10]],
['column8', 'datetime', []],
Expand Down Expand Up @@ -2410,4 +2411,48 @@ public function testCheckConstraintWithComplexExpression()
$this->expectException(PDOException::class);
$this->adapter->execute("INSERT INTO {$quotedTableName} (email, status) VALUES ('[email protected]', 'invalid')");
}

/**
* Test that DECIMAL columns with scale=0 work correctly.
*
* This tests the fix for https://github.com/cakephp/phinx/pull/2377
* In phinx, the boolean check `$column->getPrecision() && $column->getScale()`
* would fail when scale is 0 because 0 is falsy in PHP.
*
* The 5.x branch uses CakePHP's database layer instead of phinx,
* so we need to verify it handles scale=0 correctly.
*/
public function testDecimalWithScaleZero()
{
// Create table with DECIMAL(65,0)
$table = new Table('decimal_scale_zero_test', [], $this->adapter);
$table->addColumn('amount', 'decimal', ['precision' => 65, 'scale' => 0])
->create();

// Verify the column was created with correct precision and scale
$columns = $this->adapter->getColumns('decimal_scale_zero_test');
$amountColumn = null;
foreach ($columns as $column) {
if ($column->getName() === 'amount') {
$amountColumn = $column;
break;
}
}

$this->assertNotNull($amountColumn, 'Amount column should exist');
$this->assertEquals('decimal', $amountColumn->getType());
$this->assertEquals(65, $amountColumn->getPrecision());
$this->assertEquals(0, $amountColumn->getScale(), 'Scale should be 0, not null');

// Verify the actual MySQL column definition
$result = $this->adapter->fetchRow('SHOW CREATE TABLE `decimal_scale_zero_test`');
$createTableSql = $result['Create Table'];

// The CREATE TABLE should contain DECIMAL(65,0) - case insensitive
$this->assertMatchesRegularExpression(
'/decimal\(65,0\)/i',
$createTableSql,
'CREATE TABLE should contain DECIMAL(65,0) with scale=0 properly defined',
);
Comment on lines +2452 to +2456
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This feels like an assertion that may become brittle over time. We can try it and see how it goes. I think this kind of assertion is better done in cakephp/database as that is where the implementation lives.

}
}
Loading