]> git.immae.eu Git - github/wallabag/wallabag.git/blob - tests/Wallabag/ApiBundle/Controller/TagRestControllerTest.php
Exploded WallabagRestController into many controllers
[github/wallabag/wallabag.git] / tests / Wallabag / ApiBundle / Controller / TagRestControllerTest.php
1 <?php
2
3 namespace Tests\Wallabag\ApiBundle\Controller;
4
5 use Tests\Wallabag\ApiBundle\WallabagApiTestCase;
6 use Wallabag\CoreBundle\Entity\Tag;
7
8 class TagRestControllerTest extends WallabagApiTestCase
9 {
10 public function testGetUserTags()
11 {
12 $this->client->request('GET', '/api/tags.json');
13
14 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
15
16 $content = json_decode($this->client->getResponse()->getContent(), true);
17
18 $this->assertGreaterThan(0, $content);
19 $this->assertArrayHasKey('id', $content[0]);
20 $this->assertArrayHasKey('label', $content[0]);
21
22 return end($content);
23 }
24
25 /**
26 * @depends testGetUserTags
27 */
28 public function testDeleteUserTag($tag)
29 {
30 $tagName = $tag['label'];
31
32 $this->client->request('DELETE', '/api/tags/'.$tag['id'].'.json');
33
34 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
35
36 $content = json_decode($this->client->getResponse()->getContent(), true);
37
38 $this->assertArrayHasKey('label', $content);
39 $this->assertEquals($tag['label'], $content['label']);
40 $this->assertEquals($tag['slug'], $content['slug']);
41
42 $entries = $this->client->getContainer()
43 ->get('doctrine.orm.entity_manager')
44 ->getRepository('WallabagCoreBundle:Entry')
45 ->findAllByTagId($this->user->getId(), $tag['id']);
46
47 $this->assertCount(0, $entries);
48
49 $tag = $this->client->getContainer()
50 ->get('doctrine.orm.entity_manager')
51 ->getRepository('WallabagCoreBundle:Tag')
52 ->findOneByLabel($tagName);
53
54 $this->assertNull($tag, $tagName.' was removed because it begun an orphan tag');
55 }
56
57 public function testDeleteTagByLabel()
58 {
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());
64
65 $entry = $entry[0];
66
67 $tag = new Tag();
68 $tag->setLabel('Awesome tag for test');
69 $em->persist($tag);
70
71 $entry->addTag($tag);
72
73 $em->persist($entry);
74 $em->flush();
75
76 $this->client->request('DELETE', '/api/tag/label.json', ['tag' => $tag->getLabel()]);
77
78 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
79
80 $content = json_decode($this->client->getResponse()->getContent(), true);
81
82 $this->assertArrayHasKey('label', $content);
83 $this->assertEquals($tag->getLabel(), $content['label']);
84 $this->assertEquals($tag->getSlug(), $content['slug']);
85
86 $entries = $this->client->getContainer()
87 ->get('doctrine.orm.entity_manager')
88 ->getRepository('WallabagCoreBundle:Entry')
89 ->findAllByTagId($this->user->getId(), $tag->getId());
90
91 $this->assertCount(0, $entries);
92 }
93
94 public function testDeleteTagByLabelNotFound()
95 {
96 $this->client->request('DELETE', '/api/tag/label.json', ['tag' => 'does not exist']);
97
98 $this->assertEquals(404, $this->client->getResponse()->getStatusCode());
99 }
100
101 public function testDeleteTagsByLabel()
102 {
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());
108
109 $entry = $entry[0];
110
111 $tag = new Tag();
112 $tag->setLabel('Awesome tag for tagsLabel');
113 $em->persist($tag);
114
115 $tag2 = new Tag();
116 $tag2->setLabel('Awesome tag for tagsLabel 2');
117 $em->persist($tag2);
118
119 $entry->addTag($tag);
120 $entry->addTag($tag2);
121
122 $em->persist($entry);
123 $em->flush();
124
125 $this->client->request('DELETE', '/api/tags/label.json', ['tags' => $tag->getLabel().','.$tag2->getLabel()]);
126
127 $this->assertEquals(200, $this->client->getResponse()->getStatusCode());
128
129 $content = json_decode($this->client->getResponse()->getContent(), true);
130
131 $this->assertCount(2, $content);
132
133 $this->assertArrayHasKey('label', $content[0]);
134 $this->assertEquals($tag->getLabel(), $content[0]['label']);
135 $this->assertEquals($tag->getSlug(), $content[0]['slug']);
136
137 $this->assertArrayHasKey('label', $content[1]);
138 $this->assertEquals($tag2->getLabel(), $content[1]['label']);
139 $this->assertEquals($tag2->getSlug(), $content[1]['slug']);
140
141 $entries = $this->client->getContainer()
142 ->get('doctrine.orm.entity_manager')
143 ->getRepository('WallabagCoreBundle:Entry')
144 ->findAllByTagId($this->user->getId(), $tag->getId());
145
146 $this->assertCount(0, $entries);
147
148 $entries = $this->client->getContainer()
149 ->get('doctrine.orm.entity_manager')
150 ->getRepository('WallabagCoreBundle:Entry')
151 ->findAllByTagId($this->user->getId(), $tag2->getId());
152
153 $this->assertCount(0, $entries);
154 }
155
156 public function testDeleteTagsByLabelNotFound()
157 {
158 $this->client->request('DELETE', '/api/tags/label.json', ['tags' => 'does not exist']);
159
160 $this->assertEquals(404, $this->client->getResponse()->getStatusCode());
161 }
162 }