Doing some refactoring in the project task.

Adding tests for constants staying commented out when CakePHP is on the include_path.
This commit is contained in:
Mark Story 2011-07-31 15:15:03 -04:00 committed by mark_story
parent 6e2870c706
commit cdcabc79b6
2 changed files with 49 additions and 41 deletions

View file

@ -53,6 +53,7 @@ class ProjectTask extends Shell {
$project = $this->in($prompt, null, APP . 'myapp');
}
if ($project && !Folder::isAbsolute($project) && isset($_SERVER['PWD'])) {
$project = $_SERVER['PWD'] . DS . $project;
}
@ -297,47 +298,49 @@ class ProjectTask extends Shell {
* Generates and writes CAKE_CORE_INCLUDE_PATH
*
* @param string $path Project path
* @param bool $hardCode Wether or not define calls should be hardcoded.
* @return boolean Success
*/
public function corePath($path, $hardCode = true) {
$prefix = $hardCode == true ? '' : '//';
if (dirname($path) !== CAKE_CORE_INCLUDE_PATH) {
$File = new File($path . 'webroot' . DS . 'index.php');
$contents = $File->read();
$root = strpos(CAKE_CORE_INCLUDE_PATH, '/') === 0 ? " DS . '" : "'";
$corePath = $root . str_replace(DS, "' . DS . '", trim(CAKE_CORE_INCLUDE_PATH, DS)) . "'";
if (preg_match('#(\s*[/]+define\(\'CAKE_CORE_INCLUDE_PATH\', .*?\);)#', $contents, $match)) {
$result = str_replace(
$match[0],
"\n\t" . $prefix . "define('CAKE_CORE_INCLUDE_PATH', " . $corePath . ");",
$contents
);
if (!$File->write($result)) {
$filename = $path . 'webroot' . DS . 'index.php';
if (!$this->_replaceCorePath($filename, $hardCode)) {
return false;
}
} else {
return false;
}
$File = new File($path . 'webroot' . DS . 'test.php');
$contents = $File->read();
if (preg_match('#(\s*[/]+define\(\'CAKE_CORE_INCLUDE_PATH\', .*?\);)#', $contents, $match)) {
$result = str_replace(
$match[0],
"\n\t" . $prefix . "define('CAKE_CORE_INCLUDE_PATH', " . $corePath . ");",
$contents
);
if (!$File->write($result)) {
return false;
}
} else {
$filename = $path . 'webroot' . DS . 'test.php';
if (!$this->_replaceCorePath($filename, $hardCode)) {
return false;
}
return true;
}
}
/**
* Replaces the __CAKE_PATH__ placeholder in the template files.
*
* @param string $filename The filename to operate on.
* @param boolean $hardCode Whether or not the define should be uncommented.
* @retun bool Success
*/
protected function _replaceCorePath($filename, $hardCode) {
$contents = file_get_contents($filename);
$root = strpos(CAKE_CORE_INCLUDE_PATH, '/') === 0 ? " DS . '" : "'";
$corePath = $root . str_replace(DS, "' . DS . '", trim(CAKE_CORE_INCLUDE_PATH, DS)) . "'";
$result = str_replace('__CAKE_PATH__', $corePath, $contents, $count);
if ($hardCode) {
$result = str_replace('//define(\'CAKE_CORE', 'define(\'CAKE_CORE', $result);
}
if (!file_put_contents($filename, $result)) {
return false;
}
if ($count == 0) {
return false;
}
return true;
}
/**
* Enables Configure::read('Routing.prefixes') in /app/Config/core.php
*

View file

@ -118,37 +118,42 @@ class ProjectTaskTest extends CakeTestCase {
$path = $this->Task->args[0] = TMP . 'tests' . DS . 'bake_test_app';
$this->Task->params['skel'] = CAKE . 'Console' . DS . 'Templates' . DS . 'skel';
$this->Task->expects($this->at(0))->method('in')->will($this->returnValue('y'));
$this->Task->expects($this->at(3))->method('in')->will($this->returnValue('n'));
$this->Task->execute();
$this->assertTrue(is_dir($this->Task->args[0]), 'No project dir');
$file = new File($path . DS . 'webroot' . DS . 'index.php');
$contents = $file->read();
$this->assertPattern('/define\(\'CAKE_CORE_INCLUDE_PATH\', ROOT/', $contents);
$this->assertPattern('/define\(\'CAKE_CORE_INCLUDE_PATH\', .*?DS/', $contents);
$file = new File($path . DS . 'webroot' . DS . 'test.php');
$contents = $file->read();
$this->assertPattern('/define\(\'CAKE_CORE_INCLUDE_PATH\', ROOT/', $contents);
$this->assertPattern('/define\(\'CAKE_CORE_INCLUDE_PATH\', .*?DS/', $contents);
}
/**
* test bake with setting CAKE_CORE_INCLUDE_PATH in webroot/index.php
* test bake with CakePHP on the include path. The constants should remain commented out.
*
* @return void
*/
public function testExecuteWithSettingIncludePath() {
public function testExecuteWithCakeOnIncludePath() {
if (!function_exists('ini_set')) {
$this->markTestAsSkipped('Not access to ini_set, cannot proceed.');
}
$restore = ini_get('include_path');
ini_set('include_path', CAKE_CORE_INCLUDE_PATH . PATH_SEPARATOR . $restore);
$path = $this->Task->args[0] = TMP . 'tests' . DS . 'bake_test_app';
$this->Task->params['skel'] = CAKE . 'Console' . DS . 'Templates' . DS . 'skel';
$this->Task->expects($this->at(0))->method('in')->will($this->returnValue('y'));
$this->Task->expects($this->at(3))->method('in')->will($this->returnValue('y'));
$this->Task->execute();
$this->assertTrue(is_dir($this->Task->args[0]), 'No project dir');
$file = new File($path . DS . 'webroot' . DS . 'index.php');
$contents = $file->read();
$this->assertNoPattern('/define\(\'CAKE_CORE_INCLUDE_PATH\', ROOT/', $contents);
$file = new File($path . DS . 'webroot' . DS . 'test.php');
$contents = $file->read();
$this->assertNoPattern('/define\(\'CAKE_CORE_INCLUDE_PATH\', ROOT/', $contents);
$contents = file_get_contents($path . DS . 'webroot' . DS . 'index.php');
$this->assertRegExp('#//define\(\'CAKE_CORE_INCLUDE_PATH#', $contents);
$contents = file_get_contents($path . DS . 'webroot' . DS . 'test.php');
$this->assertRegExp('#//define\(\'CAKE_CORE_INCLUDE_PATH#', $contents);
ini_set('include_path', $restore);
}
/**