Skip to content

Commit

Permalink
Log phpunit output to console when in debug mode (#683)
Browse files Browse the repository at this point in the history
* Log phpunit output to console when in debug mode

* Fix issue with CI and add a note to the class level dock block
  • Loading branch information
BackEndTea authored and maks-rafalko committed May 1, 2019
1 parent 70371dd commit 5264654
Show file tree
Hide file tree
Showing 8 changed files with 66 additions and 14 deletions.
14 changes: 14 additions & 0 deletions src/Events/InitialTestSuiteFinished.php
Expand Up @@ -40,4 +40,18 @@
*/
final class InitialTestSuiteFinished
{
/**
* @var string
*/
private $outputText;

public function __construct(string $outputText)
{
$this->outputText = $outputText;
}

public function getOutputText(): string
{
return $this->outputText;
}
}
2 changes: 1 addition & 1 deletion src/Process/Builder/SubscriberBuilder.php
Expand Up @@ -228,7 +228,7 @@ private function getInitialTestsConsoleLoggerSubscriber(AbstractTestFrameworkAda
return new CiInitialTestsConsoleLoggerSubscriber($output, $testFrameworkAdapter);
}

return new InitialTestsConsoleLoggerSubscriber($output, $testFrameworkAdapter);
return new InitialTestsConsoleLoggerSubscriber($output, $testFrameworkAdapter, $this->input->getOption('debug'));
}

private function shouldSkipProgressBars(): bool
Expand Down
12 changes: 11 additions & 1 deletion src/Process/Listener/InitialTestsConsoleLoggerSubscriber.php
Expand Up @@ -63,10 +63,16 @@ final class InitialTestsConsoleLoggerSubscriber implements EventSubscriberInterf
*/
private $testFrameworkAdapter;

public function __construct(OutputInterface $output, AbstractTestFrameworkAdapter $testFrameworkAdapter)
/**
* @var bool
*/
private $debug;

public function __construct(OutputInterface $output, AbstractTestFrameworkAdapter $testFrameworkAdapter, bool $debug)
{
$this->output = $output;
$this->testFrameworkAdapter = $testFrameworkAdapter;
$this->debug = $debug;

$this->progressBar = new ProgressBar($this->output);
$this->progressBar->setFormat('verbose');
Expand Down Expand Up @@ -105,6 +111,10 @@ public function onInitialTestSuiteStarted(InitialTestSuiteStarted $event): void
public function onInitialTestSuiteFinished(InitialTestSuiteFinished $event): void
{
$this->progressBar->finish();

if ($this->debug) {
$this->output->writeln(PHP_EOL . $event->getOutputText());
}
}

public function onInitialTestCaseCompleted(InitialTestCaseCompleted $event): void
Expand Down
2 changes: 1 addition & 1 deletion src/Process/Runner/InitialTestsRunner.php
Expand Up @@ -81,7 +81,7 @@ public function run(string $testFrameworkExtraOptions, bool $skipCoverage, array
$this->eventDispatcher->dispatch(new InitialTestCaseCompleted());
});

$this->eventDispatcher->dispatch(new InitialTestSuiteFinished());
$this->eventDispatcher->dispatch(new InitialTestSuiteFinished($process->getOutput()));

return $process;
}
Expand Down
12 changes: 6 additions & 6 deletions tests/Events/InitialTestSuiteFinishedTest.php
Expand Up @@ -43,12 +43,12 @@
*/
final class InitialTestSuiteFinishedTest extends TestCase
{
/**
* This class is only used to fire events, and the only functionality it needs is being instantiated
*/
public function test_it_can_be_initialzed(): void
public function test_it_passes_the_output_along(): void
{
$class = new InitialTestSuiteFinished();
$this->assertInstanceOf(InitialTestSuiteFinished::class, $class);
$text = 'foo-bar-baz';

$class = new InitialTestSuiteFinished($text);

$this->assertSame($text, $class->getOutputText());
}
}
11 changes: 8 additions & 3 deletions tests/Process/Builder/SubscriberBuilderTest.php
Expand Up @@ -51,6 +51,10 @@

/**
* @internal
*
* NOTE:
* InputInterfaces should be mocked here so that the 'getOption' method with paramater 'no-progress'
* should return true. Otherwise you will see different results based on wheter its running in CI or not.
*/
final class SubscriberBuilderTest extends TestCase
{
Expand All @@ -61,7 +65,7 @@ public function test_it_registers_the_subscribers_when_debugging(): void
->method('getOption')
->will($this->returnValueMap(
[
['ci-friendly', false],
['no-progress', true],
['formatter', 'progress'],
['show-mutations', true],
['log-verbosity', 'all'],
Expand Down Expand Up @@ -99,7 +103,7 @@ public function test_it_registers_the_subscribers_when_not_debugging(): void
->method('getOption')
->will($this->returnValueMap(
[
['ci-friendly', false],
['no-progress', true],
['formatter', 'progress'],
['show-mutations', true],
['log-verbosity', 'all'],
Expand Down Expand Up @@ -137,9 +141,10 @@ public function test_it_throws_an_exception_when_output_formatter_is_invalid():
->method('getOption')
->will($this->returnValueMap(
[
['ci-friendly', false],
['no-progress', true],
['formatter', 'foo'],
['show-mutations', true],
['debug', true],
]
));
$calculator = new MetricsCalculator();
Expand Down
24 changes: 22 additions & 2 deletions tests/Process/Listener/InitialTestsConsoleLoggerSubscriberTest.php
Expand Up @@ -36,6 +36,7 @@
namespace Infection\Tests\Process\Listener;

use Infection\EventDispatcher\EventDispatcher;
use Infection\Events\InitialTestSuiteFinished;
use Infection\Events\InitialTestSuiteStarted;
use Infection\Process\Listener\InitialTestsConsoleLoggerSubscriber;
use Infection\TestFramework\AbstractTestFrameworkAdapter;
Expand All @@ -58,7 +59,7 @@ public function test_it_reacts_on_initial_test_suite_run(): void
->method('getVersion');

$dispatcher = new EventDispatcher();
$dispatcher->addSubscriber(new InitialTestsConsoleLoggerSubscriber($output, $testFramework));
$dispatcher->addSubscriber(new InitialTestsConsoleLoggerSubscriber($output, $testFramework, false));

$dispatcher->dispatch(new InitialTestSuiteStarted());
}
Expand All @@ -85,8 +86,27 @@ public function test_it_sets_test_framework_version_as_unknown_in_case_of_except
->will($this->throwException(new \InvalidArgumentException()));

$dispatcher = new EventDispatcher();
$dispatcher->addSubscriber(new InitialTestsConsoleLoggerSubscriber($output, $testFramework));
$dispatcher->addSubscriber(new InitialTestsConsoleLoggerSubscriber($output, $testFramework, false));

$dispatcher->dispatch(new InitialTestSuiteStarted());
}

public function test_it_outputs_the_initial_process_text_if_in_debug_mode(): void
{
$processText = 'PHPUnit Test suite ...';
$output = $this->createMock(OutputInterface::class);
$output->expects($this->once())
->method('writeln')
->with(PHP_EOL . $processText);

$output->method('getVerbosity')
->willReturn(OutputInterface::VERBOSITY_QUIET);

$testFramework = $this->createMock(AbstractTestFrameworkAdapter::class);

$dispatcher = new EventDispatcher();
$dispatcher->addSubscriber(new InitialTestsConsoleLoggerSubscriber($output, $testFramework, true));

$dispatcher->dispatch(new InitialTestSuiteFinished($processText));
}
}
3 changes: 3 additions & 0 deletions tests/Process/Runner/InitialTestsRunnerTest.php
Expand Up @@ -62,6 +62,9 @@ public function test_it_dispatches_events(): void

return true;
}));
$process->expects($this->once())
->method('getOutput')
->willReturn('foo');

$processBuilder = $this->createMock(ProcessBuilder::class);
$processBuilder->method('getProcessForInitialTestRun')
Expand Down

0 comments on commit 5264654

Please sign in to comment.