Skip to content

Commit c531c3a

Browse files
committed
Rework the tests
1 parent 4fada8e commit c531c3a

6 files changed

+233
-123
lines changed

bin/console

+2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@ require __DIR__.'/../vendor/autoload.php';
88
use Symfony\Bundle\FrameworkBundle\Console\Application;
99
use Webmozarts\Console\Parallelization\ImportMoviesCommand;
1010
use Webmozarts\Console\Parallelization\Kernel;
11+
use Webmozarts\Console\Parallelization\NoSubProcessCommand;
1112

1213
$application = new Application(new Kernel());
1314
$application->add(new ImportMoviesCommand());
15+
$application->add(new NoSubProcessCommand());
1416

1517
$application->run();

src/Parallelization.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
use Symfony\Component\Console\Command\Command;
3232
use Symfony\Component\Console\Helper\ProgressBar;
3333
use Symfony\Component\Console\Input\InputInterface;
34+
use Symfony\Component\Console\Logger\ConsoleLogger;
3435
use Symfony\Component\Console\Output\OutputInterface;
3536
use Symfony\Component\Console\Terminal;
3637
use Symfony\Component\DependencyInjection\ContainerInterface;
@@ -348,7 +349,8 @@ function () use ($input) {
348349
$this->getEnvironmentVariables($this->getContainer()),
349350
$numberOfProcesses,
350351
$segmentSize,
351-
$this->getContainer()->get('logger', ContainerInterface::NULL_ON_INVALID_REFERENCE),
352+
// TODO: offer a way to create the process launcher in a different manner
353+
new ConsoleLogger($output),
352354
function (string $type, string $buffer) use ($progressBar, $output, $terminalWidth) {
353355
$this->processChildOutput($buffer, $progressBar, $output, $terminalWidth);
354356
}

tests/ImportMoviesCommand.php

+57-6
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@
1313

1414
namespace Webmozarts\Console\Parallelization;
1515

16+
use function file_get_contents;
17+
use function json_decode;
18+
use const JSON_THROW_ON_ERROR;
1619
use Symfony\Component\Console\Input\InputInterface;
1720
use Symfony\Component\Console\Output\OutputInterface;
1821

@@ -22,9 +25,18 @@ final class ImportMoviesCommand extends ContainerAwareCommand
2225

2326
protected static $defaultName = 'import:movies';
2427

25-
public function __construct(?string $name = null)
28+
private TestLogger $logger;
29+
30+
/**
31+
* @var array<string, string>
32+
*/
33+
private array $batchMovies;
34+
35+
public function __construct()
2636
{
27-
parent::__construct($name);
37+
parent::__construct(self::$defaultName);
38+
39+
$this->logger = new TestLogger();
2840
}
2941

3042
protected function configure(): void
@@ -50,27 +62,66 @@ protected function getSegmentSize(): int
5062

5163
protected function runBeforeFirstCommand(InputInterface $input, OutputInterface $output): void
5264
{
65+
$this->logger->recordFirstCommand();
5366
}
5467

5568
protected function runBeforeBatch(
5669
InputInterface $input,
5770
OutputInterface $output,
58-
array $items
71+
array $movieFileNames
5972
): void {
73+
$this->logger->recordBeforeBatch();
74+
75+
$this->batchMovies = self::fetchMovieTitles($movieFileNames);
6076
}
6177

62-
protected function runSingleCommand(string $item, InputInterface $input, OutputInterface $output): void
78+
protected function runSingleCommand(string $movieFileName, InputInterface $input, OutputInterface $output): void
6379
{
64-
// insert into the database
80+
$this->logger->recordSingleCommand(
81+
$movieFileName,
82+
$this->batchMovies[$movieFileName],
83+
);
6584
}
6685

6786
protected function runAfterBatch(InputInterface $input, OutputInterface $output, array $items): void
6887
{
69-
// flush the database and clear the entity manager
88+
$this->logger->recordAfterBatch();
89+
90+
$this->batchMovies = null;
91+
}
92+
93+
protected function runAfterLastCommand(InputInterface $input, OutputInterface $output): void
94+
{
95+
$this->logger->recordLastCommand();
7096
}
7197

7298
protected function getItemName(int $count): string
7399
{
74100
return 1 === $count ? 'movie' : 'movies';
75101
}
102+
103+
/**
104+
* @param list<string> $movieFileNames
105+
*
106+
* @return array<string, string>
107+
*/
108+
private static function fetchMovieTitles(array $movieFileNames): array
109+
{
110+
$movies = [];
111+
112+
foreach ($movieFileNames as $movieFileName) {
113+
$moviePath = __DIR__.'/movies/'.$movieFileName;
114+
115+
$decodedContent = json_decode(
116+
file_get_contents($moviePath),
117+
null,
118+
512,
119+
JSON_THROW_ON_ERROR,
120+
);
121+
122+
$movies[$movieFileName] = $decodedContent->title;
123+
}
124+
125+
return $movies;
126+
}
76127
}

tests/NoSubProcessCommand.php

+65
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
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 DomainException;
17+
use Symfony\Component\Console\Input\InputInterface;
18+
use Symfony\Component\Console\Output\OutputInterface;
19+
20+
final class NoSubProcessCommand extends ContainerAwareCommand
21+
{
22+
use Parallelization;
23+
24+
protected static $defaultName = 'test:no-subprocess';
25+
26+
private bool $mainProcess = false;
27+
28+
protected function configure(): void
29+
{
30+
self::configureParallelization($this);
31+
}
32+
33+
protected function fetchItems(InputInterface $input): array
34+
{
35+
return [
36+
'item1',
37+
'item2',
38+
'item3',
39+
'item4',
40+
'item5',
41+
];
42+
}
43+
44+
protected function getSegmentSize(): int
45+
{
46+
return 2;
47+
}
48+
49+
protected function runBeforeFirstCommand(InputInterface $input, OutputInterface $output): void
50+
{
51+
$this->mainProcess = true;
52+
}
53+
54+
protected function runSingleCommand(string $item, InputInterface $input, OutputInterface $output): void
55+
{
56+
if (!$this->mainProcess) {
57+
throw new DomainException('Expected to be executed within the main process.');
58+
}
59+
}
60+
61+
protected function getItemName(int $count): string
62+
{
63+
return 0 === $count ? 'item' : 'items';
64+
}
65+
}

0 commit comments

Comments
 (0)