Skip to content

Commit b114bdd

Browse files
committed
add unit tests for all classes
1 parent f0198ed commit b114bdd

7 files changed

+229
-0
lines changed
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
namespace Permafrost\CoverageCheck\Tests\Commands;
4+
5+
use Permafrost\CoverageCheck\Commands\CheckCoverageCommand;
6+
use Permafrost\CoverageCheck\Tests\TestClasses\FakeOutput;
7+
use PHPUnit\Framework\TestCase;
8+
use Spatie\Snapshots\MatchesSnapshots;
9+
use Symfony\Component\Console\Input\ArrayInput;
10+
use Symfony\Component\Console\Input\InputArgument;
11+
use Symfony\Component\Console\Input\InputDefinition;
12+
use Symfony\Component\Console\Input\InputOption;
13+
14+
class CheckCoverageCommandTest extends TestCase
15+
{
16+
use MatchesSnapshots;
17+
18+
/** @var CheckCoverageCommand */
19+
protected $command;
20+
21+
/** @var FakeOutput */
22+
protected $output;
23+
24+
protected function setUp(): void
25+
{
26+
parent::setUp();
27+
28+
$this->command = new CheckCoverageCommand('check');
29+
$this->output = new FakeOutput();
30+
}
31+
32+
protected function createInput(array $input)
33+
{
34+
$inputDefinition = new InputDefinition([
35+
new InputArgument('filename', InputArgument::REQUIRED),
36+
new InputOption('require', 'r', InputOption::VALUE_REQUIRED),
37+
new InputOption('coverage-only', 'C', InputOption::VALUE_NONE),
38+
]);
39+
40+
return new ArrayInput($input, $inputDefinition);
41+
}
42+
43+
/** @test */
44+
public function it_executes_the_command_with_a_valid_filename_and_enforces_coverage()
45+
{
46+
$input = $this->createInput(['filename' => __DIR__ . '/../data/coverage-clover.xml', '--require' => 50]);
47+
48+
$this->command->execute($input, $this->output);
49+
50+
$this->assertMatchesSnapshot($this->output->writtenData);
51+
}
52+
53+
/** @test */
54+
public function it_executes_the_command_with_an_invalid_filename_and_enforces_coverage()
55+
{
56+
$input = $this->createInput(['filename' => __DIR__ . '/../data/missing.xml', '--require' => 50]);
57+
58+
$this->command->execute($input, $this->output);
59+
60+
$this->assertMatchesSnapshot($this->output->writtenData);
61+
}
62+
63+
/** @test */
64+
public function it_executes_the_command_with_a_valid_filename_and_does_not_enforce_coverage()
65+
{
66+
$input = $this->createInput(['filename' => __DIR__ . '/../data/coverage-clover.xml']);
67+
68+
$this->command->execute($input, $this->output);
69+
70+
$this->assertMatchesSnapshot($this->output->writtenData);
71+
}
72+
73+
/** @test */
74+
public function it_executes_the_command_with_a_valid_filename_does_not_enforce_coverage_and_only_displays_coverage()
75+
{
76+
$input = $this->createInput(['filename' => __DIR__ . '/../data/coverage-clover.xml', '--coverage-only' => true]);
77+
78+
$this->command->execute($input, $this->output);
79+
80+
$this->assertMatchesSnapshot($this->output->writtenData);
81+
}
82+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- 'Code coverage is 89.8765%.'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- '<info>[PASS]</info> Code coverage is 89.8765% (required minimum is 50%).'
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- 89.8765
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- '<error>[ERROR]</error> Invalid input file provided.'
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
namespace Permafrost\CoverageCheck\Tests\Configuration;
4+
5+
use Permafrost\CoverageCheck\Configuration\ConfigurationFactory;
6+
use PHPUnit\Framework\TestCase;
7+
use Symfony\Component\Console\Input\ArrayInput;
8+
use Symfony\Component\Console\Input\InputArgument;
9+
use Symfony\Component\Console\Input\InputDefinition;
10+
use Symfony\Component\Console\Input\InputOption;
11+
12+
class ConfigurationFactoryTest extends TestCase
13+
{
14+
protected function createInput(array $input)
15+
{
16+
$inputDefinition = new InputDefinition([
17+
new InputArgument('filename', InputArgument::REQUIRED),
18+
new InputOption('require', 'r', InputOption::VALUE_REQUIRED),
19+
new InputOption('coverage-only', 'C', InputOption::VALUE_NONE),
20+
]);
21+
22+
return new ArrayInput($input, $inputDefinition);
23+
}
24+
25+
/** @test */
26+
public function it_creates_a_configuration_object()
27+
{
28+
$filename = realpath(__DIR__.'/../data/coverage-clover.xml');
29+
30+
$input = $this->createInput(['filename' => $filename, '--require' => 50, '--coverage-only' => true]);
31+
32+
$config = ConfigurationFactory::create($input);
33+
34+
$this->assertTrue($config->requireMode);
35+
$this->assertEquals(50, $config->required);
36+
$this->assertEquals($filename, $config->filename);
37+
$this->assertTrue($config->displayCoverageOnly);
38+
}
39+
40+
/** @test */
41+
public function it_throws_an_exception_when_validating_the_configuration_if_the_filename_does_not_exist()
42+
{
43+
$filename = realpath(__DIR__.'/../data/missing.xml');
44+
45+
$input = $this->createInput(['filename' => $filename, '--require' => 50]);
46+
$config = ConfigurationFactory::create($input);
47+
48+
$this->expectException(\InvalidArgumentException::class);
49+
50+
$config->validate();
51+
}
52+
53+
/** @test */
54+
public function it_does_not_throw_an_exception_when_validating_the_configuration_if_the_filename_exists()
55+
{
56+
$filename = realpath(__DIR__.'/../data/coverage-clover.xml');
57+
58+
$input = $this->createInput(['filename' => $filename, '--require' => 50]);
59+
$config = ConfigurationFactory::create($input);
60+
$hasException = false;
61+
62+
try {
63+
$config->validate();
64+
} catch(\Exception $e) {
65+
$hasException = true;
66+
}
67+
68+
$this->assertFalse($hasException);
69+
}
70+
}

tests/TestClasses/FakeOutput.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
3+
namespace Permafrost\CoverageCheck\Tests\TestClasses;
4+
5+
use Symfony\Component\Console\Formatter\OutputFormatterInterface;
6+
use Symfony\Component\Console\Output\OutputInterface;
7+
8+
class FakeOutput implements OutputInterface
9+
{
10+
/** @var array|string[] */
11+
public $writtenData = [];
12+
13+
public function isDecorated()
14+
{
15+
// TODO: Implement isDecorated() method.
16+
}
17+
18+
public function setDecorated(bool $decorated)
19+
{
20+
// TODO: Implement setDecorated() method.
21+
}
22+
23+
public function write($messages, bool $newline = false, int $options = 0)
24+
{
25+
$this->writtenData[] = $messages . ($newline ? PHP_EOL : '');
26+
}
27+
28+
public function setVerbosity(int $level)
29+
{
30+
// TODO: Implement setVerbosity() method.
31+
}
32+
33+
public function setFormatter(OutputFormatterInterface $formatter)
34+
{
35+
// TODO: Implement setFormatter() method.
36+
}
37+
38+
public function isVeryVerbose()
39+
{
40+
// TODO: Implement isVeryVerbose() method.
41+
}
42+
43+
public function isVerbose()
44+
{
45+
// TODO: Implement isVerbose() method.
46+
}
47+
48+
public function isQuiet()
49+
{
50+
// TODO: Implement isQuiet() method.
51+
}
52+
53+
public function isDebug()
54+
{
55+
// TODO: Implement isDebug() method.
56+
}
57+
58+
public function getVerbosity()
59+
{
60+
// TODO: Implement getVerbosity() method.
61+
}
62+
63+
public function getFormatter()
64+
{
65+
// TODO: Implement getFormatter() method.
66+
}
67+
68+
public function writeln($messages, int $options = 0)
69+
{
70+
$this->writtenData[] = $messages;
71+
}
72+
73+
}

0 commit comments

Comments
 (0)