From: Kévin Gomez Date: Sat, 17 Oct 2015 16:34:18 +0000 (+0200) Subject: Add tests for the RuleBasedTagger class X-Git-Tag: 2.0.0-alpha.1~11^2~18 X-Git-Url: https://git.immae.eu/?a=commitdiff_plain;h=5a166c5c1af645b1b1ba77ba5f2e24094e7b60e8;p=github%2Fwallabag%2Fwallabag.git Add tests for the RuleBasedTagger class --- diff --git a/src/Wallabag/CoreBundle/Tests/Helper/RuleBasedTaggerTest.php b/src/Wallabag/CoreBundle/Tests/Helper/RuleBasedTaggerTest.php new file mode 100644 index 00000000..56a1ed61 --- /dev/null +++ b/src/Wallabag/CoreBundle/Tests/Helper/RuleBasedTaggerTest.php @@ -0,0 +1,158 @@ +rulerz = $this->getRulerZMock(); + $this->repository = $this->getTagRepositoryMock(); + + $this->tagger = new RuleBasedTagger($this->rulerz, $this->repository); + } + + public function testTagWithNoRule() + { + $entry = new Entry($this->getUser()); + + $this->tagger->tag($entry); + + $this->assertTrue($entry->getTags()->isEmpty()); + } + + public function testTagWithNoMatchingRule() + { + $taggingRule = $this->getTaggingRule('rule as string', array('foo', 'bar')); + $user = $this->getUser([$taggingRule]); + $entry = new Entry($user); + + $this->rulerz + ->expects($this->once()) + ->method('satisfies') + ->with($entry, 'rule as string') + ->willReturn(false); + + $this->tagger->tag($entry); + + $this->assertTrue($entry->getTags()->isEmpty()); + } + + public function testTagWithAMatchingRule() + { + $taggingRule = $this->getTaggingRule('rule as string', array('foo', 'bar')); + $user = $this->getUser([$taggingRule]); + $entry = new Entry($user); + + $this->rulerz + ->expects($this->once()) + ->method('satisfies') + ->with($entry, 'rule as string') + ->willReturn(true); + + $this->tagger->tag($entry); + + $this->assertFalse($entry->getTags()->isEmpty()); + + $tags = $entry->getTags(); + $this->assertSame('foo', $tags[0]->getLabel()); + $this->assertSame($user, $tags[0]->getUser()); + $this->assertSame('bar', $tags[1]->getLabel()); + $this->assertSame($user, $tags[1]->getUser()); + } + + public function testTagWithAMixOfMatchingRules() + { + $taggingRule = $this->getTaggingRule('bla bla', array('hey')); + $otherTaggingRule = $this->getTaggingRule('rule as string', array('foo')); + + $user = $this->getUser([$taggingRule, $otherTaggingRule]); + $entry = new Entry($user); + + $this->rulerz + ->method('satisfies') + ->will($this->onConsecutiveCalls(false, true)); + + $this->tagger->tag($entry); + + $this->assertFalse($entry->getTags()->isEmpty()); + + $tags = $entry->getTags(); + $this->assertSame('foo', $tags[0]->getLabel()); + $this->assertSame($user, $tags[0]->getUser()); + } + + public function testWhenTheTagExists() + { + $taggingRule = $this->getTaggingRule('rule as string', array('foo')); + $user = $this->getUser([$taggingRule]); + $entry = new Entry($user); + $tag = new Tag($user); + + $this->rulerz + ->expects($this->once()) + ->method('satisfies') + ->with($entry, 'rule as string') + ->willReturn(true); + + $this->repository + ->expects($this->once()) + ->method('findOneByLabelAndUserId') + ->willReturn($tag); + + $this->tagger->tag($entry); + + $this->assertFalse($entry->getTags()->isEmpty()); + + $tags = $entry->getTags(); + $this->assertSame($tag, $tags[0]); + } + + private function getUser(array $taggingRules = []) + { + $user = new User(); + $config = new Config($user); + + $user->setConfig($config); + + foreach ($taggingRules as $rule) { + $config->addTaggingRule($rule); + } + + return $user; + } + + private function getTaggingRule($rule, array $tags) + { + $taggingRule = new TaggingRule(); + $taggingRule->setRule($rule); + $taggingRule->setTags($tags); + + return $taggingRule; + } + + private function getRulerZMock() + { + return $this->getMockBuilder('RulerZ\RulerZ') + ->disableOriginalConstructor() + ->getMock(); + } + + private function getTagRepositoryMock() + { + return $this->getMockBuilder('Wallabag\CoreBundle\Repository\TagRepository') + ->disableOriginalConstructor() + ->getMock(); + } +}