]> git.immae.eu Git - github/wallabag/wallabag.git/blame - tests/Wallabag/CoreBundle/ParamConverter/UsernameRssTokenConverterTest.php
Jump to Symfony 3.1
[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
128 /**
129 * @expectedException InvalidArgumentException
130 * @expectedExceptionMessage Route attribute is missing
131 */
132 public function testApplyEmptyRequest()
133 {
4094ea47 134 $params = new ParamConverter([]);
371ac69a
J
135 $converter = new UsernameRssTokenConverter();
136
137 $converter->apply(new Request(), $params);
138 }
139
140 /**
141 * @expectedException Symfony\Component\HttpKernel\Exception\NotFoundHttpException
142 * @expectedExceptionMessage User not found
143 */
144 public function testApplyUserNotFound()
145 {
1210dae1 146 $repo = $this->getMockBuilder('Wallabag\UserBundle\Repository\UserRepository')
371ac69a
J
147 ->disableOriginalConstructor()
148 ->getMock();
149
150 $repo->expects($this->once())
151 ->method('findOneByUsernameAndRsstoken')
152 ->with('test', 'test')
153 ->will($this->returnValue(null));
154
155 $em = $this->getMockBuilder('Doctrine\Common\Persistence\ObjectManager')
156 ->disableOriginalConstructor()
157 ->getMock();
158
159 $em->expects($this->once())
160 ->method('getRepository')
1210dae1 161 ->with('WallabagUserBundle:User')
371ac69a
J
162 ->will($this->returnValue($repo));
163
164 $registry = $this->getMockBuilder('Doctrine\Common\Persistence\ManagerRegistry')
165 ->disableOriginalConstructor()
166 ->getMock();
167
168 $registry->expects($this->once())
169 ->method('getManagerForClass')
1210dae1 170 ->with('WallabagUserBundle:User')
371ac69a
J
171 ->will($this->returnValue($em));
172
4094ea47 173 $params = new ParamConverter(['class' => 'WallabagUserBundle:User']);
371ac69a 174 $converter = new UsernameRssTokenConverter($registry);
4094ea47 175 $request = new Request([], [], ['username' => 'test', 'token' => 'test']);
371ac69a
J
176
177 $converter->apply($request, $params);
178 }
179
180 public function testApplyUserFound()
181 {
182 $user = new User();
183
1210dae1 184 $repo = $this->getMockBuilder('Wallabag\UserBundle\Repository\UserRepository')
371ac69a
J
185 ->disableOriginalConstructor()
186 ->getMock();
187
188 $repo->expects($this->once())
189 ->method('findOneByUsernameAndRsstoken')
190 ->with('test', 'test')
191 ->will($this->returnValue($user));
192
193 $em = $this->getMockBuilder('Doctrine\Common\Persistence\ObjectManager')
194 ->disableOriginalConstructor()
195 ->getMock();
196
197 $em->expects($this->once())
198 ->method('getRepository')
1210dae1 199 ->with('WallabagUserBundle:User')
371ac69a
J
200 ->will($this->returnValue($repo));
201
202 $registry = $this->getMockBuilder('Doctrine\Common\Persistence\ManagerRegistry')
203 ->disableOriginalConstructor()
204 ->getMock();
205
206 $registry->expects($this->once())
207 ->method('getManagerForClass')
1210dae1 208 ->with('WallabagUserBundle:User')
371ac69a
J
209 ->will($this->returnValue($em));
210
4094ea47 211 $params = new ParamConverter(['class' => 'WallabagUserBundle:User', 'name' => 'user']);
371ac69a 212 $converter = new UsernameRssTokenConverter($registry);
4094ea47 213 $request = new Request([], [], ['username' => 'test', 'token' => 'test']);
371ac69a
J
214
215 $converter->apply($request, $params);
216
217 $this->assertEquals($user, $request->attributes->get('user'));
218 }
219}