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

Commit

Permalink
[TEST] Increased test converage
Browse files Browse the repository at this point in the history
  • Loading branch information
core23 committed Mar 23, 2019
1 parent 6fb3382 commit 5fbcdf2
Show file tree
Hide file tree
Showing 6 changed files with 443 additions and 6 deletions.
11 changes: 5 additions & 6 deletions src/Action/AuthSuccessAction.php
Expand Up @@ -13,7 +13,6 @@

use Core23\FacebookBundle\Core23FacebookEvents;
use Core23\FacebookBundle\Event\AuthSuccessEvent;
use Core23\FacebookBundle\Session\SessionManager;
use Core23\FacebookBundle\Session\SessionManagerInterface;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
Expand Down Expand Up @@ -59,16 +58,16 @@ public function __construct(
SessionManagerInterface $sessionManager,
EventDispatcherInterface $eventDispatcher
) {
$this->twig = $twig;
$this->router = $router;
$this->sessionManager = $sessionManager;
$this->eventDispatcher = $eventDispatcher;
$this->twig = $twig;
$this->router = $router;
$this->sessionManager = $sessionManager;
$this->eventDispatcher = $eventDispatcher;
}

/**
* @throws LoaderError
* @throws RuntimeError
* @throws SyntaxError
* @throws LoaderError
*
* @return Response
*/
Expand Down
90 changes: 90 additions & 0 deletions tests/Action/AuthErrorActionTest.php
@@ -0,0 +1,90 @@
<?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\Action;

use Core23\FacebookBundle\Action\AuthErrorAction;
use Core23\FacebookBundle\Core23FacebookEvents;
use Core23\FacebookBundle\Event\AuthFailedEvent;
use Core23\FacebookBundle\Session\SessionManagerInterface;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\RouterInterface;
use Twig\Environment;

class AuthErrorActionTest extends TestCase
{
private $twig;

private $router;

private $sessionManager;

private $eventDispatcher;

protected function setUp(): void
{
$this->twig = $this->prophesize(Environment::class);
$this->router = $this->prophesize(RouterInterface::class);
$this->sessionManager = $this->prophesize(SessionManagerInterface::class);
$this->eventDispatcher = $this->prophesize(EventDispatcherInterface::class);
}

public function testExecute(): void
{
$this->sessionManager->isAuthenticated()
->willReturn(false)
;

$this->sessionManager->clear()
->shouldBeCalled()
;

$this->eventDispatcher->dispatch(Core23FacebookEvents::AUTH_ERROR, Argument::type(AuthFailedEvent::class))
->willReturn(null)
->shouldBeCalled()
;

$action = new AuthErrorAction(
$this->twig->reveal(),
$this->router->reveal(),
$this->sessionManager->reveal(),
$this->eventDispatcher->reveal()
);

$response = $action();

$this->assertNotInstanceOf(RedirectResponse::class, $response);
$this->assertSame(200, $response->getStatusCode());
}

public function testExecuteWithNoAuth(): void
{
$this->sessionManager->isAuthenticated()
->willReturn(true)
;

$this->router->generate('core23_facebook_success', [], UrlGeneratorInterface::ABSOLUTE_PATH)
->willReturn('/success')
->shouldBeCalled()
;

$action = new AuthErrorAction(
$this->twig->reveal(),
$this->router->reveal(),
$this->sessionManager->reveal(),
$this->eventDispatcher->reveal()
);

$this->assertInstanceOf(RedirectResponse::class, $action());
}
}
122 changes: 122 additions & 0 deletions tests/Action/AuthSuccessActionTest.php
@@ -0,0 +1,122 @@
<?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\Action;

use Core23\FacebookBundle\Action\AuthSuccessAction;
use Core23\FacebookBundle\Core23FacebookEvents;
use Core23\FacebookBundle\Event\AuthSuccessEvent;
use Core23\FacebookBundle\Session\SessionInterface;
use Core23\FacebookBundle\Session\SessionManagerInterface;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Symfony\Component\Routing\RouterInterface;
use Twig\Environment;

class AuthSuccessActionTest extends TestCase
{
private $twig;

private $router;

private $sessionManager;

private $eventDispatcher;

protected function setUp(): void
{
$this->twig = $this->prophesize(Environment::class);
$this->router = $this->prophesize(RouterInterface::class);
$this->sessionManager = $this->prophesize(SessionManagerInterface::class);
$this->eventDispatcher = $this->prophesize(EventDispatcherInterface::class);
}

public function testExecute(): void
{
$session = $this->prophesize(SessionInterface::class);

$this->sessionManager->isAuthenticated()
->willReturn(true)
;
$this->sessionManager->getSession()
->willReturn($session)
;
$this->sessionManager->getUsername()
->willReturn('FooUser')
;

$this->eventDispatcher->dispatch(Core23FacebookEvents::AUTH_SUCCESS, Argument::type(AuthSuccessEvent::class))
->shouldBeCalled()
;

$this->twig->render('@Core23Facebook/Auth/success.html.twig', [
'name' => 'FooUser',
])->shouldBeCalled();

$action = new AuthSuccessAction(
$this->twig->reveal(),
$this->router->reveal(),
$this->sessionManager->reveal(),
$this->eventDispatcher->reveal()
);

$response = $action();

$this->assertNotInstanceOf(RedirectResponse::class, $response);
$this->assertSame(200, $response->getStatusCode());
}

public function testExecuteNoAuth(): void
{
$this->sessionManager->isAuthenticated()
->willReturn(false)
;

$this->router->generate('core23_facebook_error', [], UrlGeneratorInterface::ABSOLUTE_PATH)
->willReturn('/success')
->shouldBeCalled()
;

$action = new AuthSuccessAction(
$this->twig->reveal(),
$this->router->reveal(),
$this->sessionManager->reveal(),
$this->eventDispatcher->reveal()
);

$this->assertInstanceOf(RedirectResponse::class, $action());
}

public function testExecuteNoSession(): void
{
$this->sessionManager->isAuthenticated()
->willReturn(true)
;
$this->sessionManager->getSession()
->willReturn(null)
;

$this->router->generate('core23_facebook_error', [], UrlGeneratorInterface::ABSOLUTE_PATH)
->willReturn('/success')
->shouldBeCalled()
;

$action = new AuthSuccessAction(
$this->twig->reveal(),
$this->router->reveal(),
$this->sessionManager->reveal(),
$this->eventDispatcher->reveal()
);

$this->assertInstanceOf(RedirectResponse::class, $action());
}
}

0 comments on commit 5fbcdf2

Please sign in to comment.