Browse Source

Update UpgradeCommand to rename template special folders.

ADmad 7 years ago
parent
commit
71cd422f0e
2 changed files with 113 additions and 5 deletions
  1. 44 3
      src/Command/UpgradeCommand.php
  2. 69 2
      tests/TestCase/Command/UpgradeCommandTest.php

+ 44 - 3
src/Command/UpgradeCommand.php

@@ -92,7 +92,11 @@ class UpgradeCommand extends Command
     protected function processTemplates()
     {
         if (is_dir($this->path . 'src/Template')) {
-            $this->rename($this->path . 'src/Template', $this->path . 'templates');
+            $this->rename(
+                $this->path . 'src/Template',
+                $this->path . 'templates'
+            );
+            $this->renameSubFolders($this->path . 'templates');
             $this->changeExt($this->path . 'templates');
         }
 
@@ -117,12 +121,49 @@ class UpgradeCommand extends Command
         $iterIter = new RecursiveIteratorIterator($dirIter);
         $templateDirs = new RegexIterator(
             $iterIter,
-            '/Template\/\.$/',
+            '#/Template/\.$#',
             RecursiveRegexIterator::REPLACE
         );
 
         foreach ($templateDirs as $key => $val) {
-            $this->rename($val . 'Template', $val . 'Template/../../templates');
+            $this->rename(
+                $val . '/Template',
+                $val . '/../templates'
+            );
+            $this->renameSubFolders($val . '/../templates');
+        }
+    }
+
+    /**
+     * Rename Layout, Element, Cell, Plugin to layout, element, cell, plugin
+     * respectively.
+     *
+     * @param string $path Path.
+     * @return void
+     */
+    protected function renameSubFolders($path)
+    {
+        $dirIter = new RecursiveDirectoryIterator(
+            $path,
+            RecursiveDirectoryIterator::UNIX_PATHS
+        );
+        $iterIter = new RecursiveIteratorIterator($dirIter);
+
+        $folders = ['Layout', 'Element', 'Cell', 'Plugin'];
+
+        foreach ($folders as $folder) {
+            $templateDirs = new RegexIterator(
+                $iterIter,
+                '#/' . $folder . '/\.$#',
+                RecursiveRegexIterator::SPLIT
+            );
+
+            foreach ($templateDirs as $key => $val) {
+                $this->rename(
+                    $val[0] . '/' . $folder,
+                    $val[0] . '/' . strtolower($folder)
+                );
+            }
         }
     }
 

+ 69 - 2
tests/TestCase/Command/UpgradeCommandTest.php

@@ -19,6 +19,7 @@ use Cake\Core\Configure;
 use Cake\TestSuite\ConsoleIntegrationTestTrait;
 use Cake\TestSuite\TestCase;
 use org\bovigo\vfs\vfsStream;
+use org\bovigo\vfs\visitor\vfsStreamStructureVisitor;
 
 /**
  * UpgradeCommand test.
@@ -55,6 +56,17 @@ class UpgradeCommandTest extends TestCase
         $ds = [
             'src' => [
                 'Template' => [
+                    'Element' => ['foo.ctp' => ''],
+                    'Layout' => ['default.ctp' => ''],
+                    'Cell' => [
+                        'MyCell' => ['display.ctp' => ''],
+                    ],
+                    'Plugin' => [
+                        'TestPlugin' => [
+                            'Element' => ['bar.ctp' => ''],
+                            'Posts' => ['index.ctp' => ''],
+                        ],
+                    ],
                     'Pages' => [
                         'home.ctp' => '',
                     ],
@@ -63,13 +75,24 @@ class UpgradeCommandTest extends TestCase
             'plugins' => [
                 'TestPlugin' => [
                     'src' => [
+                        // This is ensure "src/Cell" does not get renamed.
+                        'Cell' => [
+                            'TestPluginCell.php' => '',
+                        ],
                         'Template' => [
                             'Element' => [
                                 'foo.ctp' => '',
                             ],
+                            'Layout' => ['plugin.ctp' => ''],
+                            'Cell' => [
+                                'TestPluginCell' => ['bar.ctp' => ''],
+                            ],
                         ],
                     ],
                 ],
+                'PluginWithoutTemplates' => [
+                    'src' => [],
+                ],
             ],
         ];
 
@@ -102,7 +125,51 @@ class UpgradeCommandTest extends TestCase
     {
         $this->exec('upgrade templates --path ' . $this->fs->url());
 
-        $this->assertTrue($this->fs->hasChild('templates/Pages/home.php'));
-        $this->assertTrue($this->fs->hasChild('plugins/TestPlugin/templates/Element/foo.php'));
+        $ds = [
+            'src' => [],
+            'templates' => [
+                'element' => ['foo.php' => ''],
+                'layout' => ['default.php' => ''],
+                'cell' => [
+                    'MyCell' => ['display.php' => ''],
+                ],
+                'plugin' => [
+                    'TestPlugin' => [
+                        'element' => ['bar.php' => ''],
+                        'Posts' => ['index.php' => ''],
+                    ],
+                ],
+                'Pages' => [
+                    'home.php' => '',
+                ],
+            ],
+            'plugins' => [
+                'TestPlugin' => [
+                    'src' => [
+                        // This is ensure "src/Cell" does not get renamed.
+                        'Cell' => [
+                            'TestPluginCell.php' => '',
+                        ],
+                    ],
+                    'templates' => [
+                        'element' => [
+                            'foo.php' => '',
+                        ],
+                        'layout' => ['plugin.php' => ''],
+                        'cell' => [
+                            'TestPluginCell' => ['bar.php' => ''],
+                        ],
+                    ],
+                ],
+                'PluginWithoutTemplates' => [
+                    'src' => [],
+                ],
+            ],
+        ];
+
+        $this->assertEquals(
+            ['root' => $ds],
+            vfsStream::inspect(new vfsStreamStructureVisitor())->getStructure()
+        );
     }
 }