Skip to content

Commit 28e348e

Browse files
committed
Fix e2e tests with actual sub-processes
1 parent 7878e88 commit 28e348e

7 files changed

+273
-44
lines changed

.php_cs

+1
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
$finder = PhpCsFixer\Finder::create()
1313
->in([
14+
__DIR__.'/bin',
1415
__DIR__.'/src',
1516
__DIR__.'/tests'
1617
])

bin/console

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#!/usr/bin/env php
2+
<?php
3+
4+
declare(strict_types=1);
5+
6+
require __DIR__.'/../vendor/autoload.php';
7+
8+
use Symfony\Bundle\FrameworkBundle\Console\Application;
9+
use Webmozarts\Console\Parallelization\ImportMoviesCommand;
10+
use Webmozarts\Console\Parallelization\Kernel;
11+
12+
$application = new Application(new Kernel());
13+
$application->add(new ImportMoviesCommand());
14+
15+
$application->run();

composer.json

+4-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,10 @@
2626
"ext-json": "*",
2727
"friendsofphp/php-cs-fixer": "^2.15",
2828
"phpunit/phpunit": "^8.4",
29-
"symfony/framework-bundle": "^4.1"
29+
"symfony/config": "^4.1",
30+
"symfony/event-dispatcher": "^4.1",
31+
"symfony/framework-bundle": "^4.1",
32+
"symfony/http-kernel": "^4.1"
3033
},
3134

3235
"autoload": {

src/Parallelization.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
use Symfony\Component\Console\Input\InputArgument;
2626
use Symfony\Component\Console\Input\InputInterface;
2727
use Symfony\Component\Console\Input\InputOption;
28+
use Symfony\Component\Console\Logger\ConsoleLogger;
2829
use Symfony\Component\Console\Output\OutputInterface;
2930
use Symfony\Component\Console\Terminal;
3031
use Symfony\Component\DependencyInjection\ContainerInterface;
@@ -395,7 +396,7 @@ protected function executeMasterProcess(InputInterface $input, OutputInterface $
395396
self::getEnvironmentVariables($this->getContainer()),
396397
$numberOfProcesses,
397398
$segmentSize,
398-
$this->getContainer()->get('logger', ContainerInterface::NULL_ON_INVALID_REFERENCE),
399+
new ConsoleLogger($output),
399400
function (string $type, string $buffer) use ($progressBar, $output, $terminalWidth) {
400401
$this->processChildOutput($buffer, $progressBar, $output, $terminalWidth);
401402
}

tests/ImportMoviesCommand.php

+31-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
1717
use Symfony\Component\Console\Input\InputInterface;
1818
use Symfony\Component\Console\Output\OutputInterface;
19+
use Webmozart\Assert\Assert;
1920

2021
final class ImportMoviesCommand extends ContainerAwareCommand
2122
{
@@ -26,6 +27,25 @@ final class ImportMoviesCommand extends ContainerAwareCommand
2627
*/
2728
protected static $defaultName = 'import:movies';
2829

30+
private $items;
31+
32+
private $segmentSize = 50;
33+
34+
/**
35+
* @param string[]
36+
*/
37+
public function setItems(array $items): void
38+
{
39+
Assert::allString($items);
40+
41+
$this->items = $items;
42+
}
43+
44+
public function setSegmentSize(int $segmentSize): void
45+
{
46+
$this->segmentSize = $segmentSize;
47+
}
48+
2949
/**
3050
* {@inheritdoc}
3151
*/
@@ -41,8 +61,9 @@ protected function fetchItems(InputInterface $input): array
4161
{
4262
// open up the file and read movie data...
4363

44-
// return items as strings
45-
return [
64+
// return items as strings; here return the injected items as priority
65+
// for test purposes
66+
return $this->items ?? [
4667
'{"id": 1, "name": "Star Wars"}',
4768
'{"id": 2, "name": "Django Unchained"}',
4869
// ...
@@ -65,6 +86,14 @@ protected function runAfterBatch(InputInterface $input, OutputInterface $output,
6586
// flush the database and clear the entity manager
6687
}
6788

89+
/**
90+
* {@inheritdoc}
91+
*/
92+
protected function getSegmentSize(): int
93+
{
94+
return $this->segmentSize;
95+
}
96+
6897
/**
6998
* {@inheritdoc}
7099
*/

tests/Kernel.php

+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Webmozarts Console Parallelization package.
5+
*
6+
* (c) Webmozarts GmbH <[email protected]>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Webmozarts\Console\Parallelization;
15+
16+
use Symfony\Component\Config\Loader\LoaderInterface;
17+
use Symfony\Component\Console\Logger\ConsoleLogger;
18+
use Symfony\Component\DependencyInjection\ContainerBuilder;
19+
use Symfony\Component\DependencyInjection\Definition;
20+
use Symfony\Component\EventDispatcher\EventDispatcher;
21+
use Symfony\Component\HttpKernel\Kernel as HttpKernel;
22+
23+
final class Kernel extends HttpKernel
24+
{
25+
/**
26+
* {@inheritdoc}
27+
*/
28+
public function __construct()
29+
{
30+
parent::__construct('dev', true);
31+
}
32+
33+
/**
34+
* {@inheritdoc}
35+
*/
36+
public function registerBundles(): array
37+
{
38+
return [];
39+
}
40+
41+
/**
42+
* {@inheritdoc}
43+
*/
44+
public function registerContainerConfiguration(LoaderInterface $loader): void
45+
{
46+
}
47+
48+
/**
49+
* {@inheritdoc}
50+
*/
51+
protected function build(ContainerBuilder $container): void
52+
{
53+
$eventDispatcherDefinition = new Definition(
54+
EventDispatcher::class,
55+
[]
56+
);
57+
$eventDispatcherDefinition->setPublic(true);
58+
59+
$loggerDefinition = new Definition(
60+
ConsoleLogger::class,
61+
[]
62+
);
63+
$loggerDefinition->setPublic(true);
64+
65+
$container->addDefinitions([
66+
'event_dispatcher' => $eventDispatcherDefinition,
67+
'logger' => $loggerDefinition,
68+
]);
69+
}
70+
}

0 commit comments

Comments
 (0)