]> git.immae.eu Git - github/wallabag/wallabag.git/commitdiff
Add more tests
authorJeremy Benoist <jeremy.benoist@gmail.com>
Sun, 11 Sep 2016 18:24:04 +0000 (20:24 +0200)
committerJeremy Benoist <jeremy.benoist@gmail.com>
Sun, 11 Sep 2016 20:15:31 +0000 (22:15 +0200)
And ability to define how many messages can be hanle by the redis worker before stopping (usefull for tests)

src/Wallabag/ImportBundle/Command/RedisWorkerCommand.php
src/Wallabag/ImportBundle/Controller/ReadabilityController.php
src/Wallabag/ImportBundle/Controller/WallabagController.php
src/Wallabag/ImportBundle/Form/Type/UploadImportType.php
tests/Wallabag/ImportBundle/Command/RedisWorkerCommandTest.php [new file with mode: 0644]
tests/Wallabag/ImportBundle/Consumer/RedisEntryConsumerTest.php
tests/Wallabag/ImportBundle/Controller/ReadabilityControllerTest.php
tests/Wallabag/ImportBundle/Controller/WallabagV1ControllerTest.php
tests/Wallabag/ImportBundle/Controller/WallabagV2ControllerTest.php

index 85c5a9035ac578f8ca4e3778183710133f5d1bc3..5f90e00fa8b0b5e071f751983cd30befcc4a4dec 100644 (file)
@@ -5,6 +5,7 @@ namespace Wallabag\ImportBundle\Command;
 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;
@@ -17,6 +18,7 @@ class RedisWorkerCommand extends ContainerAwareCommand
             ->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)
         ;
     }
 
@@ -33,7 +35,8 @@ class RedisWorkerCommand extends ContainerAwareCommand
 
         $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();
index 6124304281adccaada6bbbf8b5263b42cd8916d6..8775e8a3b5b0bfb8de330b1f4e1481a1da37b5ee 100644 (file)
@@ -31,7 +31,7 @@ class ReadabilityController extends Controller
             $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)
index 1e6114c5195a4ca4a4bd2309e9d0db24f80129ea..3c2dd6d1f7ae54af5fbcecd806f9759af8b3ba50 100644 (file)
@@ -45,7 +45,7 @@ abstract class WallabagController extends Controller
             $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)
index 92a167d9497092cf1da0d61dc0508ab41a54cd2c..f50424c16a10f21b90e1a6747c898a78095684fd 100644 (file)
@@ -15,6 +15,7 @@ class UploadImportType extends AbstractType
         $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',
diff --git a/tests/Wallabag/ImportBundle/Command/RedisWorkerCommandTest.php b/tests/Wallabag/ImportBundle/Command/RedisWorkerCommandTest.php
new file mode 100644 (file)
index 0000000..7495284
--- /dev/null
@@ -0,0 +1,74 @@
+<?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());
+    }
+}
index 0ce7ce49b0071ee318609f08c25f1591ae28186b..5e8ee41d846522cd68b2b84ee6e9428eb22ba86b 100644 (file)
@@ -220,5 +220,6 @@ JSON;
         $res = $consumer->manage($body);
 
         $this->assertFalse($res);
+        $this->assertFalse($consumer->isStopJob($body));
     }
 }
index 69635382083689adc83fa787f4e77e68cb4160a4..fb835f62d18e3ee6348af8ee6fc55c3313570e32 100644 (file)
@@ -51,6 +51,23 @@ class ReadabilityControllerTest extends WallabagCoreTestCase
         $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');
index 933ddd6c89f9f757668dccea4cdb44ae48aa32c8..f111336528311133a6b0495beb06b074ebf0fab2 100644 (file)
@@ -51,6 +51,23 @@ class WallabagV1ControllerTest extends WallabagCoreTestCase
         $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');
index 36e5221d6d0bebf508c5c5515353c2a0c9f62a01..b20226ad183469e398da44cc96e1435acbe12723 100644 (file)
@@ -51,6 +51,23 @@ class WallabagV2ControllerTest extends WallabagCoreTestCase
         $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');