|
| 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\ErrorRenderer\Tests\Command; |
| 13 | + |
| 14 | +use PHPUnit\Framework\TestCase; |
| 15 | +use Symfony\Component\Console\Application; |
| 16 | +use Symfony\Component\Console\Tester\CommandTester; |
| 17 | +use Symfony\Component\ErrorRenderer\Command\DebugCommand; |
| 18 | +use Symfony\Component\ErrorRenderer\ErrorRenderer\JsonErrorRenderer; |
| 19 | +use Symfony\Component\ErrorRenderer\ErrorRenderer\TxtErrorRenderer; |
| 20 | +use Symfony\Component\ErrorRenderer\ErrorRenderer\XmlErrorRenderer; |
| 21 | + |
| 22 | +class DebugCommandTest extends TestCase |
| 23 | +{ |
| 24 | + public function testAvailableRenderers() |
| 25 | + { |
| 26 | + $tester = $this->createCommandTester(); |
| 27 | + $ret = $tester->execute([], ['decorated' => false]); |
| 28 | + |
| 29 | + $this->assertEquals(0, $ret, 'Returns 0 in case of success'); |
| 30 | + $this->assertSame(<<<TXT |
| 31 | +
|
| 32 | +ErrorRenderer |
| 33 | +============= |
| 34 | +
|
| 35 | + The following error renderers are available: |
| 36 | +
|
| 37 | + -------- ----------------------------------------------------------------- |
| 38 | + Format Class |
| 39 | + -------- ----------------------------------------------------------------- |
| 40 | + json Symfony\Component\ErrorRenderer\ErrorRenderer\JsonErrorRenderer |
| 41 | + xml Symfony\Component\ErrorRenderer\ErrorRenderer\XmlErrorRenderer |
| 42 | + txt Symfony\Component\ErrorRenderer\ErrorRenderer\TxtErrorRenderer |
| 43 | + -------- ----------------------------------------------------------------- |
| 44 | +
|
| 45 | +
|
| 46 | +TXT |
| 47 | + , $tester->getDisplay(true)); |
| 48 | + } |
| 49 | + |
| 50 | + public function testFormatArgument() |
| 51 | + { |
| 52 | + $tester = $this->createCommandTester(); |
| 53 | + $ret = $tester->execute(['format' => 'json'], ['decorated' => false]); |
| 54 | + |
| 55 | + $this->assertEquals(0, $ret, 'Returns 0 in case of success'); |
| 56 | + $this->assertSame(<<<TXT |
| 57 | +{ |
| 58 | + "title": "Internal Server Error", |
| 59 | + "status": 500, |
| 60 | + "detail": "Something has intentionally gone wrong." |
| 61 | +} |
| 62 | +
|
| 63 | +TXT |
| 64 | + , $tester->getDisplay(true)); |
| 65 | + } |
| 66 | + |
| 67 | + private function createCommandTester() |
| 68 | + { |
| 69 | + $command = new DebugCommand([ |
| 70 | + 'json' => new JsonErrorRenderer(false), |
| 71 | + 'xml' => new XmlErrorRenderer(false), |
| 72 | + 'txt' => new TxtErrorRenderer(false), |
| 73 | + ]); |
| 74 | + |
| 75 | + $application = new Application(); |
| 76 | + $application->add($command); |
| 77 | + |
| 78 | + return new CommandTester($application->find('debug:error-renderer')); |
| 79 | + } |
| 80 | + |
| 81 | + /** |
| 82 | + * @expectedException \Symfony\Component\Console\Exception\InvalidArgumentException |
| 83 | + * @expectedExceptionMessage No error renderer found for format "foo". Known format are json, xml, txt. |
| 84 | + */ |
| 85 | + public function testInvalidFormat() |
| 86 | + { |
| 87 | + $tester = $this->createCommandTester(); |
| 88 | + $tester->execute(['format' => 'foo'], ['decorated' => false]); |
| 89 | + } |
| 90 | +} |
0 commit comments