Skip to content

feat: spark make:test creates test files in /tests/ directory #8374

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/Config/Autoload.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ class Autoload extends AutoloadConfig
* @var array<string, list<string>|string>
*/
public $psr4 = [
APP_NAMESPACE => APPPATH,
APP_NAMESPACE => [APPPATH, TESTPATH . 'app'],
];

/**
Expand Down
7 changes: 4 additions & 3 deletions system/Autoloader/Autoloader.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ class Autoloader
/**
* Stores namespaces as key, and path as values.
*
* @var array<string, array<string>>
* @var array<string, list<string>>
*/
protected $prefixes = [];

/**
* Stores class name as key, and path as values.
*
* @var array<string, string>
* @var array<class-string, string>
*/
protected $classmap = [];

Expand Down Expand Up @@ -215,7 +215,8 @@ public function addNamespace($namespace, ?string $path = null)
*
* If a prefix param is set, returns only paths to the given prefix.
*
* @return array
* @return array<string, list<string>>|list<string>
* @phpstan-return ($prefix is null ? array<string, list<string>> : list<string>)
*/
public function getNamespace(?string $prefix = null)
{
Expand Down
10 changes: 5 additions & 5 deletions system/Autoloader/FileLocator.php
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,8 @@ public function getClassname(string $file): string
* 'app/Modules/foo/Config/Routes.php',
* 'app/Modules/bar/Config/Routes.php',
* ]
*
* @return list<string>
*/
public function search(string $path, string $ext = 'php', bool $prioritizeApp = true): array
{
Expand Down Expand Up @@ -203,7 +205,7 @@ public function search(string $path, string $ext = 'php', bool $prioritizeApp =
}

// Remove any duplicates
return array_unique($foundPaths);
return array_values(array_unique($foundPaths));
}

/**
Expand Down Expand Up @@ -237,7 +239,7 @@ protected function getNamespaces()
foreach ($this->autoloader->getNamespace() as $prefix => $paths) {
foreach ($paths as $path) {
if ($prefix === 'CodeIgniter') {
$system = [
$system[] = [
'prefix' => $prefix,
'path' => rtrim($path, '\\/') . DIRECTORY_SEPARATOR,
];
Expand All @@ -252,9 +254,7 @@ protected function getNamespaces()
}
}

$namespaces[] = $system;

return $namespaces;
return array_merge($namespaces, $system);
}

/**
Expand Down
68 changes: 66 additions & 2 deletions system/Commands/Generators/TestGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@
namespace CodeIgniter\Commands\Generators;

use CodeIgniter\CLI\BaseCommand;
use CodeIgniter\CLI\CLI;
use CodeIgniter\CLI\GeneratorTrait;
use Config\Services;

/**
* Generates a skeleton command file.
Expand Down Expand Up @@ -66,7 +68,8 @@ class TestGenerator extends BaseCommand
* @var array<string, string>
*/
protected $options = [
'--force' => 'Force overwrite existing file.',
'--namespace' => 'Set root namespace. Default: "APP_NAMESPACE".',
'--force' => 'Force overwrite existing file.',
];

/**
Expand All @@ -76,9 +79,70 @@ public function run(array $params)
{
$this->component = 'Test';
$this->template = 'test.tpl.php';
$this->namespace = 'Tests';

$this->classNameLang = 'CLI.generator.className.test';
$this->generateClass($params);
}

/**
* Builds the test file path from the class name.
*
* @param string $class namespaced classname or namespaced view.
*/
protected function buildPath(string $class): string
{
$namespace = $this->getNamespace();

$base = $this->searchTestFilePath($namespace);

if ($base === null) {
CLI::error(
lang('CLI.namespaceNotDefined', [$namespace]),
'light_gray',
'red'
);
CLI::newLine();

return '';
}

$realpath = realpath($base);
$base = ($realpath !== false) ? $realpath : $base;

$file = $base . DIRECTORY_SEPARATOR
. str_replace(
'\\',
DIRECTORY_SEPARATOR,
trim(str_replace($namespace . '\\', '', $class), '\\')
) . '.php';

return implode(
DIRECTORY_SEPARATOR,
array_slice(
explode(DIRECTORY_SEPARATOR, $file),
0,
-1
)
) . DIRECTORY_SEPARATOR . $this->basename($file);
}

/**
* Returns test file path for the namespace.
*/
private function searchTestFilePath(string $namespace): ?string
{
$bases = Services::autoloader()->getNamespace($namespace);

$base = null;

foreach ($bases as $candidate) {
if (str_contains($candidate, '/tests/')) {
$base = $candidate;

break;
}
}

return $base;
}
}
4 changes: 2 additions & 2 deletions system/Config/AutoloadConfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class AutoloadConfig
* @var array<string, string>
*/
protected $corePsr4 = [
'CodeIgniter' => SYSTEMPATH,
'CodeIgniter' => [SYSTEMPATH, TESTPATH . 'system'],
'Config' => APPPATH . 'Config',
'Tests' => ROOTPATH . 'tests',
];
Expand All @@ -106,7 +106,7 @@ class AutoloadConfig
* searched for within one or more directories as they would if they
* were being autoloaded through a namespace.
*
* @var array<string, string>
* @var array<class-string, string>
*/
protected $coreClassmap = [
AbstractLogger::class => SYSTEMPATH . 'ThirdParty/PSR/Log/AbstractLogger.php',
Expand Down
4 changes: 2 additions & 2 deletions tests/system/Autoloader/AutoloaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,13 +97,13 @@ public function testInitializeTwice(): void
$loader->initialize(new Autoload(), new Modules());

$ns = $loader->getNamespace();
$this->assertCount(1, $ns['App']);
$this->assertCount(2, $ns['App']);
$this->assertSame('ROOTPATH/app', clean_path($ns['App'][0]));

$loader->initialize(new Autoload(), new Modules());

$ns = $loader->getNamespace();
$this->assertCount(1, $ns['App']);
$this->assertCount(2, $ns['App']);
$this->assertSame('ROOTPATH/app', clean_path($ns['App'][0]));
}

Expand Down
2 changes: 1 addition & 1 deletion tests/system/Commands/TestGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,6 @@ protected function tearDown(): void
public function testGenerateTest(): void
{
command('make:test Foo/Bar');
$this->assertFileExists(ROOTPATH . 'tests/Foo/Bar.php');
$this->assertFileExists(ROOTPATH . 'tests/app/Foo/Bar.php');
}
}
1 change: 1 addition & 0 deletions user_guide_src/source/cli/cli_generators.rst
Original file line number Diff line number Diff line change
Expand Up @@ -248,6 +248,7 @@ Argument:

Options:
========
* ``--namespace``: Set the root namespace. Defaults to value of ``APP_NAMESPACE``.
* ``--force``: Set this flag to overwrite existing files on destination.

make:migration
Expand Down