Skip to content

Commit

Permalink
Merge pull request hwi#1576 from Nek-/fix/missing-roles-in-test-users
Browse files Browse the repository at this point in the history
Tend to green tests
  • Loading branch information
XWB authored Dec 6, 2019
2 parents 22efcf2 + 938911c commit d24891d
Show file tree
Hide file tree
Showing 6 changed files with 120 additions and 48 deletions.
32 changes: 23 additions & 9 deletions Controller/ConnectController.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
use Symfony\Bundle\FrameworkBundle\Controller\ControllerTrait;
use Symfony\Component\DependencyInjection\ContainerAwareInterface;
use Symfony\Component\DependencyInjection\ContainerAwareTrait;
use Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy;
use Symfony\Component\Form\Extension\Core\Type\FormType;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\Request;
Expand Down Expand Up @@ -114,7 +115,7 @@ public function registrationAction(Request $request, $key)
$formHandler = $this->container->get('hwi_oauth.registration.form.handler');
if ($formHandler->process($request, $form, $userInformation)) {
$event = new FormEvent($form, $request);
$this->get('event_dispatcher')->dispatch(HWIOAuthEvents::REGISTRATION_SUCCESS, $event);
$this->dispatch($event, HWIOAuthEvents::REGISTRATION_SUCCESS);

$this->container->get('hwi_oauth.account.connector')->connect($form->getData(), $userInformation);

Expand All @@ -132,7 +133,7 @@ public function registrationAction(Request $request, $key)
}

$event = new FilterUserResponseEvent($form->getData(), $request, $response);
$this->get('event_dispatcher')->dispatch(HWIOAuthEvents::REGISTRATION_COMPLETED, $event);
$this->dispatch($event, HWIOAuthEvents::REGISTRATION_COMPLETED);

return $response;
}
Expand All @@ -143,7 +144,7 @@ public function registrationAction(Request $request, $key)
}

$event = new GetResponseUserEvent($form->getData(), $request);
$this->get('event_dispatcher')->dispatch(HWIOAuthEvents::REGISTRATION_INITIALIZE, $event);
$this->dispatch($event, HWIOAuthEvents::REGISTRATION_INITIALIZE);

if ($response = $event->getResponse()) {
return $response;
Expand Down Expand Up @@ -231,7 +232,8 @@ public function connectServiceAction(Request $request, $service)
}

$event = new GetResponseUserEvent($this->getUser(), $request);
$this->get('event_dispatcher')->dispatch(HWIOAuthEvents::CONNECT_INITIALIZE, $event);

$this->dispatch($event, HWIOAuthEvents::CONNECT_INITIALIZE);

if ($response = $event->getResponse()) {
return $response;
Expand Down Expand Up @@ -299,9 +301,9 @@ private function authenticateUser(Request $request, UserInterface $user, $resour

if ($fakeLogin) {
// Since we're "faking" normal login, we need to throw our INTERACTIVE_LOGIN event manually
$this->container->get('event_dispatcher')->dispatch(
SecurityEvents::INTERACTIVE_LOGIN,
new InteractiveLoginEvent($request, $token)
$this->dispatch(
new InteractiveLoginEvent($request, $token),
SecurityEvents::INTERACTIVE_LOGIN
);
}
}
Expand Down Expand Up @@ -349,7 +351,7 @@ private function getConfirmationResponse(Request $request, array $accessToken, $
$userInformation = $resourceOwner->getUserInformation($accessToken);

$event = new GetResponseUserEvent($currentUser, $request);
$this->get('event_dispatcher')->dispatch(HWIOAuthEvents::CONNECT_CONFIRMED, $event);
$this->dispatch($event, HWIOAuthEvents::CONNECT_CONFIRMED);

$this->container->get('hwi_oauth.account.connector')->connect($currentUser, $userInformation);

Expand All @@ -375,8 +377,20 @@ private function getConfirmationResponse(Request $request, array $accessToken, $
}

$event = new FilterUserResponseEvent($currentUser, $request, $response);
$this->get('event_dispatcher')->dispatch(HWIOAuthEvents::CONNECT_COMPLETED, $event);
$this->dispatch($event, HWIOAuthEvents::CONNECT_COMPLETED);

return $response;
}

private function dispatch($event, string $eventName = null)
{
// LegacyEventDispatcherProxy exists in Symfony >= 4.3
if (class_exists(LegacyEventDispatcherProxy::class)) {
// New Symfony 4.3 EventDispatcher signature
$this->get('event_dispatcher')->dispatch($event, $eventName);
} else {
// Old EventDispatcher signature
$this->get('event_dispatcher')->dispatch($eventName, $event);
}
}
}
1 change: 1 addition & 0 deletions Tests/App/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -110,5 +110,6 @@ fos_user:

twig:
strict_variables: '%kernel.debug%'
exception_controller: ~
paths:
'%kernel.project_dir%/templates': 'Acme'
68 changes: 48 additions & 20 deletions Tests/Controller/ConnectControllerConnectServiceActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,11 @@

namespace HWI\Bundle\OAuthBundle\Tests\Controller;

use HWI\Bundle\OAuthBundle\Event\FilterUserResponseEvent;
use HWI\Bundle\OAuthBundle\Event\GetResponseUserEvent;
use HWI\Bundle\OAuthBundle\HWIOAuthEvents;
use HWI\Bundle\OAuthBundle\Tests\Fixtures\CustomOAuthToken;
use Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy;
use Symfony\Component\Form\FormInterface;

class ConnectControllerConnectServiceActionTest extends AbstractConnectControllerTest
Expand Down Expand Up @@ -73,10 +76,16 @@ public function testConnectConfirm()
;

$this->eventDispatcher->expects($this->once())->method('dispatch');
$this->eventDispatcher->expects($this->at(0))
->method('dispatch')
->with(HWIOAuthEvents::CONNECT_INITIALIZE)
;

if (class_exists(LegacyEventDispatcherProxy::class)) {
$this->eventDispatcher->expects($this->at(0))
->method('dispatch')
->with($this->isInstanceOf(GetResponseUserEvent::class), HWIOAuthEvents::CONNECT_INITIALIZE);
} else {
$this->eventDispatcher->expects($this->at(0))
->method('dispatch')
->with(HWIOAuthEvents::CONNECT_INITIALIZE);
}

$this->twig->expects($this->once())
->method('render')
Expand Down Expand Up @@ -116,14 +125,21 @@ public function testConnectSuccess()
;

$this->eventDispatcher->expects($this->exactly(2))->method('dispatch');
$this->eventDispatcher->expects($this->at(0))
->method('dispatch')
->with(HWIOAuthEvents::CONNECT_CONFIRMED)
;
$this->eventDispatcher->expects($this->at(1))
->method('dispatch')
->with(HWIOAuthEvents::CONNECT_COMPLETED)
;
if (class_exists(LegacyEventDispatcherProxy::class)) {
$this->eventDispatcher->expects($this->at(0))
->method('dispatch')
->with($this->isInstanceOf(GetResponseUserEvent::class), HWIOAuthEvents::CONNECT_CONFIRMED);
$this->eventDispatcher->expects($this->at(1))
->method('dispatch')
->with($this->isInstanceOf(FilterUserResponseEvent::class), HWIOAuthEvents::CONNECT_COMPLETED);
} else {
$this->eventDispatcher->expects($this->at(0))
->method('dispatch')
->with(HWIOAuthEvents::CONNECT_CONFIRMED);
$this->eventDispatcher->expects($this->at(1))
->method('dispatch')
->with(HWIOAuthEvents::CONNECT_COMPLETED);
}

$this->twig->expects($this->once())
->method('render')
Expand Down Expand Up @@ -154,14 +170,26 @@ public function testConnectNoConfirmation()
;

$this->eventDispatcher->expects($this->exactly(2))->method('dispatch');
$this->eventDispatcher->expects($this->at(0))
->method('dispatch')
->with(HWIOAuthEvents::CONNECT_CONFIRMED)
;
$this->eventDispatcher->expects($this->at(1))
->method('dispatch')
->with(HWIOAuthEvents::CONNECT_COMPLETED)
;

if (class_exists(LegacyEventDispatcherProxy::class)) {
$this->eventDispatcher->expects($this->at(0))
->method('dispatch')
->with($this->isInstanceOf(GetResponseUserEvent::class), HWIOAuthEvents::CONNECT_CONFIRMED)
;
$this->eventDispatcher->expects($this->at(1))
->method('dispatch')
->with($this->isInstanceOf(FilterUserResponseEvent::class), HWIOAuthEvents::CONNECT_COMPLETED)
;
} else {
$this->eventDispatcher->expects($this->at(0))
->method('dispatch')
->with(HWIOAuthEvents::CONNECT_CONFIRMED)
;
$this->eventDispatcher->expects($this->at(1))
->method('dispatch')
->with(HWIOAuthEvents::CONNECT_COMPLETED)
;
}

$this->twig->expects($this->once())
->method('render')
Expand Down
63 changes: 46 additions & 17 deletions Tests/Controller/ConnectControllerRegistrationActionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,15 @@
namespace HWI\Bundle\OAuthBundle\Tests\Controller;

use FOS\UserBundle\Form\Factory\FactoryInterface;
use HWI\Bundle\OAuthBundle\Event\FilterUserResponseEvent;
use HWI\Bundle\OAuthBundle\Event\FormEvent;
use HWI\Bundle\OAuthBundle\Event\GetResponseUserEvent;
use HWI\Bundle\OAuthBundle\Form\RegistrationFormHandlerInterface;
use HWI\Bundle\OAuthBundle\HWIOAuthEvents;
use HWI\Bundle\OAuthBundle\Tests\Fixtures\User;
use Symfony\Component\EventDispatcher\LegacyEventDispatcherProxy;
use Symfony\Component\Form\Form;
use Symfony\Component\Security\Http\Event\InteractiveLoginEvent;
use Symfony\Component\Security\Http\SecurityEvents;

class ConnectControllerRegistrationActionTest extends AbstractConnectControllerTest
Expand Down Expand Up @@ -90,10 +95,16 @@ public function testFailedProcess()
$this->container->set('hwi_oauth.registration.form.handler', $registrationFormHandler);

$this->eventDispatcher->expects($this->once())->method('dispatch');
$this->eventDispatcher->expects($this->at(0))
->method('dispatch')
->with(HWIOAuthEvents::REGISTRATION_INITIALIZE)
;

if (class_exists(LegacyEventDispatcherProxy::class)) {
$this->eventDispatcher->expects($this->at(0))
->method('dispatch')
->with($this->isInstanceOf(GetResponseUserEvent::class), HWIOAuthEvents::REGISTRATION_INITIALIZE);
} else {
$this->eventDispatcher->expects($this->at(0))
->method('dispatch')
->with(HWIOAuthEvents::REGISTRATION_INITIALIZE);
}

$this->twig->expects($this->once())
->method('render')
Expand Down Expand Up @@ -130,20 +141,38 @@ public function test()
;

$this->eventDispatcher->expects($this->exactly(3))->method('dispatch');
$this->eventDispatcher->expects($this->at(0))
->method('dispatch')
->with(HWIOAuthEvents::REGISTRATION_SUCCESS)
;

$this->eventDispatcher->expects($this->at(1))
->method('dispatch')
->with(SecurityEvents::INTERACTIVE_LOGIN)
;

$this->eventDispatcher->expects($this->at(2))
->method('dispatch')
->with(HWIOAuthEvents::REGISTRATION_COMPLETED)
;
if (class_exists(LegacyEventDispatcherProxy::class)) {
$this->eventDispatcher->expects($this->at(0))
->method('dispatch')
->with($this->isInstanceOf(FormEvent::class), HWIOAuthEvents::REGISTRATION_SUCCESS)
;

$this->eventDispatcher->expects($this->at(1))
->method('dispatch')
->with($this->isInstanceOf(InteractiveLoginEvent::class), SecurityEvents::INTERACTIVE_LOGIN)
;

$this->eventDispatcher->expects($this->at(2))
->method('dispatch')
->with($this->isInstanceOf(FilterUserResponseEvent::class), HWIOAuthEvents::REGISTRATION_COMPLETED)
;
} else {
$this->eventDispatcher->expects($this->at(0))
->method('dispatch')
->with(HWIOAuthEvents::REGISTRATION_SUCCESS)
;

$this->eventDispatcher->expects($this->at(1))
->method('dispatch')
->with(SecurityEvents::INTERACTIVE_LOGIN)
;

$this->eventDispatcher->expects($this->at(2))
->method('dispatch')
->with(HWIOAuthEvents::REGISTRATION_COMPLETED)
;
}

$this->twig->expects($this->once())
->method('render')
Expand Down
2 changes: 1 addition & 1 deletion Tests/Fixtures/FOSUser.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public function getUsername()

public function getRoles()
{
return [];
return ['ROLE_USER'];
}

public function getPassword()
Expand Down
2 changes: 1 addition & 1 deletion Tests/Fixtures/User.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public function getUsername()

public function getRoles()
{
return [];
return ['ROLE_USER'];
}

public function getPassword()
Expand Down

0 comments on commit d24891d

Please sign in to comment.