Skip to content

Commit

Permalink
Add tests for RunCommand (#273)
Browse files Browse the repository at this point in the history
  • Loading branch information
villfa authored Dec 17, 2020
1 parent 77ed001 commit 37493b0
Showing 1 changed file with 54 additions and 0 deletions.
54 changes: 54 additions & 0 deletions tests/Integration/Command/RunCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,23 @@
use Churn\Command\RunCommand;
use Churn\Tests\BaseTestCase;
use DI\ContainerBuilder;
use function file_get_contents;
use function is_file;
use function json_decode;
use Symfony\Component\Console\Application;
use Symfony\Component\Console\Tester\CommandTester;
use function sys_get_temp_dir;
use function tempnam;
use function unlink;

class RunCommandTest extends BaseTestCase
{
/** @var CommandTester */
private $commandTester;

/** @var string|null */
private $tmpFile;

protected function setUp()
{
$container = ContainerBuilder::buildDevContainer();
Expand All @@ -25,6 +34,10 @@ protected function setUp()
protected function tearDown()
{
$this->commandTester = null;

if ($this->tmpFile !== null && is_file($this->tmpFile)) {
unlink($this->tmpFile);
}
}

/** @test */
Expand All @@ -36,4 +49,45 @@ public function it_displays_the_logo_at_the_beginning_by_default()
$this->assertEquals(0, $exitCode);
$this->assertEquals(RunCommand::LOGO, substr($display, 0, strlen(RunCommand::LOGO)));
}

/** @test */
public function it_can_return_a_json_report()
{
$exitCode = $this->commandTester->execute(['paths' => [realpath(__DIR__ . '/../..')], '--format' => 'json']);
$data = json_decode($this->commandTester->getDisplay(), true);

$this->assertEquals(0, $exitCode);
$this->assertReport($data);
}

/** @test */
public function it_can_write_a_json_report()
{
$this->tmpFile = tempnam(sys_get_temp_dir(), 'churn-test-');
$exitCode = $this->commandTester->execute([
'paths' => [realpath(__DIR__ . '/../..')],
'--format' => 'json',
'--output' => $this->tmpFile,
]);
$display = $this->commandTester->getDisplay();

$this->assertEquals(0, $exitCode);
$this->assertEquals(RunCommand::LOGO, substr($display, 0, strlen(RunCommand::LOGO)));

$this->assertFileExists($this->tmpFile);
$data = json_decode(file_get_contents($this->tmpFile), true);
$this->assertReport($data);
}

private function assertReport($data): void
{
$i = 0;
foreach ($data as $key => $value) {
$this->assertEquals($i++, $key);
$this->assertArrayHasKey('file', $value);
$this->assertArrayHasKey('commits', $value);
$this->assertArrayHasKey('complexity', $value);
$this->assertArrayHasKey('score', $value);
}
}
}

0 comments on commit 37493b0

Please sign in to comment.