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