Skip to content

Commit b327edf

Browse files
committed
Adds integration tests
1 parent fffd7e7 commit b327edf

File tree

1 file changed

+176
-0
lines changed

1 file changed

+176
-0
lines changed

tests/Commands/TreeCommandTest.php

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

0 commit comments

Comments
 (0)