2015-03-28 20:43:49 +00:00
|
|
|
<?php
|
|
|
|
|
2024-02-19 00:30:12 +00:00
|
|
|
namespace Tests\Wallabag\ParamConverter;
|
2015-03-28 20:43:49 +00:00
|
|
|
|
2022-12-13 12:39:24 +00:00
|
|
|
use Doctrine\Persistence\ManagerRegistry;
|
|
|
|
use Doctrine\Persistence\Mapping\ClassMetadata;
|
|
|
|
use Doctrine\Persistence\ObjectManager;
|
2017-12-16 21:17:42 +00:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2015-03-28 20:43:49 +00:00
|
|
|
use Sensio\Bundle\FrameworkExtraBundle\Configuration\ParamConverter;
|
|
|
|
use Symfony\Component\HttpFoundation\Request;
|
2022-08-28 14:59:43 +00:00
|
|
|
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
|
2024-02-19 00:30:12 +00:00
|
|
|
use Wallabag\Entity\User;
|
|
|
|
use Wallabag\ParamConverter\UsernameFeedTokenConverter;
|
|
|
|
use Wallabag\Repository\UserRepository;
|
2015-03-28 20:43:49 +00:00
|
|
|
|
2017-06-13 16:48:10 +00:00
|
|
|
class UsernameFeedTokenConverterTest extends TestCase
|
2015-03-28 20:43:49 +00:00
|
|
|
{
|
|
|
|
public function testSupportsWithNoRegistry()
|
|
|
|
{
|
2016-04-12 09:36:01 +00:00
|
|
|
$params = new ParamConverter([]);
|
2017-06-13 16:48:10 +00:00
|
|
|
$converter = new UsernameFeedTokenConverter();
|
2015-03-28 20:43:49 +00:00
|
|
|
|
|
|
|
$this->assertFalse($converter->supports($params));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSupportsWithNoRegistryManagers()
|
|
|
|
{
|
2022-09-01 18:54:56 +00:00
|
|
|
$registry = $this->getMockBuilder(ManagerRegistry::class)
|
2015-03-28 20:43:49 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$registry->expects($this->once())
|
|
|
|
->method('getManagers')
|
2019-05-10 13:49:39 +00:00
|
|
|
->willReturn([]);
|
2015-03-28 20:43:49 +00:00
|
|
|
|
2016-04-12 09:36:01 +00:00
|
|
|
$params = new ParamConverter([]);
|
2017-06-13 16:48:10 +00:00
|
|
|
$converter = new UsernameFeedTokenConverter($registry);
|
2015-03-28 20:43:49 +00:00
|
|
|
|
|
|
|
$this->assertFalse($converter->supports($params));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSupportsWithNoConfigurationClass()
|
|
|
|
{
|
2022-09-01 18:54:56 +00:00
|
|
|
$registry = $this->getMockBuilder(ManagerRegistry::class)
|
2015-03-28 20:43:49 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$registry->expects($this->once())
|
|
|
|
->method('getManagers')
|
2019-05-10 13:49:39 +00:00
|
|
|
->willReturn(['default' => null]);
|
2015-03-28 20:43:49 +00:00
|
|
|
|
2016-04-12 09:36:01 +00:00
|
|
|
$params = new ParamConverter([]);
|
2017-06-13 16:48:10 +00:00
|
|
|
$converter = new UsernameFeedTokenConverter($registry);
|
2015-03-28 20:43:49 +00:00
|
|
|
|
|
|
|
$this->assertFalse($converter->supports($params));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSupportsWithNotTheGoodClass()
|
|
|
|
{
|
2022-09-01 18:54:56 +00:00
|
|
|
$meta = $this->getMockBuilder(ClassMetadata::class)
|
2015-03-28 20:43:49 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$meta->expects($this->once())
|
|
|
|
->method('getName')
|
2019-05-10 13:49:39 +00:00
|
|
|
->willReturn('nothingrelated');
|
2015-03-28 20:43:49 +00:00
|
|
|
|
2022-09-01 18:54:56 +00:00
|
|
|
$em = $this->getMockBuilder(ObjectManager::class)
|
2015-03-28 20:43:49 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$em->expects($this->once())
|
|
|
|
->method('getClassMetadata')
|
|
|
|
->with('superclass')
|
2019-05-10 13:49:39 +00:00
|
|
|
->willReturn($meta);
|
2015-03-28 20:43:49 +00:00
|
|
|
|
2022-09-01 18:54:56 +00:00
|
|
|
$registry = $this->getMockBuilder(ManagerRegistry::class)
|
2015-03-28 20:43:49 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$registry->expects($this->once())
|
|
|
|
->method('getManagers')
|
2019-05-10 13:49:39 +00:00
|
|
|
->willReturn(['default' => null]);
|
2015-03-28 20:43:49 +00:00
|
|
|
|
|
|
|
$registry->expects($this->once())
|
|
|
|
->method('getManagerForClass')
|
|
|
|
->with('superclass')
|
2019-05-10 13:49:39 +00:00
|
|
|
->willReturn($em);
|
2015-03-28 20:43:49 +00:00
|
|
|
|
2016-04-12 09:36:01 +00:00
|
|
|
$params = new ParamConverter(['class' => 'superclass']);
|
2017-06-13 16:48:10 +00:00
|
|
|
$converter = new UsernameFeedTokenConverter($registry);
|
2015-03-28 20:43:49 +00:00
|
|
|
|
|
|
|
$this->assertFalse($converter->supports($params));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSupportsWithGoodClass()
|
|
|
|
{
|
2022-09-01 18:54:56 +00:00
|
|
|
$meta = $this->getMockBuilder(ClassMetadata::class)
|
2015-03-28 20:43:49 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$meta->expects($this->once())
|
|
|
|
->method('getName')
|
2022-09-01 18:54:56 +00:00
|
|
|
->willReturn(User::class);
|
2015-03-28 20:43:49 +00:00
|
|
|
|
2022-09-01 18:54:56 +00:00
|
|
|
$em = $this->getMockBuilder(ObjectManager::class)
|
2015-03-28 20:43:49 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$em->expects($this->once())
|
|
|
|
->method('getClassMetadata')
|
2022-08-25 20:25:41 +00:00
|
|
|
->with(User::class)
|
2019-05-10 13:49:39 +00:00
|
|
|
->willReturn($meta);
|
2015-03-28 20:43:49 +00:00
|
|
|
|
2022-09-01 18:54:56 +00:00
|
|
|
$registry = $this->getMockBuilder(ManagerRegistry::class)
|
2015-03-28 20:43:49 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$registry->expects($this->once())
|
|
|
|
->method('getManagers')
|
2019-05-10 13:49:39 +00:00
|
|
|
->willReturn(['default' => null]);
|
2015-03-28 20:43:49 +00:00
|
|
|
|
|
|
|
$registry->expects($this->once())
|
|
|
|
->method('getManagerForClass')
|
2022-08-25 20:25:41 +00:00
|
|
|
->with(User::class)
|
2019-05-10 13:49:39 +00:00
|
|
|
->willReturn($em);
|
2015-03-28 20:43:49 +00:00
|
|
|
|
2022-08-25 20:25:41 +00:00
|
|
|
$params = new ParamConverter(['class' => User::class]);
|
2017-06-13 16:48:10 +00:00
|
|
|
$converter = new UsernameFeedTokenConverter($registry);
|
2015-03-28 20:43:49 +00:00
|
|
|
|
|
|
|
$this->assertTrue($converter->supports($params));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testApplyEmptyRequest()
|
|
|
|
{
|
2016-04-12 09:36:01 +00:00
|
|
|
$params = new ParamConverter([]);
|
2017-06-13 16:48:10 +00:00
|
|
|
$converter = new UsernameFeedTokenConverter();
|
2015-03-28 20:43:49 +00:00
|
|
|
|
2016-10-01 12:51:54 +00:00
|
|
|
$res = $converter->apply(new Request(), $params);
|
|
|
|
|
|
|
|
$this->assertFalse($res);
|
2015-03-28 20:43:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testApplyUserNotFound()
|
|
|
|
{
|
2022-08-28 14:59:43 +00:00
|
|
|
$this->expectException(NotFoundHttpException::class);
|
2020-06-15 11:37:50 +00:00
|
|
|
$this->expectExceptionMessage('User not found');
|
|
|
|
|
2022-09-01 18:54:56 +00:00
|
|
|
$repo = $this->getMockBuilder(UserRepository::class)
|
2015-03-28 20:43:49 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$repo->expects($this->once())
|
2017-06-13 16:48:10 +00:00
|
|
|
->method('findOneByUsernameAndFeedToken')
|
2015-03-28 20:43:49 +00:00
|
|
|
->with('test', 'test')
|
2019-05-10 13:49:39 +00:00
|
|
|
->willReturn(null);
|
2015-03-28 20:43:49 +00:00
|
|
|
|
2022-09-01 18:54:56 +00:00
|
|
|
$em = $this->getMockBuilder(ObjectManager::class)
|
2015-03-28 20:43:49 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$em->expects($this->once())
|
|
|
|
->method('getRepository')
|
2022-08-25 20:25:41 +00:00
|
|
|
->with(User::class)
|
2019-05-10 13:49:39 +00:00
|
|
|
->willReturn($repo);
|
2015-03-28 20:43:49 +00:00
|
|
|
|
2022-09-01 18:54:56 +00:00
|
|
|
$registry = $this->getMockBuilder(ManagerRegistry::class)
|
2015-03-28 20:43:49 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$registry->expects($this->once())
|
|
|
|
->method('getManagerForClass')
|
2022-08-25 20:25:41 +00:00
|
|
|
->with(User::class)
|
2019-05-10 13:49:39 +00:00
|
|
|
->willReturn($em);
|
2015-03-28 20:43:49 +00:00
|
|
|
|
2022-08-25 20:25:41 +00:00
|
|
|
$params = new ParamConverter(['class' => User::class]);
|
2017-06-13 16:48:10 +00:00
|
|
|
$converter = new UsernameFeedTokenConverter($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();
|
|
|
|
|
2022-09-01 18:54:56 +00:00
|
|
|
$repo = $this->getMockBuilder(UserRepository::class)
|
2015-03-28 20:43:49 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$repo->expects($this->once())
|
2017-06-13 16:48:10 +00:00
|
|
|
->method('findOneByUsernameAndFeedtoken')
|
2015-03-28 20:43:49 +00:00
|
|
|
->with('test', 'test')
|
2019-05-10 13:49:39 +00:00
|
|
|
->willReturn($user);
|
2015-03-28 20:43:49 +00:00
|
|
|
|
2022-09-01 18:54:56 +00:00
|
|
|
$em = $this->getMockBuilder(ObjectManager::class)
|
2015-03-28 20:43:49 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$em->expects($this->once())
|
|
|
|
->method('getRepository')
|
2022-08-25 20:25:41 +00:00
|
|
|
->with(User::class)
|
2019-05-10 13:49:39 +00:00
|
|
|
->willReturn($repo);
|
2015-03-28 20:43:49 +00:00
|
|
|
|
2022-09-01 18:54:56 +00:00
|
|
|
$registry = $this->getMockBuilder(ManagerRegistry::class)
|
2015-03-28 20:43:49 +00:00
|
|
|
->disableOriginalConstructor()
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$registry->expects($this->once())
|
|
|
|
->method('getManagerForClass')
|
2022-08-25 20:25:41 +00:00
|
|
|
->with(User::class)
|
2019-05-10 13:49:39 +00:00
|
|
|
->willReturn($em);
|
2015-03-28 20:43:49 +00:00
|
|
|
|
2022-08-25 20:25:41 +00:00
|
|
|
$params = new ParamConverter(['class' => User::class, 'name' => 'user']);
|
2017-06-13 16:48:10 +00:00
|
|
|
$converter = new UsernameFeedTokenConverter($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);
|
|
|
|
|
2017-07-01 07:52:38 +00:00
|
|
|
$this->assertSame($user, $request->attributes->get('user'));
|
2015-03-28 20:43:49 +00:00
|
|
|
}
|
|
|
|
}
|