Skip to content

Commit 96d49f3

Browse files
committed
WIP Adds new commands integration tests
1 parent ad06441 commit 96d49f3

File tree

5 files changed

+185
-3
lines changed

5 files changed

+185
-3
lines changed

src/Commands/CreateCommand.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
6969
return self::FAILURE;
7070
}
7171

72-
$output->writeln(".gitattributes file has been created at {$directory}");
72+
$output->writeln("A .gitattributes file has been created at {$directory}.");
7373

7474
return self::SUCCESS;
7575
}

src/Commands/UpdateCommand.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
4848
$gitattributesPath = $this->analyser->getGitattributesFilePath();
4949

5050
if (!\file_exists($gitattributesPath)) {
51-
$output->writeln('No .gitattributes file found. Use the create command to create one first.');
51+
$output->writeln('No .gitattributes file found. Use the <info>create</info> command to create one first.');
5252

5353
return self::FAILURE;
5454
}
@@ -69,7 +69,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
6969
return self::FAILURE;
7070
}
7171

72-
$output->writeln(".gitattributes file has been updated at {$directory}");
72+
$output->writeln("The .gitattributes file at {$directory} has been updated.");
7373

7474
return self::SUCCESS;
7575
}
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
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\Commands\CreateCommand;
10+
use Stolt\LeanPackage\GitattributesFileRepository;
11+
use Stolt\LeanPackage\Presets\Finder;
12+
use Stolt\LeanPackage\Presets\PhpPreset;
13+
use Stolt\LeanPackage\Tests\TestCase;
14+
use Zenstruck\Console\Test\TestCommand;
15+
16+
final class CreateCommandTest extends TestCase
17+
{
18+
protected function setUp(): void
19+
{
20+
$this->setUpTemporaryDirectory();
21+
}
22+
23+
protected function tearDown(): void
24+
{
25+
$this->removeDirectory($this->temporaryDirectory);
26+
}
27+
28+
#[Test]
29+
public function createsNewGitattributesFileWithHeaderAndExpectedContent(): void
30+
{
31+
$analyser = (new Analyser(new Finder(new PhpPreset())))->setDirectory($this->temporaryDirectory);
32+
$repository = new GitattributesFileRepository($analyser);
33+
$command = new CreateCommand($analyser, $repository);
34+
35+
$artifactFilenames = ['README.md', '.gitignore'];
36+
37+
$this->createTemporaryFiles(
38+
$artifactFilenames,
39+
['tests']
40+
);
41+
42+
TestCommand::for($command)
43+
->addArgument($this->temporaryDirectory)
44+
->execute()
45+
->assertSuccessful()
46+
->assertOutputContains("A .gitattributes file has been created at {$this->temporaryDirectory}.");
47+
48+
$gitattributesPath = $this->temporaryDirectory . DIRECTORY_SEPARATOR . '.gitattributes';
49+
$this->assertFileExists($gitattributesPath);
50+
51+
$content = (string) \file_get_contents($gitattributesPath);
52+
53+
// Header should be present (created files use "generated" header)
54+
$this->assertStringContainsString(
55+
'# This file was generated by the lean package validator (http://git.io/lean-package-validator).',
56+
$content
57+
);
58+
59+
// Sanity check: expected export-ignores likely include .gitattributes itself and tests/
60+
$this->assertStringContainsString('.gitattributes', $content);
61+
$this->assertStringContainsString('tests/', $content);
62+
$this->assertStringContainsString('export-ignore', $content);
63+
}
64+
65+
#[Test]
66+
public function failsIfGitattributesAlreadyExists(): void
67+
{
68+
$gitattributesContent = <<<CONTENT
69+
* text=auto eol=lf
70+
71+
phpspec.yml.dist export-ignore
72+
specs/ export-ignore
73+
CONTENT;
74+
75+
$this->createTemporaryGitattributesFile($gitattributesContent);
76+
77+
$analyser = (new Analyser(new Finder(new PhpPreset())))->setDirectory($this->temporaryDirectory);
78+
$repository = new GitattributesFileRepository($analyser);
79+
$command = new CreateCommand($analyser, $repository);
80+
81+
TestCommand::for($command)
82+
->addArgument($this->temporaryDirectory)
83+
->execute()
84+
->assertFaulty()
85+
->assertOutputContains('A .gitattributes file already exists. Use the update command to modify it.');
86+
}
87+
}
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
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\Commands\UpdateCommand;
10+
use Stolt\LeanPackage\GitattributesFileRepository;
11+
use Stolt\LeanPackage\Presets\Finder;
12+
use Stolt\LeanPackage\Presets\PhpPreset;
13+
use Stolt\LeanPackage\Tests\TestCase;
14+
use Zenstruck\Console\Test\TestCommand;
15+
16+
final class UpdateCommandTest extends TestCase
17+
{
18+
protected function setUp(): void
19+
{
20+
$this->setUpTemporaryDirectory();
21+
}
22+
23+
protected function tearDown(): void
24+
{
25+
$this->removeDirectory($this->temporaryDirectory);
26+
}
27+
28+
#[Test]
29+
public function updatesExistingGitattributesAndReplacesHeader(): void
30+
{
31+
$analyser = (new Analyser(new Finder(new PhpPreset())))->setDirectory($this->temporaryDirectory);
32+
$repository = new GitattributesFileRepository($analyser);
33+
$command = new UpdateCommand($analyser, $repository);
34+
35+
$artifactFilenames = ['.gitignore'];
36+
37+
$this->createTemporaryFiles(
38+
$artifactFilenames,
39+
['tests', '.github']
40+
);
41+
42+
$gitattributesContent = <<<CONTENT
43+
# This file was generated by the lean package validator (http://git.io/lean-package-validator).
44+
45+
* text=auto eol=lf
46+
47+
.gitattributes export-ignore
48+
.github/ export-ignore
49+
tests/ export-ignore
50+
CONTENT;
51+
52+
$this->createTemporaryGitattributesFile($gitattributesContent);
53+
54+
TestCommand::for($command)
55+
->addArgument($this->temporaryDirectory)
56+
->execute()
57+
->assertSuccessful()
58+
->assertOutputContains("The .gitattributes file at {$this->temporaryDirectory} has been updated.");
59+
60+
$gitattributesPath = $this->temporaryDirectory . DIRECTORY_SEPARATOR . '.gitattributes';
61+
$this->assertFileExists($gitattributesPath);
62+
63+
$content = (string) \file_get_contents($gitattributesPath);
64+
65+
// Header should be the "modified" one after update
66+
$this->assertStringContainsString(
67+
'# This file was partly modified by the lean package validator (http://git.io/lean-package-validator).',
68+
$content
69+
);
70+
71+
// Sanity: should still contain export-ignore entries and likely 'tests/' gets added
72+
$this->assertStringContainsString('.gitattributes', $content);
73+
$this->assertStringContainsString('.github/', $content);
74+
$this->assertStringContainsString('tests/', $content);
75+
$this->assertStringContainsString('export-ignore', $content);
76+
}
77+
78+
#[Test]
79+
public function failsWhenNoGitattributesFileIsPresent(): void
80+
{
81+
// Remove the file
82+
@\unlink($this->temporaryDirectory . DIRECTORY_SEPARATOR . '.gitattributes');
83+
84+
$analyser = (new Analyser(new Finder(new PhpPreset())))->setDirectory($this->temporaryDirectory);
85+
$repository = new GitattributesFileRepository($analyser);
86+
$command = new UpdateCommand($analyser, $repository);
87+
88+
TestCommand::for($command)
89+
->addArgument($this->temporaryDirectory)
90+
->execute()
91+
->assertFaulty()
92+
->assertOutputContains('No .gitattributes file found. Use the create command to create one first.');
93+
}
94+
}

tests/TestCase.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ class TestCase extends PHPUnit
1919
* @var Application
2020
*/
2121
protected Application $application;
22+
2223
protected string $temporaryDirectory;
2324

2425
/**

0 commit comments

Comments
 (0)