Skip to content

Commit 524ebde

Browse files
authored
Fix using Cake\I18n\Date and DateTime in insert dry-run (#2359)
1 parent 198b6ca commit 524ebde

File tree

2 files changed

+38
-17
lines changed

2 files changed

+38
-17
lines changed

src/Phinx/Db/Adapter/PdoAdapter.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,12 @@ protected function quoteValue(mixed $value): mixed
403403
return (string)$value;
404404
}
405405

406+
if ($value instanceof DateTime) {
407+
$value = $value->toDateTimeString();
408+
} elseif ($value instanceof Date) {
409+
$value = $value->toDateString();
410+
}
411+
406412
return $this->getConnection()->quote($value);
407413
}
408414

tests/Phinx/Db/Adapter/PdoAdapterTest.php

Lines changed: 32 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,12 @@
33

44
namespace Test\Phinx\Db\Adapter;
55

6+
use Cake\I18n\Date;
7+
use Cake\I18n\DateTime;
68
use PDO;
79
use PDOException;
810
use Phinx\Config\Config;
11+
use Phinx\Util\Literal;
912
use PHPUnit\Framework\TestCase;
1013
use ReflectionMethod;
1114
use RuntimeException;
@@ -205,31 +208,42 @@ public function testExecuteRightTrimsSemiColons()
205208
$this->adapter->execute('SELECT 1;;');
206209
}
207210

208-
public function testQuoteValueNumeric()
211+
public function quoteValueDataProvider(): array
209212
{
210-
$method = new ReflectionMethod($this->adapter, 'quoteValue');
211-
$this->assertSame(1.0, $method->invoke($this->adapter, 1.0));
212-
$this->assertSame(2, $method->invoke($this->adapter, 2));
213+
return [
214+
[1.0, 1.0],
215+
[2, 2],
216+
[true, 1],
217+
[false, 0],
218+
[null, 'null'],
219+
[Literal::from('CURRENT_TIMESTAMP'), 'CURRENT_TIMESTAMP'],
220+
];
213221
}
214222

215-
public function testQuoteValueBoolean()
223+
/**
224+
* @dataProvider quoteValueDataProvider
225+
*/
226+
public function testQuoteValue($input, $expected): void
216227
{
217228
$method = new ReflectionMethod($this->adapter, 'quoteValue');
218-
$this->assertSame(1, $method->invoke($this->adapter, true));
219-
$this->assertSame(0, $method->invoke($this->adapter, false));
229+
$this->assertSame($expected, $method->invoke($this->adapter, $input));
220230
}
221231

222-
public function testQuoteValueNull()
232+
public function quoteValueStringDataProvider(): array
223233
{
224-
$method = new ReflectionMethod($this->adapter, 'quoteValue');
225-
$this->assertSame('null', $method->invoke($this->adapter, null));
234+
return [
235+
['mockvalue', "'mockvalue'"],
236+
[new Date('2023-01-01'), "'2023-01-01'"],
237+
[new DateTime('2023-01-01 12:00:00'), "'2023-01-01 12:00:00'"],
238+
];
226239
}
227240

228-
public function testQuoteValueString()
229-
{
230-
$mockValue = 'mockvalue';
231-
$expectedValue = 'mockvalueexpected';
241+
/**
242+
* @dataProvider quoteValueStringDataProvider
243+
*/
232244

245+
public function testQuoteValueString($input, $expected): void
246+
{
233247
/** @var \PDO&\PHPUnit\Framework\MockObject\MockObject $pdo */
234248
$pdo = $this->getMockBuilder(PDO::class)
235249
->disableOriginalConstructor()
@@ -238,12 +252,13 @@ public function testQuoteValueString()
238252

239253
$pdo->expects($this->once())
240254
->method('quote')
241-
->with($mockValue)
242-
->willReturn($expectedValue);
255+
->willReturnCallback(function (string $input) {
256+
return "'$input'";
257+
});
243258

244259
$this->adapter->setConnection($pdo);
245260

246261
$method = new ReflectionMethod($this->adapter, 'quoteValue');
247-
$this->assertSame($expectedValue, $method->invoke($this->adapter, $mockValue));
262+
$this->assertSame($expected, $method->invoke($this->adapter, $input));
248263
}
249264
}

0 commit comments

Comments
 (0)