diff options
-rw-r--r-- | src/Wallabag/CoreBundle/Tests/Helper/RuleBasedTaggerTest.php | 158 |
1 files changed, 158 insertions, 0 deletions
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 @@ | |||
1 | <?php | ||
2 | |||
3 | namespace Wallabag\CoreBundle\Tests\Helper; | ||
4 | |||
5 | use Wallabag\CoreBundle\Entity\Config; | ||
6 | use Wallabag\CoreBundle\Entity\Entry; | ||
7 | use Wallabag\CoreBundle\Entity\Tag; | ||
8 | use Wallabag\CoreBundle\Entity\TaggingRule; | ||
9 | use Wallabag\UserBundle\Entity\User; | ||
10 | use Wallabag\CoreBundle\Helper\RuleBasedTagger; | ||
11 | |||
12 | class RuleBasedTaggerTest extends \PHPUnit_Framework_TestCase | ||
13 | { | ||
14 | private $rulerz; | ||
15 | private $repository; | ||
16 | private $tagger; | ||
17 | |||
18 | public function setUp() | ||
19 | { | ||
20 | $this->rulerz = $this->getRulerZMock(); | ||
21 | $this->repository = $this->getTagRepositoryMock(); | ||
22 | |||
23 | $this->tagger = new RuleBasedTagger($this->rulerz, $this->repository); | ||
24 | } | ||
25 | |||
26 | public function testTagWithNoRule() | ||
27 | { | ||
28 | $entry = new Entry($this->getUser()); | ||
29 | |||
30 | $this->tagger->tag($entry); | ||
31 | |||
32 | $this->assertTrue($entry->getTags()->isEmpty()); | ||
33 | } | ||
34 | |||
35 | public function testTagWithNoMatchingRule() | ||
36 | { | ||
37 | $taggingRule = $this->getTaggingRule('rule as string', array('foo', 'bar')); | ||
38 | $user = $this->getUser([$taggingRule]); | ||
39 | $entry = new Entry($user); | ||
40 | |||
41 | $this->rulerz | ||
42 | ->expects($this->once()) | ||
43 | ->method('satisfies') | ||
44 | ->with($entry, 'rule as string') | ||
45 | ->willReturn(false); | ||
46 | |||
47 | $this->tagger->tag($entry); | ||
48 | |||
49 | $this->assertTrue($entry->getTags()->isEmpty()); | ||
50 | } | ||
51 | |||
52 | public function testTagWithAMatchingRule() | ||
53 | { | ||
54 | $taggingRule = $this->getTaggingRule('rule as string', array('foo', 'bar')); | ||
55 | $user = $this->getUser([$taggingRule]); | ||
56 | $entry = new Entry($user); | ||
57 | |||
58 | $this->rulerz | ||
59 | ->expects($this->once()) | ||
60 | ->method('satisfies') | ||
61 | ->with($entry, 'rule as string') | ||
62 | ->willReturn(true); | ||
63 | |||
64 | $this->tagger->tag($entry); | ||
65 | |||
66 | $this->assertFalse($entry->getTags()->isEmpty()); | ||
67 | |||
68 | $tags = $entry->getTags(); | ||
69 | $this->assertSame('foo', $tags[0]->getLabel()); | ||
70 | $this->assertSame($user, $tags[0]->getUser()); | ||
71 | $this->assertSame('bar', $tags[1]->getLabel()); | ||
72 | $this->assertSame($user, $tags[1]->getUser()); | ||
73 | } | ||
74 | |||
75 | public function testTagWithAMixOfMatchingRules() | ||
76 | { | ||
77 | $taggingRule = $this->getTaggingRule('bla bla', array('hey')); | ||
78 | $otherTaggingRule = $this->getTaggingRule('rule as string', array('foo')); | ||
79 | |||
80 | $user = $this->getUser([$taggingRule, $otherTaggingRule]); | ||
81 | $entry = new Entry($user); | ||
82 | |||
83 | $this->rulerz | ||
84 | ->method('satisfies') | ||
85 | ->will($this->onConsecutiveCalls(false, true)); | ||
86 | |||
87 | $this->tagger->tag($entry); | ||
88 | |||
89 | $this->assertFalse($entry->getTags()->isEmpty()); | ||
90 | |||
91 | $tags = $entry->getTags(); | ||
92 | $this->assertSame('foo', $tags[0]->getLabel()); | ||
93 | $this->assertSame($user, $tags[0]->getUser()); | ||
94 | } | ||
95 | |||
96 | public function testWhenTheTagExists() | ||
97 | { | ||
98 | $taggingRule = $this->getTaggingRule('rule as string', array('foo')); | ||
99 | $user = $this->getUser([$taggingRule]); | ||
100 | $entry = new Entry($user); | ||
101 | $tag = new Tag($user); | ||
102 | |||
103 | $this->rulerz | ||
104 | ->expects($this->once()) | ||
105 | ->method('satisfies') | ||
106 | ->with($entry, 'rule as string') | ||
107 | ->willReturn(true); | ||
108 | |||
109 | $this->repository | ||
110 | ->expects($this->once()) | ||
111 | ->method('findOneByLabelAndUserId') | ||
112 | ->willReturn($tag); | ||
113 | |||
114 | $this->tagger->tag($entry); | ||
115 | |||
116 | $this->assertFalse($entry->getTags()->isEmpty()); | ||
117 | |||
118 | $tags = $entry->getTags(); | ||
119 | $this->assertSame($tag, $tags[0]); | ||
120 | } | ||
121 | |||
122 | private function getUser(array $taggingRules = []) | ||
123 | { | ||
124 | $user = new User(); | ||
125 | $config = new Config($user); | ||
126 | |||
127 | $user->setConfig($config); | ||
128 | |||
129 | foreach ($taggingRules as $rule) { | ||
130 | $config->addTaggingRule($rule); | ||
131 | } | ||
132 | |||
133 | return $user; | ||
134 | } | ||
135 | |||
136 | private function getTaggingRule($rule, array $tags) | ||
137 | { | ||
138 | $taggingRule = new TaggingRule(); | ||
139 | $taggingRule->setRule($rule); | ||
140 | $taggingRule->setTags($tags); | ||
141 | |||
142 | return $taggingRule; | ||
143 | } | ||
144 | |||
145 | private function getRulerZMock() | ||
146 | { | ||
147 | return $this->getMockBuilder('RulerZ\RulerZ') | ||
148 | ->disableOriginalConstructor() | ||
149 | ->getMock(); | ||
150 | } | ||
151 | |||
152 | private function getTagRepositoryMock() | ||
153 | { | ||
154 | return $this->getMockBuilder('Wallabag\CoreBundle\Repository\TagRepository') | ||
155 | ->disableOriginalConstructor() | ||
156 | ->getMock(); | ||
157 | } | ||
158 | } | ||