From 049d87e180ed4d3ce34359b6580ec543983ed902 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Nicolas=20L=C5=93uillet?= Date: Thu, 12 Jun 2025 21:08:04 +0200 Subject: [PATCH] Add tests --- src/Command/Import/UrlCommand.php | 2 +- tests/Command/Import/UrlCommandTest.php | 99 +++++++++++++++++++++++++ 2 files changed, 100 insertions(+), 1 deletion(-) create mode 100644 tests/Command/Import/UrlCommandTest.php diff --git a/src/Command/Import/UrlCommand.php b/src/Command/Import/UrlCommand.php index 62a524d1c..9f40d7cde 100644 --- a/src/Command/Import/UrlCommand.php +++ b/src/Command/Import/UrlCommand.php @@ -101,7 +101,7 @@ class UrlCommand extends Command $this->entityManager->persist($entry); $tags = explode(',', $input->getArgument('tags')); - if (count($tags) > 0) { + if (\count($tags) > 1) { $this->tagsAssigner->assignTagsToEntry( $entry, $tags, diff --git a/tests/Command/Import/UrlCommandTest.php b/tests/Command/Import/UrlCommandTest.php new file mode 100644 index 000000000..c71c69ba7 --- /dev/null +++ b/tests/Command/Import/UrlCommandTest.php @@ -0,0 +1,99 @@ +expectException(RuntimeException::class); + $this->expectExceptionMessage('Not enough arguments'); + + $application = new Application($this->getTestClient()->getKernel()); + + $command = $application->find('wallabag:import:url'); + + $tester = new CommandTester($command); + $tester->execute([]); + } + + public function testRunUrlCommandWithWrongUsername() + { + $this->expectException(NoResultException::class); + + $application = new Application($this->getTestClient()->getKernel()); + + $command = $application->find('wallabag:import:url'); + + $tester = new CommandTester($command); + $tester->execute([ + 'username' => 'random', + 'url' => $this->url, + ]); + } + + public function testRunUrlCommand() + { + $application = new Application($this->getTestClient()->getKernel()); + + $command = $application->find('wallabag:import:url'); + + $tester = new CommandTester($command); + $tester->execute([ + 'username' => 'admin', + 'url' => $this->url, + ]); + + $this->assertStringContainsString('successfully imported', $tester->getDisplay()); + } + + public function testRunUrlCommandWithTags() + { + $application = new Application($this->getTestClient()->getKernel()); + + $command = $application->find('wallabag:import:url'); + + $tester = new CommandTester($command); + $tester->execute([ + 'username' => 'admin', + 'url' => $this->url, + 'tags' => 'sport, football', + ]); + + $this->assertStringContainsString('successfully imported', $tester->getDisplay()); + + $client = $this->getTestClient(); + $em = $client->getContainer()->get(EntityManagerInterface::class); + $entry = $em->getRepository(Entry::class)->findByUrlAndUserId($this->url, $this->getLoggedInUserId()); + $this->assertContains('football', $entry->getTagsLabel()); + $this->assertNotContains('basketball', $entry->getTagsLabel()); + } + + public function testRunUrlCommandWithUserId() + { + $this->logInAs('admin'); + + $application = new Application($this->getTestClient()->getKernel()); + + $command = $application->find('wallabag:import:url'); + + $tester = new CommandTester($command); + $tester->execute([ + 'username' => $this->getLoggedInUserId(), + 'url' => $this->url, + '--useUserId' => true, + ]); + + $this->assertStringContainsString('successfully imported', $tester->getDisplay()); + } +}