feat: enhance purge commands with environment handling and testing improvements

This commit is contained in:
hamed
2026-07-20 11:54:02 +03:30
parent 28725792d7
commit a8d33ceeaf
5 changed files with 70 additions and 25 deletions
+11 -16
View File
@@ -2,6 +2,8 @@
namespace App\Tests\Doctor;
use App\Doctor\Command\PurgeDoctorsCommand;
use Doctrine\DBAL\Connection;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\Console\Command\Command;
@@ -30,26 +32,19 @@ class PurgeDoctorsCommandTest extends KernelTestCase
$this->assertStringContainsString('dry-run', $tester->getDisplay());
}
/**
* محیط از kernel تزریق می‌شود، پس گارد با ساختِ مستقیم کامند در محیط prod تست
* می‌شود — نه با دست‌کاری $_ENV که دیگر خوانده نمی‌شود.
*/
public function testRefusesOnProdWithNoEscapeHatch(): void
{
self::bootKernel();
$original = $_ENV['APP_ENV'] ?? null;
$_ENV['APP_ENV'] = 'prod';
$command = new PurgeDoctorsCommand(static::getContainer()->get(Connection::class), 'prod');
$tester = new CommandTester($command);
try {
$tester = new CommandTester(
(new Application(self::$kernel))->find('app:doctors:purge')
);
$tester->execute(['--force' => true]);
$tester->execute(['--force' => true]);
$this->assertSame(Command::FAILURE, $tester->getStatusCode());
$this->assertStringContainsString('فقط در محیط dev', $tester->getDisplay());
} finally {
if ($original === null) {
unset($_ENV['APP_ENV']);
} else {
$_ENV['APP_ENV'] = $original;
}
}
$this->assertSame(Command::FAILURE, $tester->getStatusCode());
$this->assertStringContainsString('فقط در محیط dev', $tester->getDisplay());
}
}