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 testDeleteTagByLabel()
59 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
60 $entry = $this->client
->getContainer()
61 ->get('doctrine.orm.entity_manager')
62 ->getRepository('WallabagCoreBundle:Entry')
63 ->findOneWithTags($this->user
->getId());
68 $tag->setLabel('Awesome tag for test');
76 $this->client
->request('DELETE', '/api/tag/label.json', ['tag' => $tag->getLabel()]);
78 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
80 $content = json_decode($this->client
->getResponse()->getContent(), true);
82 $this->assertArrayHasKey('label', $content);
83 $this->assertEquals($tag->getLabel(), $content['label']);
84 $this->assertEquals($tag->getSlug(), $content['slug']);
86 $entries = $this->client
->getContainer()
87 ->get('doctrine.orm.entity_manager')
88 ->getRepository('WallabagCoreBundle:Entry')
89 ->findAllByTagId($this->user
->getId(), $tag->getId());
91 $this->assertCount(0, $entries);
94 public function testDeleteTagByLabelNotFound()
96 $this->client
->request('DELETE', '/api/tag/label.json', ['tag' => 'does not exist']);
98 $this->assertEquals(404, $this->client
->getResponse()->getStatusCode());
101 public function testDeleteTagsByLabel()
103 $em = $this->client
->getContainer()->get('doctrine.orm.entity_manager');
104 $entry = $this->client
->getContainer()
105 ->get('doctrine.orm.entity_manager')
106 ->getRepository('WallabagCoreBundle:Entry')
107 ->findOneWithTags($this->user
->getId());
112 $tag->setLabel('Awesome tag for tagsLabel');
116 $tag2->setLabel('Awesome tag for tagsLabel 2');
119 $entry->addTag($tag);
120 $entry->addTag($tag2);
122 $em->persist($entry);
125 $this->client
->request('DELETE', '/api/tags/label.json', ['tags' => $tag->getLabel().','.$tag2->getLabel()]);
127 $this->assertEquals(200, $this->client
->getResponse()->getStatusCode());
129 $content = json_decode($this->client
->getResponse()->getContent(), true);
131 $this->assertCount(2, $content);
133 $this->assertArrayHasKey('label', $content[0]);
134 $this->assertEquals($tag->getLabel(), $content[0]['label']);
135 $this->assertEquals($tag->getSlug(), $content[0]['slug']);
137 $this->assertArrayHasKey('label', $content[1]);
138 $this->assertEquals($tag2->getLabel(), $content[1]['label']);
139 $this->assertEquals($tag2->getSlug(), $content[1]['slug']);
141 $entries = $this->client
->getContainer()
142 ->get('doctrine.orm.entity_manager')
143 ->getRepository('WallabagCoreBundle:Entry')
144 ->findAllByTagId($this->user
->getId(), $tag->getId());
146 $this->assertCount(0, $entries);
148 $entries = $this->client
->getContainer()
149 ->get('doctrine.orm.entity_manager')
150 ->getRepository('WallabagCoreBundle:Entry')
151 ->findAllByTagId($this->user
->getId(), $tag2->getId());
153 $this->assertCount(0, $entries);
156 public function testDeleteTagsByLabelNotFound()
158 $this->client
->request('DELETE', '/api/tags/label.json', ['tags' => 'does not exist']);
160 $this->assertEquals(404, $this->client
->getResponse()->getStatusCode());