diff --git a/tests/Block/Service/PageFeedBlockServiceTest.php b/tests/Block/Service/PageFeedBlockServiceTest.php index 2c495b9..9aa68d0 100644 --- a/tests/Block/Service/PageFeedBlockServiceTest.php +++ b/tests/Block/Service/PageFeedBlockServiceTest.php @@ -13,6 +13,7 @@ use Core23\FacebookBundle\Block\Service\PageFeedBlockService; use Facebook\Authentication\AccessToken; +use Facebook\Exceptions\FacebookSDKException; use Facebook\Facebook; use Facebook\FacebookApp; use Facebook\FacebookResponse; @@ -89,6 +90,46 @@ public function testExecute(): void $this->assertSame($feedResponse, $this->templating->parameters['feed']); } + public function testExecuteThrowsFacebookException(): void + { + $token = $this->createMock(AccessToken::class); + + $app = $this->createMock(FacebookApp::class); + $app->expects($this->once())->method('getAccessToken') + ->willReturn($token) + ; + + $this->facebook->expects($this->once())->method('getApp') + ->willReturn($app) + ; + + $this->facebook->method('get') + ->with($this->equalTo('/0815/feed?fields=type,message,description,permalink_url,picture,created_time'), $this->equalTo($token)) + ->willThrowException(new FacebookSDKException()) + ; + + $block = new Block(); + + $blockContext = new BlockContext($block, [ + 'title' => null, + 'translation_domain' => null, + 'template' => '@Core23Facebook/Block/block_page_feed.html.twig', + 'id' => '0815', + 'fields' => 'type,message,description,permalink_url,picture,created_time', + ]); + + $blockService = new PageFeedBlockService('block.service', $this->templating, $this->facebook); + $blockService->execute($blockContext); + + $this->assertSame('@Core23Facebook/Block/block_page_feed.html.twig', $this->templating->view); + + $this->assertSame($blockContext, $this->templating->parameters['context']); + $this->assertInternalType('array', $this->templating->parameters['settings']); + $this->assertInstanceOf(BlockInterface::class, $this->templating->parameters['block']); + + $this->assertSame([], $this->templating->parameters['feed']); + } + public function testDefaultSettings(): void { $blockService = new PageFeedBlockService('block.service', $this->templating, $this->facebook); @@ -106,6 +147,22 @@ public function testDefaultSettings(): void ], $blockContext); } + public function testGetBlockMetadata(): void + { + $blockService = new PageFeedBlockService('block.service', $this->templating, $this->facebook); + + $metadata = $blockService->getBlockMetadata('description'); + + $this->assertSame('block.service', $metadata->getTitle()); + $this->assertSame('description', $metadata->getDescription()); + $this->assertNotNull($metadata->getImage()); + $this->assertStringStartsWith('data:image/png;base64,', $metadata->getImage() ?? ''); + $this->assertSame('Core23FacebookBundle', $metadata->getDomain()); + $this->assertSame([ + 'class' => 'fa fa-facebook-official', + ], $metadata->getOptions()); + } + public function testBuildEditForm(): void { $blockService = new PageFeedBlockService('block.service', $this->templating, $this->facebook); diff --git a/tests/DependencyInjection/Core23FacebookExtensionTest.php b/tests/DependencyInjection/Core23FacebookExtensionTest.php index 4454d15..23ca7ef 100644 --- a/tests/DependencyInjection/Core23FacebookExtensionTest.php +++ b/tests/DependencyInjection/Core23FacebookExtensionTest.php @@ -11,7 +11,15 @@ namespace Core23\FacebookBundle\Tests\DependencyInjection; +use Core23\FacebookBundle\Action\AuthErrorAction; +use Core23\FacebookBundle\Action\AuthSuccessAction; +use Core23\FacebookBundle\Action\CheckAuthAction; +use Core23\FacebookBundle\Action\StartAuthAction; +use Core23\FacebookBundle\Block\Service\PageFeedBlockService; +use Core23\FacebookBundle\Connection\FacebookConnection; use Core23\FacebookBundle\DependencyInjection\Core23FacebookExtension; +use Core23\FacebookBundle\Session\SessionManager; +use Core23\FacebookBundle\Session\SessionManagerInterface; use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractExtensionTestCase; final class Core23FacebookExtensionTest extends AbstractExtensionTestCase @@ -29,6 +37,29 @@ public function testLoadDefault(): void $this->assertContainerBuilderHasParameter('core23_facebook.api.app_id', 'foo_id'); $this->assertContainerBuilderHasParameter('core23_facebook.api.app_secret', 'bar_secret'); $this->assertContainerBuilderHasParameter('core23_facebook.api.permissions', ['public_profile', 'user_likes']); + + $this->assertContainerBuilderHasService(StartAuthAction::class, StartAuthAction::class); + $this->assertContainerBuilderHasService(AuthErrorAction::class, AuthErrorAction::class); + $this->assertContainerBuilderHasService(AuthSuccessAction::class, AuthSuccessAction::class); + $this->assertContainerBuilderHasService(CheckAuthAction::class, CheckAuthAction::class); + + $this->assertContainerBuilderHasAlias(SessionManagerInterface::class, 'core23_facebook.session.manager'); + $this->assertContainerBuilderHasAlias(FacebookConnection::class, 'core23_facebook.connection'); + $this->assertContainerBuilderHasService('core23_facebook.session.manager', SessionManager::class); + $this->assertContainerBuilderHasService('core23_facebook.connection', FacebookConnection::class); + } + + public function testLoadWithBlockBundle(): void + { + $this->setParameter('kernel.bundles', ['SonataBlockBundle' => true]); + $this->load([ + 'api' => [ + 'app_id' => 'foo_id', + 'app_secret' => 'bar_secret', + ], + ]); + + $this->assertContainerBuilderHasService('core23_facebook.block.page_feed', PageFeedBlockService::class); } protected function getContainerExtensions() diff --git a/tests/Event/AuthFailedEventTest.php b/tests/Event/AuthFailedEventTest.php new file mode 100644 index 0000000..659fa7b --- /dev/null +++ b/tests/Event/AuthFailedEventTest.php @@ -0,0 +1,44 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Core23\FacebookBundle\Tests\Event; + +use Core23\FacebookBundle\Event\AuthFailedEvent; +use PHPUnit\Framework\TestCase; +use Prophecy\Prophecy\ObjectProphecy; +use Symfony\Component\EventDispatcher\Event; +use Symfony\Component\HttpFoundation\Response; + +class AuthFailedEventTest extends TestCase +{ + public function testCreation(): void + { + $event = new AuthFailedEvent(); + + $this->assertInstanceOf(Event::class, $event); + } + + public function testGetResponse(): void + { + $event = new AuthFailedEvent(); + + $this->assertNull($event->getResponse()); + } + + public function testSetResponse(): void + { + /** @var ObjectProphecy&Response $reponse */ + $reponse = $this->prophesize(Response::class); + + $event = new AuthFailedEvent(); + $event->setResponse($reponse->reveal()); + + $this->assertSame($reponse->reveal(), $event->getResponse()); + } +} diff --git a/tests/Event/AuthSuccessEventTest.php b/tests/Event/AuthSuccessEventTest.php new file mode 100644 index 0000000..6689ad2 --- /dev/null +++ b/tests/Event/AuthSuccessEventTest.php @@ -0,0 +1,76 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Core23\FacebookBundle\Tests\Event; + +use Core23\FacebookBundle\Event\AuthSuccessEvent; +use Core23\FacebookBundle\Session\SessionInterface; +use PHPUnit\Framework\MockObject\MockObject; +use PHPUnit\Framework\TestCase; +use Prophecy\Prophecy\ObjectProphecy; +use Symfony\Component\EventDispatcher\Event; +use Symfony\Component\HttpFoundation\Response; + +class AuthSuccessEventTest extends TestCase +{ + public function testCreation(): void + { + /** @var ObjectProphecy&SessionInterface $session */ + $session = $this->prophesize(SessionInterface::class); + + $event = new AuthSuccessEvent($session->reveal()); + + $this->assertInstanceOf(Event::class, $event); + } + + public function testGetUsername(): void + { + /** @var ObjectProphecy&SessionInterface $session */ + $session = $this->prophesize(SessionInterface::class); + $session->getName()->willReturn('MyUser'); + + $event = new AuthSuccessEvent($session->reveal()); + + $this->assertSame('MyUser', $event->getUsername()); + } + + public function testGetSession(): void + { + /** @var ObjectProphecy&SessionInterface $session */ + $session = $this->prophesize(SessionInterface::class); + + $event = new AuthSuccessEvent($session->reveal()); + + $this->assertSame($session->reveal(), $event->getSession()); + } + + public function testGetResponse(): void + { + /** @var ObjectProphecy&SessionInterface $session */ + $session = $this->prophesize(SessionInterface::class); + + $event = new AuthSuccessEvent($session->reveal()); + + $this->assertNull($event->getResponse()); + } + + public function testSetResponse(): void + { + /** @var ObjectProphecy&SessionInterface $session */ + $session = $this->prophesize(SessionInterface::class); + + /** @var MockObject&Response $session */ + $reponse = $this->prophesize(Response::class); + + $event = new AuthSuccessEvent($session->reveal()); + $event->setResponse($reponse->reveal()); + + $this->assertSame($reponse->reveal(), $event->getResponse()); + } +} diff --git a/tests/Session/SessionManagerTest.php b/tests/Session/SessionManagerTest.php new file mode 100644 index 0000000..4c50494 --- /dev/null +++ b/tests/Session/SessionManagerTest.php @@ -0,0 +1,148 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Core23\FacebookBundle\Tests\Session; + +use Core23\FacebookBundle\Session\Session as FacebookSession; +use Core23\FacebookBundle\Session\SessionInterface; +use Core23\FacebookBundle\Session\SessionManager; +use DateTime; +use PHPUnit\Framework\TestCase; +use Prophecy\Argument; +use Prophecy\Prophecy\ObjectProphecy; +use Symfony\Component\HttpFoundation\Session\Session; + +class SessionManagerTest extends TestCase +{ + public function testIsAuthenticated(): void + { + /** @var ObjectProphecy&Session $session */ + $session = $this->prophesize(Session::class); + $session->get('_CORE23_FACEBOOK_TOKEN') + ->willReturn(true) + ; + + $manager = new SessionManager($session->reveal()); + $this->assertTrue($manager->isAuthenticated()); + } + + public function testIsNotAuthenticated(): void + { + /** @var ObjectProphecy&Session $session */ + $session = $this->prophesize(Session::class); + $session->get('_CORE23_FACEBOOK_TOKEN') + ->willReturn(false) + ; + + $manager = new SessionManager($session->reveal()); + $this->assertFalse($manager->isAuthenticated()); + } + + public function testGetUsername(): void + { + /** @var ObjectProphecy&Session $session */ + $session = $this->prophesize(Session::class); + $session->get('_CORE23_FACEBOOK_NAME') + ->willReturn('MyUser') + ; + + $manager = new SessionManager($session->reveal()); + $this->assertSame('MyUser', $manager->getUsername()); + } + + public function testGetUsernameNotExist(): void + { + /** @var ObjectProphecy&Session $session */ + $session = $this->prophesize(Session::class); + $session->get('_CORE23_FACEBOOK_NAME') + ->willReturn(null) + ; + + $manager = new SessionManager($session->reveal()); + $this->assertNull($manager->getUsername()); + } + + public function testStore(): void + { + $facebookSession = new FacebookSession('4711', 'YourName', 'YourToken', new DateTime()); + + /** @var ObjectProphecy&Session $session */ + $session = $this->prophesize(Session::class); + $session->set('_CORE23_FACEBOOK_ID', '4711')->shouldBeCalled(); + $session->set('_CORE23_FACEBOOK_NAME', 'YourName')->shouldBeCalled(); + $session->set('_CORE23_FACEBOOK_TOKEN', 'YourToken')->shouldBeCalled(); + $session->set('_CORE23_FACEBOOK_EXPIRES', Argument::type(DateTime::class))->shouldBeCalled(); + + $manager = new SessionManager($session->reveal()); + $manager->store($facebookSession); + + $this->assertTrue(true); + } + + public function testStoreWithNoExpiryDate(): void + { + $facebookSession = new FacebookSession('4711', 'YourName', 'YourToken', null); + + /** @var ObjectProphecy&Session $session */ + $session = $this->prophesize(Session::class); + $session->set('_CORE23_FACEBOOK_ID', '4711')->shouldBeCalled(); + $session->set('_CORE23_FACEBOOK_NAME', 'YourName')->shouldBeCalled(); + $session->set('_CORE23_FACEBOOK_TOKEN', 'YourToken')->shouldBeCalled(); + $session->set('_CORE23_FACEBOOK_EXPIRES', Argument::is(null))->shouldBeCalled(); + + $manager = new SessionManager($session->reveal()); + $manager->store($facebookSession); + + $this->assertTrue(true); + } + + public function testClear(): void + { + /** @var ObjectProphecy&Session $session */ + $session = $this->prophesize(Session::class); + $session->remove('_CORE23_FACEBOOK_ID')->shouldBeCalled(); + $session->remove('_CORE23_FACEBOOK_TOKEN')->shouldBeCalled(); + $session->remove('_CORE23_FACEBOOK_NAME')->shouldBeCalled(); + $session->remove('_CORE23_FACEBOOK_EXPIRES')->shouldBeCalled(); + + $manager = new SessionManager($session->reveal()); + $manager->clear(); + } + + public function testGetSession(): void + { + $tomorrow = new DateTime('tomorrow'); + + /** @var ObjectProphecy&Session $session */ + $session = $this->prophesize(Session::class); + $session->get('_CORE23_FACEBOOK_ID') + ->willReturn('0815') + ; + $session->get('_CORE23_FACEBOOK_TOKEN') + ->willReturn('TheToken') + ; + $session->get('_CORE23_FACEBOOK_NAME') + ->willReturn('MyUser') + ; + $session->get('_CORE23_FACEBOOK_EXPIRES') + ->willReturn($tomorrow) + ; + + $manager = new SessionManager($session->reveal()); + + /** @var SessionInterface $facebookSession */ + $facebookSession = $manager->getSession(); + + $this->assertNotNull($facebookSession); + $this->assertSame('0815', $facebookSession->getFacebookId()); + $this->assertSame('MyUser', $facebookSession->getName()); + $this->assertSame('TheToken', $facebookSession->getToken()); + $this->assertSame($tomorrow, $facebookSession->getExpireDate()); + } +}