Skip to content

Commit

Permalink
Lifecycle
Browse files Browse the repository at this point in the history
 - Will add Lifecycle and event logic.
 - First event 'before engine is started'.
 - Console format moved to lifecycle.
 - Shark Printer moved to lifecylce and removed from runner!
 - paraunit bin cleanup
 - container isolation wip
  • Loading branch information
ranpafin committed Aug 5, 2015
1 parent 20e4260 commit 9d4bf8e
Show file tree
Hide file tree
Showing 11 changed files with 235 additions and 44 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ bin
vendor
composer.lock
build/
swiftmailer/
23 changes: 20 additions & 3 deletions Container.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,24 @@
use Symfony\Component\DependencyInjection\Loader\YamlFileLoader;
use Symfony\Component\Config\FileLocator;

$container = new ContainerBuilder();
function getContainer(){

$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/src/Paraunit/Resources/config/'));
$loader->load('services.yml');
$container = new ContainerBuilder();

$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/src/Paraunit/Resources/config/'));
$loader->load('services.yml');

$container->addCompilerPass(
new \Paraunit\Lifecycle\CompilerPass(
'event_dispatcher',
'paraunit.event_listener',
'paraunit.event_subscriber'
)
);

$container->compile();

return $container;
}

$container = getContainer();
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"symfony/dependency-injection": ">=2.3,<=2.6",
"symfony/config": ">=2.3,<=2.6",
"symfony/process": ">=2.3,<=2.6",
"symfony/event-dispatcher": ">=2.3,<=2.6",
"phpunit/phpunit": ">=3.7,<5"
},
"require-dev": {
Expand Down
2 changes: 0 additions & 2 deletions src/Paraunit/Bin/paraunit
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ use Paraunit\Command\ParallelCommand;

require_once __DIR__ . '/../../../Container.php';

$loader->load('services.yml');

$command = new ParallelCommand(
$container->get('facile.cbr.parallel_test_bundle.filter.filter'),
$container->get('facile.cbr.parallel_test_bundle.runner.runner')
Expand Down
84 changes: 84 additions & 0 deletions src/Paraunit/Lifecycle/CompilerPass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php

namespace Paraunit\Lifecycle;

use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\DependencyInjection\Compiler\CompilerPassInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class CompilerPass implements CompilerPassInterface
{
/**
* @var string
*/
protected $dispatcherService;
/**
* @var string
*/
protected $listenerTag;
/**
* @var string
*/
protected $subscriberTag;
/**
* Constructor.
*
* @param string $dispatcherService Service name of the event dispatcher in processed container
* @param string $listenerTag Tag name used for listener
* @param string $subscriberTag Tag name used for subscribers
*/
public function __construct($dispatcherService = 'event_dispatcher', $listenerTag = 'kernel.event_listener', $subscriberTag = 'kernel.event_subscriber')
{
$this->dispatcherService = $dispatcherService;
$this->listenerTag = $listenerTag;
$this->subscriberTag = $subscriberTag;
}

public function process(ContainerBuilder $container)
{
if (!$container->hasDefinition($this->dispatcherService) && !$container->hasAlias($this->dispatcherService)) {
return;
}
$definition = $container->findDefinition($this->dispatcherService);
foreach ($container->findTaggedServiceIds($this->listenerTag) as $id => $events) {
$def = $container->getDefinition($id);
if (!$def->isPublic()) {
throw new \InvalidArgumentException(sprintf('The service "%s" must be public as event listeners are lazy-loaded.', $id));
}
if ($def->isAbstract()) {
throw new \InvalidArgumentException(sprintf('The service "%s" must not be abstract as event listeners are lazy-loaded.', $id));
}
foreach ($events as $event) {
$priority = isset($event['priority']) ? $event['priority'] : 0;
if (!isset($event['event'])) {
throw new \InvalidArgumentException(sprintf('Service "%s" must define the "event" attribute on "%s" tags.', $id, $this->listenerTag));
}
if (!isset($event['method'])) {
$event['method'] = 'on'.preg_replace_callback(array(
'/(?<=\b)[a-z]/i',
'/[^a-z0-9]/i',
), function ($matches) { return strtoupper($matches[0]); }, $event['event']);
$event['method'] = preg_replace('/[^a-z0-9]/i', '', $event['method']);
}
$definition->addMethodCall('addListenerService', array($event['event'], array($id, $event['method']), $priority));
}
}
foreach ($container->findTaggedServiceIds($this->subscriberTag) as $id => $attributes) {
$def = $container->getDefinition($id);
if (!$def->isPublic()) {
throw new \InvalidArgumentException(sprintf('The service "%s" must be public as event subscribers are lazy-loaded.', $id));
}
if ($def->isAbstract()) {
throw new \InvalidArgumentException(sprintf('The service "%s" must not be abstract as event subscribers are lazy-loaded.', $id));
}
// We must assume that the class value has been correctly filled, even if the service is created by a factory
$class = $container->getParameterBag()->resolveValue($def->getClass());
$refClass = new \ReflectionClass($class);
$interface = 'Symfony\Component\EventDispatcher\EventSubscriberInterface';
if (!$refClass->implementsInterface($interface)) {
throw new \InvalidArgumentException(sprintf('Service "%s" must implement interface "%s".', $id, $interface));
}
$definition->addMethodCall('addSubscriberService', array($id, $class));
}
}
}
47 changes: 47 additions & 0 deletions src/Paraunit/Lifecycle/EngineEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Paraunit\Lifecycle;

use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\EventDispatcher\Event;

/***
* Class EngineEvent
* @package Paraunit\Lifecycle
*/
class EngineEvent extends Event
{
// This Event will be triggered before the whole paraunit engine is started
const BEFORE_START = 'engine_event.before_start';

/** @var array */
protected $files;

/** @var OutputInterface */
protected $outputInterface;

/**
* EngineEvent constructor.
* @param $files
* @param OutputInterface $outputInterface
*/
public function __construct($files, OutputInterface $outputInterface)
{
$this->files = $files;
$this->outputInterface = $outputInterface;
}

public static function buildFromContext($files, OutputInterface $outputInteface){

return new EngineEvent($files, $outputInteface);

}

/**
* @return OutputInterface
*/
public function getOutputInterface(){
return $this->outputInterface;
}

}
41 changes: 41 additions & 0 deletions src/Paraunit/Printer/ConsoleFormatter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace Paraunit\Printer;

use Paraunit\Lifecycle\EngineEvent;
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Class ConsoleFormatter
* @package Paraunit\Printer
*/
class ConsoleFormatter
{
/**
* @param EngineEvent $engineEvent
*/
public function onEngineStart(EngineEvent $engineEvent){

$outputInterface= $engineEvent->getOutputInterface();

if ($outputInterface->getFormatter()) {
$style = new OutputFormatterStyle('green', null, array('bold', 'blink'));
$outputInterface->getFormatter()->setStyle('ok', $style);

$style = new OutputFormatterStyle('yellow', null, array('bold', 'blink'));
$outputInterface->getFormatter()->setStyle('skipped', $style);

$style = new OutputFormatterStyle('blue', null, array('bold', 'blink'));
$outputInterface->getFormatter()->setStyle('incomplete', $style);

$style = new OutputFormatterStyle('red', null, array('bold', 'blink'));
$outputInterface->getFormatter()->setStyle('fail', $style);

$style = new OutputFormatterStyle('red', null, array('bold', 'blink'));
$outputInterface->getFormatter()->setStyle('error', $style);
}

}

}
11 changes: 9 additions & 2 deletions src/Paraunit/Printer/SharkPrinter.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@

namespace Paraunit\Printer;

use Paraunit\Lifecycle\EngineEvent;
use Symfony\Component\Console\Output\OutputInterface;

/**
* Class SharkPrinter
* @package Paraunit\Printer
*/
class SharkPrinter
{
public function printSharkLogo(OutputInterface $outputInterface)
public function onEngineStart(EngineEvent $engineEvent)
{
$outputInterface = $engineEvent->getOutputInterface();

$outputInterface->writeln(' B> ');
$outputInterface->writeln(' B "Bp ');
$outputInterface->writeln('.pp.. B 9p ');
Expand All @@ -22,6 +29,6 @@ public function printSharkLogo(OutputInterface $outputInterface)
$outputInterface->writeln(' .6BSERGIOBBBB B666666B B B B B B 9 P 7 9BBBBP ');

$outputInterface->writeln('');
$outputInterface->writeln('PARAUNIT TEST 0.4 -- by Shark Dev Team @ Facile.it');
$outputInterface->writeln('PARAUNIT V0.4 -- by Francesco Panina, Alessandro Lai and Shark Dev Team @ Facile.it');
}
}
18 changes: 17 additions & 1 deletion src/Paraunit/Resources/config/services.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
parameters:
paraunit.max_retry_count: 3
paraunit.default_max_process: 10
kernel.root_dir: 'src'

services:
Expand All @@ -21,12 +22,15 @@ services:
arguments:
- @facile.cbr.parallel_test_bundle.runner.retry_manager
- @facile.cbr.parallel_test_bundle.parser.process_output_parser
- @facile.cbr.parallel_test_bundle.printer.shark_printer
- @facile.cbr.parallel_test_bundle.printer.process_printer
- @facile.cbr.parallel_test_bundle.printer.final_printer
- %paraunit.default_max_process%
- @event_dispatcher

facile.cbr.parallel_test_bundle.printer.shark_printer:
class: Paraunit\Printer\SharkPrinter
tags:
- { name: paraunit.event_listener, event: engine_event.before_start, method: onEngineStart }

facile.cbr.parallel_test_bundle.printer.process_printer:
class: Paraunit\Printer\ProcessPrinter
Expand Down Expand Up @@ -65,6 +69,18 @@ services:
facile.cbr.parallel_test_bundle.parser.test_result_parser:
class: Paraunit\Parser\TestResultsParser

event_dispatcher:
class: Symfony\Component\EventDispatcher\ContainerAwareEventDispatcher
arguments:
- @service_container

##LISTENERS

paraunit.printer.console_formatter:
class: Paraunit\Printer\ConsoleFormatter
tags:
- { name: paraunit.event_listener, event: engine_event.before_start, method: onEngineStart }

## EXTERNAL DEPs -- proxies ##

paraunit.proxy.phpunit_util_xml_proxy:
Expand Down
45 changes: 13 additions & 32 deletions src/Paraunit/Runner/Runner.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@
use Paraunit\Process\ParaunitProcessAbstract;
use Paraunit\Process\ParaunitProcessInterface;
use Paraunit\Process\SymfonyProcessWrapper;
use Paraunit\Lifecycle\EngineEvent;
use Symfony\Component\Console\Formatter\OutputFormatterStyle;
use Symfony\Component\Console\Output\OutputInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;

/**
* Class Runner.
Expand Down Expand Up @@ -74,28 +76,30 @@ class Runner
protected $phpunitBin;

/**
* @param RetryManager $retryManager
* @param RetryManager $retryManager
* @param ProcessOutputParser $processOutputParser
* @param SharkPrinter $sharkPrinter
* @param ProcessPrinter $processPrinter
* @param FinalPrinter $finalPrinter
* @param int $maxProcessNumber
* @param SharkPrinter $sharkPrinter
* @param ProcessPrinter $processPrinter
* @param FinalPrinter $finalPrinter
* @param int $maxProcessNumber
*
* @param EventDispatcherInterface $eventDispatcher
* @throws \Exception
*/
public function __construct(
RetryManager $retryManager,
ProcessOutputParser $processOutputParser,
SharkPrinter $sharkPrinter,
ProcessPrinter $processPrinter,
FinalPrinter $finalPrinter,
$maxProcessNumber = 10
$maxProcessNumber = 10,
EventDispatcherInterface $eventDispatcher
) {
$this->retryManager = $retryManager;
$this->processOutputParser = $processOutputParser;
$this->sharkPrinter = $sharkPrinter;
$this->processPrinter = $processPrinter;
$this->finalPrinter = $finalPrinter;
$this->eventDispatcher = $eventDispatcher;

$this->maxProcessNumber = $maxProcessNumber;

Expand All @@ -122,9 +126,8 @@ public function run($files, OutputInterface $outputInterface, $phpunitConfigFile
{
$this->phpunitConfigFile = $phpunitConfigFile;

$this->formatOutputInterface($outputInterface);

$this->sharkPrinter->printSharkLogo($outputInterface);
$this->eventDispatcher
->dispatch(EngineEvent::BEFORE_START, EngineEvent::buildFromContext($files, $outputInterface));

$start = new \Datetime('now');
$this->createProcessStackFromFiles($files);
Expand Down Expand Up @@ -222,26 +225,4 @@ protected function markProcessCompleted(ParaunitProcessAbstract $process)
}
}

/**
* @param OutputInterface $outputInterface
*/
protected function formatOutputInterface(OutputInterface $outputInterface)
{
if ($outputInterface->getFormatter()) {
$style = new OutputFormatterStyle('green', null, array('bold', 'blink'));
$outputInterface->getFormatter()->setStyle('ok', $style);

$style = new OutputFormatterStyle('yellow', null, array('bold', 'blink'));
$outputInterface->getFormatter()->setStyle('skipped', $style);

$style = new OutputFormatterStyle('blue', null, array('bold', 'blink'));
$outputInterface->getFormatter()->setStyle('incomplete', $style);

$style = new OutputFormatterStyle('red', null, array('bold', 'blink'));
$outputInterface->getFormatter()->setStyle('fail', $style);

$style = new OutputFormatterStyle('red', null, array('bold', 'blink'));
$outputInterface->getFormatter()->setStyle('error', $style);
}
}
}
6 changes: 2 additions & 4 deletions src/Paraunit/Tests/Functional/RunnerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,9 @@ public function setUp()
{
parent::setUp();

$container = new ContainerBuilder();
require_once getcwd(). '/Container.php';

$loader = new YamlFileLoader($container, new FileLocator(__DIR__.'/../../Resources/config/'));
$loader->load('services.yml');
$this->container = $container;
$this->container = getContainer();
}

public function testMaxRetryEntityManagerIsClosed()
Expand Down

0 comments on commit 9d4bf8e

Please sign in to comment.