Skip to content

Commit

Permalink
tests(export): progress
Browse files Browse the repository at this point in the history
  • Loading branch information
Guikingone committed Oct 24, 2021
1 parent 8a2aec0 commit e36b840
Show file tree
Hide file tree
Showing 9 changed files with 101 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@ summary.log
.php-cs-fixer.cache

# Tests
tests/**/assets/
.phpunit.result.cache
2 changes: 1 addition & 1 deletion src/Command/ExportCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ protected function execute(InputInterface $input, OutputInterface $output): int
});
} catch (Throwable $throwable) {
$style->error([
'An error occurred when exporting tasks',
'An error occurred when exporting tasks:',
$throwable->getMessage(),
]);

Expand Down
3 changes: 3 additions & 0 deletions src/DependencyInjection/SchedulerBundleExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -875,6 +875,9 @@ public function registerExportTools(ContainerBuilder $container): void
$container->setAlias(ExporterRegistryInterface::class, ExporterRegistry::class);

$container->register(CronTabExporter::class, CronTabExporter::class)
->setArguments([
$container->getParameter('kernel.project_dir'),
])
->addTag(self::TASK_EXPORTER_TAG)
->addTag('container.preload', [
'class' => CronTabExporter::class,
Expand Down
23 changes: 23 additions & 0 deletions src/Export/AbstractExporter.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

declare(strict_types=1);

namespace SchedulerBundle\Export;

/**
* @author Guillaume Loulier <contact@guillaumeloulier.fr>
*/
abstract class AbstractExporter implements ExporterInterface
{
private string $projectDir;

public function __construct(string $projectDir)
{
$this->projectDir = $projectDir;
}

protected function getProjectDir(): string
{
return $this->projectDir;
}
}
22 changes: 19 additions & 3 deletions src/Export/CronTabExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,34 @@
namespace SchedulerBundle\Export;

use SchedulerBundle\Task\TaskInterface;
use function file_exists;
use Symfony\Component\Filesystem\Filesystem;
use function sprintf;

/**
* @author Guillaume Loulier <contact@guillaumeloulier.fr>
*/
final class CronTabExporter implements ExporterInterface
final class CronTabExporter extends AbstractExporter
{
/**
* {@inheritdoc}
*/
public function export(string $filename, TaskInterface $task): void
{
if (file_exists($filename)) {
$finalFilename = sprintf('%s/%s', $this->getProjectDir(), $task->getName());

$fs = new Filesystem();

if ($fs->exists($finalFilename)) {
return;
}

$fs->touch($finalFilename);
$fs->dumpFile($finalFilename, sprintf(
'%s cd %s && php bin/console scheduler:execute --name %s',
$task->getExpression(),
$this->getProjectDir(),
$task->getName(),
));
}

/**
Expand Down
5 changes: 4 additions & 1 deletion tests/DependencyInjection/SchedulerBundleExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -389,6 +389,7 @@ public function testTransportIsRegistered(): void
$schedulerBundleExtension = new SchedulerBundleExtension();

$containerBuilder = new ContainerBuilder();
$containerBuilder->setParameter('kernel.project_dir', 'foo');
$containerBuilder->register(SerializerInterface::class, SerializerInterface::class);

$schedulerBundleExtension->load([
Expand Down Expand Up @@ -944,7 +945,8 @@ public function testExportToolsAreRegistered(): void
self::assertSame(ExporterRegistry::class, $container->getDefinition(ExporterRegistry::class)->getTag('container.preload')[0]['class']);

self::assertTrue($container->hasDefinition(CronTabExporter::class));
self::assertCount(0, $container->getDefinition(CronTabExporter::class)->getArguments());
self::assertCount(1, $container->getDefinition(CronTabExporter::class)->getArguments());
self::assertSame('foo', $container->getDefinition(CronTabExporter::class)->getArgument(0));
self::assertTrue($container->getDefinition(CronTabExporter::class)->hasTag('scheduler.task_exporter'));
self::assertTrue($container->getDefinition(CronTabExporter::class)->hasTag('container.preload'));
self::assertSame(CronTabExporter::class, $container->getDefinition(CronTabExporter::class)->getTag('container.preload')[0]['class']);
Expand Down Expand Up @@ -1805,6 +1807,7 @@ public function provideDoctrineDsn(): Generator
private function getContainer(array $configuration = []): ContainerBuilder
{
$containerBuilder = new ContainerBuilder();
$containerBuilder->setParameter('kernel.project_dir', 'foo');
$containerBuilder->registerExtension(new SchedulerBundleExtension());
$containerBuilder->loadFromExtension('scheduler_bundle', $configuration);

Expand Down
44 changes: 44 additions & 0 deletions tests/Export/CronTabExporterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,54 @@
namespace Tests\SchedulerBundle\Export;

use PHPUnit\Framework\TestCase;
use SchedulerBundle\Export\CronTabExporter;
use SchedulerBundle\Task\NullTask;
use function file_get_contents;
use function sprintf;
use function unlink;

/**
* @author Guillaume Loulier <contact@guillaumeloulier.fr>
*/
final class CronTabExporterTest extends TestCase
{
public function testExporterSupport(): void
{
$exporter = new CronTabExporter(__DIR__);

self::assertFalse($exporter->support('cli'));
self::assertTrue($exporter->support('crontab'));
}

public function testExporterCannotExportExistingFile(): void
{
$exporter = new CronTabExporter(__DIR__.'/assets');

self::assertFileExists(sprintf('%s/assets/foo', __DIR__));

$exporter->export('foo', new NullTask('foo'));

self::assertFileExists(sprintf('%s/assets/foo', __DIR__));
}

public function testExporterCanExportUndefinedFile(): void
{
unlink(__DIR__.'/assets/bar');

$task = new NullTask('bar');

$exporter = new CronTabExporter(__DIR__.'/assets');

self::assertFileDoesNotExist(sprintf('%s/bar', __DIR__.'/assets'));

$exporter->export('bar', $task);

self::assertFileExists(sprintf('%s/bar', __DIR__.'/assets'));
self::assertSame(sprintf(
'%s cd %s && php bin/console scheduler:execute --name %s',
$task->getExpression(),
__DIR__.'/assets',
$task->getName(),
), file_get_contents(__DIR__.'/assets/bar'));
}
}
12 changes: 6 additions & 6 deletions tests/Export/ExporterRegistryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function testRegistryCanReturnEmptyExporterList(): void
public function testRegistryCanFilterExporterList(): void
{
$registry = new ExporterRegistry([
new CronTabExporter(),
new CronTabExporter(__DIR__),
]);

$registry->filter(static fn (ExporterInterface $exporter): bool => $exporter instanceof CronTabExporter);
Expand All @@ -35,7 +35,7 @@ public function testRegistryCanFilterExporterList(): void
public function testRegistryCannotFindExporterWithEmptyList(): void
{
$registry = new ExporterRegistry([
new CronTabExporter(),
new CronTabExporter(__DIR__),
]);

self::expectException(InvalidArgumentException::class);
Expand All @@ -47,8 +47,8 @@ public function testRegistryCannotFindExporterWithEmptyList(): void
public function testRegistryCannotFindExporterWithMultipleSupportingExporter(): void
{
$registry = new ExporterRegistry([
new CronTabExporter(),
new CronTabExporter(),
new CronTabExporter(__DIR__),
new CronTabExporter(__DIR__),
]);

self::expectException(InvalidArgumentException::class);
Expand All @@ -60,7 +60,7 @@ public function testRegistryCannotFindExporterWithMultipleSupportingExporter():
public function testRegistryCanFindExporter(): void
{
$registry = new ExporterRegistry([
new CronTabExporter(),
new CronTabExporter(__DIR__),
]);

$exporter = $registry->find('crontab');
Expand All @@ -70,7 +70,7 @@ public function testRegistryCanFindExporter(): void
public function testRegistryCanReturnCurrentExporter(): void
{
$registry = new ExporterRegistry([
new CronTabExporter(),
new CronTabExporter(__DIR__),
]);

$exporter = $registry->current();
Expand Down
Empty file added tests/Export/assets/.gitkeep
Empty file.

0 comments on commit e36b840

Please sign in to comment.