]> git.immae.eu Git - github/wallabag/wallabag.git/blob - tests/Wallabag/CoreBundle/ParamConverter/UsernameRssTokenConverterTest.php
Merge pull request #2508 from wallabag/fix-redis-rabbit-doc
[github/wallabag/wallabag.git] / tests / Wallabag / CoreBundle / ParamConverter / UsernameRssTokenConverterTest.php
1 <?php
2
3 namespace Tests\Wallabag\CoreBundle\Command;
4
5 use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
6 use Symfony\Component\HttpFoundation\Request;
7 use Wallabag\CoreBundle\ParamConverter\UsernameRssTokenConverter;
8 use Wallabag\UserBundle\Entity\User;
9
10 class UsernameRssTokenConverterTest extends \PHPUnit_Framework_TestCase
11 {
12 public function testSupportsWithNoRegistry()
13 {
14 $params = new ParamConverter([]);
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')
28 ->will($this->returnValue([]));
29
30 $params = new ParamConverter([]);
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')
44 ->will($this->returnValue(['default' => null]));
45
46 $params = new ParamConverter([]);
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')
77 ->will($this->returnValue(['default' => null]));
78
79 $registry->expects($this->once())
80 ->method('getManagerForClass')
81 ->with('superclass')
82 ->will($this->returnValue($em));
83
84 $params = new ParamConverter(['class' => 'superclass']);
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')
98 ->will($this->returnValue('Wallabag\UserBundle\Entity\User'));
99
100 $em = $this->getMockBuilder('Doctrine\Common\Persistence\ObjectManager')
101 ->disableOriginalConstructor()
102 ->getMock();
103
104 $em->expects($this->once())
105 ->method('getClassMetadata')
106 ->with('WallabagUserBundle:User')
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')
115 ->will($this->returnValue(['default' => null]));
116
117 $registry->expects($this->once())
118 ->method('getManagerForClass')
119 ->with('WallabagUserBundle:User')
120 ->will($this->returnValue($em));
121
122 $params = new ParamConverter(['class' => 'WallabagUserBundle:User']);
123 $converter = new UsernameRssTokenConverter($registry);
124
125 $this->assertTrue($converter->supports($params));
126 }
127
128 public function testApplyEmptyRequest()
129 {
130 $params = new ParamConverter([]);
131 $converter = new UsernameRssTokenConverter();
132
133 $res = $converter->apply(new Request(), $params);
134
135 $this->assertFalse($res);
136 }
137
138 /**
139 * @expectedException Symfony\Component\HttpKernel\Exception\NotFoundHttpException
140 * @expectedExceptionMessage User not found
141 */
142 public function testApplyUserNotFound()
143 {
144 $repo = $this->getMockBuilder('Wallabag\UserBundle\Repository\UserRepository')
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')
159 ->with('WallabagUserBundle:User')
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')
168 ->with('WallabagUserBundle:User')
169 ->will($this->returnValue($em));
170
171 $params = new ParamConverter(['class' => 'WallabagUserBundle:User']);
172 $converter = new UsernameRssTokenConverter($registry);
173 $request = new Request([], [], ['username' => 'test', 'token' => 'test']);
174
175 $converter->apply($request, $params);
176 }
177
178 public function testApplyUserFound()
179 {
180 $user = new User();
181
182 $repo = $this->getMockBuilder('Wallabag\UserBundle\Repository\UserRepository')
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')
197 ->with('WallabagUserBundle:User')
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')
206 ->with('WallabagUserBundle:User')
207 ->will($this->returnValue($em));
208
209 $params = new ParamConverter(['class' => 'WallabagUserBundle:User', 'name' => 'user']);
210 $converter = new UsernameRssTokenConverter($registry);
211 $request = new Request([], [], ['username' => 'test', 'token' => 'test']);
212
213 $converter->apply($request, $params);
214
215 $this->assertEquals($user, $request->attributes->get('user'));
216 }
217 }