]> git.immae.eu Git - github/wallabag/wallabag.git/blob - tests/Wallabag/CoreBundle/ParamConverter/UsernameRssTokenConverterTest.php
Merge pull request #3857 from wallabag/php73
[github/wallabag/wallabag.git] / tests / Wallabag / CoreBundle / ParamConverter / UsernameRssTokenConverterTest.php
1 <?php
2
3 namespace Tests\Wallabag\CoreBundle\ParamConverter;
4
5 use PHPUnit\Framework\TestCase;
6 use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
7 use Symfony\Component\HttpFoundation\Request;
8 use Wallabag\CoreBundle\ParamConverter\UsernameRssTokenConverter;
9 use Wallabag\UserBundle\Entity\User;
10
11 class UsernameRssTokenConverterTest extends TestCase
12 {
13 public function testSupportsWithNoRegistry()
14 {
15 $params = new ParamConverter([]);
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([]));
30
31 $params = new ParamConverter([]);
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(['default' => null]));
46
47 $params = new ParamConverter([]);
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(['default' => null]));
79
80 $registry->expects($this->once())
81 ->method('getManagerForClass')
82 ->with('superclass')
83 ->will($this->returnValue($em));
84
85 $params = new ParamConverter(['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\UserBundle\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('WallabagUserBundle: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(['default' => null]));
117
118 $registry->expects($this->once())
119 ->method('getManagerForClass')
120 ->with('WallabagUserBundle:User')
121 ->will($this->returnValue($em));
122
123 $params = new ParamConverter(['class' => 'WallabagUserBundle:User']);
124 $converter = new UsernameRssTokenConverter($registry);
125
126 $this->assertTrue($converter->supports($params));
127 }
128
129 public function testApplyEmptyRequest()
130 {
131 $params = new ParamConverter([]);
132 $converter = new UsernameRssTokenConverter();
133
134 $res = $converter->apply(new Request(), $params);
135
136 $this->assertFalse($res);
137 }
138
139 /**
140 * @expectedException \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
141 * @expectedExceptionMessage User not found
142 */
143 public function testApplyUserNotFound()
144 {
145 $repo = $this->getMockBuilder('Wallabag\UserBundle\Repository\UserRepository')
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')
160 ->with('WallabagUserBundle:User')
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')
169 ->with('WallabagUserBundle:User')
170 ->will($this->returnValue($em));
171
172 $params = new ParamConverter(['class' => 'WallabagUserBundle:User']);
173 $converter = new UsernameRssTokenConverter($registry);
174 $request = new Request([], [], ['username' => 'test', 'token' => 'test']);
175
176 $converter->apply($request, $params);
177 }
178
179 public function testApplyUserFound()
180 {
181 $user = new User();
182
183 $repo = $this->getMockBuilder('Wallabag\UserBundle\Repository\UserRepository')
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')
198 ->with('WallabagUserBundle:User')
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')
207 ->with('WallabagUserBundle:User')
208 ->will($this->returnValue($em));
209
210 $params = new ParamConverter(['class' => 'WallabagUserBundle:User', 'name' => 'user']);
211 $converter = new UsernameRssTokenConverter($registry);
212 $request = new Request([], [], ['username' => 'test', 'token' => 'test']);
213
214 $converter->apply($request, $params);
215
216 $this->assertSame($user, $request->attributes->get('user'));
217 }
218 }