|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Stolt\LeanPackage\Tests\Commands; |
| 6 | + |
| 7 | +use PHPUnit\Framework\Attributes\Test; |
| 8 | +use Stolt\LeanPackage\Archive; |
| 9 | +use Stolt\LeanPackage\Commands\TreeCommand; |
| 10 | +use Stolt\LeanPackage\Tests\CommandTester; |
| 11 | +use Stolt\LeanPackage\Tests\TestCase; |
| 12 | +use Stolt\LeanPackage\Tree; |
| 13 | +use Symfony\Component\Console\Application; |
| 14 | + |
| 15 | +class TreeCommandTest extends TestCase |
| 16 | +{ |
| 17 | + /** |
| 18 | + * @var Application |
| 19 | + */ |
| 20 | + private $application; |
| 21 | + |
| 22 | + /** |
| 23 | + * Set up test environment. |
| 24 | + */ |
| 25 | + protected function setUp(): void |
| 26 | + { |
| 27 | + $this->setUpTemporaryDirectory(); |
| 28 | + if (!\defined('WORKING_DIRECTORY')) { |
| 29 | + \define('WORKING_DIRECTORY', $this->temporaryDirectory); |
| 30 | + } |
| 31 | + $this->application = $this->getApplication(); |
| 32 | + } |
| 33 | + |
| 34 | + /** |
| 35 | + * Tear down test environment. |
| 36 | + * |
| 37 | + * @return void |
| 38 | + */ |
| 39 | + protected function tearDown(): void |
| 40 | + { |
| 41 | + if (\is_dir($this->temporaryDirectory)) { |
| 42 | + $this->removeDirectory($this->temporaryDirectory); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + #[Test] |
| 47 | + public function displayExpectedSrcTree(): void |
| 48 | + { |
| 49 | + $command = $this->application->find('tree'); |
| 50 | + $commandTester = new CommandTester($command); |
| 51 | + |
| 52 | + $artifactFilenames = [ |
| 53 | + '.gitattributes', |
| 54 | + 'composer.json', |
| 55 | + ]; |
| 56 | + |
| 57 | + $this->createTemporaryFiles( |
| 58 | + $artifactFilenames, |
| 59 | + ['src', 'tests', '.github', 'docs', 'bin'] |
| 60 | + ); |
| 61 | + |
| 62 | + file_put_contents(WORKING_DIRECTORY . '/composer.json', \json_encode(['name' => 'test-src/package'])); |
| 63 | + |
| 64 | + $expectedDisplay = <<<CONTENT |
| 65 | +Package: test-src/package |
| 66 | +. |
| 67 | +├── bin |
| 68 | +├── docs |
| 69 | +├── .github |
| 70 | +├── src |
| 71 | +├── tests |
| 72 | +├── composer.json |
| 73 | +└── .gitattributes |
| 74 | +
|
| 75 | +5 directories, 2 files |
| 76 | +
|
| 77 | +CONTENT; |
| 78 | + |
| 79 | + $commandTester->execute([ |
| 80 | + 'command' => $command->getName(), |
| 81 | + 'directory' => WORKING_DIRECTORY, |
| 82 | + '--src' => true |
| 83 | + ]); |
| 84 | + |
| 85 | + $this->assertSame($expectedDisplay, $commandTester->getDisplay()); |
| 86 | + $commandTester->assertCommandIsSuccessful(); |
| 87 | + } |
| 88 | + |
| 89 | + #[Test] |
| 90 | + public function displayExpectedDistPackageTree(): void |
| 91 | + { |
| 92 | + $command = $this->application->find('tree'); |
| 93 | + $commandTester = new CommandTester($command); |
| 94 | + |
| 95 | + $artifactFilenames = [ |
| 96 | + '.gitattributes', |
| 97 | + '.gitignore', |
| 98 | + 'captainhook.json', |
| 99 | + 'CODE_OF_CONDUCT.md', |
| 100 | + 'CONTRIBUTING.md', |
| 101 | + 'infection.json5', |
| 102 | + 'LICENSE.txt', |
| 103 | + 'phpstan.neon', |
| 104 | + 'phpunit.xml', |
| 105 | + 'README.md', |
| 106 | + 'sonar-project.properties', |
| 107 | + 'package.json', |
| 108 | + 'package-lock.json', |
| 109 | + 'composer.json', |
| 110 | + ]; |
| 111 | + |
| 112 | + $this->createTemporaryFiles( |
| 113 | + $artifactFilenames, |
| 114 | + ['src', 'tests', '.github', 'docs', 'bin'] |
| 115 | + ); |
| 116 | + |
| 117 | + file_put_contents(WORKING_DIRECTORY . '/composer.json', \json_encode(['name' => 'test-dist/package'])); |
| 118 | + |
| 119 | + $gitattributesContent = <<<CONTENT |
| 120 | +.gitattributes export-ignore |
| 121 | +.gitignore export-ignore |
| 122 | +captainhook.json export-ignore |
| 123 | +CODE_OF_CONDUCT.md export-ignore |
| 124 | +CONTRIBUTING.md export-ignore |
| 125 | +infection.json5 export-ignore |
| 126 | +LICENSE.txt export-ignore |
| 127 | +phpstan.neon export-ignore |
| 128 | +phpunit.xml export-ignore |
| 129 | +README.md export-ignore |
| 130 | +sonar-project.properties export-ignore |
| 131 | +package.json export-ignore |
| 132 | +package-lock.json export-ignore |
| 133 | +composer.json export-ignore |
| 134 | +tests/ export-ignore |
| 135 | +.github/ export-ignore |
| 136 | +docs/ export-ignore |
| 137 | +CONTENT; |
| 138 | + |
| 139 | + $this->createTemporaryGitattributesFile($gitattributesContent); |
| 140 | + |
| 141 | + |
| 142 | + $expectedDisplay = <<<CONTENT |
| 143 | +Package: test-dist/package |
| 144 | +. |
| 145 | +├── bin |
| 146 | +├── composer.json |
| 147 | +└── src |
| 148 | +
|
| 149 | +2 directories, 1 file |
| 150 | +
|
| 151 | +CONTENT; |
| 152 | + |
| 153 | + $commandTester->execute([ |
| 154 | + 'command' => $command->getName(), |
| 155 | + 'directory' => WORKING_DIRECTORY, |
| 156 | + ]); |
| 157 | + |
| 158 | + $this->assertSame($expectedDisplay, $commandTester->getDisplay()); |
| 159 | + $commandTester->assertCommandIsSuccessful(); |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * @return Application |
| 164 | + */ |
| 165 | + protected function getApplication(): Application |
| 166 | + { |
| 167 | + $application = new Application(); |
| 168 | + $application->add(new TreeCommand(new Tree(new Archive(WORKING_DIRECTORY)))); |
| 169 | + |
| 170 | + return $application; |
| 171 | + } |
| 172 | +} |
0 commit comments