3 namespace Tests\Wallabag\CoreBundle\ParamConverter
;
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
;
11 class UsernameRssTokenConverterTest
extends TestCase
13 public function testSupportsWithNoRegistry()
15 $params = new ParamConverter([]);
16 $converter = new UsernameRssTokenConverter();
18 $this->assertFalse($converter->supports($params));
21 public function testSupportsWithNoRegistryManagers()
23 $registry = $this->getMockBuilder('Doctrine\Common\Persistence\ManagerRegistry')
24 ->disableOriginalConstructor()
27 $registry->expects($this->once())
28 ->method('getManagers')
29 ->will($this->returnValue([]));
31 $params = new ParamConverter([]);
32 $converter = new UsernameRssTokenConverter($registry);
34 $this->assertFalse($converter->supports($params));
37 public function testSupportsWithNoConfigurationClass()
39 $registry = $this->getMockBuilder('Doctrine\Common\Persistence\ManagerRegistry')
40 ->disableOriginalConstructor()
43 $registry->expects($this->once())
44 ->method('getManagers')
45 ->will($this->returnValue(['default' => null]));
47 $params = new ParamConverter([]);
48 $converter = new UsernameRssTokenConverter($registry);
50 $this->assertFalse($converter->supports($params));
53 public function testSupportsWithNotTheGoodClass()
55 $meta = $this->getMockBuilder('Doctrine\Common\Persistence\Mapping\ClassMetadata')
56 ->disableOriginalConstructor()
59 $meta->expects($this->once())
61 ->will($this->returnValue('nothingrelated'));
63 $em = $this->getMockBuilder('Doctrine\Common\Persistence\ObjectManager')
64 ->disableOriginalConstructor()
67 $em->expects($this->once())
68 ->method('getClassMetadata')
70 ->will($this->returnValue($meta));
72 $registry = $this->getMockBuilder('Doctrine\Common\Persistence\ManagerRegistry')
73 ->disableOriginalConstructor()
76 $registry->expects($this->once())
77 ->method('getManagers')
78 ->will($this->returnValue(['default' => null]));
80 $registry->expects($this->once())
81 ->method('getManagerForClass')
83 ->will($this->returnValue($em));
85 $params = new ParamConverter(['class' => 'superclass']);
86 $converter = new UsernameRssTokenConverter($registry);
88 $this->assertFalse($converter->supports($params));
91 public function testSupportsWithGoodClass()
93 $meta = $this->getMockBuilder('Doctrine\Common\Persistence\Mapping\ClassMetadata')
94 ->disableOriginalConstructor()
97 $meta->expects($this->once())
99 ->will($this->returnValue('Wallabag\UserBundle\Entity\User'));
101 $em = $this->getMockBuilder('Doctrine\Common\Persistence\ObjectManager')
102 ->disableOriginalConstructor()
105 $em->expects($this->once())
106 ->method('getClassMetadata')
107 ->with('WallabagUserBundle:User')
108 ->will($this->returnValue($meta));
110 $registry = $this->getMockBuilder('Doctrine\Common\Persistence\ManagerRegistry')
111 ->disableOriginalConstructor()
114 $registry->expects($this->once())
115 ->method('getManagers')
116 ->will($this->returnValue(['default' => null]));
118 $registry->expects($this->once())
119 ->method('getManagerForClass')
120 ->with('WallabagUserBundle:User')
121 ->will($this->returnValue($em));
123 $params = new ParamConverter(['class' => 'WallabagUserBundle:User']);
124 $converter = new UsernameRssTokenConverter($registry);
126 $this->assertTrue($converter->supports($params));
129 public function testApplyEmptyRequest()
131 $params = new ParamConverter([]);
132 $converter = new UsernameRssTokenConverter();
134 $res = $converter->apply(new Request(), $params);
136 $this->assertFalse($res);
140 * @expectedException \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
141 * @expectedExceptionMessage User not found
143 public function testApplyUserNotFound()
145 $repo = $this->getMockBuilder('Wallabag\UserBundle\Repository\UserRepository')
146 ->disableOriginalConstructor()
149 $repo->expects($this->once())
150 ->method('findOneByUsernameAndRsstoken')
151 ->with('test', 'test')
152 ->will($this->returnValue(null));
154 $em = $this->getMockBuilder('Doctrine\Common\Persistence\ObjectManager')
155 ->disableOriginalConstructor()
158 $em->expects($this->once())
159 ->method('getRepository')
160 ->with('WallabagUserBundle:User')
161 ->will($this->returnValue($repo));
163 $registry = $this->getMockBuilder('Doctrine\Common\Persistence\ManagerRegistry')
164 ->disableOriginalConstructor()
167 $registry->expects($this->once())
168 ->method('getManagerForClass')
169 ->with('WallabagUserBundle:User')
170 ->will($this->returnValue($em));
172 $params = new ParamConverter(['class' => 'WallabagUserBundle:User']);
173 $converter = new UsernameRssTokenConverter($registry);
174 $request = new Request([], [], ['username' => 'test', 'token' => 'test']);
176 $converter->apply($request, $params);
179 public function testApplyUserFound()
183 $repo = $this->getMockBuilder('Wallabag\UserBundle\Repository\UserRepository')
184 ->disableOriginalConstructor()
187 $repo->expects($this->once())
188 ->method('findOneByUsernameAndRsstoken')
189 ->with('test', 'test')
190 ->will($this->returnValue($user));
192 $em = $this->getMockBuilder('Doctrine\Common\Persistence\ObjectManager')
193 ->disableOriginalConstructor()
196 $em->expects($this->once())
197 ->method('getRepository')
198 ->with('WallabagUserBundle:User')
199 ->will($this->returnValue($repo));
201 $registry = $this->getMockBuilder('Doctrine\Common\Persistence\ManagerRegistry')
202 ->disableOriginalConstructor()
205 $registry->expects($this->once())
206 ->method('getManagerForClass')
207 ->with('WallabagUserBundle:User')
208 ->will($this->returnValue($em));
210 $params = new ParamConverter(['class' => 'WallabagUserBundle:User', 'name' => 'user']);
211 $converter = new UsernameRssTokenConverter($registry);
212 $request = new Request([], [], ['username' => 'test', 'token' => 'test']);
214 $converter->apply($request, $params);
216 $this->assertSame($user, $request->attributes
->get('user'));