Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/Db/Adapter/AbstractAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
use Cake\Database\Query\SelectQuery;
use Cake\Database\Query\UpdateQuery;
use Cake\Database\Schema\SchemaDialect;
use Cake\I18n\Date;
use Cake\I18n\DateTime;
use Exception;
use InvalidArgumentException;
use Migrations\Config\Config;
Expand Down Expand Up @@ -723,7 +725,11 @@ public function bulkinsert(TableMetadata $table, array $rows): void
$placeholder = (string)$v;
}
if ($placeholder == '?') {
if (is_bool($v)) {
if ($v instanceof DateTime) {
$vals[] = $v->toDateTimeString();
} elseif ($v instanceof Date) {
$vals[] = $v->toDateString();
} elseif (is_bool($v)) {
$vals[] = $this->castToBool($v);
} else {
$vals[] = $v;
Expand Down
8 changes: 7 additions & 1 deletion src/Db/Adapter/PostgresAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
namespace Migrations\Db\Adapter;

use Cake\Database\Connection;
use Cake\I18n\Date;
use Cake\I18n\DateTime;
use InvalidArgumentException;
use Migrations\Db\AlterInstructions;
use Migrations\Db\Literal;
Expand Down Expand Up @@ -1526,7 +1528,11 @@ public function bulkinsert(Table $table, array $rows): void
}
$values[] = $placeholder;
if ($placeholder == '?') {
if (is_bool($v)) {
if ($v instanceof DateTime) {
$vals[] = $v->toDateTimeString();
} elseif ($v instanceof Date) {
$vals[] = $v->toDateString();
} elseif (is_bool($v)) {
$vals[] = $this->castToBool($v);
} else {
$vals[] = $v;
Expand Down
8 changes: 7 additions & 1 deletion src/Db/Adapter/SqlserverAdapter.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
namespace Migrations\Db\Adapter;

use BadMethodCallException;
use Cake\I18n\Date;
use Cake\I18n\DateTime;
use InvalidArgumentException;
use Migrations\Db\AlterInstructions;
use Migrations\Db\Literal;
Expand Down Expand Up @@ -1361,7 +1363,11 @@ public function bulkinsert(TableMetadata $table, array $rows): void
$placeholder = (string)$v;
}
if ($placeholder == '?') {
if (is_bool($v)) {
if ($v instanceof DateTime) {
$vals[] = $v->toDateTimeString();
} elseif ($v instanceof Date) {
$vals[] = $v->toDateString();
} elseif (is_bool($v)) {
$vals[] = $this->castToBool($v);
} else {
$vals[] = $v;
Expand Down
75 changes: 74 additions & 1 deletion tests/TestCase/Command/SeedCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
use Cake\Event\EventManager;
use Cake\TestSuite\TestCase;
use InvalidArgumentException;
use Phinx\Config\FeatureFlags;
use ReflectionClass;
use ReflectionProperty;

class SeedCommandTest extends TestCase
Expand Down Expand Up @@ -38,6 +40,13 @@ public function tearDown(): void
$connection->execute('DROP TABLE IF EXISTS numbers');
$connection->execute('DROP TABLE IF EXISTS letters');
$connection->execute('DROP TABLE IF EXISTS stores');

if (class_exists(FeatureFlags::class)) {
$reflection = new ReflectionClass(FeatureFlags::class);
if ($reflection->hasProperty('addTimestampsUseDateTime')) {
FeatureFlags::$addTimestampsUseDateTime = false;
}
}
}

protected function resetOutput(): void
Expand Down Expand Up @@ -141,7 +150,7 @@ public function testSeederBaseSeed(): void
$this->assertEquals(2, $query->fetchColumn(0));
}

public function testSeederImplictAll(): void
public function testSeederImplicitAll(): void
{
$this->createTables();
$this->exec('migrations seed -c test');
Expand Down Expand Up @@ -192,4 +201,68 @@ public function testSeederSourceNotFound(): void

$this->exec('migrations seed -c test --source NotThere --seed LettersSeed');
}

public function testSeederWithTimestampFields(): void
{
if (class_exists(FeatureFlags::class)) {
$reflection = new ReflectionClass(FeatureFlags::class);
if ($reflection->hasProperty('addTimestampsUseDateTime')) {
FeatureFlags::$addTimestampsUseDateTime = false;
}
}

$this->createTables();
$this->exec('migrations seed -c test --seed StoresSeed');

$this->assertExitSuccess();
$this->assertOutputContains('StoresSeed:</info> <comment>seeding');
$this->assertOutputContains('All Done');

/** @var \Cake\Database\Connection $connection */
$connection = ConnectionManager::get('test');
$result = $connection->selectQuery()
->select(['*'])
->from('stores')
->orderBy('id DESC')
->limit(1)
->execute()->fetchAll('assoc');

$this->assertNotEmpty($result[0]);
$store = $result[0];
$this->assertEquals('foo_with_date', $store['name']);
$this->assertNotEmpty($store['created']);
$this->assertNotEmpty($store['modified']);
}

public function testSeederWithDateTimeFields(): void
{
$this->skipIf(!class_exists(FeatureFlags::class));

$reflection = new ReflectionClass(FeatureFlags::class);
$this->skipIf(!$reflection->hasProperty('addTimestampsUseDateTime'));

FeatureFlags::$addTimestampsUseDateTime = true;

$this->createTables();
$this->exec('migrations seed -c test --seed StoresSeed');

$this->assertExitSuccess();
$this->assertOutputContains('StoresSeed:</info> <comment>seeding');
$this->assertOutputContains('All Done');

/** @var \Cake\Database\Connection $connection */
$connection = ConnectionManager::get('test');
$result = $connection->selectQuery()
->select(['*'])
->from('stores')
->orderBy('id DESC')
->limit(1)
->execute()->fetchAll('assoc');

$this->assertNotEmpty($result[0]);
$store = $result[0];
$this->assertEquals('foo_with_date', $store['name']);
$this->assertNotEmpty($store['created']);
$this->assertNotEmpty($store['modified']);
}
}
5 changes: 0 additions & 5 deletions tests/test_app/config/Seeds/NumbersSeed.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@ class NumbersSeed extends AbstractSeed
{
/**
* Run Method.
*
* Write your database seeder using this method.
*
* More information on writing seeders is available here:
* https://book.cakephp.org/phinx/0/en/seeding.html
*/
public function run(): void
{
Expand Down
33 changes: 33 additions & 0 deletions tests/test_app/config/Seeds/StoresSeed.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

use Cake\I18n\Date;
use Cake\I18n\DateTime;
use Migrations\AbstractSeed;

/**
* NumbersSeed seed.
*/
class StoresSeed extends AbstractSeed
{
/**
* Run Method.
*/
public function run(): void
{
$data = [
[
'name' => 'foo',
'created' => new Date(),
'modified' => new Date(),
],
[
'name' => 'foo_with_date',
'created' => new DateTime(),
'modified' => new DateTime(),
],
];

$table = $this->table('stores');
$table->insert($data)->save();
}
}
Loading