Skip to content

Commit

Permalink
Further test fixes
Browse files Browse the repository at this point in the history
The test fails are in part caused by assumptions related to where things
are.

Changed the way the root composer file is found to be based on the ROOT
constant rather than "the folder above the plugins dir".
Changed all the content-comparison tests to be based on the
string-compare trait.
Attempt to clear up the actual intent of the test methods, by
simplifying and renaming test methods.
  • Loading branch information
AD7six committed Dec 17, 2014
1 parent af4385f commit 8a20c31
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 118 deletions.
19 changes: 15 additions & 4 deletions src/Shell/Task/PluginTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -187,10 +187,10 @@ protected function _generateFile($template, $root) {
* @return bool True if composer could be modified correctly
*/
protected function _modifyAutoloader($plugin, $path) {
$path = dirname($path);
$file = $path . DS . 'composer.json';
$file = $this->_rootComposerFilePath();

if (!file_exists($file)) {
$this->out(sprintf('<info>Main composer file %s not found</info>', $file));
return false;
}

Expand All @@ -200,7 +200,7 @@ protected function _modifyAutoloader($plugin, $path) {

$this->out('<info>Modifying composer autoloader</info>');

$out = json_encode($config, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
$out = json_encode($config, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES). "\n";
$this->createFile($file, $out);

$composer = $this->Project->findComposer();
Expand All @@ -214,7 +214,7 @@ protected function _modifyAutoloader($plugin, $path) {
$cwd = getcwd();

// Windows makes running multiple commands at once hard.
chdir($path);
chdir(dirname($path));
$command = 'php ' . escapeshellarg($composer) . ' dump-autoload';
$this->callProcess($command);

Expand All @@ -228,6 +228,17 @@ protected function _modifyAutoloader($plugin, $path) {
return true;
}

/**
* The path to the main application's composer file
*
* This is a test issolation wrapper
*
* @return string the abs file path
*/
protected function _rootComposerFilePath() {
return ROOT . DS . 'composer.json';
}

/**
* find and change $this->path to the user selection
*
Expand Down
185 changes: 73 additions & 112 deletions tests/TestCase/Shell/Task/PluginTaskTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,44 +17,46 @@
use Bake\Shell\Task\ProjectTask;
use Bake\Shell\Task\TemplateTask;
use Cake\Core\App;
use Cake\Core\Plugin;
use Cake\Core\Configure;
use Cake\Filesystem\File;
use Cake\Filesystem\Folder;
use Cake\TestSuite\StringCompareTrait;
use Cake\TestSuite\TestCase;

/**
* PluginTaskPlugin class
*/
class PluginTaskTest extends TestCase {

use StringCompareTrait;

/**
* setUp method
*
* @return void
*/
public function setUp() {
parent::setUp();
$this->_compareBasePath = Plugin::path('Bake') . 'tests' . DS . 'comparisons' . DS . 'Plugin' . DS;
$this->io = $this->getMock('Cake\Console\ConsoleIo', [], [], '', false);

$this->Task = $this->getMock('Bake\Shell\Task\PluginTask',
array('in', 'err', 'createFile', '_stop', 'clear', 'callProcess'),
array('in', 'err', '_stop', 'clear', 'callProcess', '_rootComposerFilePath'),
array($this->io)
);

$this->Task->Project = new ProjectTask($this->io);

$this->Task->Template = new TemplateTask($this->io);
$this->Task->Template->interactive = false;

$this->Task->path = TMP . 'tests' . DS;
$this->Task->bootstrap = TMP . 'tests' . DS . 'bootstrap.php';
$this->Task->path = TMP . 'tests' . DS . 'BakedPlugins' . DS;
new Folder($this->Task->path, true);

if (!is_dir($this->Task->path)) {
mkdir($this->Task->path);
}
$this->Task->bootstrap = TMP . 'tests' . DS . 'bootstrap.php';
touch($this->Task->bootstrap);

$this->_path = App::path('Plugin');

}

/**
Expand All @@ -63,9 +65,9 @@ public function setUp() {
* @return void
*/
public function tearDown() {
if (file_exists($this->Task->bootstrap)) {
unlink($this->Task->bootstrap);
}
$Folder = new Folder(TMP . 'tests' . DS . 'BakedPlugins');
$Folder->delete();

parent::tearDown();
}

Expand All @@ -74,80 +76,38 @@ public function tearDown() {
*
* @return void
*/
public function testBakeFoldersAndFiles() {
$this->Task->expects($this->at(0))->method('in')->will($this->returnValue('y'));

$path = $this->Task->path . 'BakeTestBake';

$files = [
'README.md',
'composer.json',
'config/routes.php',
'phpunit.xml.dist',
'src/Controller/AppController.php',
'tests/bootstrap.php',
'webroot/empty'
];

$i = 1;
foreach($files as $file) {
$this->Task->expects($this->at($i++))
->method('createFile')
->with($path . DS . $file);
}
public function testBake() {
$this->Task->expects($this->at(0))->method('in')
->will($this->returnValue('y'));

$this->Task->bake('BakeTestBake');
$this->Task->bake('SimpleExample');
$this->assertPluginContents('SimpleExample');
}

/**
* test execute with no args, flowing into interactive,
* Test the main method
*
* @return void
*/
public function testExecuteWithNoArgs() {
$path = $this->Task->path . 'TestBake';

$this->Task->expects($this->at(0))
->method('err')
->with($this->stringContains('You must'));

$this->Task->expects($this->never())
->method('createFile');

$this->Task->main();
public function testMain() {
$this->Task->expects($this->at(0))->method('in')
->will($this->returnValue('y'));

$Folder = new Folder($path);
$Folder->delete();
$this->Task->main('SimpleExample');
$this->assertPluginContents('SimpleExample');
}

/**
* Test Execute
* With no args, main should do nothing
*
* @return void
*/
public function testExecuteWithOneArg() {
$this->Task->expects($this->at(0))->method('in')
->will($this->returnValue('y'));

$path = $this->Task->path . 'BakeTestBake';
$files = [
'README.md',
'composer.json',
'config/routes.php',
'phpunit.xml.dist',
'src/Controller/AppController.php',
'tests/bootstrap.php',
'webroot/empty'
];

$i = 1;
foreach($files as $file) {
$this->Task->expects($this->at($i++))
->method('createFile')
->with($path . DS . $file);
}
public function testMainWithNoArgs() {
$this->Task->expects($this->at(0))
->method('err')
->with($this->stringContains('You must'));

$this->Task->main('BakeTestBake');
$this->Task->main();
}

/**
Expand All @@ -156,63 +116,34 @@ public function testExecuteWithOneArg() {
*
* @return void
*/
public function testExecuteUpdateComposer() {
public function testMainUpdateComposer() {
$this->Task->expects($this->at(0))->method('in')
->will($this->returnValue('y'));

$this->io->expects($this->any())
->method('askChoice')
->will($this->returnValue('y'));

$this->Task->Project = $this->getMock('ComposerProject', ['findComposer']);
$this->Task->Project->expects($this->at(0))
->method('findComposer')
->will($this->returnValue('composer.phar'));

$path = $this->Task->path . 'BakeTestBake';
if (!is_dir($path)) {
mkdir($path);
}
$file = $path . DS . 'composer.json';
$file = TMP . 'tests' . DS . 'main-composer.json';
file_put_contents($file, '{}');

$config = [
'name' => 'your-name-here/BakeTestBake',
'description' => 'BakeTestBake plugin for CakePHP',
'type' => 'cakephp-plugin',
'require' => [
'php' => '>=5.4',
'cakephp/plugin-installer' => '*',
'cakephp/cakephp' => '3.0.x-dev'
],
'require-dev' => [
'phpunit/phpunit' => '*'
],
'autoload' => [
'psr-4' => [
'BakeTestBake\\' => 'src',
],
],
'autoload-dev' => [
'psr-4' => [
'BakeTestBake\\Test\\' => 'tests',
'Cake\\Test\\' => './vendor/cakephp/cakephp/tests',
],
],
];
$config = json_encode($config, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES) . "\n";

$this->Task->expects($this->at(2))
->method('createFile')
->with($file, $config);
$this->Task->expects($this->any())
->method('_rootComposerFilePath')
->will($this->returnValue($file));

$this->Task->expects($this->once())
->method('callProcess')
->with('php ' . escapeshellarg('composer.phar') . ' dump-autoload');

$this->Task->main('BakeTestBake');
$this->Task->main('ComposerExample');

$Folder = new Folder($this->Task->path . 'BakeTestBake');
$Folder->delete();

$File = new File($file);
$File->delete();
$result = file_get_contents($file);
$this->assertSameAsFile(__FUNCTION__ . '.json', $result);
}

/**
Expand All @@ -228,7 +159,7 @@ public function testFindPathNonExistant() {
$paths[] = '/fake/path2';

$this->Task = $this->getMock('Bake\Shell\Task\PluginTask',
array('in', 'out', 'err', 'createFile', '_stop'),
array('in', 'out', 'err', '_stop'),
array($this->io)
);
$this->Task->path = TMP . 'tests' . DS;
Expand All @@ -244,4 +175,34 @@ public function testFindPathNonExistant() {
$this->Task->findPath($paths);
}

/**
* Check the baked plugin matches the expected output
*
* Compare to a static copy of the plugin in the comparison folder
*
* @param string $pluginName the name of the plugin to compare to
* @return void
*/
protected function assertPluginContents($pluginName) {
$comparisonRoot = $this->_compareBasePath . $pluginName . DS;
$comparisonDir = new Folder($comparisonRoot);
$comparisonFiles = $comparisonDir->findRecursive();

$bakedRoot = $this->Task->path . $pluginName . DS;
$bakedDir = new Folder($bakedRoot);
$bakedFiles = $comparisonDir->findRecursive();

$this->assertSame(
count($comparisonFiles),
count($bakedFiles),
'A different number of files were created than expected'
);

foreach ($comparisonFiles as $file) {
$file = substr($file, strlen($comparisonRoot));
$result = file_get_contents($bakedRoot . $file);
$this->assertSameAsFile($pluginName . DS . $file, $result);
}
}

}
4 changes: 2 additions & 2 deletions tests/comparisons/Plugin/testMainUpdateComposer.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
{
"autoload": {
"psr-4": {
"ComposerExample\\": "src"
"ComposerExample\\": "./plugins/ComposerExample/src"
}
},
"autoload-dev": {
"psr-4": {
"ComposerExample\\Test\\": "tests"
"ComposerExample\\Test\\": "./plugins/ComposerExample/tests"
}
}
}

0 comments on commit 8a20c31

Please sign in to comment.