Skip to content

Commit 09bce44

Browse files
authored
[Tests] Add feature tests (#4)
1 parent ce96196 commit 09bce44

17 files changed

+264
-52
lines changed

bin/mr-linter

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#!/usr/bin/env php
22
<?php
33

4+
use ArtARTs36\MergeRequestLinter\Configuration\PhpConfigLoader;
45
use ArtARTs36\MergeRequestLinter\Console\DumpCommand;
56
use ArtARTs36\MergeRequestLinter\Console\InstallCommand;
67
use ArtARTs36\MergeRequestLinter\Console\LintCommand;
@@ -28,8 +29,10 @@ if ($loaded === false) {
2829

2930
$application = new Application('Merge Request Linter', '0.1.0');
3031

31-
$application->add(new LintCommand());
32+
$configLoader = new PhpConfigLoader();
33+
34+
$application->add(new LintCommand($configLoader));
3235
$application->add(new InstallCommand());
33-
$application->add(new DumpCommand());
36+
$application->add(new DumpCommand($configLoader));
3437

3538
$application->run();

src/Configuration/ConfigLoader.php renamed to src/Configuration/PhpConfigLoader.php

+2-4
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,11 @@
22

33
namespace ArtARTs36\MergeRequestLinter\Configuration;
44

5+
use ArtARTs36\MergeRequestLinter\Contracts\ConfigLoader;
56
use ArtARTs36\MergeRequestLinter\Exception\ConfigInvalidException;
67

7-
class ConfigLoader
8+
class PhpConfigLoader implements ConfigLoader
89
{
9-
/**
10-
* @throws ConfigInvalidException
11-
*/
1210
public function load(string $path): Config
1311
{
1412
try {

src/Console/DumpCommand.php

+7-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace ArtARTs36\MergeRequestLinter\Console;
44

5-
use ArtARTs36\MergeRequestLinter\Configuration\ConfigLoader;
5+
use ArtARTs36\MergeRequestLinter\Contracts\ConfigLoader;
66
use Symfony\Component\Console\Command\Command;
77
use Symfony\Component\Console\Input\InputInterface;
88
use Symfony\Component\Console\Output\OutputInterface;
@@ -14,9 +14,14 @@ class DumpCommand extends Command
1414

1515
protected static $defaultDescription = 'Print current rules';
1616

17+
public function __construct(protected ConfigLoader $configLoader, string $name = null)
18+
{
19+
parent::__construct($name);
20+
}
21+
1722
protected function execute(InputInterface $input, OutputInterface $output)
1823
{
19-
$config = (new ConfigLoader())->load($path = getcwd() . DIRECTORY_SEPARATOR . '.mr-linter.php');
24+
$config = $this->configLoader->load($path = getcwd() . DIRECTORY_SEPARATOR . '.mr-linter.php');
2025

2126
$style = new SymfonyStyle($input, $output);
2227

src/Console/InstallCommand.php

+5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ class InstallCommand extends Command
1313

1414
protected static $defaultDescription = 'Install this tool';
1515

16+
public function __construct(string $name = null)
17+
{
18+
parent::__construct($name);
19+
}
20+
1621
protected function execute(InputInterface $input, OutputInterface $output)
1722
{
1823
$dir = getcwd();

src/Console/LintCommand.php

+7-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace ArtARTs36\MergeRequestLinter\Console;
44

5-
use ArtARTs36\MergeRequestLinter\Configuration\ConfigLoader;
5+
use ArtARTs36\MergeRequestLinter\Contracts\ConfigLoader;
66
use ArtARTs36\MergeRequestLinter\Contracts\LinterRunnerFactory;
77
use ArtARTs36\MergeRequestLinter\Contracts\Note;
88
use ArtARTs36\MergeRequestLinter\Environment\LocalEnvironment;
@@ -21,16 +21,19 @@ class LintCommand extends Command
2121

2222
protected LinterRunnerFactory $runnerFactory;
2323

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+
) {
2629
$this->runnerFactory = $runnerFactory ?? new RunnerFactory(new LocalEnvironment());
2730

2831
parent::__construct($name);
2932
}
3033

3134
protected function execute(InputInterface $input, OutputInterface $output)
3235
{
33-
$config = (new ConfigLoader())->load($path = getcwd() . DIRECTORY_SEPARATOR . '.mr-linter.php');
36+
$config = $this->configLoader->load($path = getcwd() . DIRECTORY_SEPARATOR . '.mr-linter.php');
3437
$linter = new Linter($config->getRules());
3538

3639
$result = $this->runnerFactory->create($config)->run($linter);

src/Contracts/ConfigLoader.php

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
}

tests/Feature/DumpCommandTest.php

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
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+
}

tests/Feature/InstallCommandTest.php

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
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+
}

tests/Feature/LintCommandTest.php

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
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+
}

tests/Mocks/Cwd.php

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
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+
}

tests/Mocks/MockCIDetector.php

-39
This file was deleted.

tests/Mocks/MockCi.php

+7
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ public function __construct(
2222
//
2323
}
2424

25+
public static function fromMergeRequest(MergeRequest $request): self
26+
{
27+
return new self([
28+
'is_pull_request' => 'true',
29+
], $request);
30+
}
31+
2532
public static function is(Environment $environment): bool
2633
{
2734
return true;

tests/Mocks/MockCiSystemFactory.php

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
}

tests/Mocks/MockConfigLoader.php

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
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+
}

tests/Mocks/MockRunnerFactory.php

+22
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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+
}

0 commit comments

Comments
 (0)