Skip to content

Commit

Permalink
Merge pull request #644 from cakephp/filesystem
Browse files Browse the repository at this point in the history
Update use of deprecate File and Folder classes
  • Loading branch information
markstory authored Feb 20, 2020
2 parents a78d203 + 54ac868 commit 16aaca7
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 27 deletions.
11 changes: 8 additions & 3 deletions src/Command/PluginCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
use Cake\Core\App;
use Cake\Core\Configure;
use Cake\Core\Plugin;
use Cake\Filesystem\Folder;
use Cake\Filesystem\Filesystem;
use Cake\Utility\Inflector;

/**
Expand Down Expand Up @@ -195,10 +195,15 @@ protected function _generateFiles(
$paths = array_merge($paths, Configure::read('App.paths.templates'));
$paths[] = Plugin::templatePath('Bake');

$fs = new Filesystem();
$templates = [];
do {
$templatesPath = array_shift($paths) . BakeView::BAKE_TEMPLATE_FOLDER . '/Plugin';
$templatesDir = new Folder($templatesPath);
$templates = $templatesDir->findRecursive('.*\.(twig|php)');
if (is_dir($templatesPath)) {
$templates = array_keys(iterator_to_array(
$fs->findRecursive($templatesPath, '/.*\.(twig|php)/')
));
}
} while (!$templates);

sort($templates);
Expand Down
13 changes: 8 additions & 5 deletions src/Command/TestCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
use Cake\Core\Configure;
use Cake\Core\Exception\Exception;
use Cake\Core\Plugin;
use Cake\Filesystem\Folder;
use Cake\Filesystem\Filesystem;
use Cake\Http\Response;
use Cake\Http\ServerRequest as Request;
use Cake\ORM\Table;
Expand Down Expand Up @@ -204,12 +204,15 @@ protected function _getClassOptions(string $namespace): array
if ($this->plugin) {
$base = Plugin::classPath($this->plugin);
}

$path = $base . str_replace('\\', DS, $namespace);
$folder = new Folder($path);
[, $files] = $folder->read();
foreach ($files as $file) {
$classes[] = str_replace('.php', '', $file);
$files = (new Filesystem())->find($path);
foreach ($files as $fileObj) {
if ($fileObj->isFile()) {
$classes[] = substr($fileObj->getFileName(), 0, -4);
}
}
sort($classes);

return $classes;
}
Expand Down
6 changes: 2 additions & 4 deletions src/Shell/Task/BakeTask.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@
use Cake\Console\Shell;
use Cake\Core\Configure;
use Cake\Core\ConventionsTrait;
use Cake\Filesystem\File;

/**
* Base class for Bake Tasks.
Expand Down Expand Up @@ -181,9 +180,8 @@ protected function _getName(string $name): string
*/
protected function _deleteEmptyFile(string $path): void
{
$File = new File($path);
if ($File->exists()) {
$File->delete();
if (file_exists($path)) {
unlink($path);
$this->out(sprintf('<success>Deleted</success> `%s`', $path), 1, Shell::QUIET);
}
}
Expand Down
52 changes: 38 additions & 14 deletions tests/TestCase/Command/PluginCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
use Cake\Core\App;
use Cake\Core\Configure;
use Cake\Core\Plugin;
use Cake\Filesystem\Folder;
use Cake\Filesystem\Filesystem;
use SplFileInfo;

/**
* PluginCommand Test
Expand Down Expand Up @@ -66,8 +67,8 @@ public function setUp(): void
*/
public function tearDown(): void
{
$folder = new Folder(TMP . 'plugin_task');
$folder->delete();
$fs = new Filesystem();
$fs->deleteDir(TMP . 'plugin_task');

if (file_exists(APP . 'Application.php.bak')) {
rename(APP . 'Application.php.bak', APP . 'Application.php');
Expand Down Expand Up @@ -97,7 +98,6 @@ public function testMainCustomAppNamespace()
{
$this->exec('bake plugin Simple', ['y', 'n']);
$this->assertExitCode(Command::CODE_SUCCESS);
$this->assertPluginContents('Simple');

$bakedRoot = App::path('plugins')[0];
$appController = $bakedRoot . 'Simple/src/Controller/AppController.php';
Expand Down Expand Up @@ -189,8 +189,8 @@ public function testMainUpdateComposer()
copy(ROOT . 'composer.json.bak', $composerConfig);
unlink(ROOT . 'composer.json.bak');

$folder = new Folder(ROOT . 'vendor');
$folder->delete();
$fs = new Filesystem();
$fs->deleteDir(ROOT . 'vendor');
}

/**
Expand Down Expand Up @@ -244,23 +244,47 @@ public function assertPluginContents($pluginName)
{
$pluginName = str_replace('/', DS, $pluginName);
$comparisonRoot = $this->_compareBasePath . $pluginName . DS;
$comparisonDir = new Folder($comparisonRoot);
$comparisonFiles = $comparisonDir->findRecursive();
$comparisonFiles = $this->getFiles($comparisonRoot);

$bakedRoot = App::path('plugins')[0] . $pluginName . DS;
$bakedDir = new Folder($bakedRoot);
$bakedFiles = $comparisonDir->findRecursive();
$bakedFiles = $this->getFiles($bakedRoot);

$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);
foreach ($comparisonFiles as $key => $file) {
$result = file_get_contents($file);
$this->assertSameAsFile($bakedFiles[$key], $result);
}
}

/**
* Get recursive files list for given path.
*
* @param string $path
* @return array
*/
protected function getFiles(string $path): array
{
if (!is_dir($path)) {
return [];
}

$fs = new Filesystem();

$iterator = $fs->findRecursive(
$path,
function (SplFileInfo $fileInfo) {
return $fileInfo->isFile();
}
);

$files = array_keys(iterator_to_array($iterator));
sort($files);

return $files;
}
}
3 changes: 2 additions & 1 deletion tests/TestCase/Command/SimpleBakeCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ class SimpleBakeCommandTest extends TestCase
public function setUp(): void
{
parent::setUp();
$this->_compareBasePath = Plugin::path('Bake') . 'tests' . DS . 'comparisons' . DS . 'Simple' . DS;
$this->_compareBasePath = Plugin::path('Bake') . 'tests' . DS . 'comparisons' . DS
. 'Plugin' . DS . 'Simple' . DS;
$this->useCommandRunner();
$this->setAppNamespace('Bake\Test\App');
}
Expand Down
4 changes: 4 additions & 0 deletions tests/comparisons/Plugin/Company/Example/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/composer.lock
/phpunit.xml
/vendor
config/Migrations/schema-dump-default.lock
File renamed without changes.
File renamed without changes.
4 changes: 4 additions & 0 deletions tests/comparisons/Plugin/SimpleExample/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/composer.lock
/phpunit.xml
/vendor
config/Migrations/schema-dump-default.lock

0 comments on commit 16aaca7

Please sign in to comment.