File tree 17 files changed +264
-52
lines changed
17 files changed +264
-52
lines changed Original file line number Diff line number Diff line change 1
1
#!/usr/bin/env php
2
2
<?php
3
3
4
+ use ArtARTs36 \MergeRequestLinter \Configuration \PhpConfigLoader ;
4
5
use ArtARTs36 \MergeRequestLinter \Console \DumpCommand ;
5
6
use ArtARTs36 \MergeRequestLinter \Console \InstallCommand ;
6
7
use ArtARTs36 \MergeRequestLinter \Console \LintCommand ;
@@ -28,8 +29,10 @@ if ($loaded === false) {
28
29
29
30
$ application = new Application ('Merge Request Linter ' , '0.1.0 ' );
30
31
31
- $ application ->add (new LintCommand ());
32
+ $ configLoader = new PhpConfigLoader ();
33
+
34
+ $ application ->add (new LintCommand ($ configLoader ));
32
35
$ application ->add (new InstallCommand ());
33
- $ application ->add (new DumpCommand ());
36
+ $ application ->add (new DumpCommand ($ configLoader ));
34
37
35
38
$ application ->run ();
Original file line number Diff line number Diff line change 2
2
3
3
namespace ArtARTs36 \MergeRequestLinter \Configuration ;
4
4
5
+ use ArtARTs36 \MergeRequestLinter \Contracts \ConfigLoader ;
5
6
use ArtARTs36 \MergeRequestLinter \Exception \ConfigInvalidException ;
6
7
7
- class ConfigLoader
8
+ class PhpConfigLoader implements ConfigLoader
8
9
{
9
- /**
10
- * @throws ConfigInvalidException
11
- */
12
10
public function load (string $ path ): Config
13
11
{
14
12
try {
Original file line number Diff line number Diff line change 2
2
3
3
namespace ArtARTs36 \MergeRequestLinter \Console ;
4
4
5
- use ArtARTs36 \MergeRequestLinter \Configuration \ConfigLoader ;
5
+ use ArtARTs36 \MergeRequestLinter \Contracts \ConfigLoader ;
6
6
use Symfony \Component \Console \Command \Command ;
7
7
use Symfony \Component \Console \Input \InputInterface ;
8
8
use Symfony \Component \Console \Output \OutputInterface ;
@@ -14,9 +14,14 @@ class DumpCommand extends Command
14
14
15
15
protected static $ defaultDescription = 'Print current rules ' ;
16
16
17
+ public function __construct (protected ConfigLoader $ configLoader , string $ name = null )
18
+ {
19
+ parent ::__construct ($ name );
20
+ }
21
+
17
22
protected function execute (InputInterface $ input , OutputInterface $ output )
18
23
{
19
- $ config = ( new ConfigLoader ()) ->load ($ path = getcwd () . DIRECTORY_SEPARATOR . '.mr-linter.php ' );
24
+ $ config = $ this -> configLoader ->load ($ path = getcwd () . DIRECTORY_SEPARATOR . '.mr-linter.php ' );
20
25
21
26
$ style = new SymfonyStyle ($ input , $ output );
22
27
Original file line number Diff line number Diff line change @@ -13,6 +13,11 @@ class InstallCommand extends Command
13
13
14
14
protected static $ defaultDescription = 'Install this tool ' ;
15
15
16
+ public function __construct (string $ name = null )
17
+ {
18
+ parent ::__construct ($ name );
19
+ }
20
+
16
21
protected function execute (InputInterface $ input , OutputInterface $ output )
17
22
{
18
23
$ dir = getcwd ();
Original file line number Diff line number Diff line change 2
2
3
3
namespace ArtARTs36 \MergeRequestLinter \Console ;
4
4
5
- use ArtARTs36 \MergeRequestLinter \Configuration \ConfigLoader ;
5
+ use ArtARTs36 \MergeRequestLinter \Contracts \ConfigLoader ;
6
6
use ArtARTs36 \MergeRequestLinter \Contracts \LinterRunnerFactory ;
7
7
use ArtARTs36 \MergeRequestLinter \Contracts \Note ;
8
8
use ArtARTs36 \MergeRequestLinter \Environment \LocalEnvironment ;
@@ -21,16 +21,19 @@ class LintCommand extends Command
21
21
22
22
protected LinterRunnerFactory $ runnerFactory ;
23
23
24
- public function __construct (?LinterRunnerFactory $ runnerFactory = null , string $ name = null )
25
- {
24
+ public function __construct (
25
+ protected ConfigLoader $ configLoader ,
26
+ ?LinterRunnerFactory $ runnerFactory = null ,
27
+ string $ name = null
28
+ ) {
26
29
$ this ->runnerFactory = $ runnerFactory ?? new RunnerFactory (new LocalEnvironment ());
27
30
28
31
parent ::__construct ($ name );
29
32
}
30
33
31
34
protected function execute (InputInterface $ input , OutputInterface $ output )
32
35
{
33
- $ config = ( new ConfigLoader ()) ->load ($ path = getcwd () . DIRECTORY_SEPARATOR . '.mr-linter.php ' );
36
+ $ config = $ this -> configLoader ->load ($ path = getcwd () . DIRECTORY_SEPARATOR . '.mr-linter.php ' );
34
37
$ linter = new Linter ($ config ->getRules ());
35
38
36
39
$ result = $ this ->runnerFactory ->create ($ config )->run ($ linter );
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace ArtARTs36 \MergeRequestLinter \Contracts ;
4
+
5
+ use ArtARTs36 \MergeRequestLinter \Configuration \Config ;
6
+ use ArtARTs36 \MergeRequestLinter \Exception \ConfigInvalidException ;
7
+
8
+ /**
9
+ * Config Loader.
10
+ */
11
+ interface ConfigLoader
12
+ {
13
+ /**
14
+ * Load config from path.
15
+ * @throws ConfigInvalidException
16
+ */
17
+ public function load (string $ path ): Config ;
18
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace ArtARTs36 \MergeRequestLinter \Tests \Feature ;
4
+
5
+ use ArtARTs36 \MergeRequestLinter \Configuration \Config ;
6
+ use ArtARTs36 \MergeRequestLinter \Console \DumpCommand ;
7
+ use ArtARTs36 \MergeRequestLinter \Tests \Mocks \MockConfigLoader ;
8
+ use ArtARTs36 \MergeRequestLinter \Tests \Mocks \SuccessRule ;
9
+ use ArtARTs36 \MergeRequestLinter \Tests \TestCase ;
10
+ use ArtARTs36 \Str \Facade \Str ;
11
+ use Symfony \Component \Console \Tester \CommandTester ;
12
+
13
+ final class DumpCommandTest extends TestCase
14
+ {
15
+ /**
16
+ * @covers \ArtARTs36\MergeRequestLinter\Console\DumpCommand::execute
17
+ */
18
+ public function testExecute (): void
19
+ {
20
+ $ configLoader = new MockConfigLoader (Config::fromArray ([
21
+ 'rules ' => [
22
+ new SuccessRule (),
23
+ ],
24
+ 'credentials ' => [],
25
+ 'http_client ' => fn () => null ,
26
+ ]));
27
+
28
+ $ tester = new CommandTester (new DumpCommand ($ configLoader ));
29
+
30
+ $ tester ->execute ([]);
31
+
32
+ $ tester ->assertCommandIsSuccessful ();
33
+
34
+ self ::assertStringContainsString (
35
+ '1 Success rule ArtARTs36\MergeRequestLinter\Tests\Mocks\SuccessRule ' ,
36
+ Str::deleteUnnecessarySpaces ($ tester ->getDisplay ())
37
+ );
38
+ }
39
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace ArtARTs36 \MergeRequestLinter \Tests \Feature ;
4
+
5
+ use ArtARTs36 \MergeRequestLinter \Console \InstallCommand ;
6
+ use ArtARTs36 \MergeRequestLinter \Tests \Mocks \Cwd ;
7
+ use ArtARTs36 \MergeRequestLinter \Tests \TestCase ;
8
+ use Symfony \Component \Console \Tester \CommandTester ;
9
+
10
+ final class InstallCommandTest extends TestCase
11
+ {
12
+ /**
13
+ * @covers \ArtARTs36\MergeRequestLinter\Console\InstallCommand::execute
14
+ */
15
+ public function testExecute (): void
16
+ {
17
+ $ cwd = new Cwd ();
18
+
19
+ $ cwd ->set (__DIR__ );
20
+
21
+ $ command = new InstallCommand ();
22
+ $ tester = new CommandTester ($ command );
23
+
24
+ $ tester ->execute ([]);
25
+
26
+ $ tester ->assertCommandIsSuccessful ();
27
+ self ::assertFileExists ('.mr-linter.php ' );
28
+ self ::assertStringContainsString ('Was copied configuration file to: ' , $ tester ->getDisplay ());
29
+
30
+ @unlink ('.mr-linter.php ' );
31
+
32
+ $ cwd ->revert ();
33
+ }
34
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace ArtARTs36 \MergeRequestLinter \Tests \Feature ;
4
+
5
+ use ArtARTs36 \MergeRequestLinter \Configuration \Config ;
6
+ use ArtARTs36 \MergeRequestLinter \Console \LintCommand ;
7
+ use ArtARTs36 \MergeRequestLinter \Tests \Mocks \MockCi ;
8
+ use ArtARTs36 \MergeRequestLinter \Tests \Mocks \MockCiSystemFactory ;
9
+ use ArtARTs36 \MergeRequestLinter \Tests \Mocks \MockConfigLoader ;
10
+ use ArtARTs36 \MergeRequestLinter \Tests \Mocks \MockRunnerFactory ;
11
+ use ArtARTs36 \MergeRequestLinter \Tests \Mocks \SuccessRule ;
12
+ use ArtARTs36 \MergeRequestLinter \Tests \TestCase ;
13
+ use Symfony \Component \Console \Tester \CommandTester ;
14
+
15
+ final class LintCommandTest extends TestCase
16
+ {
17
+ /**
18
+ * @covers \ArtARTs36\MergeRequestLinter\Console\LintCommand::execute
19
+ */
20
+ public function testExecuteAllGood (): void
21
+ {
22
+ $ configLoader = new MockConfigLoader (Config::fromArray ([
23
+ 'rules ' => [
24
+ new SuccessRule (),
25
+ ],
26
+ 'credentials ' => [],
27
+ 'http_client ' => fn () => null ,
28
+ ]));
29
+
30
+ $ tester = new CommandTester (new LintCommand ($ configLoader , new MockRunnerFactory (
31
+ new MockCiSystemFactory (MockCi::fromMergeRequest ($ this ->makeMergeRequest ()))
32
+ )));
33
+
34
+ $ tester ->execute ([]);
35
+
36
+ $ tester ->assertCommandIsSuccessful ();
37
+
38
+ self ::assertStringContainsString ('All good! ' , $ tester ->getDisplay ());
39
+ }
40
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace ArtARTs36 \MergeRequestLinter \Tests \Mocks ;
4
+
5
+ class Cwd
6
+ {
7
+ private string $ prevCwd ;
8
+
9
+ private string $ cwd ;
10
+
11
+ public function __construct ()
12
+ {
13
+ $ this ->prevCwd = getcwd ();
14
+ $ this ->cwd = $ this ->prevCwd ;
15
+ }
16
+
17
+ public function set (string $ cwd ): self
18
+ {
19
+ $ this ->prevCwd = $ this ->cwd ;
20
+ $ this ->cwd = $ cwd ;
21
+
22
+ return $ this ->update ();
23
+ }
24
+
25
+ public function revert (): self
26
+ {
27
+ $ this ->set ($ this ->prevCwd );
28
+
29
+ return $ this ;
30
+ }
31
+
32
+ private function update (): self
33
+ {
34
+ chdir ($ this ->cwd );
35
+
36
+ return $ this ;
37
+ }
38
+ }
Load Diff This file was deleted.
Original file line number Diff line number Diff line change @@ -22,6 +22,13 @@ public function __construct(
22
22
//
23
23
}
24
24
25
+ public static function fromMergeRequest (MergeRequest $ request ): self
26
+ {
27
+ return new self ([
28
+ 'is_pull_request ' => 'true ' ,
29
+ ], $ request );
30
+ }
31
+
25
32
public static function is (Environment $ environment ): bool
26
33
{
27
34
return true ;
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace ArtARTs36 \MergeRequestLinter \Tests \Mocks ;
4
+
5
+ use ArtARTs36 \MergeRequestLinter \Contracts \CiSystem ;
6
+ use ArtARTs36 \MergeRequestLinter \Contracts \CiSystemFactory ;
7
+
8
+ final class MockCiSystemFactory implements CiSystemFactory
9
+ {
10
+ public function __construct (private CiSystem $ system )
11
+ {
12
+ //
13
+ }
14
+
15
+ public function createCurrently (): CiSystem
16
+ {
17
+ return $ this ->system ;
18
+ }
19
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace ArtARTs36 \MergeRequestLinter \Tests \Mocks ;
4
+
5
+ use ArtARTs36 \MergeRequestLinter \Configuration \Config ;
6
+ use ArtARTs36 \MergeRequestLinter \Contracts \ConfigLoader ;
7
+
8
+ final class MockConfigLoader implements ConfigLoader
9
+ {
10
+ public function __construct (private Config $ config )
11
+ {
12
+ //
13
+ }
14
+
15
+ public function load (string $ path ): Config
16
+ {
17
+ return $ this ->config ;
18
+ }
19
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace ArtARTs36 \MergeRequestLinter \Tests \Mocks ;
4
+
5
+ use ArtARTs36 \MergeRequestLinter \Configuration \Config ;
6
+ use ArtARTs36 \MergeRequestLinter \Contracts \CiSystemFactory ;
7
+ use ArtARTs36 \MergeRequestLinter \Contracts \LinterRunner ;
8
+ use ArtARTs36 \MergeRequestLinter \Contracts \LinterRunnerFactory ;
9
+ use ArtARTs36 \MergeRequestLinter \Linter \Runner \Runner ;
10
+
11
+ final class MockRunnerFactory implements LinterRunnerFactory
12
+ {
13
+ public function __construct (private CiSystemFactory $ ciSystemFactory )
14
+ {
15
+ //
16
+ }
17
+
18
+ public function create (Config $ config ): LinterRunner
19
+ {
20
+ return new Runner ($ this ->ciSystemFactory );
21
+ }
22
+ }
You can’t perform that action at this time.
0 commit comments