Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- namespace UserBundle\Tests\Manager;
- use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
- use Facebook\Facebook;
- use Facebook\Helpers\FacebookRedirectLoginHelper;
- use Facebook\PersistentData\FacebookSessionPersistentDataHandler;
- use Facebook\FacebookApp;
- use Facebook\FacebookRequest;
- use Facebook\FacebookResponse;
- use Facebook\Exceptions\FacebookResponseException;
- use Facebook\Exceptions\FacebookSDKException;
- use UserBundle\Manager\FacebookAuthManager;
- class FacebookAuthManagerTest extends KernelTestCase
- {
- private $facebookAuthManager;
- const FACEBOOK_RESPONSE_EXCEPTION = 'fb_response_exception';
- const FACEBOOK_SDK_EXCEPTION = 'fb_sdk_exception';
- public function setUp()
- {
- self::bootKernel();
- $this->facebookAuthManager = self::$kernel->getContainer()->get('user.manager.facebook_auth');
- $this->httpClient = self::$kernel->getContainer()->get('curl.manager.httpclient');
- }
- public function testConnect()
- {
- $url = $this->facebookAuthManager->connect();
- $url_check = 'https://www.facebook.com/v2.10/dialog/oauth?';
- $this->assertTrue((strpos($url, $url_check) !== false), 'Should return authentication URL');
- }
- public function testGetAccessToken()
- {
- $auth = $this->facebookAuthManager->getAccessToken('ABCDE');
- $this->assertFalse(isset($auth['accessToken']));
- $this->assertEquals($auth['accessToken'], null);
- $fbManager = $this->initFbException(self::FACEBOOK_RESPONSE_EXCEPTION);
- $setFacebookAuthManager = self::$kernel->getContainer()->set('user.manager.facebook_auth', $fbManager);
- $facebookAuthManager = self::$kernel->getContainer()->get('user.manager.facebook_auth');
- $result = $facebookAuthManager->getAccessToken('ABCDE');
- $fbManager = $this->initFbException(self::FACEBOOK_SDK_EXCEPTION);
- $setFacebookAuthManager = self::$kernel->getContainer()->set('user.manager.facebook_auth', $fbManager);
- $facebookAuthManager = self::$kernel->getContainer()->get('user.manager.facebook_auth');
- $result = $facebookAuthManager->getAccessToken('ABCDE');
- }
- public function testGetUser()
- {
- $auth = $this->facebookAuthManager->getUser('1234');
- $this->assertTrue(isset($auth['error_desc']), 'Should return error_desc');
- $fbManager = $this->initFbException(self::FACEBOOK_SDK_EXCEPTION);
- $setFacebookAuthManager = self::$kernel->getContainer()->set('user.manager.facebook_auth', $fbManager);
- $facebookAuthManager = self::$kernel->getContainer()->get('user.manager.facebook_auth');
- $result = $facebookAuthManager->getUser('1234');
- $fbManager = $this->initFbException(null, true);
- $setFacebookAuthManager = self::$kernel->getContainer()->set('user.manager.facebook_auth', $fbManager);
- $facebookAuthManager = self::$kernel->getContainer()->get('user.manager.facebook_auth');
- $result = $facebookAuthManager->getUser('1234');
- }
- public function testHasValidToken()
- {
- $this->assertFalse($this->facebookAuthManager->hasValidToken('1234'), 'Should raised exception');
- }
- private function initFbException($type, $is_success = false)
- {
- $request = new FacebookRequest(new FacebookApp('123', 'foo'));
- $params = [
- 'error' => [
- 'code' => 100,
- 'message' => 'errmsg',
- 'error_subcode' => 0,
- 'type' => 'exception'
- ],
- ];
- $response = !$is_success ?
- new FacebookResponse($request, json_encode($params), 401) :
- new FacebookResponse($request);
- switch ($type) {
- case self::FACEBOOK_RESPONSE_EXCEPTION:
- $exception = FacebookResponseException::create($response);
- break;
- case self::FACEBOOK_SDK_EXCEPTION:
- $exception = new FacebookSDKException();
- break;
- default:
- $exception = new \Exception('something wrong');
- break;
- }
- $mockFbDataHandler = $this->getMockBuilder(FacebookSessionPersistentDataHandler::class)
- ->disableOriginalConstructor()
- ->getMock();
- $mockFbDataHandler->expects($this->any())
- ->method('set')
- ->willReturn('test');
- $mockFbLoginHelper = $this->getMockBuilder(FacebookRedirectLoginHelper::class)
- ->disableOriginalConstructor()
- ->getMock();
- $mockFbLoginHelper->expects($this->any())
- ->method('getPersistentDataHandler')
- ->willReturn($mockFbDataHandler);
- $mockFbLoginHelper->expects($this->any())
- ->method('getAccessToken')
- ->willThrowException($exception);
- $mockFacebook = $this->getMockBuilder(Facebook::class)
- ->disableOriginalConstructor()
- ->getMock();
- $mockFacebook->expects($this->any())
- ->method('getRedirectLoginHelper')
- ->willReturn($mockFbLoginHelper);
- if ($is_success) {
- $mockFacebook->expects($this->any())
- ->method('get')
- ->willReturn($response);
- } else {
- $mockFacebook->expects($this->any())
- ->method('get')
- ->willThrowException($exception);
- }
- $fbManager = $this->getMockBuilder(FacebookAuthManager::class)
- ->disableOriginalConstructor()
- ->setMethods(['initFacebook'])
- ->getMock();
- $fbManager->expects($this->any())
- ->method('initFacebook')
- ->willReturn($mockFacebook);
- return $fbManager;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement