From 842c374c6fe93c9ca29f14dfe68996c2f1016479 Mon Sep 17 00:00:00 2001 From: Matthew Peveler Date: Thu, 22 May 2025 11:05:07 -0400 Subject: [PATCH 1/3] Fix build status README badge (#2356) --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) 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/) From 198b6ca44664f90c8f4175f097604bdff0f823ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Julien=20Chav=C3=A9e?= Date: Sun, 25 May 2025 17:22:02 +0200 Subject: [PATCH 2/3] Fix --dry-run option when values to insert are booleans (#2358) --- src/Phinx/Db/Adapter/PdoAdapter.php | 4 +++ tests/Phinx/Db/Adapter/PdoAdapterTest.php | 43 +++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/src/Phinx/Db/Adapter/PdoAdapter.php b/src/Phinx/Db/Adapter/PdoAdapter.php index d5e8aed96..9f8c62f10 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'; } diff --git a/tests/Phinx/Db/Adapter/PdoAdapterTest.php b/tests/Phinx/Db/Adapter/PdoAdapterTest.php index dd5b7a07c..2e894c001 100644 --- a/tests/Phinx/Db/Adapter/PdoAdapterTest.php +++ b/tests/Phinx/Db/Adapter/PdoAdapterTest.php @@ -7,6 +7,7 @@ use PDOException; use Phinx\Config\Config; use PHPUnit\Framework\TestCase; +use ReflectionMethod; use RuntimeException; use Test\Phinx\DeprecationException; use Test\Phinx\TestUtils; @@ -203,4 +204,46 @@ public function testExecuteRightTrimsSemiColons() $this->adapter->setConnection($pdo); $this->adapter->execute('SELECT 1;;'); } + + public function testQuoteValueNumeric() + { + $method = new ReflectionMethod($this->adapter, 'quoteValue'); + $this->assertSame(1.0, $method->invoke($this->adapter, 1.0)); + $this->assertSame(2, $method->invoke($this->adapter, 2)); + } + + public function testQuoteValueBoolean() + { + $method = new ReflectionMethod($this->adapter, 'quoteValue'); + $this->assertSame(1, $method->invoke($this->adapter, true)); + $this->assertSame(0, $method->invoke($this->adapter, false)); + } + + public function testQuoteValueNull() + { + $method = new ReflectionMethod($this->adapter, 'quoteValue'); + $this->assertSame('null', $method->invoke($this->adapter, null)); + } + + public function testQuoteValueString() + { + $mockValue = 'mockvalue'; + $expectedValue = 'mockvalueexpected'; + + /** @var \PDO&\PHPUnit\Framework\MockObject\MockObject $pdo */ + $pdo = $this->getMockBuilder(PDO::class) + ->disableOriginalConstructor() + ->onlyMethods(['quote']) + ->getMock(); + + $pdo->expects($this->once()) + ->method('quote') + ->with($mockValue) + ->willReturn($expectedValue); + + $this->adapter->setConnection($pdo); + + $method = new ReflectionMethod($this->adapter, 'quoteValue'); + $this->assertSame($expectedValue, $method->invoke($this->adapter, $mockValue)); + } } From 524ebdeb0e1838a845d752a3418726b38cd1e654 Mon Sep 17 00:00:00 2001 From: Matthew Peveler Date: Sun, 25 May 2025 16:07:44 +0000 Subject: [PATCH 3/3] Fix using Cake\I18n\Date and DateTime in insert dry-run (#2359) --- src/Phinx/Db/Adapter/PdoAdapter.php | 6 +++ tests/Phinx/Db/Adapter/PdoAdapterTest.php | 49 +++++++++++++++-------- 2 files changed, 38 insertions(+), 17 deletions(-) diff --git a/src/Phinx/Db/Adapter/PdoAdapter.php b/src/Phinx/Db/Adapter/PdoAdapter.php index 9f8c62f10..46e6b0870 100644 --- a/src/Phinx/Db/Adapter/PdoAdapter.php +++ b/src/Phinx/Db/Adapter/PdoAdapter.php @@ -403,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 2e894c001..794e096a8 100644 --- a/tests/Phinx/Db/Adapter/PdoAdapterTest.php +++ b/tests/Phinx/Db/Adapter/PdoAdapterTest.php @@ -3,9 +3,12 @@ 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; @@ -205,31 +208,42 @@ public function testExecuteRightTrimsSemiColons() $this->adapter->execute('SELECT 1;;'); } - public function testQuoteValueNumeric() + public function quoteValueDataProvider(): array { - $method = new ReflectionMethod($this->adapter, 'quoteValue'); - $this->assertSame(1.0, $method->invoke($this->adapter, 1.0)); - $this->assertSame(2, $method->invoke($this->adapter, 2)); + return [ + [1.0, 1.0], + [2, 2], + [true, 1], + [false, 0], + [null, 'null'], + [Literal::from('CURRENT_TIMESTAMP'), 'CURRENT_TIMESTAMP'], + ]; } - public function testQuoteValueBoolean() + /** + * @dataProvider quoteValueDataProvider + */ + public function testQuoteValue($input, $expected): void { $method = new ReflectionMethod($this->adapter, 'quoteValue'); - $this->assertSame(1, $method->invoke($this->adapter, true)); - $this->assertSame(0, $method->invoke($this->adapter, false)); + $this->assertSame($expected, $method->invoke($this->adapter, $input)); } - public function testQuoteValueNull() + public function quoteValueStringDataProvider(): array { - $method = new ReflectionMethod($this->adapter, 'quoteValue'); - $this->assertSame('null', $method->invoke($this->adapter, null)); + 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'"], + ]; } - public function testQuoteValueString() - { - $mockValue = 'mockvalue'; - $expectedValue = 'mockvalueexpected'; + /** + * @dataProvider quoteValueStringDataProvider + */ + public function testQuoteValueString($input, $expected): void + { /** @var \PDO&\PHPUnit\Framework\MockObject\MockObject $pdo */ $pdo = $this->getMockBuilder(PDO::class) ->disableOriginalConstructor() @@ -238,12 +252,13 @@ public function testQuoteValueString() $pdo->expects($this->once()) ->method('quote') - ->with($mockValue) - ->willReturn($expectedValue); + ->willReturnCallback(function (string $input) { + return "'$input'"; + }); $this->adapter->setConnection($pdo); $method = new ReflectionMethod($this->adapter, 'quoteValue'); - $this->assertSame($expectedValue, $method->invoke($this->adapter, $mockValue)); + $this->assertSame($expected, $method->invoke($this->adapter, $input)); } }