]> git.immae.eu Git - github/wallabag/wallabag.git/blame - tests/Wallabag/CoreBundle/ParamConverter/UsernameRssTokenConverterTest.php
Added check if there is only one user
[github/wallabag/wallabag.git] / tests / Wallabag / CoreBundle / ParamConverter / UsernameRssTokenConverterTest.php
CommitLineData
371ac69a
J
1<?php
2
23634d5d 3namespace Tests\Wallabag\CoreBundle\Command;
371ac69a 4
371ac69a
J
5use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
6use Symfony\Component\HttpFoundation\Request;
619cc453 7use Wallabag\CoreBundle\ParamConverter\UsernameRssTokenConverter;
1210dae1 8use Wallabag\UserBundle\Entity\User;
371ac69a 9
8a493541 10class UsernameRssTokenConverterTest extends \PHPUnit_Framework_TestCase
371ac69a
J
11{
12 public function testSupportsWithNoRegistry()
13 {
4094ea47 14 $params = new ParamConverter([]);
371ac69a
J
15 $converter = new UsernameRssTokenConverter();
16
17 $this->assertFalse($converter->supports($params));
18 }
19
20 public function testSupportsWithNoRegistryManagers()
21 {
22 $registry = $this->getMockBuilder('Doctrine\Common\Persistence\ManagerRegistry')
23 ->disableOriginalConstructor()
24 ->getMock();
25
26 $registry->expects($this->once())
27 ->method('getManagers')
4094ea47 28 ->will($this->returnValue([]));
371ac69a 29
4094ea47 30 $params = new ParamConverter([]);
371ac69a
J
31 $converter = new UsernameRssTokenConverter($registry);
32
33 $this->assertFalse($converter->supports($params));
34 }
35
36 public function testSupportsWithNoConfigurationClass()
37 {
38 $registry = $this->getMockBuilder('Doctrine\Common\Persistence\ManagerRegistry')
39 ->disableOriginalConstructor()
40 ->getMock();
41
42 $registry->expects($this->once())
43 ->method('getManagers')
4094ea47 44 ->will($this->returnValue(['default' => null]));
371ac69a 45
4094ea47 46 $params = new ParamConverter([]);
371ac69a
J
47 $converter = new UsernameRssTokenConverter($registry);
48
49 $this->assertFalse($converter->supports($params));
50 }
51
52 public function testSupportsWithNotTheGoodClass()
53 {
54 $meta = $this->getMockBuilder('Doctrine\Common\Persistence\Mapping\ClassMetadata')
55 ->disableOriginalConstructor()
56 ->getMock();
57
58 $meta->expects($this->once())
59 ->method('getName')
60 ->will($this->returnValue('nothingrelated'));
61
62 $em = $this->getMockBuilder('Doctrine\Common\Persistence\ObjectManager')
63 ->disableOriginalConstructor()
64 ->getMock();
65
66 $em->expects($this->once())
67 ->method('getClassMetadata')
68 ->with('superclass')
69 ->will($this->returnValue($meta));
70
71 $registry = $this->getMockBuilder('Doctrine\Common\Persistence\ManagerRegistry')
72 ->disableOriginalConstructor()
73 ->getMock();
74
75 $registry->expects($this->once())
76 ->method('getManagers')
4094ea47 77 ->will($this->returnValue(['default' => null]));
371ac69a
J
78
79 $registry->expects($this->once())
80 ->method('getManagerForClass')
81 ->with('superclass')
82 ->will($this->returnValue($em));
83
4094ea47 84 $params = new ParamConverter(['class' => 'superclass']);
371ac69a
J
85 $converter = new UsernameRssTokenConverter($registry);
86
87 $this->assertFalse($converter->supports($params));
88 }
89
90 public function testSupportsWithGoodClass()
91 {
92 $meta = $this->getMockBuilder('Doctrine\Common\Persistence\Mapping\ClassMetadata')
93 ->disableOriginalConstructor()
94 ->getMock();
95
96 $meta->expects($this->once())
97 ->method('getName')
1210dae1 98 ->will($this->returnValue('Wallabag\UserBundle\Entity\User'));
371ac69a
J
99
100 $em = $this->getMockBuilder('Doctrine\Common\Persistence\ObjectManager')
101 ->disableOriginalConstructor()
102 ->getMock();
103
104 $em->expects($this->once())
105 ->method('getClassMetadata')
1210dae1 106 ->with('WallabagUserBundle:User')
371ac69a
J
107 ->will($this->returnValue($meta));
108
109 $registry = $this->getMockBuilder('Doctrine\Common\Persistence\ManagerRegistry')
110 ->disableOriginalConstructor()
111 ->getMock();
112
113 $registry->expects($this->once())
114 ->method('getManagers')
4094ea47 115 ->will($this->returnValue(['default' => null]));
371ac69a
J
116
117 $registry->expects($this->once())
118 ->method('getManagerForClass')
1210dae1 119 ->with('WallabagUserBundle:User')
371ac69a
J
120 ->will($this->returnValue($em));
121
4094ea47 122 $params = new ParamConverter(['class' => 'WallabagUserBundle:User']);
371ac69a
J
123 $converter = new UsernameRssTokenConverter($registry);
124
125 $this->assertTrue($converter->supports($params));
126 }
127
371ac69a
J
128 public function testApplyEmptyRequest()
129 {
4094ea47 130 $params = new ParamConverter([]);
371ac69a
J
131 $converter = new UsernameRssTokenConverter();
132
40e21962
JB
133 $res = $converter->apply(new Request(), $params);
134
135 $this->assertFalse($res);
371ac69a
J
136 }
137
138 /**
139 * @expectedException Symfony\Component\HttpKernel\Exception\NotFoundHttpException
140 * @expectedExceptionMessage User not found
141 */
142 public function testApplyUserNotFound()
143 {
1210dae1 144 $repo = $this->getMockBuilder('Wallabag\UserBundle\Repository\UserRepository')
371ac69a
J
145 ->disableOriginalConstructor()
146 ->getMock();
147
148 $repo->expects($this->once())
149 ->method('findOneByUsernameAndRsstoken')
150 ->with('test', 'test')
151 ->will($this->returnValue(null));
152
153 $em = $this->getMockBuilder('Doctrine\Common\Persistence\ObjectManager')
154 ->disableOriginalConstructor()
155 ->getMock();
156
157 $em->expects($this->once())
158 ->method('getRepository')
1210dae1 159 ->with('WallabagUserBundle:User')
371ac69a
J
160 ->will($this->returnValue($repo));
161
162 $registry = $this->getMockBuilder('Doctrine\Common\Persistence\ManagerRegistry')
163 ->disableOriginalConstructor()
164 ->getMock();
165
166 $registry->expects($this->once())
167 ->method('getManagerForClass')
1210dae1 168 ->with('WallabagUserBundle:User')
371ac69a
J
169 ->will($this->returnValue($em));
170
4094ea47 171 $params = new ParamConverter(['class' => 'WallabagUserBundle:User']);
371ac69a 172 $converter = new UsernameRssTokenConverter($registry);
4094ea47 173 $request = new Request([], [], ['username' => 'test', 'token' => 'test']);
371ac69a
J
174
175 $converter->apply($request, $params);
176 }
177
178 public function testApplyUserFound()
179 {
180 $user = new User();
181
1210dae1 182 $repo = $this->getMockBuilder('Wallabag\UserBundle\Repository\UserRepository')
371ac69a
J
183 ->disableOriginalConstructor()
184 ->getMock();
185
186 $repo->expects($this->once())
187 ->method('findOneByUsernameAndRsstoken')
188 ->with('test', 'test')
189 ->will($this->returnValue($user));
190
191 $em = $this->getMockBuilder('Doctrine\Common\Persistence\ObjectManager')
192 ->disableOriginalConstructor()
193 ->getMock();
194
195 $em->expects($this->once())
196 ->method('getRepository')
1210dae1 197 ->with('WallabagUserBundle:User')
371ac69a
J
198 ->will($this->returnValue($repo));
199
200 $registry = $this->getMockBuilder('Doctrine\Common\Persistence\ManagerRegistry')
201 ->disableOriginalConstructor()
202 ->getMock();
203
204 $registry->expects($this->once())
205 ->method('getManagerForClass')
1210dae1 206 ->with('WallabagUserBundle:User')
371ac69a
J
207 ->will($this->returnValue($em));
208
4094ea47 209 $params = new ParamConverter(['class' => 'WallabagUserBundle:User', 'name' => 'user']);
371ac69a 210 $converter = new UsernameRssTokenConverter($registry);
4094ea47 211 $request = new Request([], [], ['username' => 'test', 'token' => 'test']);
371ac69a
J
212
213 $converter->apply($request, $params);
214
215 $this->assertEquals($user, $request->attributes->get('user'));
216 }
217}