Skip to content

Commit

Permalink
Merge pull request #99 from cakephp/issue-98
Browse files Browse the repository at this point in the history
Fix datetime field generation, and entity usage.
  • Loading branch information
lorenzo committed May 24, 2015
2 parents 0bfd148 + 1c1be1b commit 3fd850a
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 21 deletions.
18 changes: 9 additions & 9 deletions src/Shell/Task/FixtureTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Cake\ORM\TableRegistry;
use Cake\Utility\Inflector;
use Cake\Utility\Text;
use DateTime;

/**
* Task class for creating and updating fixtures files.
Expand Down Expand Up @@ -386,6 +387,9 @@ protected function _makeRecordString($records)
foreach ($records as $record) {
$values = [];
foreach ($record as $field => $value) {
if ($value instanceof DateTime) {
$value = $value->format('Y-m-d H:i:s');
}
$val = var_export($value, true);
if ($val === 'NULL') {
$val = 'null';
Expand Down Expand Up @@ -420,15 +424,11 @@ protected function _getRecordsFromTable($modelName, $useTable = null)
'connection' => ConnectionManager::get($this->connection)
]);
}
$records = $model->find('all', [
'conditions' => $conditions,
'limit' => $recordCount
]);
$records = $model->find('all')
->where($conditions)
->limit($recordCount)
->hydrate(false);

$out = [];
foreach ($records as $record) {
$out[] = $record->toArray();
}
return $out;
return $records;
}
}
18 changes: 6 additions & 12 deletions tests/TestCase/Shell/Task/FixtureTaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ public function setUp()
$this->Task->BakeTemplate = new BakeTemplateTask($io);
$this->Task->BakeTemplate->interactive = false;
$this->Task->BakeTemplate->initialize();

$this->_compareBasePath = Plugin::path('Bake') . 'tests' . DS . 'comparisons' . DS . 'Fixture' . DS;
}

/**
Expand Down Expand Up @@ -100,25 +102,17 @@ public function testGetPath()
}

/**
* test generating a fixture with database conditions.
* test generating a fixture with database rows.
*
* @return void
*/
public function testImportRecordsFromDatabaseWithConditionsPoo()
public function testImportRecordsFromDatabase()
{
$this->Task->connection = 'test';
$this->Task->params = ['schema' => true, 'records' => true];

$result = $this->Task->bake('Articles');

$this->assertContains('namespace App\Test\Fixture;', $result);
$this->assertContains('use Cake\TestSuite\Fixture\TestFixture;', $result);
$this->assertContains('class ArticlesFixture extends TestFixture', $result);
$this->assertContains('public $records', $result);
$this->assertContains('public $import', $result);
$this->assertContains("'title' => 'First Article'", $result, 'Missing import data %s');
$this->assertContains('Second Article', $result, 'Missing import data %s');
$this->assertContains('Third Article', $result, 'Missing import data %s');
$result = $this->Task->bake('Users');
$this->assertSameAsFile(__FUNCTION__ . '.php', $result);
}

/**
Expand Down
55 changes: 55 additions & 0 deletions tests/comparisons/Fixture/testImportRecordsFromDatabase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php
namespace App\Test\Fixture;

use Cake\TestSuite\Fixture\TestFixture;

/**
* UsersFixture
*
*/
class UsersFixture extends TestFixture
{

/**
* Import
*
* @var array
*/
public $import = ['model' => 'Users', 'connection' => 'test'];

/**
* Records
*
* @var array
*/
public $records = [
[
'id' => 1,
'username' => 'mariano',
'password' => '$2a$10$u05j8FjsvLBNdfhBhc21LOuVMpzpabVXQ9OpC2wO3pSO0q6t7HHMO',
'created' => '2007-03-17 01:16:23',
'updated' => '2007-03-17 01:18:31'
],
[
'id' => 2,
'username' => 'nate',
'password' => '$2a$10$u05j8FjsvLBNdfhBhc21LOuVMpzpabVXQ9OpC2wO3pSO0q6t7HHMO',
'created' => '2008-03-17 01:18:23',
'updated' => '2008-03-17 01:20:31'
],
[
'id' => 3,
'username' => 'larry',
'password' => '$2a$10$u05j8FjsvLBNdfhBhc21LOuVMpzpabVXQ9OpC2wO3pSO0q6t7HHMO',
'created' => '2010-05-10 01:20:23',
'updated' => '2010-05-10 01:22:31'
],
[
'id' => 4,
'username' => 'garrett',
'password' => '$2a$10$u05j8FjsvLBNdfhBhc21LOuVMpzpabVXQ9OpC2wO3pSO0q6t7HHMO',
'created' => '2012-06-10 01:22:23',
'updated' => '2012-06-12 01:24:31'
],
];
}

0 comments on commit 3fd850a

Please sign in to comment.