Skip to content

Commit 344f68f

Browse files
committed
Fix up DateTime support.
1 parent 330c0e1 commit 344f68f

4 files changed

Lines changed: 92 additions & 6 deletions

File tree

tests/TestCase/Command/Phinx/SeedTest.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
use Migrations\Test\CommandTester;
2222
use Migrations\Test\TestCase\DriverConnectionTrait;
2323
use PDO;
24+
use Phinx\Config\FeatureFlags;
2425
use Phinx\Db\Adapter\WrapperInterface;
2526
use Symfony\Component\Console\Input\ArrayInput;
2627
use Symfony\Component\Console\Output\StreamOutput;
@@ -90,6 +91,8 @@ public function tearDown(): void
9091
$this->connection->execute('DROP TABLE IF EXISTS numbers');
9192
$this->connection->execute('DROP TABLE IF EXISTS letters');
9293
$this->connection->execute('DROP TABLE IF EXISTS stores');
94+
95+
FeatureFlags::$addTimestampsUseDateTime = false;
9396
}
9497

9598
/**

tests/TestCase/Command/SeedCommandTest.php

Lines changed: 56 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
use Cake\Event\EventManager;
1212
use Cake\TestSuite\TestCase;
1313
use InvalidArgumentException;
14+
use Phinx\Config\FeatureFlags;
1415
use ReflectionProperty;
1516

1617
class SeedCommandTest extends TestCase
@@ -141,7 +142,7 @@ public function testSeederBaseSeed(): void
141142
$this->assertEquals(2, $query->fetchColumn(0));
142143
}
143144

144-
public function testSeederImplictAll(): void
145+
public function testSeederImplicitAll(): void
145146
{
146147
$this->createTables();
147148
$this->exec('migrations seed -c test');
@@ -192,4 +193,58 @@ public function testSeederSourceNotFound(): void
192193

193194
$this->exec('migrations seed -c test --source NotThere --seed LettersSeed');
194195
}
196+
197+
public function testSeederWithDateTimeFields(): void
198+
{
199+
FeatureFlags::$addTimestampsUseDateTime = true;
200+
201+
$this->createTables();
202+
$this->exec('migrations seed -c test --seed StoresSeed');
203+
204+
$this->assertExitSuccess();
205+
$this->assertOutputContains('StoresSeed:</info> <comment>seeding');
206+
$this->assertOutputContains('All Done');
207+
208+
/** @var \Cake\Database\Connection $connection */
209+
$connection = ConnectionManager::get('test');
210+
$result = $connection->selectQuery()
211+
->select(['*'])
212+
->from('stores')
213+
->orderBy('id DESC')
214+
->limit(1)
215+
->execute()->fetchAll('assoc');
216+
217+
$this->assertNotEmpty($result[0]);
218+
$store = $result[0];
219+
$this->assertEquals('foo_with_date', $store['name']);
220+
$this->assertNotEmpty($store['created']);
221+
$this->assertNotEmpty($store['modified']);
222+
}
223+
224+
public function testSeederWithTimestampFields(): void
225+
{
226+
FeatureFlags::$addTimestampsUseDateTime = false;
227+
228+
$this->createTables();
229+
$this->exec('migrations seed -c test --seed StoresSeed');
230+
231+
$this->assertExitSuccess();
232+
$this->assertOutputContains('StoresSeed:</info> <comment>seeding');
233+
$this->assertOutputContains('All Done');
234+
235+
/** @var \Cake\Database\Connection $connection */
236+
$connection = ConnectionManager::get('test');
237+
$result = $connection->selectQuery()
238+
->select(['*'])
239+
->from('stores')
240+
->orderBy('id DESC')
241+
->limit(1)
242+
->execute()->fetchAll('assoc');
243+
244+
$this->assertNotEmpty($result[0]);
245+
$store = $result[0];
246+
$this->assertEquals('foo_with_date', $store['name']);
247+
$this->assertNotEmpty($store['created']);
248+
$this->assertNotEmpty($store['modified']);
249+
}
195250
}

tests/test_app/config/Seeds/NumbersSeed.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,6 @@ class NumbersSeed extends AbstractSeed
99
{
1010
/**
1111
* Run Method.
12-
*
13-
* Write your database seeder using this method.
14-
*
15-
* More information on writing seeders is available here:
16-
* https://book.cakephp.org/phinx/0/en/seeding.html
1712
*/
1813
public function run(): void
1914
{
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
use Cake\I18n\Date;
4+
use Cake\I18n\DateTime;
5+
use Migrations\AbstractSeed;
6+
7+
/**
8+
* NumbersSeed seed.
9+
*/
10+
class StoresSeed extends AbstractSeed
11+
{
12+
/**
13+
* Run Method.
14+
*/
15+
public function run(): void
16+
{
17+
$data = [
18+
[
19+
'name' => 'foo',
20+
'created' => new Date(),
21+
'modified' => new Date(),
22+
],
23+
[
24+
'name' => 'foo_with_date',
25+
'created' => new DateTime(),
26+
'modified' => new DateTime(),
27+
],
28+
];
29+
30+
$table = $this->table('stores');
31+
$table->insert($data)->save();
32+
}
33+
}

0 commit comments

Comments
 (0)