Skip to content

Commit 56b3191

Browse files
authored
Fixed installed packages detection (#18)
* Fixed installed packages detection : use Composer InstalledVersions instead of class|interface_exists * Added some tests for Symfony bundle * Use packages list to detect job launcher instead of ifs
1 parent 2a983e1 commit 56b3191

File tree

3 files changed

+100
-9
lines changed

3 files changed

+100
-9
lines changed

src/DependencyInjection/YokaiBatchExtension.php

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,12 @@
77
use Composer\InstalledVersions;
88
use Symfony\Component\Config\FileLocator;
99
use Symfony\Component\Config\Loader as ConfigLoader;
10-
use Symfony\Component\Console\Application;
1110
use Symfony\Component\DependencyInjection\ContainerBuilder;
1211
use Symfony\Component\DependencyInjection\Exception\LogicException;
1312
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
1413
use Symfony\Component\DependencyInjection\Loader as DependencyInjectionLoader;
1514
use Symfony\Component\DependencyInjection\Reference;
1615
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
17-
use Symfony\Component\Messenger\MessageBusInterface;
1816
use Yokai\Batch\Bridge\Doctrine\DBAL\DoctrineDBALJobExecutionStorage;
1917
use Yokai\Batch\Launcher\JobLauncherInterface;
2018
use Yokai\Batch\Storage\FilesystemJobExecutionStorage;
@@ -49,13 +47,14 @@ public function load(array $configs, ContainerBuilder $container): void
4947

5048
$this->configureStorage($container, $config['storage']);
5149

52-
$launcher = 'yokai_batch.job_launcher.simple';
53-
if (class_exists(MessageBusInterface::class)) {
54-
$launcher = 'yokai_batch.job_launcher.dispatch_message';
55-
} elseif (class_exists(Application::class)) {
56-
$launcher = 'yokai_batch.job_launcher.run_command';
57-
}
58-
$container->setAlias(JobLauncherInterface::class, $launcher);
50+
$launchers = [
51+
'yokai_batch.job_launcher.dispatch_message' => $this->installed('symfony-messenger'),
52+
'yokai_batch.job_launcher.run_command' => $this->installed('symfony-console'),
53+
];
54+
$container->setAlias(
55+
JobLauncherInterface::class,
56+
\array_keys(\array_filter($launchers))[0] ?? 'yokai_batch.job_launcher.simple'
57+
);
5958
}
6059

6160
private function installed(string $package): bool
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Yokai\Batch\Tests\Bridge\Symfony\Framework\DependencyInjection;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Symfony\Component\DependencyInjection\ContainerBuilder;
9+
use Yokai\Batch\Bridge\Symfony\Framework\DependencyInjection\YokaiBatchExtension;
10+
use Yokai\Batch\Launcher\JobLauncherInterface;
11+
use Yokai\Batch\Storage\JobExecutionStorageInterface;
12+
use Yokai\Batch\Storage\NullJobExecutionStorage;
13+
14+
class YokaiBatchExtensionTest extends TestCase
15+
{
16+
/**
17+
* @dataProvider configs
18+
*/
19+
public function test(array $config, string $storage): void
20+
{
21+
$container = new ContainerBuilder();
22+
$container->register('app.yokai_batch.storage', NullJobExecutionStorage::class);
23+
24+
(new YokaiBatchExtension())->load([$config], $container);
25+
26+
self::assertSame(
27+
'yokai_batch.job_launcher.dispatch_message',
28+
(string)$container->getAlias(JobLauncherInterface::class)
29+
);
30+
self::assertSame(
31+
$storage,
32+
(string)$container->getAlias(JobExecutionStorageInterface::class)
33+
);
34+
}
35+
36+
public function configs(): \Generator
37+
{
38+
yield [[], 'yokai_batch.storage.filesystem'];
39+
yield [['storage' => ['filesystem' => null]], 'yokai_batch.storage.filesystem'];
40+
yield [['storage' => ['dbal' => null]], 'yokai_batch.storage.dbal'];
41+
yield [['storage' => ['service' => 'app.yokai_batch.storage']], 'app.yokai_batch.storage'];
42+
}
43+
}

tests/YokaiBatchBundleTest.php

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Yokai\Batch\Tests\Bridge\Symfony\Framework;
6+
7+
use PHPUnit\Framework\TestCase;
8+
use Symfony\Component\DependencyInjection\ContainerBuilder;
9+
use Symfony\Component\DependencyInjection\Definition;
10+
use Yokai\Batch\Bridge\Symfony\Framework\YokaiBatchBundle;
11+
use Yokai\Batch\Job\Item\ItemJob;
12+
use Yokai\Batch\Job\Item\Processor\NullProcessor;
13+
use Yokai\Batch\Job\Item\Reader\StaticIterableReader;
14+
use Yokai\Batch\Registry\JobRegistry;
15+
use Yokai\Batch\Storage\NullJobExecutionStorage;
16+
use Yokai\Batch\Test\Job\Item\Writer\InMemoryWriter;
17+
18+
class YokaiBatchBundleTest extends TestCase
19+
{
20+
public function testBuild(): void
21+
{
22+
$container = new ContainerBuilder();
23+
$container->register('yokai_batch.job_registry', JobRegistry::class)
24+
->setPublic(true);
25+
26+
$this->job($container, 'job.named.with.service.id')
27+
->addTag('yokai_batch.job');
28+
$this->job($container, 'job.named.with.tag.attribute')
29+
->addTag('yokai_batch.job', ['job' => 'job.name.in.attribute']);
30+
31+
(new YokaiBatchBundle())->build($container);
32+
33+
$container->compile();
34+
$registry = $container->get('yokai_batch.job_registry');
35+
self::assertInstanceOf(ItemJob::class, $registry->get('job.named.with.service.id'));
36+
self::assertInstanceOf(ItemJob::class, $registry->get('job.name.in.attribute'));
37+
}
38+
39+
private function job(ContainerBuilder $container, string $id): Definition
40+
{
41+
return $container->register($id, ItemJob::class)
42+
->setArgument('$batchSize', 100)
43+
->setArgument('$reader', (new Definition(StaticIterableReader::class))->setArgument('$items', []))
44+
->setArgument('$processor', new Definition(NullProcessor::class))
45+
->setArgument('$writer', new Definition(InMemoryWriter::class))
46+
->setArgument('$executionStorage', new Definition(NullJobExecutionStorage::class))
47+
;
48+
}
49+
}

0 commit comments

Comments
 (0)