Skip to content

Commit fba2141

Browse files
committed
tests (FileReader) Add tests to check configuration file priority
1 parent c6c5edb commit fba2141

File tree

2 files changed

+57
-2
lines changed

2 files changed

+57
-2
lines changed

src/ConfigurationFile/FileReader.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public function readFile(): array
3333

3434
try {
3535
$fileExtension = pathinfo($configurationFilePath, PATHINFO_EXTENSION);
36-
36+
// dd($configurationFilePath);
3737
if ($fileExtension === 'yml' || $fileExtension === 'yaml') {
3838
$configurationFile = Yaml::parseFile($configurationFilePath);
3939
} elseif ($fileExtension === 'php') {
@@ -69,7 +69,6 @@ public function findConfigurationFile(): string
6969

7070
foreach ($possiblePaths as $path) {
7171
if (file_exists($path)) {
72-
// dd($path);
7372
$configFile = $path;
7473
break;
7574
}

tests/Unit/ConfigurationFile/FileReaderTest.php

+56
Original file line numberDiff line numberDiff line change
@@ -130,4 +130,60 @@ function it_throws_exception_when_file_type_is_not_supported()
130130
$this->expectException(ConfigurationFileNotFoundException::class);
131131
$this->fileReader->readFile();
132132
}
133+
134+
/** @test */
135+
function it_prioritizes_php_files_over_yml()
136+
{
137+
$phpConfig = "<?php return ['tools' => ['phpunit']];";
138+
$ymlConfig = $this->configurationFileBuilder->setTools(['phpcs'])->buildYalm();
139+
140+
$fileSystemStructure = [
141+
'githooks.php' => $phpConfig,
142+
'githooks.yml' => $ymlConfig
143+
];
144+
145+
$this->createFileSystem($fileSystemStructure);
146+
147+
$expectedConfig = ['tools' => ['phpunit']];
148+
$fileReaded = $this->fileReader->readFile();
149+
150+
$this->assertEquals($expectedConfig, $fileReaded);
151+
}
152+
153+
public function configurationFilesInRootAndQaDataProvider()
154+
{
155+
return [
156+
'PHP files' => [
157+
"<?php return ['tools' => ['phpunit']];",
158+
"<?php return ['tools' => ['phpcs']];",
159+
['tools' => ['phpunit']],
160+
'php'
161+
],
162+
'YAML files' => [
163+
$this->setConfigurationFileBuilder()->setTools(['phpunit'])->buildYalm(),
164+
$this->setConfigurationFileBuilder()->setTools(['phpcs'])->buildYalm(),
165+
$this->setConfigurationFileBuilder()->setTools(['phpunit'])->buildArray(),
166+
'yml'
167+
]
168+
];
169+
}
170+
171+
/**
172+
* @test
173+
* @dataProvider configurationFilesInRootAndQaDataProvider
174+
* When there are valid configuration files in both root and qa directories, it prioritizes files in the root directory
175+
*/
176+
function it_prioritizes_files_in_root_directory_over_files_in_qa_directory($rootFile, $qaFile, $expectedConfig, $fileExtension)
177+
{
178+
$fileSystemStructure = [
179+
"githooks.$fileExtension" => $rootFile,
180+
'qa' => ["githooks.$fileExtension" => $qaFile]
181+
];
182+
183+
$this->createFileSystem($fileSystemStructure);
184+
185+
$fileReaded = $this->fileReader->readFile();
186+
187+
$this->assertEquals($expectedConfig, $fileReaded);
188+
}
133189
}

0 commit comments

Comments
 (0)