3 namespace Tests\Wallabag\ApiBundle\Controller
;
5 use Tests\Wallabag\ApiBundle\WallabagApiTestCase
;
6 use Wallabag\CoreBundle\Entity\Tag
;
8 class TagRestControllerTest
extends WallabagApiTestCase
10 public function testGetUserTags()
12 $this->client
->request('GET', '/api/tags.json');
14 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
16 $content = json_decode($this->client
->getResponse()->getContent(), true);
18 $this->assertGreaterThan(0, $content);
19 $this->assertArrayHasKey('id', $content[0]);
20 $this->assertArrayHasKey('label', $content[0]);
26 * @depends testGetUserTags
28 public function testDeleteUserTag($tag)
30 $tagName = $tag['label'];
32 $this->client
->request('DELETE', '/api/tags/'.$tag['id'].'.json');
34 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
36 $content = json_decode($this->client
->getResponse()->getContent(), true);
38 $this->assertArrayHasKey('label', $content);
39 $this->assertEquals($tag['label'], $content['label']);
40 $this->assertEquals($tag['slug'], $content['slug']);
42 $entries = $this->client
->getContainer()
43 ->get('doctrine.orm.entity_manager')
44 ->getRepository('WallabagCoreBundle:Entry')
45 ->findAllByTagId($this->user
->getId(), $tag['id']);
47 $this->assertCount(0, $entries);
49 $tag = $this->client
->getContainer()
50 ->get('doctrine.orm.entity_manager')
51 ->getRepository('WallabagCoreBundle:Tag')
52 ->findOneByLabel($tagName);
54 $this->assertNull($tag, $tagName.' was removed because it begun an orphan tag');
57 public function dataForDeletingTagByLabel()
66 * @dataProvider dataForDeletingTagByLabel
68 public function testDeleteTagByLabel($useQueryString)
70 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
71 $entry = $this->client
->getContainer()
72 ->get('doctrine.orm.entity_manager')
73 ->getRepository('WallabagCoreBundle:Entry')
74 ->findOneWithTags($this->user
->getId());
79 $tag->setLabel('Awesome tag for test');
87 if ($useQueryString) {
88 $this->client
->request('DELETE', '/api/tag/label.json?tag='.$tag->getLabel());
90 $this->client
->request('DELETE', '/api/tag/label.json', ['tag' => $tag->getLabel()]);
93 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
95 $content = json_decode($this->client
->getResponse()->getContent(), true);
97 $this->assertArrayHasKey('label', $content);
98 $this->assertEquals($tag->getLabel(), $content['label']);
99 $this->assertEquals($tag->getSlug(), $content['slug']);
101 $entries = $this->client
->getContainer()
102 ->get('doctrine.orm.entity_manager')
103 ->getRepository('WallabagCoreBundle:Entry')
104 ->findAllByTagId($this->user
->getId(), $tag->getId());
106 $this->assertCount(0, $entries);
109 public function testDeleteTagByLabelNotFound()
111 $this->client
->request('DELETE', '/api/tag/label.json', ['tag' => 'does not exist']);
113 $this->assertEquals(404, $this->client
->getResponse()->getStatusCode());
117 * @dataProvider dataForDeletingTagByLabel
119 public function testDeleteTagsByLabel($useQueryString)
121 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
122 $entry = $this->client
->getContainer()
123 ->get('doctrine.orm.entity_manager')
124 ->getRepository('WallabagCoreBundle:Entry')
125 ->findOneWithTags($this->user
->getId());
130 $tag->setLabel('Awesome tag for tagsLabel');
134 $tag2->setLabel('Awesome tag for tagsLabel 2');
137 $entry->addTag($tag);
138 $entry->addTag($tag2);
140 $em->persist($entry);
143 if ($useQueryString) {
144 $this->client
->request('DELETE', '/api/tags/label.json?tags='.$tag->getLabel().','.$tag2->getLabel());
146 $this->client
->request('DELETE', '/api/tags/label.json', ['tags' => $tag->getLabel().','.$tag2->getLabel()]);
149 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
151 $content = json_decode($this->client
->getResponse()->getContent(), true);
153 $this->assertCount(2, $content);
155 $this->assertArrayHasKey('label', $content[0]);
156 $this->assertEquals($tag->getLabel(), $content[0]['label']);
157 $this->assertEquals($tag->getSlug(), $content[0]['slug']);
159 $this->assertArrayHasKey('label', $content[1]);
160 $this->assertEquals($tag2->getLabel(), $content[1]['label']);
161 $this->assertEquals($tag2->getSlug(), $content[1]['slug']);
163 $entries = $this->client
->getContainer()
164 ->get('doctrine.orm.entity_manager')
165 ->getRepository('WallabagCoreBundle:Entry')
166 ->findAllByTagId($this->user
->getId(), $tag->getId());
168 $this->assertCount(0, $entries);
170 $entries = $this->client
->getContainer()
171 ->get('doctrine.orm.entity_manager')
172 ->getRepository('WallabagCoreBundle:Entry')
173 ->findAllByTagId($this->user
->getId(), $tag2->getId());
175 $this->assertCount(0, $entries);
178 public function testDeleteTagsByLabelNotFound()
180 $this->client
->request('DELETE', '/api/tags/label.json', ['tags' => 'does not exist']);
182 $this->assertEquals(404, $this->client
->getResponse()->getStatusCode());