|
8 | 8 | use PDO; |
9 | 9 | use PDOException; |
10 | 10 | use Phinx\Config\Config; |
| 11 | +use Phinx\Db\Adapter\AdapterInterface; |
11 | 12 | use Phinx\Util\Literal; |
12 | 13 | use PHPUnit\Framework\TestCase; |
13 | 14 | use ReflectionMethod; |
@@ -261,4 +262,46 @@ public function testQuoteValueString($input, $expected): void |
261 | 262 | $method = new ReflectionMethod($this->adapter, 'quoteValue'); |
262 | 263 | $this->assertSame($expected, $method->invoke($this->adapter, $input)); |
263 | 264 | } |
| 265 | + |
| 266 | + public function defaultValueDefinitionDataProvider(): array |
| 267 | + { |
| 268 | + return [ |
| 269 | + ['some string', AdapterInterface::PHINX_TYPE_STRING, " DEFAULT 'some string'"], |
| 270 | + [123, AdapterInterface::PHINX_TYPE_INTEGER, ' DEFAULT 123'], |
| 271 | + [true, AdapterInterface::PHINX_TYPE_BOOLEAN, ' DEFAULT 1'], |
| 272 | + [false, AdapterInterface::PHINX_TYPE_BOOLEAN, ' DEFAULT 0'], |
| 273 | + [null, AdapterInterface::PHINX_TYPE_STRING, ''], |
| 274 | + [Literal::from('foo'), AdapterInterface::PHINX_TYPE_STRING, ' DEFAULT foo'], |
| 275 | + ['CURRENT_TIMESTAMP', AdapterInterface::PHINX_TYPE_STRING, " DEFAULT 'CURRENT_TIMESTAMP'"], |
| 276 | + ['CURRENT_TIMESTAMP', AdapterInterface::PHINX_TYPE_DATETIME, ' DEFAULT CURRENT_TIMESTAMP'], |
| 277 | + ['CURRENT_TIMESTAMP(3)', AdapterInterface::PHINX_TYPE_DATETIME, ' DEFAULT CURRENT_TIMESTAMP(3)'], |
| 278 | + ['CURRENT_TIMESTAMP()', AdapterInterface::PHINX_TYPE_DATETIME, ' DEFAULT CURRENT_TIMESTAMP()'], |
| 279 | + ['CURRENT_TIMESTAMP', AdapterInterface::PHINX_TYPE_TIMESTAMP, ' DEFAULT CURRENT_TIMESTAMP'], |
| 280 | + ['CURRENT_TIME', AdapterInterface::PHINX_TYPE_TIME, ' DEFAULT CURRENT_TIME'], |
| 281 | + ['CURRENT_DATE', AdapterInterface::PHINX_TYPE_DATE, ' DEFAULT CURRENT_DATE'], |
| 282 | + ['NOW', AdapterInterface::PHINX_TYPE_DATETIME, ' DEFAULT NOW'], |
| 283 | + ]; |
| 284 | + } |
| 285 | + |
| 286 | + /** |
| 287 | + * @dataProvider defaultValueDefinitionDataProvider |
| 288 | + */ |
| 289 | + public function testGetDefaultValueDefinition($input, $columnType, $expected): void |
| 290 | + { |
| 291 | + /** @var \PDO&\PHPUnit\Framework\MockObject\MockObject $pdo */ |
| 292 | + $pdo = $this->getMockBuilder(PDO::class) |
| 293 | + ->disableOriginalConstructor() |
| 294 | + ->onlyMethods(['quote']) |
| 295 | + ->getMock(); |
| 296 | + |
| 297 | + $pdo->method('quote') |
| 298 | + ->willReturnCallback(function (string $input) { |
| 299 | + return "'$input'"; |
| 300 | + }); |
| 301 | + |
| 302 | + $this->adapter->setConnection($pdo); |
| 303 | + |
| 304 | + $method = new ReflectionMethod($this->adapter, 'getDefaultValueDefinition'); |
| 305 | + $this->assertSame($expected, $method->invoke($this->adapter, $input, $columnType)); |
| 306 | + } |
264 | 307 | } |
0 commit comments