Skip to content

Commit ef90b42

Browse files
committed
tests (CheckConfigurationFileCommand) Added tests for config argument
1 parent bd867f6 commit ef90b42

7 files changed

+68
-39
lines changed

tests/System/Commands/CheckConfigurationFileCommandTest.php

+21
Original file line numberDiff line numberDiff line change
@@ -103,4 +103,25 @@ function it_not_founds_configuration_file()
103103
->assertExitCode(1)
104104
->containsStringInOutput("Configuration file must be 'githooks.yml' in root directory or in qa/ directory");
105105
}
106+
107+
/** @test */
108+
function it_check_the_config_file_pass_as_argument()
109+
{
110+
// valid file in custom path
111+
$configFilePath = 'custom/path';
112+
$this->configurationFileBuilder->buildInFileSystem($configFilePath);
113+
114+
// file with erros in root path
115+
$this->configurationFileBuilder
116+
->setTools([])
117+
->setOptions(['invent option' => 1])
118+
->buildInFileSystem();
119+
120+
$this->artisan("conf:check --config custom/path/githooks.yml")
121+
->assertExitCode(0)
122+
->expectsTable(['Options', 'Values'], [['execution', 'full'], ['processes', 1]])
123+
->expectsOutput('The file githooks.yml has the correct format.')
124+
->notContainsStringInOutput("The 'Tools' tag from configuration file is empty")
125+
->notcontainsStringInOutput("The key 'invent option' is not a valid option");
126+
}
106127
}

tests/System/Commands/ExecuteToolCommandTest.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ public function allToolsAtSameTimeDataProvider()
174174
*/
175175
function it_runs_all_configured_tools_at_same_time($tools, $commandUnderTheHood)
176176
{
177-
file_put_contents($this->path . '/githooks.yml', $this->configurationFileBuilder->setTools($tools)->buildYalm());
177+
file_put_contents($this->path . '/githooks.yml', $this->configurationFileBuilder->setTools($tools)->buildYaml());
178178

179179
file_put_contents($this->path . '/src/File.php', $this->fileBuilder->build());
180180

@@ -243,7 +243,7 @@ public function onlyConfiguredToolsAtSameTimeDataProvider()
243243
*/
244244
function it_runs_only_configured_tools_in_Tools_tag_at_same_time($tools, $runnedTools, $notRunnedTools)
245245
{
246-
file_put_contents($this->path . '/githooks.yml', $this->configurationFileBuilder->setTools($tools)->buildYalm());
246+
file_put_contents($this->path . '/githooks.yml', $this->configurationFileBuilder->setTools($tools)->buildYaml());
247247

248248
file_put_contents($this->path . '/src/File.php', $this->fileBuilder->build());
249249

@@ -332,7 +332,7 @@ function it_prints_error_when_tries_to_run_an_not_supported_tool()
332332
/** @test */
333333
function it_prints_error_when_the_tool_has_not_configuration_and_it_is_required()
334334
{
335-
file_put_contents($this->path . '/githooks.yml', $this->configurationFileBuilder->setConfigurationTools([])->buildYalm());
335+
file_put_contents($this->path . '/githooks.yml', $this->configurationFileBuilder->setConfigurationTools([])->buildYaml());
336336

337337
file_put_contents($this->path . '/src/File.php', $this->fileBuilder->build());
338338

@@ -352,7 +352,7 @@ function it_prints_error_when_the_tool_has_not_configuration_and_it_is_required(
352352
*/
353353
function it_can_be_override_full_execution_from_githooksyml_for_fast_execution_from_cli()
354354
{
355-
file_put_contents($this->path . '/githooks.yml', $this->configurationFileBuilder->setOptions(['execution' => 'full'])->buildYalm());
355+
file_put_contents($this->path . '/githooks.yml', $this->configurationFileBuilder->setOptions(['execution' => 'full'])->buildYaml());
356356

357357
$pathForFileWithoutErrors = $this->path . '/src/File.php';
358358
file_put_contents($pathForFileWithoutErrors, $this->fileBuilder->build());
@@ -404,7 +404,7 @@ function it_skip_the_tool_when_the_tool_runs_against_root_directory_in_fast_mode
404404
'standard' => 'PSR12',
405405
'ignore' => ['vendor'],
406406
])
407-
->buildYalm()
407+
->buildYaml()
408408
);
409409

410410
$file = $this->path . '/src/File.php';

tests/System/Release/CheckConfigurationFileTest.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ function it_checks_the_configuration_file_and_return_exit_0()
1414
{
1515
file_put_contents(
1616
'githooks.yml',
17-
$this->configurationFileBuilder->buildYalm()
17+
$this->configurationFileBuilder->buildYaml()
1818
);
1919

2020
passthru("$this->githooks conf:check", $exitCode);
@@ -28,7 +28,7 @@ function it_checks_the_configuration_file_and_show_warning_and_return_exit_0()
2828
{
2929
file_put_contents(
3030
'githooks.yml',
31-
$this->configurationFileBuilder->setOptions(['invalidOptionTest' => 1])->buildYalm()
31+
$this->configurationFileBuilder->setOptions(['invalidOptionTest' => 1])->buildYaml()
3232
);
3333

3434
passthru("$this->githooks conf:check", $exitCode);
@@ -43,7 +43,7 @@ function it_checks_the_configuration_file_and_show_error_and_return_exit_1()
4343
{
4444
file_put_contents(
4545
'githooks.yml',
46-
$this->configurationFileBuilder->setTools([])->buildYalm()
46+
$this->configurationFileBuilder->setTools([])->buildYaml()
4747
);
4848

4949
passthru("$this->githooks conf:check", $exitCode);

tests/System/Release/CreateConfigurationFileTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ function it_creates_the_configuration_file_and_returns_exit_code_0()
1717
mkdir('vendor/wtyd/githooks/qa/', 0777, true);
1818
file_put_contents(
1919
'vendor/wtyd/githooks/qa/githooks.dist.yml',
20-
$this->configurationFileBuilder->buildYalm()
20+
$this->configurationFileBuilder->buildYaml()
2121
);
2222

2323
passthru("$this->githooks conf:init", $exitCode);

tests/System/Release/ExecuteToolTest.php

+5-5
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ function it_returns_exit_0_when_executes_all_tools_and_all_pass($executionModeAr
4646
'githooks.yml',
4747
$this->configurationFileBuilder->setTools(['phpcs', 'phpcbf', 'parallel-lint', 'phpmd', 'phpcpd', 'phpstan'])
4848
->setOptions($executionModeFile)
49-
->buildYalm()
49+
->buildYaml()
5050
);
5151
passthru("$this->githooks tool all $executionModeArgument", $exitCode);
5252

@@ -83,7 +83,7 @@ function it_executes_all_tools_with_fast_execution_mode($executionModeArgument,
8383
'githooks.yml',
8484
$this->configurationFileBuilder->setTools(['phpcs', 'phpcbf', 'parallel-lint', 'phpmd', 'phpcpd', 'phpstan'])
8585
->setOptions($executionModeFile)
86-
->buildYalm()
86+
->buildYaml()
8787
);
8888

8989
file_put_contents(
@@ -114,7 +114,7 @@ function it_returns_1_when_phpcs_finds_bugs_and_fixes_them_automatically()
114114
file_put_contents(
115115
'githooks.yml',
116116
$this->configurationFileBuilder->setTools(['phpcbf'])
117-
->buildYalm()
117+
->buildYaml()
118118
);
119119

120120
file_put_contents(
@@ -162,7 +162,7 @@ function it_returns_exit_code_0_when_the_tool_fails_and_has_ignoreErrorsOnExit_s
162162
'githooks.yml',
163163
$this->configurationFileBuilder->setTools([$tool])
164164
->changeToolOption($tool, ['ignoreErrorsOnExit' => true])
165-
->buildYalm()
165+
->buildYaml()
166166
);
167167

168168
file_put_contents(
@@ -182,7 +182,7 @@ function it_runs_all_tools_in_multipe_processes()
182182
file_put_contents(
183183
'githooks.yml',
184184
$this->configurationFileBuilder->setTools(['phpcs', 'phpcbf', 'parallel-lint', 'phpmd', 'phpcpd', 'phpstan'])
185-
->buildYalm()
185+
->buildYaml()
186186
);
187187
passthru("$this->githooks tool all --processes=2", $exitCode);
188188

tests/Unit/ConfigurationFile/FileReaderTest.php

+9-9
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ public function validConfigurationFilesDataProvider()
5050
{
5151
return [
5252
'From root directory' => [
53-
['githooks.yml' => $this->setConfigurationFileBuilder()->buildYalm()]
53+
['githooks.yml' => $this->setConfigurationFileBuilder()->buildYaml()]
5454
],
5555
'From qa/ directory' => [
56-
'qa' => ['githooks.yml' => $this->setConfigurationFileBuilder()->buildYalm()]
56+
'qa' => ['githooks.yml' => $this->setConfigurationFileBuilder()->buildYaml()]
5757
]
5858
];
5959
}
@@ -74,10 +74,10 @@ function it_can_read_file_configuration_githooksDotYml($fileSystemStructure)
7474
*/
7575
function it_searchs_configuration_file_first_in_the_root_directory()
7676
{
77-
$rootFileYalm = $this->configurationFileBuilder->setTools(['phpcs'])->buildYalm();
77+
$rootFileYalm = $this->configurationFileBuilder->setTools(['phpcs'])->buildYaml();
7878
$rootFileArray = $this->configurationFileBuilder->buildArray();
7979

80-
$qaFileYalm = $this->configurationFileBuilder->setTools(['parrallel-lint'])->buildYalm();
80+
$qaFileYalm = $this->configurationFileBuilder->setTools(['parrallel-lint'])->buildYaml();
8181
$qaFileArray = $this->configurationFileBuilder->buildArray();
8282

8383
$fileSystemStructure = [
@@ -136,7 +136,7 @@ function it_throws_exception_when_file_type_is_not_supported()
136136
function it_prioritizes_php_files_over_yml()
137137
{
138138
$phpConfig = "<?php return ['tools' => ['phpunit']];";
139-
$ymlConfig = $this->configurationFileBuilder->setTools(['phpcs'])->buildYalm();
139+
$ymlConfig = $this->configurationFileBuilder->setTools(['phpcs'])->buildYaml();
140140

141141
$fileSystemStructure = [
142142
'githooks.php' => $phpConfig,
@@ -161,8 +161,8 @@ public function configurationFilesInRootAndQaDataProvider()
161161
'php'
162162
],
163163
'YAML files' => [
164-
$this->setConfigurationFileBuilder()->setTools(['phpunit'])->buildYalm(),
165-
$this->setConfigurationFileBuilder()->setTools(['phpcs'])->buildYalm(),
164+
$this->setConfigurationFileBuilder()->setTools(['phpunit'])->buildYaml(),
165+
$this->setConfigurationFileBuilder()->setTools(['phpcs'])->buildYaml(),
166166
$this->setConfigurationFileBuilder()->setTools(['phpunit'])->buildArray(),
167167
'yml'
168168
]
@@ -191,7 +191,7 @@ function it_prioritizes_files_in_root_directory_over_files_in_qa_directory($root
191191
/** @test */
192192
function it_reads_configuration_file_from_relative_path()
193193
{
194-
$yamlConfig = $this->configurationFileBuilder->setTools(['phpcs'])->buildYalm();
194+
$yamlConfig = $this->configurationFileBuilder->setTools(['phpcs'])->buildYaml();
195195
$relativePath = 'custom/path/githooks.yml';
196196

197197
$fileSystemStructure = [
@@ -211,7 +211,7 @@ function it_reads_configuration_file_from_relative_path()
211211
/** @test */
212212
function it_prioritizes_configFile_parameter_over_default_search()
213213
{
214-
$yamlConfig = $this->configurationFileBuilder->setTools(['phpcs'])->buildYalm();
214+
$yamlConfig = $this->configurationFileBuilder->setTools(['phpcs'])->buildYaml();
215215
$phpConfig = "<?php return ['tools' => ['phpunit']];";
216216
$relativePath = 'custom/path/githooks.yml';
217217
$fileSystemStructure = [

tests/Utils/ConfigurationFileBuilder.php

+24-16
Original file line numberDiff line numberDiff line change
@@ -2,29 +2,25 @@
22

33
namespace Tests\Utils;
44

5-
use Wtyd\GitHooks\Tools\Tool\{
6-
Phpcpd,
7-
SecurityChecker,
8-
ParallelLint,
9-
Phpmd,
10-
Phpstan,
11-
ToolAbstract
12-
};
13-
use Wtyd\GitHooks\Tools\Tool\CodeSniffer\{
14-
Phpcs,
15-
Phpcbf
16-
};
175
use Symfony\Component\Yaml\Yaml;
186
use Wtyd\GitHooks\ConfigurationFile\ConfigurationFile;
197
use Wtyd\GitHooks\ConfigurationFile\OptionsConfiguration;
208
use Wtyd\GitHooks\LoadTools\ExecutionMode;
9+
use Wtyd\GitHooks\Tools\Tool\CodeSniffer\Phpcbf;
10+
use Wtyd\GitHooks\Tools\Tool\CodeSniffer\Phpcs;
11+
use Wtyd\GitHooks\Tools\Tool\ParallelLint;
12+
use Wtyd\GitHooks\Tools\Tool\Phpcpd;
13+
use Wtyd\GitHooks\Tools\Tool\Phpmd;
14+
use Wtyd\GitHooks\Tools\Tool\Phpstan;
15+
use Wtyd\GitHooks\Tools\Tool\SecurityChecker;
16+
use Wtyd\GitHooks\Tools\Tool\ToolAbstract;
2117

2218
/**
2319
* Facilitates the creation of custom 'githooks.yml' configuration files for testing.
2420
* By default prepares a full configuration file. After, you can change each part of the file with the 'set' methods.
2521
* Finally you must 'build' the file:
2622
* 1. build method: build the file on array associative format.
27-
* 2. buildYalm: build the file on yalm format.
23+
* 2. buildYaml: build the file on yalm format.
2824
*/
2925
class ConfigurationFileBuilder
3026
{
@@ -159,7 +155,7 @@ public function buildArray($optionsIsNotAssociative = false): array
159155
*
160156
* @return string
161157
*/
162-
public function buildYalm(): string
158+
public function buildYaml(): string
163159
{
164160
return Yaml::dump($this->buildArray());
165161
}
@@ -169,9 +165,21 @@ public function buildYalm(): string
169165
*
170166
* @return void
171167
*/
172-
public function buildInFileSystem(): void
168+
public function buildInFileSystem($path = ''): void
173169
{
174-
file_put_contents($this->rootPath . '/' . self::FILE_NAME, $this->buildYalm());
170+
$finalPath = '';
171+
if (!empty($path)) {
172+
$finalPath = "$this->rootPath/$path";
173+
$finalPath = rtrim($finalPath, '/');
174+
} else {
175+
$finalPath = $this->rootPath;
176+
}
177+
178+
if (!is_dir($finalPath)) {
179+
mkdir($finalPath, 0777, true);
180+
}
181+
182+
file_put_contents($finalPath . '/' . self::FILE_NAME, $this->buildYaml());
175183
}
176184

177185
/**

0 commit comments

Comments
 (0)