|
| 1 | +<?php |
| 2 | + |
| 3 | +/* |
| 4 | + * This file is part of the Symfony package. |
| 5 | + * |
| 6 | + * (c) Fabien Potencier <fabien@symfony.com> |
| 7 | + * |
| 8 | + * For the full copyright and license information, please view the LICENSE |
| 9 | + * file that was distributed with this source code. |
| 10 | + */ |
| 11 | + |
| 12 | +namespace Symfony\Component\ErrorHandler\Tests\Command; |
| 13 | + |
| 14 | +use PHPUnit\Framework\MockObject\MockObject; |
| 15 | +use Symfony\Bundle\FrameworkBundle\Console\Application; |
| 16 | +use Symfony\Bundle\TwigBundle\Tests\TestCase; |
| 17 | +use Symfony\Component\Console\Tester\CommandTester; |
| 18 | +use Symfony\Component\DependencyInjection\Container; |
| 19 | +use Symfony\Component\ErrorHandler\Command\ErrorDumpCommand; |
| 20 | +use Symfony\Component\ErrorHandler\ErrorRenderer\ErrorRendererInterface; |
| 21 | +use Symfony\Component\ErrorHandler\Exception\FlattenException; |
| 22 | +use Symfony\Component\Filesystem\Filesystem; |
| 23 | +use Symfony\Component\HttpKernel\Exception\HttpException; |
| 24 | +use Symfony\Component\HttpKernel\KernelInterface; |
| 25 | +use Symfony\WebpackEncoreBundle\Asset\EntrypointLookupInterface; |
| 26 | + |
| 27 | +class ErrorDumpCommandTest extends TestCase |
| 28 | +{ |
| 29 | + private string $tmpDir = ''; |
| 30 | + |
| 31 | + protected function setUp(): void |
| 32 | + { |
| 33 | + $this->tmpDir = sys_get_temp_dir().'/error_pages'; |
| 34 | + |
| 35 | + $fs = new Filesystem(); |
| 36 | + $fs->remove($this->tmpDir); |
| 37 | + } |
| 38 | + |
| 39 | + public function testDumpPages() |
| 40 | + { |
| 41 | + $tester = $this->getCommandTester($this->getKernel(), []); |
| 42 | + $tester->execute([ |
| 43 | + 'path' => $this->tmpDir, |
| 44 | + ]); |
| 45 | + |
| 46 | + $this->assertFileExists($this->tmpDir.\DIRECTORY_SEPARATOR.'404.html'); |
| 47 | + $this->assertStringContainsString('Error 404', file_get_contents($this->tmpDir.\DIRECTORY_SEPARATOR.'404.html')); |
| 48 | + } |
| 49 | + |
| 50 | + public function testDumpPagesOnlyForGivenStatusCodes() |
| 51 | + { |
| 52 | + $fs = new Filesystem(); |
| 53 | + $fs->mkdir($this->tmpDir); |
| 54 | + $fs->touch($this->tmpDir.\DIRECTORY_SEPARATOR.'test.html'); |
| 55 | + |
| 56 | + $tester = $this->getCommandTester($this->getKernel()); |
| 57 | + $tester->execute([ |
| 58 | + 'path' => $this->tmpDir, |
| 59 | + 'status-codes' => ['400', '500'], |
| 60 | + ]); |
| 61 | + |
| 62 | + $this->assertFileExists($this->tmpDir.\DIRECTORY_SEPARATOR.'test.html'); |
| 63 | + $this->assertFileDoesNotExist($this->tmpDir.\DIRECTORY_SEPARATOR.'404.html'); |
| 64 | + |
| 65 | + $this->assertFileExists($this->tmpDir.\DIRECTORY_SEPARATOR.'400.html'); |
| 66 | + $this->assertStringContainsString('Error 400', file_get_contents($this->tmpDir.\DIRECTORY_SEPARATOR.'400.html')); |
| 67 | + } |
| 68 | + |
| 69 | + public function testForceRemovalPages() |
| 70 | + { |
| 71 | + $fs = new Filesystem(); |
| 72 | + $fs->mkdir($this->tmpDir); |
| 73 | + $fs->touch($this->tmpDir.\DIRECTORY_SEPARATOR.'test.html'); |
| 74 | + |
| 75 | + $tester = $this->getCommandTester($this->getKernel()); |
| 76 | + $tester->execute([ |
| 77 | + 'path' => $this->tmpDir, |
| 78 | + '--force' => true, |
| 79 | + ]); |
| 80 | + |
| 81 | + $this->assertFileDoesNotExist($this->tmpDir.\DIRECTORY_SEPARATOR.'test.html'); |
| 82 | + $this->assertFileExists($this->tmpDir.\DIRECTORY_SEPARATOR.'404.html'); |
| 83 | + } |
| 84 | + |
| 85 | + private function getKernel(): MockObject&KernelInterface |
| 86 | + { |
| 87 | + return $this->createMock(KernelInterface::class); |
| 88 | + } |
| 89 | + |
| 90 | + private function getCommandTester(KernelInterface $kernel): CommandTester |
| 91 | + { |
| 92 | + $errorRenderer = $this->createStub(ErrorRendererInterface::class); |
| 93 | + $errorRenderer |
| 94 | + ->method('render') |
| 95 | + ->willReturnCallback(function (HttpException $e) { |
| 96 | + $exception = FlattenException::createFromThrowable($e); |
| 97 | + $exception->setAsString(\sprintf('<html><body>Error %s</body></html>', $e->getStatusCode())); |
| 98 | + |
| 99 | + return $exception; |
| 100 | + }) |
| 101 | + ; |
| 102 | + |
| 103 | + $entrypointLookup = $this->createMock(EntrypointLookupInterface::class); |
| 104 | + |
| 105 | + $application = new Application($kernel); |
| 106 | + $application->add(new ErrorDumpCommand( |
| 107 | + new Filesystem(), |
| 108 | + $errorRenderer, |
| 109 | + $entrypointLookup, |
| 110 | + )); |
| 111 | + |
| 112 | + return new CommandTester($application->find('error:dump')); |
| 113 | + } |
| 114 | +} |
0 commit comments