Files

83 lines
2.7 KiB
PHP
Raw Permalink Normal View History

<?php
declare(strict_types=1);
namespace PhpQml\Bridge\Tests\Command;
use PhpQml\Bridge\Command\BridgeExportCommand;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use Symfony\Component\Console\Tester\CommandTester;
#[CoversClass(BridgeExportCommand::class)]
final class BridgeExportCommandTest extends TestCase
{
private string $tmpDir = '';
protected function setUp(): void
{
$this->tmpDir = sys_get_temp_dir().'/bridge-export-test-'.uniqid();
mkdir($this->tmpDir, 0o700, true);
}
protected function tearDown(): void
{
// Brutal but adequate for a flat test dir.
foreach (glob($this->tmpDir.'/*') ?: [] as $f) {
@unlink($f);
}
@rmdir($this->tmpDir);
}
public function testCopiesSqliteFileToDestination(): void
{
$source = $this->tmpDir.'/source.sqlite';
file_put_contents($source, 'fake-sqlite-bytes');
$tester = new CommandTester(new BridgeExportCommand("sqlite:///{$source}"));
$destination = $this->tmpDir.'/exported.sqlite';
$exitCode = $tester->execute(['destination' => $destination]);
self::assertSame(0, $exitCode);
self::assertFileExists($destination);
self::assertSame('fake-sqlite-bytes', file_get_contents($destination));
self::assertStringContainsString('Exported', $tester->getDisplay());
}
public function testFailsForNonSqliteUrl(): void
{
$tester = new CommandTester(new BridgeExportCommand('postgres://user@host/db'));
$exitCode = $tester->execute(['destination' => $this->tmpDir.'/x.sqlite']);
self::assertSame(1, $exitCode);
self::assertStringContainsString('not a sqlite:/// URL', $tester->getDisplay());
}
public function testFailsWhenSourceMissing(): void
{
$tester = new CommandTester(new BridgeExportCommand("sqlite:///{$this->tmpDir}/no-such.sqlite"));
$exitCode = $tester->execute(['destination' => $this->tmpDir.'/x.sqlite']);
self::assertSame(1, $exitCode);
self::assertStringContainsString('does not exist', $tester->getDisplay());
}
public function testOverwritesExistingDestination(): void
{
$source = $this->tmpDir.'/source.sqlite';
file_put_contents($source, 'new-bytes');
$destination = $this->tmpDir.'/exported.sqlite';
file_put_contents($destination, 'old-bytes');
$tester = new CommandTester(new BridgeExportCommand("sqlite:///{$source}"));
$exitCode = $tester->execute(['destination' => $destination]);
self::assertSame(0, $exitCode);
self::assertSame('new-bytes', file_get_contents($destination));
}
}