And ability to define how many messages can be hanle by the redis worker before stopping (usefull for tests)
use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand;
use Symfony\Component\Config\Definition\Exception\Exception;
use Symfony\Component\Console\Input\InputArgument;
+use Symfony\Component\Console\Input\InputOption;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
use Simpleue\Worker\QueueWorker;
->setName('wallabag:import:redis-worker')
->setDescription('Launch Redis worker')
->addArgument('serviceName', InputArgument::REQUIRED, 'Service to use: wallabag_v1, wallabag_v2, pocket or readability')
+ ->addOption('maxIterations', '', InputOption::VALUE_OPTIONAL, 'Number of iterations before stoping', false)
;
}
$worker = new QueueWorker(
$this->getContainer()->get('wallabag_import.queue.redis.'.$serviceName),
- $this->getContainer()->get('wallabag_import.consumer.redis.'.$serviceName)
+ $this->getContainer()->get('wallabag_import.consumer.redis.'.$serviceName),
+ $input->getOption('maxIterations')
);
$worker->start();
$markAsRead = $form->get('mark_as_read')->getData();
$name = 'readability_'.$this->getUser()->getId().'.json';
- if (in_array($file->getClientMimeType(), $this->getParameter('wallabag_import.allow_mimetypes')) && $file->move($this->getParameter('wallabag_import.resource_dir'), $name)) {
+ if (null !== $file && in_array($file->getClientMimeType(), $this->getParameter('wallabag_import.allow_mimetypes')) && $file->move($this->getParameter('wallabag_import.resource_dir'), $name)) {
$res = $readability
->setFilepath($this->getParameter('wallabag_import.resource_dir').'/'.$name)
->setMarkAsRead($markAsRead)
$markAsRead = $form->get('mark_as_read')->getData();
$name = $this->getUser()->getId().'.json';
- if (in_array($file->getClientMimeType(), $this->getParameter('wallabag_import.allow_mimetypes')) && $file->move($this->getParameter('wallabag_import.resource_dir'), $name)) {
+ if (null !== $file && in_array($file->getClientMimeType(), $this->getParameter('wallabag_import.allow_mimetypes')) && $file->move($this->getParameter('wallabag_import.resource_dir'), $name)) {
$res = $wallabag
->setFilepath($this->getParameter('wallabag_import.resource_dir').'/'.$name)
->setMarkAsRead($markAsRead)
$builder
->add('file', FileType::class, [
'label' => 'import.form.file_label',
+ 'required' => true,
])
->add('mark_as_read', CheckboxType::class, [
'label' => 'import.form.mark_as_read_label',
--- /dev/null
+<?php
+
+namespace Tests\Wallabag\ImportBundle\Command;
+
+use Symfony\Bundle\FrameworkBundle\Console\Application;
+use Symfony\Component\Console\Tester\CommandTester;
+use Wallabag\ImportBundle\Command\RedisWorkerCommand;
+use Tests\Wallabag\CoreBundle\WallabagCoreTestCase;
+use M6Web\Component\RedisMock\RedisMockFactory;
+
+class RedisWorkerCommandTest extends WallabagCoreTestCase
+{
+ /**
+ * @expectedException Symfony\Component\Console\Exception\RuntimeException
+ * @expectedExceptionMessage Not enough arguments (missing: "serviceName")
+ */
+ public function testRunRedisWorkerCommandWithoutArguments()
+ {
+ $application = new Application($this->getClient()->getKernel());
+ $application->add(new RedisWorkerCommand());
+
+ $command = $application->find('wallabag:import:redis-worker');
+
+ $tester = new CommandTester($command);
+ $tester->execute([
+ 'command' => $command->getName(),
+ ]);
+ }
+
+ /**
+ * @expectedException Symfony\Component\Config\Definition\Exception\Exception
+ * @expectedExceptionMessage No queue or consumer found for service name
+ */
+ public function testRunRedisWorkerCommandWithBadService()
+ {
+ $application = new Application($this->getClient()->getKernel());
+ $application->add(new RedisWorkerCommand());
+
+ $command = $application->find('wallabag:import:redis-worker');
+
+ $tester = new CommandTester($command);
+ $tester->execute([
+ 'command' => $command->getName(),
+ 'serviceName' => 'YOMONSERVICE',
+ ]);
+ }
+
+ public function testRunRedisWorkerCommand()
+ {
+ $application = new Application($this->getClient()->getKernel());
+ $application->add(new RedisWorkerCommand());
+
+ $factory = new RedisMockFactory();
+ $redisMock = $factory->getAdapter('Predis\Client', true);
+
+ $application->getKernel()->getContainer()->set('wallabag_core.redis.client', $redisMock);
+
+ // put a fake message in the queue so the worker will stop after reading that message
+ // instead of waiting for others
+ $redisMock->lpush('wallabag.import.readability', '{}');
+
+ $command = $application->find('wallabag:import:redis-worker');
+
+ $tester = new CommandTester($command);
+ $tester->execute([
+ 'command' => $command->getName(),
+ 'serviceName' => 'readability',
+ '--maxIterations' => 1,
+ ]);
+
+ $this->assertContains('Worker started at', $tester->getDisplay());
+ $this->assertContains('Waiting for message', $tester->getDisplay());
+ }
+}
$res = $consumer->manage($body);
$this->assertFalse($res);
+ $this->assertFalse($consumer->isStopJob($body));
}
}
$client->getContainer()->get('craue_config')->set('import_with_redis', 0);
}
+ public function testImportReadabilityBadFile()
+ {
+ $this->logInAs('admin');
+ $client = $this->getClient();
+
+ $crawler = $client->request('GET', '/import/readability');
+ $form = $crawler->filter('form[name=upload_import_file] > button[type=submit]')->form();
+
+ $data = [
+ 'upload_import_file[file]' => '',
+ ];
+
+ $client->submit($form, $data);
+
+ $this->assertEquals(200, $client->getResponse()->getStatusCode());
+ }
+
public function testImportReadabilityWithFile()
{
$this->logInAs('admin');
$client->getContainer()->get('craue_config')->set('import_with_redis', 0);
}
+ public function testImportWallabagBadFile()
+ {
+ $this->logInAs('admin');
+ $client = $this->getClient();
+
+ $crawler = $client->request('GET', '/import/wallabag-v1');
+ $form = $crawler->filter('form[name=upload_import_file] > button[type=submit]')->form();
+
+ $data = [
+ 'upload_import_file[file]' => '',
+ ];
+
+ $client->submit($form, $data);
+
+ $this->assertEquals(200, $client->getResponse()->getStatusCode());
+ }
+
public function testImportWallabagWithFile()
{
$this->logInAs('admin');
$client->getContainer()->get('craue_config')->set('import_with_redis', 0);
}
+ public function testImportWallabagBadFile()
+ {
+ $this->logInAs('admin');
+ $client = $this->getClient();
+
+ $crawler = $client->request('GET', '/import/wallabag-v2');
+ $form = $crawler->filter('form[name=upload_import_file] > button[type=submit]')->form();
+
+ $data = [
+ 'upload_import_file[file]' => '',
+ ];
+
+ $client->submit($form, $data);
+
+ $this->assertEquals(200, $client->getResponse()->getStatusCode());
+ }
+
public function testImportWallabagWithFile()
{
$this->logInAs('admin');