feat: enhance purge commands with environment handling and testing improvements
This commit is contained in:
@@ -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());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,10 +3,12 @@
|
||||
namespace App\Tests\Doctor;
|
||||
|
||||
use App\Auth\Entity\User;
|
||||
use App\Doctor\Command\PurgeUnclaimedDoctorsCommand;
|
||||
use App\Doctor\Entity\Doctor;
|
||||
use Doctrine\DBAL\Connection;
|
||||
use Symfony\Bundle\FrameworkBundle\Console\Application;
|
||||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
|
||||
use Symfony\Component\Console\Command\Command;
|
||||
use Symfony\Component\Console\Tester\CommandTester;
|
||||
|
||||
/**
|
||||
@@ -78,6 +80,39 @@ class PurgeUnclaimedDoctorsCommandTest extends KernelTestCase
|
||||
);
|
||||
}
|
||||
|
||||
/** برخلاف app:doctors:purge، این کامند روی prod قابل اجراست — ولی فقط با سوییچ صریح. */
|
||||
private function prodTester(): CommandTester
|
||||
{
|
||||
self::bootKernel();
|
||||
|
||||
return new CommandTester(
|
||||
new PurgeUnclaimedDoctorsCommand(static::getContainer()->get(Connection::class), 'prod')
|
||||
);
|
||||
}
|
||||
|
||||
public function testRefusesOnProdWithoutExplicitSwitch(): void
|
||||
{
|
||||
$doctor = $this->makeDoctor('irimc', 'unclaimed');
|
||||
|
||||
$tester = $this->prodTester();
|
||||
$tester->execute(['--force' => true]);
|
||||
|
||||
$this->assertSame(Command::FAILURE, $tester->getStatusCode());
|
||||
$this->assertStringContainsString('i-know-this-is-prod', $tester->getDisplay());
|
||||
$this->assertTrue($this->doctorExists($doctor->getId()), 'گاردِ prod نباید چیزی حذف کند');
|
||||
}
|
||||
|
||||
public function testRunsOnProdWithExplicitSwitch(): void
|
||||
{
|
||||
$doctor = $this->makeDoctor('irimc', 'unclaimed');
|
||||
|
||||
$tester = $this->prodTester();
|
||||
$tester->execute(['--force' => true, '--i-know-this-is-prod' => true]);
|
||||
|
||||
$tester->assertCommandIsSuccessful();
|
||||
$this->assertFalse($this->doctorExists($doctor->getId()), 'با سوییچ صریح باید روی prod حذف کند');
|
||||
}
|
||||
|
||||
public function testClaimedAndOtherSourceDoctorsSurvive(): void
|
||||
{
|
||||
$claimed = $this->makeDoctor('irimc', 'claimed', userStatus: 1);
|
||||
|
||||
Reference in New Issue
Block a user