|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace App\Tests\Command; |
| 4 | + |
| 5 | +use PHPUnit\Framework\TestCase; |
| 6 | +use NotSoSimple\Commands\InitCommand; |
| 7 | +use NotSoSimple\Commands\ScanCommand; |
| 8 | +use Symfony\Component\Console\Application; |
| 9 | +use Symfony\Component\Console\Tester\TesterTrait; |
| 10 | +use Symfony\Component\Console\Tester\CommandTester; |
| 11 | + |
| 12 | +class ScanTest extends TestCase |
| 13 | +{ |
| 14 | + use TesterTrait; |
| 15 | + |
| 16 | + private static ?CommandTester $commandTester = null; |
| 17 | + |
| 18 | + private static string $outputDir = ''; |
| 19 | + |
| 20 | + public static function setUpBeforeClass(): void |
| 21 | + { |
| 22 | + parent::setUpBeforeClass(); |
| 23 | + $application = new Application(); |
| 24 | + $application->add(new ScanCommand()); |
| 25 | + |
| 26 | + $command = $application->find('scan'); |
| 27 | + static::$commandTester = new CommandTester($command); |
| 28 | + |
| 29 | + static::$outputDir = getenv('TEST_OUTPUT_DIR') ?? __DIR__; |
| 30 | + } |
| 31 | + |
| 32 | + public function tearDown(): void |
| 33 | + { |
| 34 | + parent::tearDown(); |
| 35 | + |
| 36 | + // @unlink(static::$outputDir . '/report.json'); |
| 37 | + // @unlink(static::$outputDir . '/report.html'); |
| 38 | + // @unlink(static::$outputDir . '/report.junit.xml'); |
| 39 | + } |
| 40 | + |
| 41 | + /** @test */ |
| 42 | + function test_execute() |
| 43 | + { |
| 44 | + static::$commandTester->execute(['--files' => __DIR__ . '/../../README.md', '-e' => './vendor/']); |
| 45 | + $output = static::$commandTester->getDisplay(); |
| 46 | + $this->assertRegExp('/Scanning [\.\/a-z]+README.md.../', $output); |
| 47 | + $this->assertRegExp('/Simple took \d\.\d+ seconds to run\./', $output); |
| 48 | + $this->assertRegExp('/simple\(\d\)\s*in\s*[\.\/a-z]+README\.md:\d+\s*\n\s+\=\>/', $output); |
| 49 | + } |
| 50 | + |
| 51 | + /** @test */ |
| 52 | + function test_execute_json_report() |
| 53 | + { |
| 54 | + static::$commandTester->execute([ |
| 55 | + '--files' => __DIR__ . '/../../README.md', |
| 56 | + '-e' => './vendor/', |
| 57 | + '--report-file' => static::$outputDir . '/report.json', |
| 58 | + ]); |
| 59 | + |
| 60 | + $this->assertFileExists(static::$outputDir . '/report.json'); |
| 61 | + } |
| 62 | + |
| 63 | + /** @test */ |
| 64 | + function test_execute_html_report() |
| 65 | + { |
| 66 | + static::$commandTester->execute([ |
| 67 | + '--files' => __DIR__ . '/../../README.md', |
| 68 | + '-e' => './vendor/', |
| 69 | + '--report-file' => static::$outputDir . '/report.html', |
| 70 | + ]); |
| 71 | + |
| 72 | + $this->assertFileExists(static::$outputDir . '/report.html'); |
| 73 | + } |
| 74 | + |
| 75 | + /** @test */ |
| 76 | + function test_execute_junit_report() |
| 77 | + { |
| 78 | + static::$commandTester->execute([ |
| 79 | + '--files' => __DIR__ . '/../../README.md', |
| 80 | + '-e' => './vendor/', |
| 81 | + '--report-file' => static::$outputDir . '/report.junit.xml', |
| 82 | + ]); |
| 83 | + |
| 84 | + $this->assertFileExists(static::$outputDir . '/report.junit.xml'); |
| 85 | + } |
| 86 | +} |
0 commit comments