Skip to content

Commit

Permalink
Merge pull request cakephp#46 from cakephp/issue-44
Browse files Browse the repository at this point in the history
Fix fixture task duplicating primary key index
  • Loading branch information
lorenzo committed Nov 24, 2014
2 parents 6187f1e + 070fc17 commit 7aa6409
Show file tree
Hide file tree
Showing 6 changed files with 165 additions and 10 deletions.
7 changes: 6 additions & 1 deletion src/Shell/Task/FixturesTask.php
Expand Up @@ -66,7 +66,7 @@ protected function _process($path) {
$out = [];
foreach ($data as $field => $properties) {
// Move primary key into a constraint
if (isset($properties['key']) && $properties['key'] === 'primary') {
if (isset($properties['key']) && strtolower($properties['key']) === 'primary') {
$constraints['primary'] = [
'type' => 'primary',
'columns' => [$field]
Expand All @@ -87,6 +87,11 @@ protected function _process($path) {
$indexProps['columns'] = $indexProps['column'];
unset($indexProps['column']);
}

if (strtolower($index) === 'primary' && isset($constraints['primary'])) {
continue;
}

// Move unique indexes over
if (!empty($indexProps['unique'])) {
unset($indexProps['unique']);
Expand Down
21 changes: 17 additions & 4 deletions src/Shell/Task/SkeletonTask.php
Expand Up @@ -47,10 +47,19 @@ protected function _process($path) {
}

$sourcePath = ROOT . DS . 'vendor' . DS . 'cakephp' . DS . 'app' . DS;
$files = array('bin' . DS . 'cake', 'bin' . DS . 'cake.bat', 'bin' . DS . 'cake.php',
'phpunit.xml.dist', 'index.php', 'webroot' . DS . 'index.php', 'config' . DS . 'paths.php', 'tests' . DS . 'bootstrap.php',
'src' . DS . 'Template' . DS . 'Error' . DS . 'error400.ctp', 'src' . DS . 'Template' . DS . 'Error' . DS . 'error500.ctp',
'src' . DS . 'Template' . DS . 'Layout' . DS . 'error.ctp');
$files = array(
'bin' . DS . 'cake',
'bin' . DS . 'cake.bat',
'bin' . DS . 'cake.php',
'phpunit.xml.dist',
'index.php',
'webroot' . DS . 'index.php',
'config' . DS . 'paths.php',
'tests' . DS . 'bootstrap.php',
'src' . DS . 'Template' . DS . 'Error' . DS . 'error400.ctp',
'src' . DS . 'Template' . DS . 'Error' . DS . 'error500.ctp',
'src' . DS . 'Template' . DS . 'Layout' . DS . 'error.ctp'
);
$ret = 0;
foreach ($files as $file) {
$ret |= $this->_addFile($file, $sourcePath, $path);
Expand All @@ -75,6 +84,10 @@ protected function _addFile($file, $sourcePath, $targetPath, $targetFile = null)
if ($targetFile === null) {
$targetFile = $file;
}
$targetPathName = $targetPath . dirname($targetFile);
if (!is_dir($targetPathName)) {
mkdir($targetPathName, 0755, true);
}
$result = copy($sourcePath . $file, $targetPath . $targetFile);
}
$this->out('Adding ' . $file, 1, Shell::VERBOSE);
Expand Down
60 changes: 60 additions & 0 deletions tests/TestCase/Shell/Task/FixturesTaskTest.php
@@ -0,0 +1,60 @@
<?php
namespace Cake\Upgrade\Test\TestCase\Shell\Task;

use Cake\TestSuite\TestCase;
use Cake\Upgrade\Shell\Task\FixturesTask;
use Cake\Utility\Folder;

/**
* FixturesTaskTest
*
*/
class FixturesTest extends TestCase {

/**
* Task instance
*
* @var mixed
*/
public $sut;

/**
* setUp
*
* Create a mock for all tests to use
*
* @return void
*/
public function setUp() {
parent::setUp();

$io = $this->getMock('Cake\Console\ConsoleIo', [], [], '', false);

$this->sut = $this->getMock(
'Cake\Upgrade\Shell\Task\FixturesTask',
['in', 'out', 'hr', 'err', '_shouldProcess'],
[$io]
);
$this->sut->loadTasks();
}

/**
* SkeletonTaskTest::testProcess()
*
* @return void
*/
public function testProcess() {
$this->sut->expects($this->any())
->method('_shouldProcess')
->will($this->returnValue(true));

$path = TESTS . 'test_files' . DS;
$result = $this->sut->process($path . 'ArticleFixture.php');
$this->assertTrue($result);

$result = $this->sut->Stage->source($path . 'ArticleFixture.php');
$expected = file_get_contents($path . 'ArticleFixtureAfter.php');
$this->assertTextEquals($expected, $result);
}

}
11 changes: 6 additions & 5 deletions tests/TestCase/Shell/Task/SkeletonTaskTest.php
Expand Up @@ -38,6 +38,11 @@ public function setUp() {
$this->sut->loadTasks();
}

/**
* Teardown
*
* @return void
*/
public function tearDown() {
$Folder = new Folder(TMP . 'skeleton_test' . DS);
$Folder->delete();
Expand All @@ -59,11 +64,7 @@ public function testProcess() {
$this->assertTrue($result);

$this->assertTrue(file_exists($path . 'logs' . DS . 'empty'));

return;
$result = $this->sut->Stage->source($path . 'tests_before.php');
$expected = file_get_contents($path . 'tests_after.php');
$this->assertTextEquals($expected, $result);
$this->assertTrue(file_exists($path . 'tests' . DS . 'bootstrap.php'));
}

}
39 changes: 39 additions & 0 deletions tests/test_files/ArticleFixture.php
@@ -0,0 +1,39 @@
<?php
/**
* ArticleFixture
*/
class ArticleFixture extends CakeTestFixture {

/**
* Fields
*
* @var array
*/
public $fields = array(
'id' => array('type' => 'integer', 'null' => false, 'default' => null, 'unsigned' => false, 'key' => 'primary'),
'title' => array('type' => 'string', 'null' => false, 'default' => null, 'collate' => 'utf8_general_ci', 'charset' => 'utf8'),
'body' => array('type' => 'text', 'null' => true, 'default' => null, 'collate' => 'utf8_general_ci', 'charset' => 'utf8'),
'created' => array('type' => 'datetime', 'null' => true, 'default' => null),
'modified' => array('type' => 'datetime', 'null' => true, 'default' => null),
'indexes' => array(
'PRIMARY' => array('column' => 'id', 'unique' => 1)
),
'tableParameters' => array('charset' => 'utf8', 'collate' => 'utf8_general_ci', 'engine' => 'InnoDB')
);

/**
* Records
*
* @var array
*/
public $records = array(
array(
'id' => 1,
'title' => 'A title',
'body' => 'A body',
'created' => '2014-10-09 20:34:42',
'modified' => '2014-10-09 20:34:42'
),
);

}
37 changes: 37 additions & 0 deletions tests/test_files/ArticleFixtureAfter.php
@@ -0,0 +1,37 @@
<?php
/**
* ArticleFixture
*/
class ArticleFixture extends CakeTestFixture {

/**
* Fields
*
* @var array
*/
public $fields = array(
'id' => ['type' => 'integer', 'null' => false, 'default' => null, 'unsigned' => false],
'title' => ['type' => 'string', 'null' => false, 'default' => null, 'collate' => 'utf8_general_ci', 'charset' => 'utf8'],
'body' => ['type' => 'text', 'null' => true, 'default' => null, 'collate' => 'utf8_general_ci', 'charset' => 'utf8'],
'created' => ['type' => 'datetime', 'null' => true, 'default' => null],
'modified' => ['type' => 'datetime', 'null' => true, 'default' => null],
'_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]],
'_options' => ['charset' => 'utf8', 'collate' => 'utf8_general_ci', 'engine' => 'InnoDB']
);

/**
* Records
*
* @var array
*/
public $records = array(
array(
'id' => 1,
'title' => 'A title',
'body' => 'A body',
'created' => '2014-10-09 20:34:42',
'modified' => '2014-10-09 20:34:42'
),
);

}

0 comments on commit 7aa6409

Please sign in to comment.