-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDevkitConfigTest.php
More file actions
131 lines (105 loc) · 3.58 KB
/
DevkitConfigTest.php
File metadata and controls
131 lines (105 loc) · 3.58 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
131
<?php
declare(strict_types=1);
namespace KaririCode\Devkit\Tests\Unit\Core;
use KaririCode\Devkit\Core\DevkitConfig;
use KaririCode\Devkit\Exception\ConfigurationException;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
#[CoversClass(DevkitConfig::class)]
final class DevkitConfigTest extends TestCase
{
private string $tmpDir;
protected function setUp(): void
{
$this->tmpDir = sys_get_temp_dir() . '/devkit_config_test_' . uniqid();
mkdir($this->tmpDir, 0o777, true);
}
protected function tearDown(): void
{
// Clean up devkit.php if left
$configPath = $this->tmpDir . '/devkit.php';
if (file_exists($configPath)) {
unlink($configPath);
}
if (is_dir($this->tmpDir)) {
rmdir($this->tmpDir);
}
}
#[Test]
public function withoutConfigFileOverridesAreEmpty(): void
{
$config = new DevkitConfig($this->tmpDir);
$this->assertFalse($config->hasOverrides());
$this->assertSame([], $config->overrides);
}
#[Test]
public function getReturnsDefaultWhenKeyNotSet(): void
{
$config = new DevkitConfig($this->tmpDir);
$this->assertSame(9, $config->get('phpstan_level', 9));
$this->assertSame('8.4', $config->get('php_version', '8.4'));
}
#[Test]
public function withValidConfigFileOverridesAreLoaded(): void
{
file_put_contents(
$this->tmpDir . '/devkit.php',
"<?php return ['phpstan_level' => 5, 'php_version' => '8.3'];",
);
$config = new DevkitConfig($this->tmpDir);
$this->assertTrue($config->hasOverrides());
$this->assertSame(5, $config->get('phpstan_level', 9));
$this->assertSame('8.3', $config->get('php_version', '8.4'));
}
#[Test]
public function getReturnsDefaultForUnknownKeyEvenWithConfigFile(): void
{
file_put_contents(
$this->tmpDir . '/devkit.php',
"<?php return ['phpstan_level' => 5];",
);
$config = new DevkitConfig($this->tmpDir);
$this->assertSame(3, $config->get('psalm_level', 3));
}
#[Test]
public function getThrowsConfigurationExceptionOnTypeMismatch(): void
{
file_put_contents(
$this->tmpDir . '/devkit.php',
"<?php return ['phpstan_level' => 'nine'];", // string instead of int
);
$config = new DevkitConfig($this->tmpDir);
$this->expectException(ConfigurationException::class);
$config->get('phpstan_level', 9); // expects int, got string
}
#[Test]
public function invalidConfigFileThrowsConfigurationException(): void
{
file_put_contents(
$this->tmpDir . '/devkit.php',
"<?php return 'not an array';",
);
$this->expectException(ConfigurationException::class);
new DevkitConfig($this->tmpDir);
}
#[Test]
public function toolVersionsReturnsEmptyArrayWhenNotConfigured(): void
{
$config = new DevkitConfig($this->tmpDir);
$this->assertSame([], $config->toolVersions());
}
#[Test]
public function toolVersionsReturnsToolsArrayFromConfig(): void
{
file_put_contents(
$this->tmpDir . '/devkit.php',
"<?php return ['tools' => ['phpunit' => '^11.0', 'phpstan' => '^2.0']];",
);
$config = new DevkitConfig($this->tmpDir);
$this->assertSame(
['phpunit' => '^11.0', 'phpstan' => '^2.0'],
$config->toolVersions(),
);
}
}