Files
ludikevent_crm/tests/Command/SitemapCommandTest.php

78 lines
2.5 KiB
PHP
Raw Normal View History

<?php
namespace App\Tests\Command;
use App\Command\SitemapCommand;
use PHPUnit\Framework\Attributes\AllowMockObjectsWithoutExpectations;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Presta\SitemapBundle\Service\DumperInterface;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\HttpKernel\KernelInterface;
#[AllowMockObjectsWithoutExpectations]
class SitemapCommandTest extends TestCase
{
private MockObject&KernelInterface $kernel;
private MockObject&DumperInterface $dumper;
private string $tempDir;
protected function setUp(): void
{
$this->kernel = $this->createMock(KernelInterface::class);
$this->dumper = $this->createMock(DumperInterface::class);
$this->tempDir = sys_get_temp_dir() . '/sitemap_test_' . uniqid();
mkdir($this->tempDir . '/public/seo', 0777, true);
$_ENV['DEFAULT_URI'] = 'https://test.com';
}
protected function tearDown(): void
{
$this->removeDirectory($this->tempDir);
}
public function testExecute()
{
// 1. Setup Files (Old sitemap to delete)
touch($this->tempDir . '/public/seo/old.xml');
$this->kernel->method('getProjectDir')->willReturn($this->tempDir);
// 2. Expect Dumper call
$this->dumper->expects($this->once())
->method('dump')
->with(
$this->stringEndsWith('/public/seo'),
'https://test.com/seo',
'',
[]
);
// 3. Execute
$command = new SitemapCommand($this->kernel, $this->dumper);
$application = new Application();
$application->addCommand($command);
$commandTester = new CommandTester($application->find('app:sitemap'));
$commandTester->execute([]);
// 4. Assertions
$output = $commandTester->getDisplay();
$this->assertStringContainsString('Anciens fichiers sitemap supprimés', $output);
$this->assertStringContainsString('Sitemap généré avec succès', $output);
$this->assertFileDoesNotExist($this->tempDir . '/public/seo/old.xml');
}
private function removeDirectory($dir) {
if (!is_dir($dir)) return;
$files = array_diff(scandir($dir), array('.','..'));
foreach ($files as $file) {
(is_dir("$dir/$file")) ? $this->removeDirectory("$dir/$file") : unlink("$dir/$file");
}
rmdir($dir);
}
}