wallabag/tests/Wallabag/CoreBundle/ParamConverter/UsernameFeedTokenConverterTest.php

219 lines
7.1 KiB
PHP
Raw Normal View History

2015-03-28 21:43:49 +01:00
<?php
namespace Tests\Wallabag\CoreBundle\ParamConverter;
2015-03-28 21:43:49 +01:00
2017-12-16 22:17:42 +01:00
use PHPUnit\Framework\TestCase;
2015-03-28 21:43:49 +01:00
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
use Symfony\Component\HttpFoundation\Request;
use Wallabag\CoreBundle\ParamConverter\UsernameFeedTokenConverter;
use Wallabag\UserBundle\Entity\User;
2015-03-28 21:43:49 +01:00
class UsernameFeedTokenConverterTest extends TestCase
2015-03-28 21:43:49 +01:00
{
public function testSupportsWithNoRegistry()
{
$params = new ParamConverter([]);
$converter = new UsernameFeedTokenConverter();
2015-03-28 21:43:49 +01:00
$this->assertFalse($converter->supports($params));
}
public function testSupportsWithNoRegistryManagers()
{
$registry = $this->getMockBuilder('Doctrine\Common\Persistence\ManagerRegistry')
->disableOriginalConstructor()
->getMock();
$registry->expects($this->once())
->method('getManagers')
2019-05-10 15:49:39 +02:00
->willReturn([]);
2015-03-28 21:43:49 +01:00
$params = new ParamConverter([]);
$converter = new UsernameFeedTokenConverter($registry);
2015-03-28 21:43:49 +01:00
$this->assertFalse($converter->supports($params));
}
public function testSupportsWithNoConfigurationClass()
{
$registry = $this->getMockBuilder('Doctrine\Common\Persistence\ManagerRegistry')
->disableOriginalConstructor()
->getMock();
$registry->expects($this->once())
->method('getManagers')
2019-05-10 15:49:39 +02:00
->willReturn(['default' => null]);
2015-03-28 21:43:49 +01:00
$params = new ParamConverter([]);
$converter = new UsernameFeedTokenConverter($registry);
2015-03-28 21:43:49 +01:00
$this->assertFalse($converter->supports($params));
}
public function testSupportsWithNotTheGoodClass()
{
$meta = $this->getMockBuilder('Doctrine\Common\Persistence\Mapping\ClassMetadata')
->disableOriginalConstructor()
->getMock();
$meta->expects($this->once())
->method('getName')
2019-05-10 15:49:39 +02:00
->willReturn('nothingrelated');
2015-03-28 21:43:49 +01:00
$em = $this->getMockBuilder('Doctrine\Common\Persistence\ObjectManager')
->disableOriginalConstructor()
->getMock();
$em->expects($this->once())
->method('getClassMetadata')
->with('superclass')
2019-05-10 15:49:39 +02:00
->willReturn($meta);
2015-03-28 21:43:49 +01:00
$registry = $this->getMockBuilder('Doctrine\Common\Persistence\ManagerRegistry')
->disableOriginalConstructor()
->getMock();
$registry->expects($this->once())
->method('getManagers')
2019-05-10 15:49:39 +02:00
->willReturn(['default' => null]);
2015-03-28 21:43:49 +01:00
$registry->expects($this->once())
->method('getManagerForClass')
->with('superclass')
2019-05-10 15:49:39 +02:00
->willReturn($em);
2015-03-28 21:43:49 +01:00
$params = new ParamConverter(['class' => 'superclass']);
$converter = new UsernameFeedTokenConverter($registry);
2015-03-28 21:43:49 +01:00
$this->assertFalse($converter->supports($params));
}
public function testSupportsWithGoodClass()
{
$meta = $this->getMockBuilder('Doctrine\Common\Persistence\Mapping\ClassMetadata')
->disableOriginalConstructor()
->getMock();
$meta->expects($this->once())
->method('getName')
2019-05-10 15:49:39 +02:00
->willReturn('Wallabag\UserBundle\Entity\User');
2015-03-28 21:43:49 +01:00
$em = $this->getMockBuilder('Doctrine\Common\Persistence\ObjectManager')
->disableOriginalConstructor()
->getMock();
$em->expects($this->once())
->method('getClassMetadata')
->with('WallabagUserBundle:User')
2019-05-10 15:49:39 +02:00
->willReturn($meta);
2015-03-28 21:43:49 +01:00
$registry = $this->getMockBuilder('Doctrine\Common\Persistence\ManagerRegistry')
->disableOriginalConstructor()
->getMock();
$registry->expects($this->once())
->method('getManagers')
2019-05-10 15:49:39 +02:00
->willReturn(['default' => null]);
2015-03-28 21:43:49 +01:00
$registry->expects($this->once())
->method('getManagerForClass')
->with('WallabagUserBundle:User')
2019-05-10 15:49:39 +02:00
->willReturn($em);
2015-03-28 21:43:49 +01:00
$params = new ParamConverter(['class' => 'WallabagUserBundle:User']);
$converter = new UsernameFeedTokenConverter($registry);
2015-03-28 21:43:49 +01:00
$this->assertTrue($converter->supports($params));
}
public function testApplyEmptyRequest()
{
$params = new ParamConverter([]);
$converter = new UsernameFeedTokenConverter();
2015-03-28 21:43:49 +01:00
$res = $converter->apply(new Request(), $params);
$this->assertFalse($res);
2015-03-28 21:43:49 +01:00
}
/**
2017-05-31 10:38:15 +02:00
* @expectedException \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
2015-03-28 21:43:49 +01:00
* @expectedExceptionMessage User not found
*/
public function testApplyUserNotFound()
{
$repo = $this->getMockBuilder('Wallabag\UserBundle\Repository\UserRepository')
2015-03-28 21:43:49 +01:00
->disableOriginalConstructor()
->getMock();
$repo->expects($this->once())
->method('findOneByUsernameAndFeedToken')
2015-03-28 21:43:49 +01:00
->with('test', 'test')
2019-05-10 15:49:39 +02:00
->willReturn(null);
2015-03-28 21:43:49 +01:00
$em = $this->getMockBuilder('Doctrine\Common\Persistence\ObjectManager')
->disableOriginalConstructor()
->getMock();
$em->expects($this->once())
->method('getRepository')
->with('WallabagUserBundle:User')
2019-05-10 15:49:39 +02:00
->willReturn($repo);
2015-03-28 21:43:49 +01:00
$registry = $this->getMockBuilder('Doctrine\Common\Persistence\ManagerRegistry')
->disableOriginalConstructor()
->getMock();
$registry->expects($this->once())
->method('getManagerForClass')
->with('WallabagUserBundle:User')
2019-05-10 15:49:39 +02:00
->willReturn($em);
2015-03-28 21:43:49 +01:00
$params = new ParamConverter(['class' => 'WallabagUserBundle:User']);
$converter = new UsernameFeedTokenConverter($registry);
$request = new Request([], [], ['username' => 'test', 'token' => 'test']);
2015-03-28 21:43:49 +01:00
$converter->apply($request, $params);
}
public function testApplyUserFound()
{
$user = new User();
$repo = $this->getMockBuilder('Wallabag\UserBundle\Repository\UserRepository')
2015-03-28 21:43:49 +01:00
->disableOriginalConstructor()
->getMock();
$repo->expects($this->once())
->method('findOneByUsernameAndFeedtoken')
2015-03-28 21:43:49 +01:00
->with('test', 'test')
2019-05-10 15:49:39 +02:00
->willReturn($user);
2015-03-28 21:43:49 +01:00
$em = $this->getMockBuilder('Doctrine\Common\Persistence\ObjectManager')
->disableOriginalConstructor()
->getMock();
$em->expects($this->once())
->method('getRepository')
->with('WallabagUserBundle:User')
2019-05-10 15:49:39 +02:00
->willReturn($repo);
2015-03-28 21:43:49 +01:00
$registry = $this->getMockBuilder('Doctrine\Common\Persistence\ManagerRegistry')
->disableOriginalConstructor()
->getMock();
$registry->expects($this->once())
->method('getManagerForClass')
->with('WallabagUserBundle:User')
2019-05-10 15:49:39 +02:00
->willReturn($em);
2015-03-28 21:43:49 +01:00
$params = new ParamConverter(['class' => 'WallabagUserBundle:User', 'name' => 'user']);
$converter = new UsernameFeedTokenConverter($registry);
$request = new Request([], [], ['username' => 'test', 'token' => 'test']);
2015-03-28 21:43:49 +01:00
$converter->apply($request, $params);
2017-07-01 09:52:38 +02:00
$this->assertSame($user, $request->attributes->get('user'));
2015-03-28 21:43:49 +01:00
}
}