]> git.immae.eu Git - github/wallabag/wallabag.git/blobdiff - src/Wallabag/CoreBundle/Tests/Helper/RuleBasedTaggerTest.php
Merge pull request #1583 from wallabag/v2-fix-delete
[github/wallabag/wallabag.git] / src / Wallabag / CoreBundle / Tests / Helper / RuleBasedTaggerTest.php
index 56a1ed61e50762743e9146562f10acffc1794d8b..c43c62c3bf86800d80787f3d2ece61739867ec1b 100644 (file)
@@ -6,21 +6,23 @@ use Wallabag\CoreBundle\Entity\Config;
 use Wallabag\CoreBundle\Entity\Entry;
 use Wallabag\CoreBundle\Entity\Tag;
 use Wallabag\CoreBundle\Entity\TaggingRule;
-use Wallabag\UserBundle\Entity\User;
 use Wallabag\CoreBundle\Helper\RuleBasedTagger;
+use Wallabag\UserBundle\Entity\User;
 
 class RuleBasedTaggerTest extends \PHPUnit_Framework_TestCase
 {
     private $rulerz;
-    private $repository;
+    private $tagRepository;
+    private $entryRepository;
     private $tagger;
 
     public function setUp()
     {
-        $this->rulerz     = $this->getRulerZMock();
-        $this->repository = $this->getTagRepositoryMock();
+        $this->rulerz = $this->getRulerZMock();
+        $this->tagRepository = $this->getTagRepositoryMock();
+        $this->entryRepository = $this->getEntryRepositoryMock();
 
-        $this->tagger     = new RuleBasedTagger($this->rulerz, $this->repository);
+        $this->tagger = new RuleBasedTagger($this->rulerz, $this->tagRepository, $this->entryRepository);
     }
 
     public function testTagWithNoRule()
@@ -35,8 +37,8 @@ class RuleBasedTaggerTest extends \PHPUnit_Framework_TestCase
     public function testTagWithNoMatchingRule()
     {
         $taggingRule = $this->getTaggingRule('rule as string', array('foo', 'bar'));
-        $user        = $this->getUser([$taggingRule]);
-        $entry       = new Entry($user);
+        $user = $this->getUser([$taggingRule]);
+        $entry = new Entry($user);
 
         $this->rulerz
             ->expects($this->once())
@@ -52,8 +54,8 @@ class RuleBasedTaggerTest extends \PHPUnit_Framework_TestCase
     public function testTagWithAMatchingRule()
     {
         $taggingRule = $this->getTaggingRule('rule as string', array('foo', 'bar'));
-        $user        = $this->getUser([$taggingRule]);
-        $entry       = new Entry($user);
+        $user = $this->getUser([$taggingRule]);
+        $entry = new Entry($user);
 
         $this->rulerz
             ->expects($this->once())
@@ -67,17 +69,15 @@ class RuleBasedTaggerTest extends \PHPUnit_Framework_TestCase
 
         $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'));
+        $taggingRule = $this->getTaggingRule('bla bla', array('hey'));
         $otherTaggingRule = $this->getTaggingRule('rule as string', array('foo'));
 
-        $user  = $this->getUser([$taggingRule, $otherTaggingRule]);
+        $user = $this->getUser([$taggingRule, $otherTaggingRule]);
         $entry = new Entry($user);
 
         $this->rulerz
@@ -90,15 +90,14 @@ class RuleBasedTaggerTest extends \PHPUnit_Framework_TestCase
 
         $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);
+        $user = $this->getUser([$taggingRule]);
+        $entry = new Entry($user);
+        $tag = new Tag();
 
         $this->rulerz
             ->expects($this->once())
@@ -106,9 +105,11 @@ class RuleBasedTaggerTest extends \PHPUnit_Framework_TestCase
             ->with($entry, 'rule as string')
             ->willReturn(true);
 
-        $this->repository
+        $this->tagRepository
             ->expects($this->once())
-            ->method('findOneByLabelAndUserId')
+            // the method `findOneByLabel` doesn't exist, EntityRepository will then call `_call` method
+            // to magically call the `findOneBy` with ['label' => 'foo']
+            ->method('__call')
             ->willReturn($tag);
 
         $this->tagger->tag($entry);
@@ -119,9 +120,55 @@ class RuleBasedTaggerTest extends \PHPUnit_Framework_TestCase
         $this->assertSame($tag, $tags[0]);
     }
 
+    public function testSameTagWithDifferentfMatchingRules()
+    {
+        $taggingRule = $this->getTaggingRule('bla bla', array('hey'));
+        $otherTaggingRule = $this->getTaggingRule('rule as string', array('hey'));
+
+        $user = $this->getUser([$taggingRule, $otherTaggingRule]);
+        $entry = new Entry($user);
+
+        $this->rulerz
+            ->method('satisfies')
+            ->willReturn(true);
+
+        $this->tagger->tag($entry);
+
+        $this->assertFalse($entry->getTags()->isEmpty());
+
+        $tags = $entry->getTags();
+        $this->assertCount(1, $tags);
+    }
+
+    public function testTagAllEntriesForAUser()
+    {
+        $taggingRule = $this->getTaggingRule('bla bla', array('hey'));
+
+        $user = $this->getUser([$taggingRule]);
+
+        $this->rulerz
+            ->method('satisfies')
+            ->willReturn(true);
+
+        $this->rulerz
+            ->method('filter')
+            ->willReturn(array(new Entry($user), new Entry($user)));
+
+        $entries = $this->tagger->tagAllForUser($user);
+
+        $this->assertCount(2, $entries);
+
+        foreach ($entries as $entry) {
+            $tags = $entry->getTags();
+
+            $this->assertCount(1, $tags);
+            $this->assertEquals('hey', $tags[0]->getLabel());
+        }
+    }
+
     private function getUser(array $taggingRules = [])
     {
-        $user   = new User();
+        $user = new User();
         $config = new Config($user);
 
         $user->setConfig($config);
@@ -155,4 +202,11 @@ class RuleBasedTaggerTest extends \PHPUnit_Framework_TestCase
             ->disableOriginalConstructor()
             ->getMock();
     }
+
+    private function getEntryRepositoryMock()
+    {
+        return $this->getMockBuilder('Wallabag\CoreBundle\Repository\EntryRepository')
+            ->disableOriginalConstructor()
+            ->getMock();
+    }
 }