3 namespace Tests\Wallabag\CoreBundle\Command
;
5 use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter
;
6 use Symfony\Component\HttpFoundation\Request
;
7 use Wallabag\CoreBundle\ParamConverter\UsernameRssTokenConverter
;
8 use Wallabag\UserBundle\Entity\User
;
10 class UsernameRssTokenConverterTest
extends \PHPUnit_Framework_TestCase
12 public function testSupportsWithNoRegistry()
14 $params = new ParamConverter([]);
15 $converter = new UsernameRssTokenConverter();
17 $this->assertFalse($converter->supports($params));
20 public function testSupportsWithNoRegistryManagers()
22 $registry = $this->getMockBuilder('Doctrine\Common\Persistence\ManagerRegistry')
23 ->disableOriginalConstructor()
26 $registry->expects($this->once())
27 ->method('getManagers')
28 ->will($this->returnValue([]));
30 $params = new ParamConverter([]);
31 $converter = new UsernameRssTokenConverter($registry);
33 $this->assertFalse($converter->supports($params));
36 public function testSupportsWithNoConfigurationClass()
38 $registry = $this->getMockBuilder('Doctrine\Common\Persistence\ManagerRegistry')
39 ->disableOriginalConstructor()
42 $registry->expects($this->once())
43 ->method('getManagers')
44 ->will($this->returnValue(['default' => null]));
46 $params = new ParamConverter([]);
47 $converter = new UsernameRssTokenConverter($registry);
49 $this->assertFalse($converter->supports($params));
52 public function testSupportsWithNotTheGoodClass()
54 $meta = $this->getMockBuilder('Doctrine\Common\Persistence\Mapping\ClassMetadata')
55 ->disableOriginalConstructor()
58 $meta->expects($this->once())
60 ->will($this->returnValue('nothingrelated'));
62 $em = $this->getMockBuilder('Doctrine\Common\Persistence\ObjectManager')
63 ->disableOriginalConstructor()
66 $em->expects($this->once())
67 ->method('getClassMetadata')
69 ->will($this->returnValue($meta));
71 $registry = $this->getMockBuilder('Doctrine\Common\Persistence\ManagerRegistry')
72 ->disableOriginalConstructor()
75 $registry->expects($this->once())
76 ->method('getManagers')
77 ->will($this->returnValue(['default' => null]));
79 $registry->expects($this->once())
80 ->method('getManagerForClass')
82 ->will($this->returnValue($em));
84 $params = new ParamConverter(['class' => 'superclass']);
85 $converter = new UsernameRssTokenConverter($registry);
87 $this->assertFalse($converter->supports($params));
90 public function testSupportsWithGoodClass()
92 $meta = $this->getMockBuilder('Doctrine\Common\Persistence\Mapping\ClassMetadata')
93 ->disableOriginalConstructor()
96 $meta->expects($this->once())
98 ->will($this->returnValue('Wallabag\UserBundle\Entity\User'));
100 $em = $this->getMockBuilder('Doctrine\Common\Persistence\ObjectManager')
101 ->disableOriginalConstructor()
104 $em->expects($this->once())
105 ->method('getClassMetadata')
106 ->with('WallabagUserBundle:User')
107 ->will($this->returnValue($meta));
109 $registry = $this->getMockBuilder('Doctrine\Common\Persistence\ManagerRegistry')
110 ->disableOriginalConstructor()
113 $registry->expects($this->once())
114 ->method('getManagers')
115 ->will($this->returnValue(['default' => null]));
117 $registry->expects($this->once())
118 ->method('getManagerForClass')
119 ->with('WallabagUserBundle:User')
120 ->will($this->returnValue($em));
122 $params = new ParamConverter(['class' => 'WallabagUserBundle:User']);
123 $converter = new UsernameRssTokenConverter($registry);
125 $this->assertTrue($converter->supports($params));
128 public function testApplyEmptyRequest()
130 $params = new ParamConverter([]);
131 $converter = new UsernameRssTokenConverter();
133 $res = $converter->apply(new Request(), $params);
135 $this->assertFalse($res);
139 * @expectedException Symfony\Component\HttpKernel\Exception\NotFoundHttpException
140 * @expectedExceptionMessage User not found
142 public function testApplyUserNotFound()
144 $repo = $this->getMockBuilder('Wallabag\UserBundle\Repository\UserRepository')
145 ->disableOriginalConstructor()
148 $repo->expects($this->once())
149 ->method('findOneByUsernameAndRsstoken')
150 ->with('test', 'test')
151 ->will($this->returnValue(null));
153 $em = $this->getMockBuilder('Doctrine\Common\Persistence\ObjectManager')
154 ->disableOriginalConstructor()
157 $em->expects($this->once())
158 ->method('getRepository')
159 ->with('WallabagUserBundle:User')
160 ->will($this->returnValue($repo));
162 $registry = $this->getMockBuilder('Doctrine\Common\Persistence\ManagerRegistry')
163 ->disableOriginalConstructor()
166 $registry->expects($this->once())
167 ->method('getManagerForClass')
168 ->with('WallabagUserBundle:User')
169 ->will($this->returnValue($em));
171 $params = new ParamConverter(['class' => 'WallabagUserBundle:User']);
172 $converter = new UsernameRssTokenConverter($registry);
173 $request = new Request([], [], ['username' => 'test', 'token' => 'test']);
175 $converter->apply($request, $params);
178 public function testApplyUserFound()
182 $repo = $this->getMockBuilder('Wallabag\UserBundle\Repository\UserRepository')
183 ->disableOriginalConstructor()
186 $repo->expects($this->once())
187 ->method('findOneByUsernameAndRsstoken')
188 ->with('test', 'test')
189 ->will($this->returnValue($user));
191 $em = $this->getMockBuilder('Doctrine\Common\Persistence\ObjectManager')
192 ->disableOriginalConstructor()
195 $em->expects($this->once())
196 ->method('getRepository')
197 ->with('WallabagUserBundle:User')
198 ->will($this->returnValue($repo));
200 $registry = $this->getMockBuilder('Doctrine\Common\Persistence\ManagerRegistry')
201 ->disableOriginalConstructor()
204 $registry->expects($this->once())
205 ->method('getManagerForClass')
206 ->with('WallabagUserBundle:User')
207 ->will($this->returnValue($em));
209 $params = new ParamConverter(['class' => 'WallabagUserBundle:User', 'name' => 'user']);
210 $converter = new UsernameRssTokenConverter($registry);
211 $request = new Request([], [], ['username' => 'test', 'token' => 'test']);
213 $converter->apply($request, $params);
215 $this->assertEquals($user, $request->attributes
->get('user'));