Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 .github/scripts/random-tests-config.txt
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ AutoReview
Autoloader
# Cache
CLI
# Commands
Commands
Config
Cookie
# DataCaster
Expand Down
12 changes: 10 additions & 2 deletions system/Cache/FactoriesCache/FileVarExportHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,19 @@ public function save(string $key, mixed $val): void
{
$val = var_export($val, true);

if (! is_dir($this->path) && ! @mkdir($this->path, 0777, true) && ! is_dir($this->path)) {
Comment thread
memleakd marked this conversation as resolved.
return;
Comment thread
memleakd marked this conversation as resolved.
}

// Write to temp file first to ensure atomicity
$tmp = $this->path . "/{$key}." . uniqid('', true) . '.tmp';
file_put_contents($tmp, '<?php return ' . $val . ';', LOCK_EX);
if (file_put_contents($tmp, '<?php return ' . $val . ';', LOCK_EX) === false) {
return;
Comment thread
memleakd marked this conversation as resolved.
}

rename($tmp, $this->path . "/{$key}");
if (! @rename($tmp, $this->path . "/{$key}")) {
Comment thread
memleakd marked this conversation as resolved.
@unlink($tmp);
Comment thread
memleakd marked this conversation as resolved.
}
}

public function delete(string $key): void
Expand Down
2 changes: 1 addition & 1 deletion system/Commands/Database/ShowTableInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -243,7 +243,7 @@ private function makeTbodyForShowAllTables(array $tables): array
{
$this->removeDBPrefix();

foreach ($tables as $id => $tableName) {
foreach (array_values($tables) as $id => $tableName) {
Comment thread
memleakd marked this conversation as resolved.
Outdated
$table = $this->db->protectIdentifiers($tableName);
$db = $this->db->query("SELECT * FROM {$table}");

Expand Down
4 changes: 3 additions & 1 deletion system/HotReloader/DirectoryHasher.php
Original file line number Diff line number Diff line change
Expand Up @@ -73,10 +73,12 @@ public function hashDirectory(string $path): string

foreach ($iterator as $file) {
if ($file->isFile()) {
$hashes[] = md5_file($file->getRealPath());
$hashes[$file->getRealPath()] = md5_file($file->getRealPath());
}
}

ksort($hashes);

return md5(implode('', $hashes));
}
}
2 changes: 1 addition & 1 deletion system/Log/Handlers/FileHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public function handle($level, $message): bool
fclose($fp);

if ($newfile) {
chmod($filepath, $this->filePermissions);
@chmod($filepath, $this->filePermissions);
Comment thread
memleakd marked this conversation as resolved.
}

return is_int($result);
Expand Down
10 changes: 10 additions & 0 deletions tests/_support/Commands/Unsuffixable.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,4 +76,14 @@ public function run(array $params): void
$this->setEnabledSuffixing(false);
$this->generateClass($params);
}

protected function prepare(string $class): string
{
return $this->parseTemplate(
$class,
['{group}', '{command}'],
['Generators', 'make:foo'],
['type' => 'basic'],
);
}
}
109 changes: 62 additions & 47 deletions tests/system/AutoReview/CreateNewChangelogTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,56 +48,71 @@ protected function setUp(): void
#[DataProvider('provideCreateNewChangelog')]
public function testCreateNewChangelog(string $mode): void
{
$output = exec('git status --porcelain | wc -l');

if ($output !== '0') {
$currentVersion = $this->currentVersion;
$newVersion = $this->incrementVersion($currentVersion, $mode);
$versionWithoutDots = str_replace('.', '', $newVersion);
$changelogPath = "./user_guide_src/source/changelogs/v{$newVersion}.rst";
$upgradePath = "./user_guide_src/source/installation/upgrade_{$versionWithoutDots}.rst";
$trackedPathArgs = implode(' ', array_map(escapeshellarg(...), [
'./system/CodeIgniter.php',
'./user_guide_src/source/changelogs/index.rst',
'./user_guide_src/source/installation/upgrading.rst',
]));
$generatedPathArgs = implode(' ', array_map(escapeshellarg(...), [
$changelogPath,
$upgradePath,
]));
$statusCount = trim((string) exec(
"git status --porcelain -- {$trackedPathArgs} {$generatedPathArgs} | wc -l",
));

if ($statusCount !== '0') {
$this->markTestSkipped('You have uncommitted operations that will be erased by this test.');
}

$currentVersion = $this->currentVersion;
$newVersion = $this->incrementVersion($currentVersion, $mode);

exec(
sprintf('php ./admin/create-new-changelog.php %s %s --dry-run', $currentVersion, $newVersion),
$output,
$exitCode,
);

$this->assertSame(0, $exitCode, "Script exited with code {$exitCode}. Output: " . implode("\n", $output));

$this->assertStringContainsString(
"public const CI_VERSION = '{$newVersion}-dev';",
$this->getContents('./system/CodeIgniter.php'),
);

$this->assertFileExists("./user_guide_src/source/changelogs/v{$newVersion}.rst");
$this->assertStringContainsString(
"Version {$newVersion}",
$this->getContents("./user_guide_src/source/changelogs/v{$newVersion}.rst"),
);
$this->assertStringContainsString(
"**{$newVersion} release of CodeIgniter4**",
$this->getContents("./user_guide_src/source/changelogs/v{$newVersion}.rst"),
);
$this->assertStringContainsString(
$newVersion,
$this->getContents('./user_guide_src/source/changelogs/index.rst'),
);

$versionWithoutDots = str_replace('.', '', $newVersion);
$this->assertFileExists("./user_guide_src/source/installation/upgrade_{$versionWithoutDots}.rst");
$this->assertStringContainsString(
"Upgrading from {$currentVersion} to {$newVersion}",
$this->getContents("./user_guide_src/source/installation/upgrade_{$versionWithoutDots}.rst"),
);
$this->assertStringContainsString(
"upgrade_{$versionWithoutDots}",
$this->getContents('./user_guide_src/source/installation/upgrading.rst'),
);

// cleanup added and modified files
exec('git restore .');
exec('git clean -fd');
try {
$output = [];

exec(
sprintf('php ./admin/create-new-changelog.php %s %s --dry-run', $currentVersion, $newVersion),
$output,
$exitCode,
);

$this->assertSame(0, $exitCode, "Script exited with code {$exitCode}. Output: " . implode("\n", $output));

$this->assertStringContainsString(
"public const CI_VERSION = '{$newVersion}-dev';",
$this->getContents('./system/CodeIgniter.php'),
);

$this->assertFileExists($changelogPath);
$this->assertStringContainsString(
"Version {$newVersion}",
$this->getContents($changelogPath),
);
$this->assertStringContainsString(
"**{$newVersion} release of CodeIgniter4**",
$this->getContents($changelogPath),
);
$this->assertStringContainsString(
$newVersion,
$this->getContents('./user_guide_src/source/changelogs/index.rst'),
);

$this->assertFileExists($upgradePath);
$this->assertStringContainsString(
"Upgrading from {$currentVersion} to {$newVersion}",
$this->getContents($upgradePath),
);
$this->assertStringContainsString(
"upgrade_{$versionWithoutDots}",
$this->getContents('./user_guide_src/source/installation/upgrading.rst'),
);
} finally {
exec("git restore -- {$trackedPathArgs}");
exec("git clean -f -- {$generatedPathArgs}");
}
}

/**
Expand Down
1 change: 1 addition & 0 deletions tests/system/Commands/Cache/ClearCacheTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ public function testClearCacheFails(): void
Services::injectMock('cache', $cache);

command('cache:clear');
Services::resetSingle('cache');

$this->assertSame(
"\nError while clearing the cache.\n",
Expand Down
23 changes: 21 additions & 2 deletions tests/system/Commands/CreateDatabaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ final class CreateDatabaseTest extends CIUnitTestCase

protected function setUp(): void
{
$this->connection = Database::connect();
$this->connection = Database::connect(null, false);

parent::setUp();

Expand All @@ -54,12 +54,31 @@ private function dropDatabase(): void
if ($this->connection instanceof SQLite3Connection) {
$file = WRITEPATH . 'database.db';

$this->closeDatabaseConnections();

if (is_file($file)) {
unlink($file);
}
} elseif (Database::utils('tests')->databaseExists('database')) {

return;
}

if (Database::utils('tests')->databaseExists('database')) {
Database::forge()->dropDatabase('database');
}

$this->closeDatabaseConnections();
}

private function closeDatabaseConnections(): void
{
$this->connection->close();

foreach (Database::getConnections() as $connection) {
$connection->close();
}

$this->setPrivateProperty(Database::class, 'instances', []);
}

protected function getBuffer(): string
Expand Down
71 changes: 22 additions & 49 deletions tests/system/Commands/Database/MigrateStatusTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,33 +29,19 @@ final class MigrateStatusTest extends CIUnitTestCase
use StreamFilterTrait;
use DatabaseTestTrait;

private string $migrationFileFrom = SUPPORTPATH . 'MigrationTestMigrations/Database/Migrations/2018-01-24-102301_Some_migration.php';
private string $migrationFileTo = APPPATH . 'Database/Migrations/2018-01-24-102301_Some_migration.php';
private string $migrationNamespace = 'Tests\\Support\\MigrationTestMigrations';
private string $migrationNamespacePath = SUPPORTPATH . 'MigrationTestMigrations/';

protected function setUp(): void
{
$this->resetServices();

parent::setUp();

Database::connect()->table('migrations')->emptyTable();
Database::forge()->dropTable('foo', true);

if (! is_file($this->migrationFileFrom)) {
$this->fail(clean_path($this->migrationFileFrom) . ' is not found.');
}

if (is_file($this->migrationFileTo)) {
@unlink($this->migrationFileTo);
}

copy($this->migrationFileFrom, $this->migrationFileTo);

$contents = file_get_contents($this->migrationFileTo);
$contents = str_replace(
'namespace Tests\Support\MigrationTestMigrations\Database\Migrations;',
'namespace App\Database\Migrations;',
$contents,
);
file_put_contents($this->migrationFileTo, $contents);
service('autoloader')->addNamespace($this->migrationNamespace, $this->migrationNamespacePath);

putenv('NO_COLOR=1');
CLI::init();
Expand All @@ -66,13 +52,12 @@ protected function tearDown(): void
parent::tearDown();

Database::connect()->table('migrations')->emptyTable();

if (is_file($this->migrationFileTo)) {
@unlink($this->migrationFileTo);
}
Database::forge()->dropTable('foo', true);

putenv('NO_COLOR');
CLI::init();

$this->resetServices();
}

public function testMigrateAllWithWithTwoNamespaces(): void
Expand All @@ -82,41 +67,29 @@ public function testMigrateAllWithWithTwoNamespaces(): void

command('migrate:status');

$result = str_replace(PHP_EOL, "\n", $this->getStreamFilterBuffer());
$result = preg_replace('/\d{4}-\d\d-\d\d \d\d:\d\d:\d\d/', 'YYYY-MM-DD HH:MM:SS', $result);
$expected = <<<'EOL'
+---------------+-------------------+--------------------+-------+---------------------+-------+
| Namespace | Version | Filename | Group | Migrated On | Batch |
+---------------+-------------------+--------------------+-------+---------------------+-------+
| App | 2018-01-24-102301 | Some_migration | tests | YYYY-MM-DD HH:MM:SS | 1 |
| Tests\Support | 20160428212500 | Create_test_tables | tests | YYYY-MM-DD HH:MM:SS | 1 |
+---------------+-------------------+--------------------+-------+---------------------+-------+


EOL;
$this->assertSame($expected, $result);
$this->assertMigrationStatusHasBothNamespaceMigrations();
Comment thread
memleakd marked this conversation as resolved.
}

public function testMigrateWithWithTwoNamespaces(): void
{
command('migrate -n App');
command('migrate -n Tests\\\\Support\\\\MigrationTestMigrations');
command('migrate -n Tests\\\\Support');
$this->resetStreamFilterBuffer();

command('migrate:status');

$result = str_replace(PHP_EOL, "\n", $this->getStreamFilterBuffer());
$result = preg_replace('/\d{4}-\d\d-\d\d \d\d:\d\d:\d\d/', 'YYYY-MM-DD HH:MM:SS', $result);
$expected = <<<'EOL'
+---------------+-------------------+--------------------+-------+---------------------+-------+
| Namespace | Version | Filename | Group | Migrated On | Batch |
+---------------+-------------------+--------------------+-------+---------------------+-------+
| App | 2018-01-24-102301 | Some_migration | tests | YYYY-MM-DD HH:MM:SS | 1 |
| Tests\Support | 20160428212500 | Create_test_tables | tests | YYYY-MM-DD HH:MM:SS | 2 |
+---------------+-------------------+--------------------+-------+---------------------+-------+

$this->assertMigrationStatusHasBothNamespaceMigrations();
}

EOL;
$this->assertSame($expected, $result);
private function assertMigrationStatusHasBothNamespaceMigrations(): void
{
$result = str_replace(PHP_EOL, "\n", $this->getStreamFilterBuffer());

$this->assertStringContainsString($this->migrationNamespace, $result);
$this->assertStringContainsString('2018-01-24-102301', $result);
$this->assertStringContainsString('Some_migration', $result);
$this->assertStringContainsString('Tests\Support', $result);
$this->assertStringContainsString('20160428212500', $result);
$this->assertStringContainsString('Create_test_tables', $result);
}
}
11 changes: 8 additions & 3 deletions tests/system/Commands/Database/ShowTableInfoMockIOTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\DatabaseTestTrait;
use CodeIgniter\Test\Mock\MockInputOutput;
use Config\Database;
use PHPUnit\Framework\Attributes\Group;

/**
Expand Down Expand Up @@ -51,12 +52,16 @@ protected function tearDown(): void

public function testDbTableWithInputs(): void
{
$tableIndex = array_search('db_migrations', Database::connect()->listTables(), true);

$this->assertIsInt($tableIndex);

// Set MockInputOutput to CLI.
$io = new MockInputOutput();
CLI::setInputOutput($io);

// User will input "a" (invalid value) and "0".
$io->setInputs(['a', '0']);
// User will input "a" (invalid value) and then select db_migrations.
$io->setInputs(['a', (string) $tableIndex]);

command('db:table');

Expand All @@ -71,7 +76,7 @@ public function testDbTableWithInputs(): void
$result,
);
$this->assertMatchesRegularExpression(
'/Which table do you want to see\? \[[\d,\s]+\]\: 0/',
'/Which table do you want to see\? \[[\d,\s]+\]\: ' . $tableIndex . '/',
$result,
);
$this->assertMatchesRegularExpression(
Expand Down
Loading
Loading