Skip to content
Open
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 .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)) {
return;
}

// 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;
}

rename($tmp, $this->path . "/{$key}");
if (! @rename($tmp, $this->path . "/{$key}")) {
@unlink($tmp);
}
}

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) {
$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);
}

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'],
);
}
}
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();
}

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
6 changes: 1 addition & 5 deletions tests/system/Commands/Database/ShowTableInfoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\DatabaseTestTrait;
use CodeIgniter\Test\StreamFilterTrait;
use Config\Database;
use PHPUnit\Framework\Attributes\Group;
use Tests\Support\Database\Seeds\CITestSeeder;

Expand All @@ -30,7 +29,7 @@ final class ShowTableInfoTest extends CIUnitTestCase
use DatabaseTestTrait;
use StreamFilterTrait;

protected $migrateOnce = true;
protected $seed = CITestSeeder::class;

protected function setUp(): void
{
Expand Down Expand Up @@ -121,9 +120,6 @@ public function testDbTableMetadata(): void

public function testDbTableDesc(): void
{
$seeder = Database::seeder();
$seeder->call(CITestSeeder::class);

command('db:table db_user --desc');

$result = $this->getNormalizedResult();
Expand Down
2 changes: 2 additions & 0 deletions tests/system/Commands/DatabaseCommandsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
namespace CodeIgniter\Commands;

use CodeIgniter\Test\CIUnitTestCase;
use CodeIgniter\Test\DatabaseTestTrait;
use CodeIgniter\Test\StreamFilterTrait;
use PHPUnit\Framework\Attributes\Group;

Expand All @@ -25,6 +26,7 @@
#[Group('DatabaseLive')]
final class DatabaseCommandsTest extends CIUnitTestCase
{
use DatabaseTestTrait;
use StreamFilterTrait;

protected function tearDown(): void
Expand Down
22 changes: 14 additions & 8 deletions tests/system/Commands/Generators/CommandGeneratorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,21 @@ final class CommandGeneratorTest extends CIUnitTestCase

protected function tearDown(): void
{
$result = str_replace(["\033[0;32m", "\033[0m", "\n"], '', $this->getStreamFilterBuffer());
$file = str_replace('APPPATH' . DIRECTORY_SEPARATOR, APPPATH, trim(substr($result, 14)));
$dir = dirname($file);
preg_match_all('/File (?:created|overwritten): (APPPATH[^\r\n\x1b]+)/', $this->getStreamFilterBuffer(), $matches);

if (is_file($file)) {
unlink($file);
}
if (is_dir($dir) && str_contains($dir, 'Commands')) {
rmdir($dir);
foreach ($matches[1] as $file) {
$path = str_replace('APPPATH' . DIRECTORY_SEPARATOR, APPPATH, $file);

if (is_file($path)) {
unlink($path);
}

$dir = dirname($path);
$dirFiles = is_dir($dir) ? scandir($dir) : false;

if (str_starts_with($dir, APPPATH . 'Commands') && $dirFiles !== false && count($dirFiles) === 2) {
rmdir($dir);
}
}
}

Expand Down
Loading
Loading