Skip to content

Commit

Permalink
Merge pull request #4867 from paulbalandan/spark-command-lost
Browse files Browse the repository at this point in the history
  • Loading branch information
MGatner authored Jun 25, 2021
2 parents 6797d1f + a351963 commit f072c86
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 3 deletions.
6 changes: 3 additions & 3 deletions system/CLI/GeneratorTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,14 +229,14 @@ protected function qualifyClassName(): string
// Trims input, normalize separators, and ensure that all paths are in Pascalcase.
$class = ltrim(implode('\\', array_map('pascalize', explode('\\', str_replace('/', '\\', trim($class))))), '\\/');

// Gets the namespace from input.
$namespace = trim(str_replace('/', '\\', $this->getOption('namespace') ?? APP_NAMESPACE), '\\');
// Gets the namespace from input. Don't forget the ending backslash!
$namespace = trim(str_replace('/', '\\', $this->getOption('namespace') ?? APP_NAMESPACE), '\\') . '\\';

if (strncmp($class, $namespace, strlen($namespace)) === 0) {
return $class; // @codeCoverageIgnore
}

return $namespace . '\\' . $this->directory . '\\' . str_replace('/', '\\', $class);
return $namespace . $this->directory . '\\' . str_replace('/', '\\', $class);
}

/**
Expand Down
20 changes: 20 additions & 0 deletions tests/system/Commands/CommandGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -119,4 +119,24 @@ public function testGeneratorPreservesCaseButChangesComponentName(): void
$this->assertStringContainsString('File created: ', CITestStreamFilter::$buffer);
$this->assertFileExists(APPPATH . 'Controllers/TestModuleController.php');
}

/**
* @see https://github.com/codeigniter4/CodeIgniter4/issues/4857
*/
public function testGeneratorIsNotConfusedWithNamespaceLikeClassNames(): void
{
$time = time();
$notExists = true;
command('make:migration App_Lesson');

// we got 5 chances to prove that the file created went to app/Database/Migrations
foreach (range(0, 4) as $increment) {
$expectedFile = sprintf('%sDatabase/Migrations/%s_AppLesson.php', APPPATH, gmdate('Y-m-d-His', $time + $increment));
clearstatcache(true, $expectedFile);

$notExists = $notExists && ! is_file($expectedFile);
}

$this->assertFalse($notExists, 'Creating migration file for class "AppLesson" did not go to "app/Database/Migrations"');
}
}

0 comments on commit f072c86

Please sign in to comment.