Skip to content

Commit

Permalink
Merge pull request #193 from facile-it/use-autowire
Browse files Browse the repository at this point in the history
Use autowire
  • Loading branch information
Jean85 authored Mar 2, 2023
2 parents ba237ca + de4e264 commit 9d5c968
Show file tree
Hide file tree
Showing 12 changed files with 96 additions and 174 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,16 @@

namespace Paraunit\Configuration\DependencyInjection;

use Paraunit\Configuration\ChunkSize;
use Paraunit\Configuration\PHPDbgBinFile;
use Paraunit\Configuration\PHPUnitBinFile;
use Paraunit\Configuration\PHPUnitConfig;
use Paraunit\Configuration\TempFilenameFactory;
use Paraunit\Coverage\CoverageFetcher;
use Paraunit\Coverage\CoverageMerger;
use Paraunit\Coverage\CoverageResult;
use Paraunit\Printer\CoveragePrinter;
use Paraunit\Process\CommandLineWithCoverage;
use Paraunit\Process\ProcessFactoryInterface;
use Paraunit\Process\ProcessFactory;
use Paraunit\Proxy\PcovProxy;
use Paraunit\Proxy\XDebugProxy;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;

class CoverageContainerDefinition extends ParallelContainerDefinition
Expand All @@ -36,29 +31,17 @@ public function configure(ContainerBuilder $container): ContainerBuilder

private function configureCoverageConfiguration(ContainerBuilder $container): void
{
$container->setDefinition(PHPDbgBinFile::class, new Definition(PHPDbgBinFile::class));
$container->setDefinition(XDebugProxy::class, new Definition(XDebugProxy::class));
$container->setDefinition(PcovProxy::class, new Definition(PcovProxy::class));
$container->autowire(PHPDbgBinFile::class);
$container->autowire(XDebugProxy::class);
$container->autowire(PcovProxy::class);
}

private function configureProcessWithCoverage(ContainerBuilder $container): void
{
$container->setDefinition(CommandLineWithCoverage::class, new Definition(CommandLineWithCoverage::class, [
new Reference(PHPUnitBinFile::class),
new Reference(ChunkSize::class),
new Reference(PcovProxy::class),
new Reference(XDebugProxy::class),
new Reference(PHPDbgBinFile::class),
new Reference(TempFilenameFactory::class),
]));
$container->autowire(CommandLineWithCoverage::class);

$container->getDefinition(ProcessFactoryInterface::class)
->setArguments([
new Reference(CommandLineWithCoverage::class),
new Reference(PHPUnitConfig::class),
new Reference(TempFilenameFactory::class),
new Reference(ChunkSize::class),
]);
$container->autowire(ProcessFactory::class)
->setArgument('$cliCommand', new Reference(CommandLineWithCoverage::class));
}

private function configureCoverage(ContainerBuilder $container): void
Expand Down
165 changes: 64 additions & 101 deletions src/Configuration/DependencyInjection/ParallelContainerDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
use Paraunit\File\Cleaner;
use Paraunit\File\TempDirectory;
use Paraunit\Filter\Filter;
use Paraunit\Logs\JSON\LogFetcher;
use Paraunit\Logs\JSON\LogHandler;
use Paraunit\Logs\JSON\LogParser;
use Paraunit\Logs\JSON\RetryParser;
use Paraunit\Printer\ConsoleFormatter;
use Paraunit\Printer\FailuresPrinter;
use Paraunit\Printer\FilesRecapPrinter;
Expand All @@ -19,7 +23,6 @@
use Paraunit\Printer\SharkPrinter;
use Paraunit\Process\CommandLine;
use Paraunit\Process\ProcessFactory;
use Paraunit\Process\ProcessFactoryInterface;
use Paraunit\Runner\ChunkFile;
use Paraunit\Runner\PipelineCollection;
use Paraunit\Runner\PipelineFactory;
Expand All @@ -30,153 +33,113 @@
use Symfony\Component\Console\Formatter\OutputFormatterInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\EventDispatcher\DependencyInjection\RegisterListenersPass;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\EventDispatcherInterface as SymfonyEventDispatcherInterface;

class ParallelContainerDefinition
{
private readonly ParserDefinition $parserDefinition;

public function __construct()
{
$this->parserDefinition = new ParserDefinition();
}

public function configure(ContainerBuilder $container): ContainerBuilder
{
$container->setParameter('paraunit.max_retry_count', 3);
$container->setParameter('kernel.root_dir', 'src');

foreach ($this->getAutowirableClasses() as $class) {
$container->autowire($class);
}

$this->configureConfiguration($container);
$this->configureEventDispatcher($container);
$this->configureFile($container);
$this->parserDefinition->configure($container);
$this->configurePrinter($container);
$this->configureProcess($container);
$this->configureRunner($container);
$this->configureServices($container);

return $container;
}

/**
* @return list<class-string>
*/
protected function getAutowirableClasses(): array
{
// alphabetic order
return [
ChunkFile::class,
ChunkSize::class,
Cleaner::class,
CommandLine::class,
ConsoleFormatter::class,
Facade::class,
FailuresPrinter::class,
FilesRecapPrinter::class,
Filter::class,
FinalPrinter::class,
LogFetcher::class,
LogHandler::class,
LogParser::class,
OutputFormatterInterface::class,
PHPUnitBinFile::class,
PHPUnitConfig::class,
PipelineFactory::class,
PipelineCollection::class,
ProcessFactory::class,
ProgressPrinter::class,
RetryParser::class,
SharkPrinter::class,
TempDirectory::class,
TempFilenameFactory::class,
TestResultContainer::class,
];
}

private function configureConfiguration(ContainerBuilder $container): void
{
$container->setDefinition(PHPUnitBinFile::class, new Definition(PHPUnitBinFile::class));
$container->setDefinition(PHPUnitConfig::class, new Definition(PHPUnitConfig::class, [
'%paraunit.phpunit_config_filename%',
]))
$container->autowire(PHPUnitConfig::class)
->setArgument('$inputPathOrFileName', '%paraunit.phpunit_config_filename%')
->setPublic(true);
$container->setDefinition(TempFilenameFactory::class, new Definition(TempFilenameFactory::class, [
new Reference(TempDirectory::class),
]));
$container->setDefinition(ChunkSize::class, new Definition(ChunkSize::class, [
'%paraunit.chunk_size%',
]));

$container->autowire(ChunkSize::class)
->setArgument('$chunkSize', '%paraunit.chunk_size%');
}

private function configureEventDispatcher(ContainerBuilder $container): void
{
$dispatcher = new Definition(EventDispatcher::class);
$container->setDefinition(SymfonyEventDispatcherInterface::class, $dispatcher);
$container->autowire(SymfonyEventDispatcherInterface::class, EventDispatcher::class);
$container->setAlias('event_dispatcher', SymfonyEventDispatcherInterface::class);
$container->setAlias(PsrEventDispatcherInterface::class, SymfonyEventDispatcherInterface::class);

$container->addCompilerPass(new RegisterListenersPass());
}

private function configureFile(ContainerBuilder $container): void
{
$container->setDefinition(TempDirectory::class, new Definition(TempDirectory::class));
$container->setDefinition(Cleaner::class, new Definition(Cleaner::class, [
new Reference(TempDirectory::class),
]));
}

private function configurePrinter(ContainerBuilder $container): void
{
$container->autowire(TestResultContainer::class);

$container->setDefinition(SharkPrinter::class, new Definition(SharkPrinter::class, [
new Reference(OutputInterface::class),
'%paraunit.show_logo%',
]));
$container->setDefinition(ProgressPrinter::class, new Definition(ProgressPrinter::class, [
new Reference(OutputInterface::class),
]));

$container->setDefinition(FinalPrinter::class, new Definition(FinalPrinter::class, [
new Reference(OutputInterface::class),
new Reference(ChunkSize::class),
]));

$container->setDefinition(FailuresPrinter::class, new Definition(FailuresPrinter::class, [
new Reference(OutputInterface::class),
new Reference(TestResultContainer::class),
]));

$container->setDefinition(FilesRecapPrinter::class, new Definition(FilesRecapPrinter::class, [
new Reference(OutputInterface::class),
new Reference(TestResultContainer::class),
new Reference(ChunkSize::class),
]));

$container->setDefinition(ConsoleFormatter::class, new Definition(ConsoleFormatter::class, [
(new Definition(OutputFormatterInterface::class))->setFactory([new Reference(OutputInterface::class), 'getFormatter']),
]));
}
$container->autowire(SharkPrinter::class)
->setArgument('$showLogo', '%paraunit.show_logo%');

private function configureProcess(ContainerBuilder $container): void
{
$container->setDefinition(CommandLine::class, new Definition(CommandLine::class, [
new Reference(PHPUnitBinFile::class),
new Reference(ChunkSize::class),
]));

$container->setDefinition(ProcessFactoryInterface::class, new Definition(ProcessFactory::class, [
new Reference(CommandLine::class),
new Reference(PHPUnitConfig::class),
new Reference(TempFilenameFactory::class),
new Reference(ChunkSize::class),
]));
$container->autowire(OutputFormatterInterface::class)
->setFactory([new Reference(OutputInterface::class), 'getFormatter']);
}

private function configureRunner(ContainerBuilder $container): void
{
$container->setDefinition(PipelineFactory::class, new Definition(PipelineFactory::class, [
new Reference(PsrEventDispatcherInterface::class),
]));
$container->setDefinition(PipelineCollection::class, new Definition(PipelineCollection::class, [
new Reference(PipelineFactory::class),
'%paraunit.max_process_count%',
]));
$container->setDefinition(Runner::class, new Definition(Runner::class, [
new Reference(PsrEventDispatcherInterface::class),
new Reference(ProcessFactoryInterface::class),
new Reference(Filter::class),
new Reference(PipelineCollection::class),
new Reference(ChunkSize::class),
new Reference(ChunkFile::class),
]))
$container->autowire(PipelineCollection::class)
->setArgument('$maxProcessNumber', '%paraunit.max_process_count%');
$container->autowire(Runner::class)
->setPublic(true);

$container->setDefinition(ChunkFile::class, new Definition(ChunkFile::class, [
new Reference(PHPUnitConfig::class),
]));
}

private function configureServices(ContainerBuilder $container): void
{
$container->register(OutputInterface::class, OutputInterface::class)
->setPublic(true)
->setSynthetic(true);
$container->setDefinition(Facade::class, new Definition(Facade::class));
$container->setDefinition(Filter::class, new Definition(Filter::class, [
new Reference(Facade::class),
new Reference(PHPUnitConfig::class),
'%paraunit.testsuite%',
'%paraunit.string_filter%',
]));

$container->autowire(Filter::class)
->setArgument('$testSuiteFilter', '%paraunit.testsuite%')
->setArgument('$stringFilter', '%paraunit.string_filter%');

$container->autowire(RetryParser::class)
->setArgument('$maxRetryCount', '%paraunit.max_retry_count%');
}
}
22 changes: 4 additions & 18 deletions src/Configuration/DependencyInjection/ParserDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,23 @@

namespace Paraunit\Configuration\DependencyInjection;

use Paraunit\Configuration\TempFilenameFactory;
use Paraunit\Logs\JSON\LogFetcher;
use Paraunit\Logs\JSON\LogHandler;
use Paraunit\Logs\JSON\LogParser;
use Paraunit\Logs\JSON\RetryParser;
use Paraunit\TestResult\TestResultContainer;
use Psr\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Reference;

class ParserDefinition
{
public function configure(ContainerBuilder $container): void
{
$container->autowire(LogHandler::class);

$container->setDefinition(LogParser::class, new Definition(LogParser::class, [
new Reference(LogFetcher::class),
new Reference(LogHandler::class),
new Reference(RetryParser::class),
new Reference(EventDispatcherInterface::class),
]));
$container->autowire(LogParser::class);

$container->setDefinition(RetryParser::class, new Definition(RetryParser::class, [
new Reference(TestResultContainer::class),
'%paraunit.max_retry_count%',
]));
$container->autowire(RetryParser::class)
->setArgument('$maxRetryCount', '%paraunit.max_retry_count%');

$container->setDefinition(LogFetcher::class, new Definition(LogFetcher::class, [
new Reference(TempFilenameFactory::class),
]));
$container->autowire(LogFetcher::class);
}
}
8 changes: 2 additions & 6 deletions src/Configuration/ParallelConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@
use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\ContainerInterface as SymfonyContainerInterface;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Exception\BadMethodCallException;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class ParallelConfiguration
Expand Down Expand Up @@ -80,10 +78,8 @@ protected function loadCommandLineOptions(ContainerBuilder $containerBuilder, In

private function enableDebugMode(ContainerBuilder $containerBuilder): void
{
$definition = new Definition(DebugPrinter::class, [new Reference(OutputInterface::class)]);
$definition->addTag(self::TAG_EVENT_SUBSCRIBER);

$containerBuilder->setDefinition(DebugPrinter::class, $definition);
$containerBuilder->autowire(DebugPrinter::class)
->addTag(self::TAG_EVENT_SUBSCRIBER);
}

private function createPublicAliases(ContainerBuilder $containerBuilder): void
Expand Down
2 changes: 1 addition & 1 deletion src/Process/ProcessFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Paraunit\Coverage\CoverageDriver;
use Symfony\Component\Process\Process as SymfonyProcess;

class ProcessFactory implements ProcessFactoryInterface
class ProcessFactory
{
/** @var string[] */
private readonly array $baseCommandLine;
Expand Down
10 changes: 0 additions & 10 deletions src/Process/ProcessFactoryInterface.php

This file was deleted.

Loading

0 comments on commit 9d5c968

Please sign in to comment.