Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,12 @@
use Composer\InstalledVersions;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Config\Loader as ConfigLoader;
use Symfony\Component\Console\Application;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Exception\LogicException;
use Symfony\Component\DependencyInjection\Exception\ServiceNotFoundException;
use Symfony\Component\DependencyInjection\Loader as DependencyInjectionLoader;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\HttpKernel\DependencyInjection\Extension;
use Symfony\Component\Messenger\MessageBusInterface;
use Yokai\Batch\Bridge\Doctrine\DBAL\DoctrineDBALJobExecutionStorage;
use Yokai\Batch\Launcher\JobLauncherInterface;
use Yokai\Batch\Storage\FilesystemJobExecutionStorage;
Expand Down Expand Up @@ -49,13 +47,14 @@ public function load(array $configs, ContainerBuilder $container): void

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

$launcher = 'yokai_batch.job_launcher.simple';
if (class_exists(MessageBusInterface::class)) {
$launcher = 'yokai_batch.job_launcher.dispatch_message';
} elseif (class_exists(Application::class)) {
$launcher = 'yokai_batch.job_launcher.run_command';
}
$container->setAlias(JobLauncherInterface::class, $launcher);
$launchers = [
'yokai_batch.job_launcher.dispatch_message' => $this->installed('symfony-messenger'),
'yokai_batch.job_launcher.run_command' => $this->installed('symfony-console'),
];
$container->setAlias(
JobLauncherInterface::class,
\array_keys(\array_filter($launchers))[0] ?? 'yokai_batch.job_launcher.simple'
);
}

private function installed(string $package): bool
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

declare(strict_types=1);

namespace Yokai\Batch\Tests\Bridge\Symfony\Framework\DependencyInjection;

use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Yokai\Batch\Bridge\Symfony\Framework\DependencyInjection\YokaiBatchExtension;
use Yokai\Batch\Launcher\JobLauncherInterface;
use Yokai\Batch\Storage\JobExecutionStorageInterface;
use Yokai\Batch\Storage\NullJobExecutionStorage;

class YokaiBatchExtensionTest extends TestCase
{
/**
* @dataProvider configs
*/
public function test(array $config, string $storage): void
{
$container = new ContainerBuilder();
$container->register('app.yokai_batch.storage', NullJobExecutionStorage::class);

(new YokaiBatchExtension())->load([$config], $container);

self::assertSame(
'yokai_batch.job_launcher.dispatch_message',
(string)$container->getAlias(JobLauncherInterface::class)
);
self::assertSame(
$storage,
(string)$container->getAlias(JobExecutionStorageInterface::class)
);
}

public function configs(): \Generator
{
yield [[], 'yokai_batch.storage.filesystem'];
yield [['storage' => ['filesystem' => null]], 'yokai_batch.storage.filesystem'];
yield [['storage' => ['dbal' => null]], 'yokai_batch.storage.dbal'];
yield [['storage' => ['service' => 'app.yokai_batch.storage']], 'app.yokai_batch.storage'];
}
}
49 changes: 49 additions & 0 deletions src/batch-symfony-framework/tests/YokaiBatchBundleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

declare(strict_types=1);

namespace Yokai\Batch\Tests\Bridge\Symfony\Framework;

use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Yokai\Batch\Bridge\Symfony\Framework\YokaiBatchBundle;
use Yokai\Batch\Job\Item\ItemJob;
use Yokai\Batch\Job\Item\Processor\NullProcessor;
use Yokai\Batch\Job\Item\Reader\StaticIterableReader;
use Yokai\Batch\Registry\JobRegistry;
use Yokai\Batch\Storage\NullJobExecutionStorage;
use Yokai\Batch\Test\Job\Item\Writer\InMemoryWriter;

class YokaiBatchBundleTest extends TestCase
{
public function testBuild(): void
{
$container = new ContainerBuilder();
$container->register('yokai_batch.job_registry', JobRegistry::class)
->setPublic(true);

$this->job($container, 'job.named.with.service.id')
->addTag('yokai_batch.job');
$this->job($container, 'job.named.with.tag.attribute')
->addTag('yokai_batch.job', ['job' => 'job.name.in.attribute']);

(new YokaiBatchBundle())->build($container);

$container->compile();
$registry = $container->get('yokai_batch.job_registry');
self::assertInstanceOf(ItemJob::class, $registry->get('job.named.with.service.id'));
self::assertInstanceOf(ItemJob::class, $registry->get('job.name.in.attribute'));
}

private function job(ContainerBuilder $container, string $id): Definition
{
return $container->register($id, ItemJob::class)
->setArgument('$batchSize', 100)
->setArgument('$reader', (new Definition(StaticIterableReader::class))->setArgument('$items', []))
->setArgument('$processor', new Definition(NullProcessor::class))
->setArgument('$writer', new Definition(InMemoryWriter::class))
->setArgument('$executionStorage', new Definition(NullJobExecutionStorage::class))
;
}
}