2015-03-28 20:43:49 +00:00
|
|
|
<?php
|
|
|
|
|
2016-06-01 19:27:35 +00:00
|
|
|
namespace Tests\Wallabag\CoreBundle\Command;
|
2015-03-28 20:43:49 +00:00
|
|
|
|
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
|
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
2015-12-22 09:16:34 +00:00
|
|
|
use Wallabag\CoreBundle\ParamConverter\UsernameRssTokenConverter;
|
2015-10-02 12:51:41 +00:00
|
|
|
use Wallabag\UserBundle\Entity\User;
|
2015-03-28 20:43:49 +00:00
|
|
|
|
2015-11-01 22:42:52 +00:00
|
|
|
class UsernameRssTokenConverterTest extends \PHPUnit_Framework_TestCase
|
2015-03-28 20:43:49 +00:00
|
|
|
{
|
|
|
|
public function testSupportsWithNoRegistry()
|
|
|
|
{
|
2016-04-12 09:36:01 +00:00
|
|
|
$params = new ParamConverter([]);
|
2015-03-28 20:43:49 +00:00
|
|
|
$converter = new UsernameRssTokenConverter();
|
|
|
|
|
|
|
|
$this->assertFalse($converter->supports($params));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSupportsWithNoRegistryManagers()
|
|
|
|
{
|
|
|
|
$registry = $this->getMockBuilder('Doctrine\Common\Persistence\ManagerRegistry')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$registry->expects($this->once())
|
|
|
|
->method('getManagers')
|
2016-04-12 09:36:01 +00:00
|
|
|
->will($this->returnValue([]));
|
2015-03-28 20:43:49 +00:00
|
|
|
|
2016-04-12 09:36:01 +00:00
|
|
|
$params = new ParamConverter([]);
|
2015-03-28 20:43:49 +00:00
|
|
|
$converter = new UsernameRssTokenConverter($registry);
|
|
|
|
|
|
|
|
$this->assertFalse($converter->supports($params));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSupportsWithNoConfigurationClass()
|
|
|
|
{
|
|
|
|
$registry = $this->getMockBuilder('Doctrine\Common\Persistence\ManagerRegistry')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$registry->expects($this->once())
|
|
|
|
->method('getManagers')
|
2016-04-12 09:36:01 +00:00
|
|
|
->will($this->returnValue(['default' => null]));
|
2015-03-28 20:43:49 +00:00
|
|
|
|
2016-04-12 09:36:01 +00:00
|
|
|
$params = new ParamConverter([]);
|
2015-03-28 20:43:49 +00:00
|
|
|
$converter = new UsernameRssTokenConverter($registry);
|
|
|
|
|
|
|
|
$this->assertFalse($converter->supports($params));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSupportsWithNotTheGoodClass()
|
|
|
|
{
|
|
|
|
$meta = $this->getMockBuilder('Doctrine\Common\Persistence\Mapping\ClassMetadata')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$meta->expects($this->once())
|
|
|
|
->method('getName')
|
|
|
|
->will($this->returnValue('nothingrelated'));
|
|
|
|
|
|
|
|
$em = $this->getMockBuilder('Doctrine\Common\Persistence\ObjectManager')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$em->expects($this->once())
|
|
|
|
->method('getClassMetadata')
|
|
|
|
->with('superclass')
|
|
|
|
->will($this->returnValue($meta));
|
|
|
|
|
|
|
|
$registry = $this->getMockBuilder('Doctrine\Common\Persistence\ManagerRegistry')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$registry->expects($this->once())
|
|
|
|
->method('getManagers')
|
2016-04-12 09:36:01 +00:00
|
|
|
->will($this->returnValue(['default' => null]));
|
2015-03-28 20:43:49 +00:00
|
|
|
|
|
|
|
$registry->expects($this->once())
|
|
|
|
->method('getManagerForClass')
|
|
|
|
->with('superclass')
|
|
|
|
->will($this->returnValue($em));
|
|
|
|
|
2016-04-12 09:36:01 +00:00
|
|
|
$params = new ParamConverter(['class' => 'superclass']);
|
2015-03-28 20:43:49 +00:00
|
|
|
$converter = new UsernameRssTokenConverter($registry);
|
|
|
|
|
|
|
|
$this->assertFalse($converter->supports($params));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSupportsWithGoodClass()
|
|
|
|
{
|
|
|
|
$meta = $this->getMockBuilder('Doctrine\Common\Persistence\Mapping\ClassMetadata')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$meta->expects($this->once())
|
|
|
|
->method('getName')
|
2015-10-02 12:51:41 +00:00
|
|
|
->will($this->returnValue('Wallabag\UserBundle\Entity\User'));
|
2015-03-28 20:43:49 +00:00
|
|
|
|
|
|
|
$em = $this->getMockBuilder('Doctrine\Common\Persistence\ObjectManager')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$em->expects($this->once())
|
|
|
|
->method('getClassMetadata')
|
2015-10-02 12:51:41 +00:00
|
|
|
->with('WallabagUserBundle:User')
|
2015-03-28 20:43:49 +00:00
|
|
|
->will($this->returnValue($meta));
|
|
|
|
|
|
|
|
$registry = $this->getMockBuilder('Doctrine\Common\Persistence\ManagerRegistry')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$registry->expects($this->once())
|
|
|
|
->method('getManagers')
|
2016-04-12 09:36:01 +00:00
|
|
|
->will($this->returnValue(['default' => null]));
|
2015-03-28 20:43:49 +00:00
|
|
|
|
|
|
|
$registry->expects($this->once())
|
|
|
|
->method('getManagerForClass')
|
2015-10-02 12:51:41 +00:00
|
|
|
->with('WallabagUserBundle:User')
|
2015-03-28 20:43:49 +00:00
|
|
|
->will($this->returnValue($em));
|
|
|
|
|
2016-04-12 09:36:01 +00:00
|
|
|
$params = new ParamConverter(['class' => 'WallabagUserBundle:User']);
|
2015-03-28 20:43:49 +00:00
|
|
|
$converter = new UsernameRssTokenConverter($registry);
|
|
|
|
|
|
|
|
$this->assertTrue($converter->supports($params));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException InvalidArgumentException
|
|
|
|
* @expectedExceptionMessage Route attribute is missing
|
|
|
|
*/
|
|
|
|
public function testApplyEmptyRequest()
|
|
|
|
{
|
2016-04-12 09:36:01 +00:00
|
|
|
$params = new ParamConverter([]);
|
2015-03-28 20:43:49 +00:00
|
|
|
$converter = new UsernameRssTokenConverter();
|
|
|
|
|
|
|
|
$converter->apply(new Request(), $params);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @expectedException Symfony\Component\HttpKernel\Exception\NotFoundHttpException
|
|
|
|
* @expectedExceptionMessage User not found
|
|
|
|
*/
|
|
|
|
public function testApplyUserNotFound()
|
|
|
|
{
|
2015-10-02 12:51:41 +00:00
|
|
|
$repo = $this->getMockBuilder('Wallabag\UserBundle\Repository\UserRepository')
|
2015-03-28 20:43:49 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$repo->expects($this->once())
|
|
|
|
->method('findOneByUsernameAndRsstoken')
|
|
|
|
->with('test', 'test')
|
|
|
|
->will($this->returnValue(null));
|
|
|
|
|
|
|
|
$em = $this->getMockBuilder('Doctrine\Common\Persistence\ObjectManager')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$em->expects($this->once())
|
|
|
|
->method('getRepository')
|
2015-10-02 12:51:41 +00:00
|
|
|
->with('WallabagUserBundle:User')
|
2015-03-28 20:43:49 +00:00
|
|
|
->will($this->returnValue($repo));
|
|
|
|
|
|
|
|
$registry = $this->getMockBuilder('Doctrine\Common\Persistence\ManagerRegistry')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$registry->expects($this->once())
|
|
|
|
->method('getManagerForClass')
|
2015-10-02 12:51:41 +00:00
|
|
|
->with('WallabagUserBundle:User')
|
2015-03-28 20:43:49 +00:00
|
|
|
->will($this->returnValue($em));
|
|
|
|
|
2016-04-12 09:36:01 +00:00
|
|
|
$params = new ParamConverter(['class' => 'WallabagUserBundle:User']);
|
2015-03-28 20:43:49 +00:00
|
|
|
$converter = new UsernameRssTokenConverter($registry);
|
2016-04-12 09:36:01 +00:00
|
|
|
$request = new Request([], [], ['username' => 'test', 'token' => 'test']);
|
2015-03-28 20:43:49 +00:00
|
|
|
|
|
|
|
$converter->apply($request, $params);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testApplyUserFound()
|
|
|
|
{
|
|
|
|
$user = new User();
|
|
|
|
|
2015-10-02 12:51:41 +00:00
|
|
|
$repo = $this->getMockBuilder('Wallabag\UserBundle\Repository\UserRepository')
|
2015-03-28 20:43:49 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$repo->expects($this->once())
|
|
|
|
->method('findOneByUsernameAndRsstoken')
|
|
|
|
->with('test', 'test')
|
|
|
|
->will($this->returnValue($user));
|
|
|
|
|
|
|
|
$em = $this->getMockBuilder('Doctrine\Common\Persistence\ObjectManager')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$em->expects($this->once())
|
|
|
|
->method('getRepository')
|
2015-10-02 12:51:41 +00:00
|
|
|
->with('WallabagUserBundle:User')
|
2015-03-28 20:43:49 +00:00
|
|
|
->will($this->returnValue($repo));
|
|
|
|
|
|
|
|
$registry = $this->getMockBuilder('Doctrine\Common\Persistence\ManagerRegistry')
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$registry->expects($this->once())
|
|
|
|
->method('getManagerForClass')
|
2015-10-02 12:51:41 +00:00
|
|
|
->with('WallabagUserBundle:User')
|
2015-03-28 20:43:49 +00:00
|
|
|
->will($this->returnValue($em));
|
|
|
|
|
2016-04-12 09:36:01 +00:00
|
|
|
$params = new ParamConverter(['class' => 'WallabagUserBundle:User', 'name' => 'user']);
|
2015-03-28 20:43:49 +00:00
|
|
|
$converter = new UsernameRssTokenConverter($registry);
|
2016-04-12 09:36:01 +00:00
|
|
|
$request = new Request([], [], ['username' => 'test', 'token' => 'test']);
|
2015-03-28 20:43:49 +00:00
|
|
|
|
|
|
|
$converter->apply($request, $params);
|
|
|
|
|
|
|
|
$this->assertEquals($user, $request->attributes->get('user'));
|
|
|
|
}
|
|
|
|
}
|