Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Commit

Permalink
[BUILD] Added more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
core23 committed Mar 20, 2019
1 parent 3402fe2 commit ac880e7
Show file tree
Hide file tree
Showing 5 changed files with 356 additions and 0 deletions.
57 changes: 57 additions & 0 deletions tests/Block/Service/PageFeedBlockServiceTest.php
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
31 changes: 31 additions & 0 deletions tests/DependencyInjection/Core23FacebookExtensionTest.php
Expand Up @@ -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
Expand All @@ -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()
Expand Down
44 changes: 44 additions & 0 deletions tests/Event/AuthFailedEventTest.php
@@ -0,0 +1,44 @@
<?php

/*
* (c) Christian Gripp <mail@core23.de>
*
* 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());
}
}
76 changes: 76 additions & 0 deletions tests/Event/AuthSuccessEventTest.php
@@ -0,0 +1,76 @@
<?php

/*
* (c) Christian Gripp <mail@core23.de>
*
* 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());
}
}
148 changes: 148 additions & 0 deletions tests/Session/SessionManagerTest.php
@@ -0,0 +1,148 @@
<?php

/*
* (c) Christian Gripp <mail@core23.de>
*
* 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());
}
}

0 comments on commit ac880e7

Please sign in to comment.