-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathKcodeComposerGeneratorTest.php
More file actions
130 lines (110 loc) · 4.33 KB
/
Copy pathKcodeComposerGeneratorTest.php
File metadata and controls
130 lines (110 loc) · 4.33 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
declare(strict_types=1);
namespace KaririCode\Devkit\Tests\Unit\Configuration;
use KaririCode\Devkit\Configuration\KcodeComposerGenerator;
use KaririCode\Devkit\Core\ProjectContext;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\Attributes\UsesClass;
use PHPUnit\Framework\TestCase;
#[CoversClass(KcodeComposerGenerator::class)]
#[UsesClass(ProjectContext::class)]
final class KcodeComposerGeneratorTest extends TestCase
{
private ProjectContext $context;
protected function setUp(): void
{
$this->context = new ProjectContext(
projectRoot: '/tmp/test-project',
projectName: 'test/project',
namespace: 'Test\\Project',
phpVersion: '8.4',
phpstanLevel: 9,
psalmLevel: 3,
sourceDirs: ['/tmp/test-project/src'],
testDirs: ['/tmp/test-project/tests'],
excludeDirs: ['src/Contract'],
testSuites: ['Unit' => 'tests/Unit'],
coverageExclude: ['src/Exception'],
csFixerRules: [],
rectorSets: [],
toolVersions: [],
);
}
#[Test]
public function toolNameIsKcodeComposer(): void
{
$generator = new KcodeComposerGenerator();
$this->assertSame('kcode-composer', $generator->toolName());
}
#[Test]
public function outputPathIsComposerJson(): void
{
$generator = new KcodeComposerGenerator();
$this->assertSame('composer.json', $generator->outputPath());
}
#[Test]
public function generateProducesValidJsonWithDefaultVersions(): void
{
$generator = new KcodeComposerGenerator();
$output = $generator->generate($this->context);
/** @var array<string, mixed> $manifest */
$manifest = json_decode($output, true, 512, \JSON_THROW_ON_ERROR);
$this->assertSame('kariricode/devkit-tools', $manifest['name']);
$this->assertIsArray($manifest['require']);
// All 5 default tools must be present
$require = $manifest['require'];
$this->assertArrayHasKey('phpunit/phpunit', $require);
$this->assertArrayHasKey('phpstan/phpstan', $require);
$this->assertArrayHasKey('friendsofphp/php-cs-fixer', $require);
$this->assertArrayHasKey('rector/rector', $require);
$this->assertArrayHasKey('vimeo/psalm', $require);
}
#[Test]
public function generateRespectsUserToolVersionOverrides(): void
{
$contextWithVersions = new ProjectContext(
projectRoot: '/tmp/test-project',
projectName: 'test/project',
namespace: 'Test\\Project',
phpVersion: '8.4',
phpstanLevel: 9,
psalmLevel: 3,
sourceDirs: ['/tmp/test-project/src'],
testDirs: ['/tmp/test-project/tests'],
excludeDirs: [],
testSuites: [],
coverageExclude: [],
csFixerRules: [],
rectorSets: [],
toolVersions: ['phpunit' => '^11.0', 'phpstan' => '^1.12'],
);
$generator = new KcodeComposerGenerator();
$output = $generator->generate($contextWithVersions);
/** @var array<string, mixed> $manifest */
$manifest = json_decode($output, true, 512, \JSON_THROW_ON_ERROR);
$require = $manifest['require'];
$this->assertSame('^11.0', $require['phpunit/phpunit']);
$this->assertSame('^1.12', $require['phpstan/phpstan']);
// Unoverridden tool still uses default
$this->assertStringStartsWith('^', $require['rector/rector']);
}
#[Test]
public function generateIncludesComposerConfigSection(): void
{
$generator = new KcodeComposerGenerator();
$output = $generator->generate($this->context);
/** @var array<string, mixed> $manifest */
$manifest = json_decode($output, true, 512, \JSON_THROW_ON_ERROR);
$this->assertIsArray($manifest['config']);
$this->assertSame('full', $manifest['config']['bin-compat']);
$this->assertTrue($manifest['config']['optimize-autoloader']);
}
#[Test]
public function generateOutputEndsWithNewline(): void
{
$generator = new KcodeComposerGenerator();
$output = $generator->generate($this->context);
$this->assertStringEndsWith(\PHP_EOL, $output);
}
}