]>
Commit | Line | Data |
---|---|---|
371ac69a J |
1 | <?php |
2 | ||
3 | namespace Wallabag\CoreBundle\Tests\Command; | |
4 | ||
5 | use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; | |
6 | use Wallabag\CoreBundle\ParamConverter\UsernameRssTokenConverter; | |
7 | use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter; | |
8 | use Symfony\Component\HttpFoundation\Request; | |
9 | use Wallabag\CoreBundle\Entity\User; | |
10 | ||
11 | class UsernameRssTokenConverterTest extends KernelTestCase | |
12 | { | |
13 | public function testSupportsWithNoRegistry() | |
14 | { | |
15 | $params = new ParamConverter(array()); | |
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') | |
29 | ->will($this->returnValue(array())); | |
30 | ||
31 | $params = new ParamConverter(array()); | |
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') | |
45 | ->will($this->returnValue(array('default' => null))); | |
46 | ||
47 | $params = new ParamConverter(array()); | |
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') | |
78 | ->will($this->returnValue(array('default' => null))); | |
79 | ||
80 | $registry->expects($this->once()) | |
81 | ->method('getManagerForClass') | |
82 | ->with('superclass') | |
83 | ->will($this->returnValue($em)); | |
84 | ||
85 | $params = new ParamConverter(array('class' => 'superclass')); | |
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') | |
99 | ->will($this->returnValue('Wallabag\CoreBundle\Entity\User')); | |
100 | ||
101 | $em = $this->getMockBuilder('Doctrine\Common\Persistence\ObjectManager') | |
102 | ->disableOriginalConstructor() | |
103 | ->getMock(); | |
104 | ||
105 | $em->expects($this->once()) | |
106 | ->method('getClassMetadata') | |
107 | ->with('WallabagCoreBundle:User') | |
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') | |
116 | ->will($this->returnValue(array('default' => null))); | |
117 | ||
118 | $registry->expects($this->once()) | |
119 | ->method('getManagerForClass') | |
120 | ->with('WallabagCoreBundle:User') | |
121 | ->will($this->returnValue($em)); | |
122 | ||
123 | $params = new ParamConverter(array('class' => 'WallabagCoreBundle:User')); | |
124 | $converter = new UsernameRssTokenConverter($registry); | |
125 | ||
126 | $this->assertTrue($converter->supports($params)); | |
127 | } | |
128 | ||
129 | /** | |
130 | * @expectedException InvalidArgumentException | |
131 | * @expectedExceptionMessage Route attribute is missing | |
132 | */ | |
133 | public function testApplyEmptyRequest() | |
134 | { | |
135 | $params = new ParamConverter(array()); | |
136 | $converter = new UsernameRssTokenConverter(); | |
137 | ||
138 | $converter->apply(new Request(), $params); | |
139 | } | |
140 | ||
141 | /** | |
142 | * @expectedException Symfony\Component\HttpKernel\Exception\NotFoundHttpException | |
143 | * @expectedExceptionMessage User not found | |
144 | */ | |
145 | public function testApplyUserNotFound() | |
146 | { | |
147 | $repo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\UserRepository') | |
148 | ->disableOriginalConstructor() | |
149 | ->getMock(); | |
150 | ||
151 | $repo->expects($this->once()) | |
152 | ->method('findOneByUsernameAndRsstoken') | |
153 | ->with('test', 'test') | |
154 | ->will($this->returnValue(null)); | |
155 | ||
156 | $em = $this->getMockBuilder('Doctrine\Common\Persistence\ObjectManager') | |
157 | ->disableOriginalConstructor() | |
158 | ->getMock(); | |
159 | ||
160 | $em->expects($this->once()) | |
161 | ->method('getRepository') | |
162 | ->with('WallabagCoreBundle:User') | |
163 | ->will($this->returnValue($repo)); | |
164 | ||
165 | $registry = $this->getMockBuilder('Doctrine\Common\Persistence\ManagerRegistry') | |
166 | ->disableOriginalConstructor() | |
167 | ->getMock(); | |
168 | ||
169 | $registry->expects($this->once()) | |
170 | ->method('getManagerForClass') | |
171 | ->with('WallabagCoreBundle:User') | |
172 | ->will($this->returnValue($em)); | |
173 | ||
174 | $params = new ParamConverter(array('class' => 'WallabagCoreBundle:User')); | |
175 | $converter = new UsernameRssTokenConverter($registry); | |
176 | $request = new Request(array(), array(), array('username' => 'test', 'token' => 'test')); | |
177 | ||
178 | $converter->apply($request, $params); | |
179 | } | |
180 | ||
181 | public function testApplyUserFound() | |
182 | { | |
183 | $user = new User(); | |
184 | ||
185 | $repo = $this->getMockBuilder('Wallabag\CoreBundle\Repository\UserRepository') | |
186 | ->disableOriginalConstructor() | |
187 | ->getMock(); | |
188 | ||
189 | $repo->expects($this->once()) | |
190 | ->method('findOneByUsernameAndRsstoken') | |
191 | ->with('test', 'test') | |
192 | ->will($this->returnValue($user)); | |
193 | ||
194 | $em = $this->getMockBuilder('Doctrine\Common\Persistence\ObjectManager') | |
195 | ->disableOriginalConstructor() | |
196 | ->getMock(); | |
197 | ||
198 | $em->expects($this->once()) | |
199 | ->method('getRepository') | |
200 | ->with('WallabagCoreBundle:User') | |
201 | ->will($this->returnValue($repo)); | |
202 | ||
203 | $registry = $this->getMockBuilder('Doctrine\Common\Persistence\ManagerRegistry') | |
204 | ->disableOriginalConstructor() | |
205 | ->getMock(); | |
206 | ||
207 | $registry->expects($this->once()) | |
208 | ->method('getManagerForClass') | |
209 | ->with('WallabagCoreBundle:User') | |
210 | ->will($this->returnValue($em)); | |
211 | ||
212 | $params = new ParamConverter(array('class' => 'WallabagCoreBundle:User', 'name' => 'user')); | |
213 | $converter = new UsernameRssTokenConverter($registry); | |
214 | $request = new Request(array(), array(), array('username' => 'test', 'token' => 'test')); | |
215 | ||
216 | $converter->apply($request, $params); | |
217 | ||
218 | $this->assertEquals($user, $request->attributes->get('user')); | |
219 | } | |
220 | } |