From 48656e0eaac006a80f21e9aec8900747fe76283a Mon Sep 17 00:00:00 2001 From: Jeremy Benoist Date: Sun, 30 Oct 2016 11:27:09 +0100 Subject: Fixing tests --- .../Event/Listener/LocaleListenerTest.php | 86 ++++++++++++++++ .../Event/Listener/UserLocaleListenerTest.php | 58 +++++++++++ .../Event/Subscriber/TablePrefixSubscriberTest.php | 114 +++++++++++++++++++++ .../EventListener/LocaleListenerTest.php | 86 ---------------- .../EventListener/UserLocaleListenerTest.php | 58 ----------- .../CoreBundle/Helper/DownloadImagesTest.php | 19 ++++ .../Subscriber/TablePrefixSubscriberTest.php | 114 --------------------- 7 files changed, 277 insertions(+), 258 deletions(-) create mode 100644 tests/Wallabag/CoreBundle/Event/Listener/LocaleListenerTest.php create mode 100644 tests/Wallabag/CoreBundle/Event/Listener/UserLocaleListenerTest.php create mode 100644 tests/Wallabag/CoreBundle/Event/Subscriber/TablePrefixSubscriberTest.php delete mode 100644 tests/Wallabag/CoreBundle/EventListener/LocaleListenerTest.php delete mode 100644 tests/Wallabag/CoreBundle/EventListener/UserLocaleListenerTest.php delete mode 100644 tests/Wallabag/CoreBundle/Subscriber/TablePrefixSubscriberTest.php (limited to 'tests/Wallabag') diff --git a/tests/Wallabag/CoreBundle/Event/Listener/LocaleListenerTest.php b/tests/Wallabag/CoreBundle/Event/Listener/LocaleListenerTest.php new file mode 100644 index 00000000..84a54d3a --- /dev/null +++ b/tests/Wallabag/CoreBundle/Event/Listener/LocaleListenerTest.php @@ -0,0 +1,86 @@ +getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface') + ->disableOriginalConstructor() + ->getMock(); + + return new GetResponseEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST); + } + + public function testWithoutSession() + { + $request = Request::create('/'); + + $listener = new LocaleListener('fr'); + $event = $this->getEvent($request); + + $listener->onKernelRequest($event); + $this->assertEquals('en', $request->getLocale()); + } + + public function testWithPreviousSession() + { + $request = Request::create('/'); + // generate a previous session + $request->cookies->set('MOCKSESSID', 'foo'); + $request->setSession(new Session(new MockArraySessionStorage())); + + $listener = new LocaleListener('fr'); + $event = $this->getEvent($request); + + $listener->onKernelRequest($event); + $this->assertEquals('fr', $request->getLocale()); + } + + public function testLocaleFromRequestAttribute() + { + $request = Request::create('/'); + // generate a previous session + $request->cookies->set('MOCKSESSID', 'foo'); + $request->setSession(new Session(new MockArraySessionStorage())); + $request->attributes->set('_locale', 'es'); + + $listener = new LocaleListener('fr'); + $event = $this->getEvent($request); + + $listener->onKernelRequest($event); + $this->assertEquals('en', $request->getLocale()); + $this->assertEquals('es', $request->getSession()->get('_locale')); + } + + public function testSubscribedEvents() + { + $request = Request::create('/'); + // generate a previous session + $request->cookies->set('MOCKSESSID', 'foo'); + $request->setSession(new Session(new MockArraySessionStorage())); + + $listener = new LocaleListener('fr'); + $event = $this->getEvent($request); + + $dispatcher = new EventDispatcher(); + $dispatcher->addSubscriber($listener); + + $dispatcher->dispatch( + KernelEvents::REQUEST, + $event + ); + + $this->assertEquals('fr', $request->getLocale()); + } +} diff --git a/tests/Wallabag/CoreBundle/Event/Listener/UserLocaleListenerTest.php b/tests/Wallabag/CoreBundle/Event/Listener/UserLocaleListenerTest.php new file mode 100644 index 00000000..45aecc63 --- /dev/null +++ b/tests/Wallabag/CoreBundle/Event/Listener/UserLocaleListenerTest.php @@ -0,0 +1,58 @@ +setEnabled(true); + + $config = new Config($user); + $config->setLanguage('fr'); + + $user->setConfig($config); + + $userToken = new UsernamePasswordToken($user, '', 'test'); + $request = Request::create('/'); + $event = new InteractiveLoginEvent($request, $userToken); + + $listener->onInteractiveLogin($event); + + $this->assertEquals('fr', $session->get('_locale')); + } + + public function testWithoutLanguage() + { + $session = new Session(new MockArraySessionStorage()); + $listener = new UserLocaleListener($session); + + $user = new User(); + $user->setEnabled(true); + + $config = new Config($user); + + $user->setConfig($config); + + $userToken = new UsernamePasswordToken($user, '', 'test'); + $request = Request::create('/'); + $event = new InteractiveLoginEvent($request, $userToken); + + $listener->onInteractiveLogin($event); + + $this->assertEquals('', $session->get('_locale')); + } +} diff --git a/tests/Wallabag/CoreBundle/Event/Subscriber/TablePrefixSubscriberTest.php b/tests/Wallabag/CoreBundle/Event/Subscriber/TablePrefixSubscriberTest.php new file mode 100644 index 00000000..b8cd0fad --- /dev/null +++ b/tests/Wallabag/CoreBundle/Event/Subscriber/TablePrefixSubscriberTest.php @@ -0,0 +1,114 @@ +getMockBuilder('Doctrine\ORM\EntityManager') + ->disableOriginalConstructor() + ->getMock(); + + $subscriber = new TablePrefixSubscriber($prefix); + + $metaClass = new ClassMetadata($entityName); + $metaClass->setPrimaryTable(['name' => $tableName]); + + $metaDataEvent = new LoadClassMetadataEventArgs($metaClass, $em); + + $this->assertEquals($tableNameExpected, $metaDataEvent->getClassMetadata()->getTableName()); + + $subscriber->loadClassMetadata($metaDataEvent); + + $this->assertEquals($finalTableName, $metaDataEvent->getClassMetadata()->getTableName()); + $this->assertEquals($finalTableNameQuoted, $metaDataEvent->getClassMetadata()->getQuotedTableName($platform)); + } + + /** + * @dataProvider dataForPrefix + */ + public function testSubscribedEvents($prefix, $entityName, $tableName, $tableNameExpected, $finalTableName, $finalTableNameQuoted, $platform) + { + $em = $this->getMockBuilder('Doctrine\ORM\EntityManager') + ->disableOriginalConstructor() + ->getMock(); + + $metaClass = new ClassMetadata($entityName); + $metaClass->setPrimaryTable(['name' => $tableName]); + + $metaDataEvent = new LoadClassMetadataEventArgs($metaClass, $em); + + $subscriber = new TablePrefixSubscriber($prefix); + + $evm = new EventManager(); + $evm->addEventSubscriber($subscriber); + + $evm->dispatchEvent('loadClassMetadata', $metaDataEvent); + + $this->assertEquals($finalTableName, $metaDataEvent->getClassMetadata()->getTableName()); + $this->assertEquals($finalTableNameQuoted, $metaDataEvent->getClassMetadata()->getQuotedTableName($platform)); + } + + public function testPrefixManyToMany() + { + $em = $this->getMockBuilder('Doctrine\ORM\EntityManager') + ->disableOriginalConstructor() + ->getMock(); + + $subscriber = new TablePrefixSubscriber('yo_'); + + $metaClass = new ClassMetadata('Wallabag\UserBundle\Entity\Entry'); + $metaClass->setPrimaryTable(['name' => 'entry']); + $metaClass->mapManyToMany([ + 'fieldName' => 'tags', + 'joinTable' => ['name' => null, 'schema' => null], + 'targetEntity' => 'Tag', + 'mappedBy' => null, + 'inversedBy' => 'entries', + 'cascade' => ['persist'], + 'indexBy' => null, + 'orphanRemoval' => false, + 'fetch' => 2, + ]); + + $metaDataEvent = new LoadClassMetadataEventArgs($metaClass, $em); + + $this->assertEquals('entry', $metaDataEvent->getClassMetadata()->getTableName()); + + $subscriber->loadClassMetadata($metaDataEvent); + + $this->assertEquals('yo_entry', $metaDataEvent->getClassMetadata()->getTableName()); + $this->assertEquals('yo_entry_tag', $metaDataEvent->getClassMetadata()->associationMappings['tags']['joinTable']['name']); + $this->assertEquals('yo_entry', $metaDataEvent->getClassMetadata()->getQuotedTableName(new \Doctrine\DBAL\Platforms\MySqlPlatform())); + } +} diff --git a/tests/Wallabag/CoreBundle/EventListener/LocaleListenerTest.php b/tests/Wallabag/CoreBundle/EventListener/LocaleListenerTest.php deleted file mode 100644 index 078bb69a..00000000 --- a/tests/Wallabag/CoreBundle/EventListener/LocaleListenerTest.php +++ /dev/null @@ -1,86 +0,0 @@ -getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface') - ->disableOriginalConstructor() - ->getMock(); - - return new GetResponseEvent($kernel, $request, HttpKernelInterface::MASTER_REQUEST); - } - - public function testWithoutSession() - { - $request = Request::create('/'); - - $listener = new LocaleListener('fr'); - $event = $this->getEvent($request); - - $listener->onKernelRequest($event); - $this->assertEquals('en', $request->getLocale()); - } - - public function testWithPreviousSession() - { - $request = Request::create('/'); - // generate a previous session - $request->cookies->set('MOCKSESSID', 'foo'); - $request->setSession(new Session(new MockArraySessionStorage())); - - $listener = new LocaleListener('fr'); - $event = $this->getEvent($request); - - $listener->onKernelRequest($event); - $this->assertEquals('fr', $request->getLocale()); - } - - public function testLocaleFromRequestAttribute() - { - $request = Request::create('/'); - // generate a previous session - $request->cookies->set('MOCKSESSID', 'foo'); - $request->setSession(new Session(new MockArraySessionStorage())); - $request->attributes->set('_locale', 'es'); - - $listener = new LocaleListener('fr'); - $event = $this->getEvent($request); - - $listener->onKernelRequest($event); - $this->assertEquals('en', $request->getLocale()); - $this->assertEquals('es', $request->getSession()->get('_locale')); - } - - public function testSubscribedEvents() - { - $request = Request::create('/'); - // generate a previous session - $request->cookies->set('MOCKSESSID', 'foo'); - $request->setSession(new Session(new MockArraySessionStorage())); - - $listener = new LocaleListener('fr'); - $event = $this->getEvent($request); - - $dispatcher = new EventDispatcher(); - $dispatcher->addSubscriber($listener); - - $dispatcher->dispatch( - KernelEvents::REQUEST, - $event - ); - - $this->assertEquals('fr', $request->getLocale()); - } -} diff --git a/tests/Wallabag/CoreBundle/EventListener/UserLocaleListenerTest.php b/tests/Wallabag/CoreBundle/EventListener/UserLocaleListenerTest.php deleted file mode 100644 index e9ac7c1d..00000000 --- a/tests/Wallabag/CoreBundle/EventListener/UserLocaleListenerTest.php +++ /dev/null @@ -1,58 +0,0 @@ -setEnabled(true); - - $config = new Config($user); - $config->setLanguage('fr'); - - $user->setConfig($config); - - $userToken = new UsernamePasswordToken($user, '', 'test'); - $request = Request::create('/'); - $event = new InteractiveLoginEvent($request, $userToken); - - $listener->onInteractiveLogin($event); - - $this->assertEquals('fr', $session->get('_locale')); - } - - public function testWithoutLanguage() - { - $session = new Session(new MockArraySessionStorage()); - $listener = new UserLocaleListener($session); - - $user = new User(); - $user->setEnabled(true); - - $config = new Config($user); - - $user->setConfig($config); - - $userToken = new UsernamePasswordToken($user, '', 'test'); - $request = Request::create('/'); - $event = new InteractiveLoginEvent($request, $userToken); - - $listener->onInteractiveLogin($event); - - $this->assertEquals('', $session->get('_locale')); - } -} diff --git a/tests/Wallabag/CoreBundle/Helper/DownloadImagesTest.php b/tests/Wallabag/CoreBundle/Helper/DownloadImagesTest.php index 0273693e..e000d681 100644 --- a/tests/Wallabag/CoreBundle/Helper/DownloadImagesTest.php +++ b/tests/Wallabag/CoreBundle/Helper/DownloadImagesTest.php @@ -83,6 +83,25 @@ class DownloadImagesTest extends \PHPUnit_Framework_TestCase $this->assertContains('/assets/images/4/2/4258f71e/ebe60399.'.$extension, $res); } + public function testProcessSingleImageWithBadUrl() + { + $client = new Client(); + + $mock = new Mock([ + new Response(404, []), + ]); + + $client->getEmitter()->attach($mock); + + $logHandler = new TestHandler(); + $logger = new Logger('test', array($logHandler)); + + $download = new DownloadImages($client, sys_get_temp_dir().'/wallabag_test', $logger); + $res = $download->processSingleImage('T9qgcHc.jpg', 'http://imgur.com/gallery/WxtWY'); + + $this->assertFalse($res, 'Image can not be found, so it will not be replaced'); + } + public function testProcessSingleImageWithBadImage() { $client = new Client(); diff --git a/tests/Wallabag/CoreBundle/Subscriber/TablePrefixSubscriberTest.php b/tests/Wallabag/CoreBundle/Subscriber/TablePrefixSubscriberTest.php deleted file mode 100644 index 4ae76703..00000000 --- a/tests/Wallabag/CoreBundle/Subscriber/TablePrefixSubscriberTest.php +++ /dev/null @@ -1,114 +0,0 @@ -getMockBuilder('Doctrine\ORM\EntityManager') - ->disableOriginalConstructor() - ->getMock(); - - $subscriber = new TablePrefixSubscriber($prefix); - - $metaClass = new ClassMetadata($entityName); - $metaClass->setPrimaryTable(['name' => $tableName]); - - $metaDataEvent = new LoadClassMetadataEventArgs($metaClass, $em); - - $this->assertEquals($tableNameExpected, $metaDataEvent->getClassMetadata()->getTableName()); - - $subscriber->loadClassMetadata($metaDataEvent); - - $this->assertEquals($finalTableName, $metaDataEvent->getClassMetadata()->getTableName()); - $this->assertEquals($finalTableNameQuoted, $metaDataEvent->getClassMetadata()->getQuotedTableName($platform)); - } - - /** - * @dataProvider dataForPrefix - */ - public function testSubscribedEvents($prefix, $entityName, $tableName, $tableNameExpected, $finalTableName, $finalTableNameQuoted, $platform) - { - $em = $this->getMockBuilder('Doctrine\ORM\EntityManager') - ->disableOriginalConstructor() - ->getMock(); - - $metaClass = new ClassMetadata($entityName); - $metaClass->setPrimaryTable(['name' => $tableName]); - - $metaDataEvent = new LoadClassMetadataEventArgs($metaClass, $em); - - $subscriber = new TablePrefixSubscriber($prefix); - - $evm = new EventManager(); - $evm->addEventSubscriber($subscriber); - - $evm->dispatchEvent('loadClassMetadata', $metaDataEvent); - - $this->assertEquals($finalTableName, $metaDataEvent->getClassMetadata()->getTableName()); - $this->assertEquals($finalTableNameQuoted, $metaDataEvent->getClassMetadata()->getQuotedTableName($platform)); - } - - public function testPrefixManyToMany() - { - $em = $this->getMockBuilder('Doctrine\ORM\EntityManager') - ->disableOriginalConstructor() - ->getMock(); - - $subscriber = new TablePrefixSubscriber('yo_'); - - $metaClass = new ClassMetadata('Wallabag\UserBundle\Entity\Entry'); - $metaClass->setPrimaryTable(['name' => 'entry']); - $metaClass->mapManyToMany([ - 'fieldName' => 'tags', - 'joinTable' => ['name' => null, 'schema' => null], - 'targetEntity' => 'Tag', - 'mappedBy' => null, - 'inversedBy' => 'entries', - 'cascade' => ['persist'], - 'indexBy' => null, - 'orphanRemoval' => false, - 'fetch' => 2, - ]); - - $metaDataEvent = new LoadClassMetadataEventArgs($metaClass, $em); - - $this->assertEquals('entry', $metaDataEvent->getClassMetadata()->getTableName()); - - $subscriber->loadClassMetadata($metaDataEvent); - - $this->assertEquals('yo_entry', $metaDataEvent->getClassMetadata()->getTableName()); - $this->assertEquals('yo_entry_tag', $metaDataEvent->getClassMetadata()->associationMappings['tags']['joinTable']['name']); - $this->assertEquals('yo_entry', $metaDataEvent->getClassMetadata()->getQuotedTableName(new \Doctrine\DBAL\Platforms\MySqlPlatform())); - } -} -- cgit v1.2.3