diff --git a/README.md b/README.md index ec5658434..e9bcc2ecf 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ # [Phinx](https://phinx.org): Simple PHP Database Migrations -[![Build Status](https://github.com/cakephp/phinx/workflows/CI/badge.svg?branch=0.x&event=push)](https://github.com/cakephp/phinx/actions?query=workflow%3A%22CI%22+branch%3A0.x+event%3Apush) +[![CI](https://github.com/cakephp/phinx/actions/workflows/ci.yml/badge.svg?branch=0.x&event=push)](https://github.com/cakephp/phinx/actions/workflows/ci.yml) [![Code Coverage](https://codecov.io/gh/cakephp/phinx/branch/master/graph/badge.svg)](https://codecov.io/gh/cakephp/phinx) ![Packagist Version](https://img.shields.io/packagist/v/robmorgan/phinx) [![Minimum PHP Version](https://img.shields.io/badge/php-%3E%3D%208.1-8892BF.svg)](https://php.net/) diff --git a/src/Phinx/Db/Adapter/PdoAdapter.php b/src/Phinx/Db/Adapter/PdoAdapter.php index d5e8aed96..46e6b0870 100644 --- a/src/Phinx/Db/Adapter/PdoAdapter.php +++ b/src/Phinx/Db/Adapter/PdoAdapter.php @@ -391,6 +391,10 @@ protected function quoteValue(mixed $value): mixed return $value; } + if (is_bool($value)) { + return $this->castToBool($value); + } + if ($value === null) { return 'null'; } @@ -399,6 +403,12 @@ protected function quoteValue(mixed $value): mixed return (string)$value; } + if ($value instanceof DateTime) { + $value = $value->toDateTimeString(); + } elseif ($value instanceof Date) { + $value = $value->toDateString(); + } + return $this->getConnection()->quote($value); } diff --git a/tests/Phinx/Db/Adapter/PdoAdapterTest.php b/tests/Phinx/Db/Adapter/PdoAdapterTest.php index dd5b7a07c..794e096a8 100644 --- a/tests/Phinx/Db/Adapter/PdoAdapterTest.php +++ b/tests/Phinx/Db/Adapter/PdoAdapterTest.php @@ -3,10 +3,14 @@ namespace Test\Phinx\Db\Adapter; +use Cake\I18n\Date; +use Cake\I18n\DateTime; use PDO; use PDOException; use Phinx\Config\Config; +use Phinx\Util\Literal; use PHPUnit\Framework\TestCase; +use ReflectionMethod; use RuntimeException; use Test\Phinx\DeprecationException; use Test\Phinx\TestUtils; @@ -203,4 +207,58 @@ public function testExecuteRightTrimsSemiColons() $this->adapter->setConnection($pdo); $this->adapter->execute('SELECT 1;;'); } + + public function quoteValueDataProvider(): array + { + return [ + [1.0, 1.0], + [2, 2], + [true, 1], + [false, 0], + [null, 'null'], + [Literal::from('CURRENT_TIMESTAMP'), 'CURRENT_TIMESTAMP'], + ]; + } + + /** + * @dataProvider quoteValueDataProvider + */ + public function testQuoteValue($input, $expected): void + { + $method = new ReflectionMethod($this->adapter, 'quoteValue'); + $this->assertSame($expected, $method->invoke($this->adapter, $input)); + } + + public function quoteValueStringDataProvider(): array + { + return [ + ['mockvalue', "'mockvalue'"], + [new Date('2023-01-01'), "'2023-01-01'"], + [new DateTime('2023-01-01 12:00:00'), "'2023-01-01 12:00:00'"], + ]; + } + + /** + * @dataProvider quoteValueStringDataProvider + */ + + public function testQuoteValueString($input, $expected): void + { + /** @var \PDO&\PHPUnit\Framework\MockObject\MockObject $pdo */ + $pdo = $this->getMockBuilder(PDO::class) + ->disableOriginalConstructor() + ->onlyMethods(['quote']) + ->getMock(); + + $pdo->expects($this->once()) + ->method('quote') + ->willReturnCallback(function (string $input) { + return "'$input'"; + }); + + $this->adapter->setConnection($pdo); + + $method = new ReflectionMethod($this->adapter, 'quoteValue'); + $this->assertSame($expected, $method->invoke($this->adapter, $input)); + } }