|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +/** |
| 5 | + * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org) |
| 6 | + * |
| 7 | + * Licensed under The MIT License |
| 8 | + * Redistributions of files must retain the above copyright notice. |
| 9 | + * |
| 10 | + * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org) |
| 11 | + * @link https://cakephp.org CakePHP(tm) Project |
| 12 | + * @license https://www.opensource.org/licenses/mit-license.php MIT License |
| 13 | + */ |
| 14 | +namespace Migrations\Test\TestCase\Command; |
| 15 | + |
| 16 | +use Cake\Database\Schema\TableSchema; |
| 17 | +use Migrations\Command\BakeMigrationDiffCommand; |
| 18 | +use Migrations\Test\TestCase\TestCase; |
| 19 | +use ReflectionClass; |
| 20 | +use ReflectionMethod; |
| 21 | + |
| 22 | +/** |
| 23 | + * Unit tests for BakeMigrationDiffCommand |
| 24 | + */ |
| 25 | +class BakeMigrationDiffCommandUnitTest extends TestCase |
| 26 | +{ |
| 27 | + /** |
| 28 | + * Test that decimal column changes generate correct precision and scale |
| 29 | + * |
| 30 | + * This tests the fix for https://github.com/cakephp/migrations/issues/659 |
| 31 | + * |
| 32 | + * @return void |
| 33 | + */ |
| 34 | + public function testDecimalColumnChangesUsePrecisionAndScale(): void |
| 35 | + { |
| 36 | + // Create mock schemas |
| 37 | + $oldSchema = new TableSchema('products'); |
| 38 | + $oldSchema->addColumn('id', ['type' => 'integer', 'autoIncrement' => true]); |
| 39 | + $oldSchema->addColumn('price', [ |
| 40 | + 'type' => 'decimal', |
| 41 | + 'length' => 4, |
| 42 | + 'precision' => 2, |
| 43 | + 'null' => false, |
| 44 | + 'default' => null, |
| 45 | + ]); |
| 46 | + $oldSchema->addConstraint('primary', ['type' => 'primary', 'columns' => ['id']]); |
| 47 | + |
| 48 | + $currentSchema = new TableSchema('products'); |
| 49 | + $currentSchema->addColumn('id', ['type' => 'integer', 'autoIncrement' => true]); |
| 50 | + $currentSchema->addColumn('price', [ |
| 51 | + 'type' => 'decimal', |
| 52 | + 'length' => 6, // Changed from 4 to 6 |
| 53 | + 'precision' => 2, |
| 54 | + 'null' => false, |
| 55 | + 'default' => null, |
| 56 | + ]); |
| 57 | + $currentSchema->addConstraint('primary', ['type' => 'primary', 'columns' => ['id']]); |
| 58 | + |
| 59 | + // Set up the command |
| 60 | + $command = new BakeMigrationDiffCommand(); |
| 61 | + |
| 62 | + // Use reflection to set protected properties |
| 63 | + $reflection = new ReflectionClass($command); |
| 64 | + |
| 65 | + $dumpSchemaProperty = $reflection->getProperty('dumpSchema'); |
| 66 | + $dumpSchemaProperty->setAccessible(true); |
| 67 | + $dumpSchemaProperty->setValue($command, ['products' => $oldSchema]); |
| 68 | + |
| 69 | + $currentSchemaProperty = $reflection->getProperty('currentSchema'); |
| 70 | + $currentSchemaProperty->setAccessible(true); |
| 71 | + $currentSchemaProperty->setValue($command, ['products' => $currentSchema]); |
| 72 | + |
| 73 | + $commonTablesProperty = $reflection->getProperty('commonTables'); |
| 74 | + $commonTablesProperty->setAccessible(true); |
| 75 | + $commonTablesProperty->setValue($command, ['products' => $currentSchema]); |
| 76 | + |
| 77 | + $templateDataProperty = $reflection->getProperty('templateData'); |
| 78 | + $templateDataProperty->setAccessible(true); |
| 79 | + $templateDataProperty->setValue($command, []); |
| 80 | + |
| 81 | + // Call the protected getColumns method |
| 82 | + $getColumnsMethod = $reflection->getMethod('getColumns'); |
| 83 | + $getColumnsMethod->setAccessible(true); |
| 84 | + $getColumnsMethod->invoke($command); |
| 85 | + |
| 86 | + // Get the template data |
| 87 | + $templateData = $templateDataProperty->getValue($command); |
| 88 | + |
| 89 | + // Assert that the decimal column change has precision and scale, not limit |
| 90 | + $this->assertArrayHasKey('products', $templateData); |
| 91 | + $this->assertArrayHasKey('columns', $templateData['products']); |
| 92 | + $this->assertArrayHasKey('changed', $templateData['products']['columns']); |
| 93 | + $this->assertArrayHasKey('price', $templateData['products']['columns']['changed']); |
| 94 | + |
| 95 | + $priceChanges = $templateData['products']['columns']['changed']['price']; |
| 96 | + |
| 97 | + // Should have precision and scale |
| 98 | + $this->assertArrayHasKey('precision', $priceChanges, 'Decimal column should have precision'); |
| 99 | + $this->assertArrayHasKey('scale', $priceChanges, 'Decimal column should have scale'); |
| 100 | + $this->assertEquals(6, $priceChanges['precision'], 'Precision should be 6 (the total digits)'); |
| 101 | + $this->assertEquals(2, $priceChanges['scale'], 'Scale should be 2 (the decimal places)'); |
| 102 | + |
| 103 | + // Should NOT have length or limit |
| 104 | + $this->assertArrayNotHasKey('length', $priceChanges, 'Decimal column should not have length'); |
| 105 | + $this->assertArrayNotHasKey('limit', $priceChanges, 'Decimal column should not have limit'); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * Test that non-decimal column changes use limit (not precision/scale) |
| 110 | + * |
| 111 | + * @return void |
| 112 | + */ |
| 113 | + public function testNonDecimalColumnChangesUseLimit(): void |
| 114 | + { |
| 115 | + // Create mock schemas |
| 116 | + $oldSchema = new TableSchema('products'); |
| 117 | + $oldSchema->addColumn('id', ['type' => 'integer', 'autoIncrement' => true]); |
| 118 | + $oldSchema->addColumn('name', [ |
| 119 | + 'type' => 'string', |
| 120 | + 'length' => 100, |
| 121 | + 'null' => false, |
| 122 | + 'default' => null, |
| 123 | + ]); |
| 124 | + $oldSchema->addConstraint('primary', ['type' => 'primary', 'columns' => ['id']]); |
| 125 | + |
| 126 | + $currentSchema = new TableSchema('products'); |
| 127 | + $currentSchema->addColumn('id', ['type' => 'integer', 'autoIncrement' => true]); |
| 128 | + $currentSchema->addColumn('name', [ |
| 129 | + 'type' => 'string', |
| 130 | + 'length' => 255, // Changed from 100 to 255 |
| 131 | + 'null' => false, |
| 132 | + 'default' => null, |
| 133 | + ]); |
| 134 | + $currentSchema->addConstraint('primary', ['type' => 'primary', 'columns' => ['id']]); |
| 135 | + |
| 136 | + // Set up the command |
| 137 | + $command = new BakeMigrationDiffCommand(); |
| 138 | + |
| 139 | + // Use reflection to set protected properties |
| 140 | + $reflection = new ReflectionClass($command); |
| 141 | + |
| 142 | + $dumpSchemaProperty = $reflection->getProperty('dumpSchema'); |
| 143 | + $dumpSchemaProperty->setAccessible(true); |
| 144 | + $dumpSchemaProperty->setValue($command, ['products' => $oldSchema]); |
| 145 | + |
| 146 | + $currentSchemaProperty = $reflection->getProperty('currentSchema'); |
| 147 | + $currentSchemaProperty->setAccessible(true); |
| 148 | + $currentSchemaProperty->setValue($command, ['products' => $currentSchema]); |
| 149 | + |
| 150 | + $commonTablesProperty = $reflection->getProperty('commonTables'); |
| 151 | + $commonTablesProperty->setAccessible(true); |
| 152 | + $commonTablesProperty->setValue($command, ['products' => $currentSchema]); |
| 153 | + |
| 154 | + $templateDataProperty = $reflection->getProperty('templateData'); |
| 155 | + $templateDataProperty->setAccessible(true); |
| 156 | + $templateDataProperty->setValue($command, []); |
| 157 | + |
| 158 | + // Call the protected getColumns method |
| 159 | + $getColumnsMethod = $reflection->getMethod('getColumns'); |
| 160 | + $getColumnsMethod->setAccessible(true); |
| 161 | + $getColumnsMethod->invoke($command); |
| 162 | + |
| 163 | + // Get the template data |
| 164 | + $templateData = $templateDataProperty->getValue($command); |
| 165 | + |
| 166 | + // Assert that the string column change has limit, not precision/scale |
| 167 | + $this->assertArrayHasKey('products', $templateData); |
| 168 | + $this->assertArrayHasKey('columns', $templateData['products']); |
| 169 | + $this->assertArrayHasKey('changed', $templateData['products']['columns']); |
| 170 | + $this->assertArrayHasKey('name', $templateData['products']['columns']['changed']); |
| 171 | + |
| 172 | + $nameChanges = $templateData['products']['columns']['changed']['name']; |
| 173 | + |
| 174 | + // Should have limit |
| 175 | + $this->assertArrayHasKey('limit', $nameChanges, 'String column should have limit'); |
| 176 | + $this->assertEquals(255, $nameChanges['limit'], 'Limit should be 255'); |
| 177 | + |
| 178 | + // Should NOT have length, precision, or scale |
| 179 | + $this->assertArrayNotHasKey('length', $nameChanges, 'String column should not have length'); |
| 180 | + $this->assertArrayNotHasKey('precision', $nameChanges, 'String column should not have precision'); |
| 181 | + $this->assertArrayNotHasKey('scale', $nameChanges, 'String column should not have scale'); |
| 182 | + } |
| 183 | + |
| 184 | + /** |
| 185 | + * Test that decimal columns with only scale change are handled correctly |
| 186 | + * |
| 187 | + * @return void |
| 188 | + */ |
| 189 | + public function testDecimalColumnScaleChangeOnly(): void |
| 190 | + { |
| 191 | + // Create mock schemas |
| 192 | + $oldSchema = new TableSchema('products'); |
| 193 | + $oldSchema->addColumn('id', ['type' => 'integer', 'autoIncrement' => true]); |
| 194 | + $oldSchema->addColumn('price', [ |
| 195 | + 'type' => 'decimal', |
| 196 | + 'length' => 6, |
| 197 | + 'precision' => 2, |
| 198 | + 'null' => false, |
| 199 | + 'default' => null, |
| 200 | + ]); |
| 201 | + $oldSchema->addConstraint('primary', ['type' => 'primary', 'columns' => ['id']]); |
| 202 | + |
| 203 | + $currentSchema = new TableSchema('products'); |
| 204 | + $currentSchema->addColumn('id', ['type' => 'integer', 'autoIncrement' => true]); |
| 205 | + $currentSchema->addColumn('price', [ |
| 206 | + 'type' => 'decimal', |
| 207 | + 'length' => 6, // Same |
| 208 | + 'precision' => 3, // Changed from 2 to 3 |
| 209 | + 'null' => false, |
| 210 | + 'default' => null, |
| 211 | + ]); |
| 212 | + $currentSchema->addConstraint('primary', ['type' => 'primary', 'columns' => ['id']]); |
| 213 | + |
| 214 | + // Set up the command |
| 215 | + $command = new BakeMigrationDiffCommand(); |
| 216 | + |
| 217 | + // Use reflection to set protected properties |
| 218 | + $reflection = new ReflectionClass($command); |
| 219 | + |
| 220 | + $dumpSchemaProperty = $reflection->getProperty('dumpSchema'); |
| 221 | + $dumpSchemaProperty->setAccessible(true); |
| 222 | + $dumpSchemaProperty->setValue($command, ['products' => $oldSchema]); |
| 223 | + |
| 224 | + $currentSchemaProperty = $reflection->getProperty('currentSchema'); |
| 225 | + $currentSchemaProperty->setAccessible(true); |
| 226 | + $currentSchemaProperty->setValue($command, ['products' => $currentSchema]); |
| 227 | + |
| 228 | + $commonTablesProperty = $reflection->getProperty('commonTables'); |
| 229 | + $commonTablesProperty->setAccessible(true); |
| 230 | + $commonTablesProperty->setValue($command, ['products' => $currentSchema]); |
| 231 | + |
| 232 | + $templateDataProperty = $reflection->getProperty('templateData'); |
| 233 | + $templateDataProperty->setAccessible(true); |
| 234 | + $templateDataProperty->setValue($command, []); |
| 235 | + |
| 236 | + // Call the protected getColumns method |
| 237 | + $getColumnsMethod = $reflection->getMethod('getColumns'); |
| 238 | + $getColumnsMethod->setAccessible(true); |
| 239 | + $getColumnsMethod->invoke($command); |
| 240 | + |
| 241 | + // Get the template data |
| 242 | + $templateData = $templateDataProperty->getValue($command); |
| 243 | + |
| 244 | + // Assert that the decimal column change has precision and scale |
| 245 | + $this->assertArrayHasKey('products', $templateData); |
| 246 | + $this->assertArrayHasKey('columns', $templateData['products']); |
| 247 | + $this->assertArrayHasKey('changed', $templateData['products']['columns']); |
| 248 | + $this->assertArrayHasKey('price', $templateData['products']['columns']['changed']); |
| 249 | + |
| 250 | + $priceChanges = $templateData['products']['columns']['changed']['price']; |
| 251 | + |
| 252 | + // Should have precision (same as before) and scale (new value) |
| 253 | + $this->assertArrayHasKey('precision', $priceChanges, 'Decimal column should have precision'); |
| 254 | + $this->assertArrayHasKey('scale', $priceChanges, 'Decimal column should have scale'); |
| 255 | + $this->assertEquals(6, $priceChanges['precision'], 'Precision should be 6'); |
| 256 | + $this->assertEquals(3, $priceChanges['scale'], 'Scale should be 3 (changed)'); |
| 257 | + } |
| 258 | +} |
0 commit comments