Skip to content

Commit

Permalink
added tests for provider
Browse files Browse the repository at this point in the history
  • Loading branch information
wachterjohannes committed Sep 14, 2018
1 parent a533921 commit 455ef59
Show file tree
Hide file tree
Showing 6 changed files with 310 additions and 4 deletions.
1 change: 0 additions & 1 deletion Controller/RegistrationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
namespace Sulu\Bundle\CommunityBundle\Controller;

use Sulu\Bundle\CommunityBundle\DependencyInjection\Configuration;
use Sulu\Bundle\SecurityBundle\Entity\User;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

Expand Down
2 changes: 1 addition & 1 deletion Entity/UserAccessToken.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class UserAccessToken
/**
* @var string
*/
private $accessToken;
private $accessToken = '';

public function __construct(UserInterface $user, string $service, string $identifier)
{
Expand Down
3 changes: 2 additions & 1 deletion Tests/Unit/Controller/SaveMediaTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@

namespace Sulu\Bundle\CommunityBundle\Tests\Unit\Controller;

use PHPUnit\Framework\TestCase;
use Sulu\Bundle\CommunityBundle\Controller\SaveMediaTrait;
use Sulu\Bundle\ContactBundle\Entity\Contact;
use Sulu\Bundle\MediaBundle\Api\Media as ApiMedia;
Expand All @@ -21,7 +22,7 @@
use Symfony\Component\Form\FormInterface;
use Symfony\Component\HttpFoundation\File\UploadedFile;

class SaveMediaTraitTest extends \PHPUnit_Framework_TestCase
class SaveMediaTraitTest extends TestCase
{
use SaveMediaTrait {
getMediaManager as mockedGetMediaManager;
Expand Down
60 changes: 60 additions & 0 deletions Tests/Unit/Entity/UserAccessTokenTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

/*
* This file is part of Sulu.
*
* (c) MASSIVE ART WebServices GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Tests\Unit\Entity;

use PHPUnit\Framework\TestCase;
use Sulu\Bundle\CommunityBundle\Entity\UserAccessToken;
use Sulu\Component\Security\Authentication\UserInterface;

class UserAccessTokenTest extends TestCase
{
public function testGetUser()
{
$user = $this->prophesize(UserInterface::class);
$entity = new UserAccessToken($user->reveal(), 'facebook', '123-123-123');

$this->assertEquals($user->reveal(), $entity->getUser());
}

public function testGetService()
{
$user = $this->prophesize(UserInterface::class);
$entity = new UserAccessToken($user->reveal(), 'facebook', '123-123-123');

$this->assertEquals('facebook', $entity->getService());
}

public function testGetIdentifier()
{
$user = $this->prophesize(UserInterface::class);
$entity = new UserAccessToken($user->reveal(), 'facebook', '123-123-123');

$this->assertEquals('123-123-123', $entity->getService());
}

public function testGetAccessToken()
{
$user = $this->prophesize(UserInterface::class);
$entity = new UserAccessToken($user->reveal(), 'facebook', '123-123-123');

$this->assertEquals('', $entity->getAccessToken());
}

public function testSetAccessToken()
{
$user = $this->prophesize(UserInterface::class);
$entity = new UserAccessToken($user->reveal(), 'facebook', '123-123-123');

$this->assertEquals($entity, $entity->setAccessToken('my-token'));
$this->assertEquals('my-token', $entity->getAccessToken());
}
}
3 changes: 2 additions & 1 deletion Tests/Unit/Manager/CommunityManagerRegistryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@

namespace Sulu\Bundle\CommunityBundle\Tests\Unit\Manager;

use PHPUnit\Framework\TestCase;
use Sulu\Bundle\CommunityBundle\Manager\CommunityManager;
use Sulu\Bundle\CommunityBundle\Manager\CommunityManagerRegistry;

class CommunityManagerRegistryTest extends \PHPUnit_Framework_TestCase
class CommunityManagerRegistryTest extends TestCase
{
public function testGet()
{
Expand Down
245 changes: 245 additions & 0 deletions Tests/Unit/SocialMedia/SocialMediaUserProviderTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,245 @@
<?php

/*
* This file is part of Sulu.
*
* (c) MASSIVE ART WebServices GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Tests\Unit\SocialMedia;

use Doctrine\Bundle\DoctrineBundle\Registry;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\Mapping\ClassMetadata;
use HWI\Bundle\OAuthBundle\OAuth\ResourceOwnerInterface;
use HWI\Bundle\OAuthBundle\OAuth\Response\UserResponseInterface;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Sulu\Bundle\CommunityBundle\Entity\UserAccessToken;
use Sulu\Bundle\CommunityBundle\Entity\UserAccessTokenRepository;
use Sulu\Bundle\CommunityBundle\Manager\CommunityManagerInterface;
use Sulu\Bundle\CommunityBundle\Manager\CommunityManagerRegistryInterface;
use Sulu\Bundle\CommunityBundle\SocialMedia\SocialMediaUserProvider;
use Sulu\Bundle\ContactBundle\Entity\Contact;
use Sulu\Bundle\ContactBundle\Entity\ContactInterface;
use Sulu\Bundle\ContactBundle\Entity\ContactRepositoryInterface;
use Sulu\Bundle\SecurityBundle\Entity\User;
use Sulu\Component\Security\Authentication\UserInterface;
use Sulu\Component\Security\Authentication\UserRepositoryInterface;
use Sulu\Component\Webspace\Analyzer\Attributes\RequestAttributes;
use Sulu\Component\Webspace\Webspace;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;

class SocialMediaUserProviderTest extends TestCase
{
/**
* @var CommunityManagerRegistryInterface
*/
private $communityManagerRegistry;

/**
* @var RequestStack
*/
private $requestStack;

/**
* @var Registry
*/
private $registry;

/**
* @var EntityManagerInterface
*/
private $entityManager;

/**
* @var UserRepositoryInterface
*/
private $userRepository;

/**
* @var ContactRepositoryInterface
*/
private $contactRepository;

/**
* @var UserAccessTokenRepository
*/
private $userAccessTokenRepository;

protected function setUp()
{
$this->communityManagerRegistry = $this->prophesize(CommunityManagerRegistryInterface::class);
$this->requestStack = $this->prophesize(RequestStack::class);
$this->registry = $this->prophesize(Registry::class);

$this->entityManager = $this->prophesize(EntityManagerInterface::class);
$userMetadata = $this->prophesize(ClassMetadata::class);
$userMetadata->getName()->willReturn(User::class);
$contactMetadata = $this->prophesize(ClassMetadata::class);
$contactMetadata->getName()->willReturn(Contact::class);
$this->entityManager->getClassMetadata(UserInterface::class)->willReturn($userMetadata->reveal());
$this->entityManager->getClassMetadata(ContactInterface::class)->willReturn($contactMetadata->reveal());

$this->userRepository = $this->prophesize(UserRepositoryInterface::class);
$this->contactRepository = $this->prophesize(ContactRepositoryInterface::class);
$this->userAccessTokenRepository = $this->prophesize(UserAccessTokenRepository::class);

$this->entityManager->getRepository(User::class)->willReturn($this->userRepository->reveal());
$this->entityManager->getRepository(Contact::class)->willReturn($this->contactRepository->reveal());
$this->entityManager->getRepository(UserAccessToken::class)->willReturn($this->userAccessTokenRepository->reveal());
}

public function testConnect()
{
$provider = new SocialMediaUserProvider(
$this->communityManagerRegistry->reveal(),
$this->requestStack->reveal(),
$this->registry->reveal()
);

$user = $this->prophesize(UserInterface::class);
$response = $this->prophesize(UserResponseInterface::class);
$resourceOwner = $this->prophesize(ResourceOwnerInterface::class);
$resourceOwner->getName()->willReturn('facebook');
$response->getResourceOwner()->willReturn($resourceOwner->reveal());
$response->getUsername()->willReturn('test');
$response->getAccessToken()->willReturn('123-123-123');

$this->userAccessTokenRepository->findByIdentifier('facebook', 'test')->willReturn(null);

$accessToken = $this->prophesize(UserAccessToken::class);
$accessToken->setAccessToken('123-123-123')->willReturn($accessToken)->shouldBeCalled();

$this->entityManager->flush()->shouldBeCalled();

$provider->connect($user->reveal(), $response->reveal());
}

public function testConnectRemoveExisting()
{
$provider = new SocialMediaUserProvider(
$this->communityManagerRegistry->reveal(),
$this->requestStack->reveal(),
$this->registry->reveal()
);

$user = $this->prophesize(UserInterface::class);
$response = $this->prophesize(UserResponseInterface::class);
$resourceOwner = $this->prophesize(ResourceOwnerInterface::class);
$resourceOwner->getName()->willReturn('facebook');
$response->getResourceOwner()->willReturn($resourceOwner->reveal());
$response->getUsername()->willReturn('test');
$response->getAccessToken()->willReturn('123-123-123');

$oldUserAccessToken = $this->prophesize(UserAccessToken::class);
$this->userAccessTokenRepository->findByIdentifier('facebook', 'test')
->willReturn($oldUserAccessToken->reveal());

$this->entityManager->remove($oldUserAccessToken->reveal());
$this->entityManager->flush()->shouldBeCalled();

$accessToken = $this->prophesize(UserAccessToken::class);
$accessToken->setAccessToken('123-123-123')->willReturn($accessToken->reveal())->shouldBeCalled();

$provider->connect($user->reveal(), $response->reveal());
}

public function testLoadUserByOAuthUserResponseExistingUser()
{
$provider = new SocialMediaUserProvider(
$this->communityManagerRegistry->reveal(),
$this->requestStack->reveal(),
$this->registry->reveal()
);

$response = $this->prophesize(UserResponseInterface::class);
$resourceOwner = $this->prophesize(ResourceOwnerInterface::class);
$resourceOwner->getName()->willReturn('facebook');
$response->getResourceOwner()->willReturn($resourceOwner->reveal());
$response->getUsername()->willReturn('test');
$response->getFirstName()->willReturn('Max');
$response->getLastName()->willReturn('Mustermann');
$response->getEmail()->willReturn(null);
$response->getAccessToken()->willReturn('123-123-123');

$accessToken = $this->prophesize(UserAccessToken::class);
$this->userAccessTokenRepository->findByIdentifier('facebook', 'test')
->willReturn($accessToken->reveal());
$accessToken->setAccessToken('123-123-123')->willReturn($accessToken->reveal())->shouldBeCalled();

$user = $this->prophesize(User::class);
$contact = $this->prophesize(ContactInterface::class);
$user->getContact()->willReturn($contact->reveal());

$contact->setFirstName('Max')->shouldBeCalled();
$contact->setLastName('Mustermann')->shouldBeCalled();

$this->entityManager->flush()->shouldBeCalled();

$accessToken->getUser()->willReturn($user->reveal());

$result = $provider->loadUserByOAuthUserResponse($response->reveal());
$this->assertEquals($user->reveal(), $result);
}

public function testLoadUserByOAuthUserResponse()
{
$provider = new SocialMediaUserProvider(
$this->communityManagerRegistry->reveal(),
$this->requestStack->reveal(),
$this->registry->reveal()
);

$response = $this->prophesize(UserResponseInterface::class);
$resourceOwner = $this->prophesize(ResourceOwnerInterface::class);
$resourceOwner->getName()->willReturn('facebook');
$response->getResourceOwner()->willReturn($resourceOwner->reveal());
$response->getUsername()->willReturn('test');
$response->getFirstName()->willReturn('Max');
$response->getLastName()->willReturn('Mustermann');
$response->getEmail()->willReturn('[email protected]');
$response->getAccessToken()->willReturn('123-123-123');

$this->userAccessTokenRepository->findByIdentifier('facebook', 'test')->willReturn(null);

$accessToken = $this->prophesize(UserAccessToken::class);
$accessToken->setAccessToken('123-123-123')->willReturn($accessToken->reveal())->shouldBeCalled();

$user = $this->prophesize(User::class);
$this->userRepository->createNew()->willReturn($user->reveal());

$contact = $this->prophesize(ContactInterface::class);
$this->contactRepository->createNew()->willReturn($contact->reveal());
$contact->setFirstName('Max')->shouldBeCalled();
$contact->setLastName('Mustermann')->shouldBeCalled();

$user->setContact($contact->reveal())->shouldBeCalled();
$user->setUsername(Argument::type('string'))->shouldBeCalled();
$user->setEmail('[email protected]')->shouldBeCalled();
$user->setSalt(Argument::type('string'))->shouldBeCalled();
$user->setPassword(Argument::type('string'))->shouldBeCalled();

$request = new Request();
$requestAttributes = $this->prophesize(RequestAttributes::class);
$request->attributes->set('_sulu', $requestAttributes->reveal());

$webspace = $this->prophesize(Webspace::class);
$webspace->getKey()->willReturn('sulu_io');
$requestAttributes->getAttribute('webspace')->willReturn($webspace->reveal());

$communityManager = $this->prophesize(CommunityManagerInterface::class);
$communityManager->register($user->reveal())->shouldBeCalled();
$this->communityManagerRegistry->get('sulu_io')->willReturn($communityManager->reveal());
$user->setEnabled(true)->shouldBeCalled();

$this->entityManager->flush()->shouldBeCalled();

$result = $provider->loadUserByOAuthUserResponse($response->reveal());
$this->assertEquals($user->reveal(), $result);
}
}

0 comments on commit 455ef59

Please sign in to comment.