Advertisement
Guest User

FacebookAuthManagerTest

a guest
Jun 26th, 2019
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.78 KB | None | 0 0
  1. <?php
  2.  
  3. namespace UserBundle\Tests\Manager;
  4.  
  5. use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
  6. use Facebook\Facebook;
  7. use Facebook\Helpers\FacebookRedirectLoginHelper;
  8. use Facebook\PersistentData\FacebookSessionPersistentDataHandler;
  9. use Facebook\FacebookApp;
  10. use Facebook\FacebookRequest;
  11. use Facebook\FacebookResponse;
  12. use Facebook\Exceptions\FacebookResponseException;
  13. use Facebook\Exceptions\FacebookSDKException;
  14. use UserBundle\Manager\FacebookAuthManager;
  15.  
  16. class FacebookAuthManagerTest extends KernelTestCase
  17. {
  18. private $facebookAuthManager;
  19. const FACEBOOK_RESPONSE_EXCEPTION = 'fb_response_exception';
  20. const FACEBOOK_SDK_EXCEPTION = 'fb_sdk_exception';
  21.  
  22. public function setUp()
  23. {
  24. self::bootKernel();
  25. $this->facebookAuthManager = self::$kernel->getContainer()->get('user.manager.facebook_auth');
  26. $this->httpClient = self::$kernel->getContainer()->get('curl.manager.httpclient');
  27. }
  28.  
  29. public function testConnect()
  30. {
  31. $url = $this->facebookAuthManager->connect();
  32. $url_check = 'https://www.facebook.com/v2.10/dialog/oauth?';
  33. $this->assertTrue((strpos($url, $url_check) !== false), 'Should return authentication URL');
  34. }
  35.  
  36. public function testGetAccessToken()
  37. {
  38. $auth = $this->facebookAuthManager->getAccessToken('ABCDE');
  39. $this->assertFalse(isset($auth['accessToken']));
  40. $this->assertEquals($auth['accessToken'], null);
  41.  
  42. $fbManager = $this->initFbException(self::FACEBOOK_RESPONSE_EXCEPTION);
  43.  
  44. $setFacebookAuthManager = self::$kernel->getContainer()->set('user.manager.facebook_auth', $fbManager);
  45. $facebookAuthManager = self::$kernel->getContainer()->get('user.manager.facebook_auth');
  46.  
  47. $result = $facebookAuthManager->getAccessToken('ABCDE');
  48.  
  49. $fbManager = $this->initFbException(self::FACEBOOK_SDK_EXCEPTION);
  50.  
  51. $setFacebookAuthManager = self::$kernel->getContainer()->set('user.manager.facebook_auth', $fbManager);
  52. $facebookAuthManager = self::$kernel->getContainer()->get('user.manager.facebook_auth');
  53.  
  54. $result = $facebookAuthManager->getAccessToken('ABCDE');
  55. }
  56.  
  57. public function testGetUser()
  58. {
  59. $auth = $this->facebookAuthManager->getUser('1234');
  60. $this->assertTrue(isset($auth['error_desc']), 'Should return error_desc');
  61.  
  62. $fbManager = $this->initFbException(self::FACEBOOK_SDK_EXCEPTION);
  63.  
  64. $setFacebookAuthManager = self::$kernel->getContainer()->set('user.manager.facebook_auth', $fbManager);
  65. $facebookAuthManager = self::$kernel->getContainer()->get('user.manager.facebook_auth');
  66.  
  67. $result = $facebookAuthManager->getUser('1234');
  68.  
  69. $fbManager = $this->initFbException(null, true);
  70.  
  71. $setFacebookAuthManager = self::$kernel->getContainer()->set('user.manager.facebook_auth', $fbManager);
  72. $facebookAuthManager = self::$kernel->getContainer()->get('user.manager.facebook_auth');
  73.  
  74. $result = $facebookAuthManager->getUser('1234');
  75. }
  76.  
  77. public function testHasValidToken()
  78. {
  79. $this->assertFalse($this->facebookAuthManager->hasValidToken('1234'), 'Should raised exception');
  80. }
  81.  
  82. private function initFbException($type, $is_success = false)
  83. {
  84. $request = new FacebookRequest(new FacebookApp('123', 'foo'));
  85. $params = [
  86. 'error' => [
  87. 'code' => 100,
  88. 'message' => 'errmsg',
  89. 'error_subcode' => 0,
  90. 'type' => 'exception'
  91. ],
  92. ];
  93. $response = !$is_success ?
  94. new FacebookResponse($request, json_encode($params), 401) :
  95. new FacebookResponse($request);
  96.  
  97. switch ($type) {
  98. case self::FACEBOOK_RESPONSE_EXCEPTION:
  99. $exception = FacebookResponseException::create($response);
  100. break;
  101.  
  102. case self::FACEBOOK_SDK_EXCEPTION:
  103. $exception = new FacebookSDKException();
  104. break;
  105.  
  106. default:
  107. $exception = new \Exception('something wrong');
  108. break;
  109. }
  110.  
  111. $mockFbDataHandler = $this->getMockBuilder(FacebookSessionPersistentDataHandler::class)
  112. ->disableOriginalConstructor()
  113. ->getMock();
  114. $mockFbDataHandler->expects($this->any())
  115. ->method('set')
  116. ->willReturn('test');
  117.  
  118. $mockFbLoginHelper = $this->getMockBuilder(FacebookRedirectLoginHelper::class)
  119. ->disableOriginalConstructor()
  120. ->getMock();
  121. $mockFbLoginHelper->expects($this->any())
  122. ->method('getPersistentDataHandler')
  123. ->willReturn($mockFbDataHandler);
  124. $mockFbLoginHelper->expects($this->any())
  125. ->method('getAccessToken')
  126. ->willThrowException($exception);
  127.  
  128. $mockFacebook = $this->getMockBuilder(Facebook::class)
  129. ->disableOriginalConstructor()
  130. ->getMock();
  131. $mockFacebook->expects($this->any())
  132. ->method('getRedirectLoginHelper')
  133. ->willReturn($mockFbLoginHelper);
  134. if ($is_success) {
  135. $mockFacebook->expects($this->any())
  136. ->method('get')
  137. ->willReturn($response);
  138. } else {
  139. $mockFacebook->expects($this->any())
  140. ->method('get')
  141. ->willThrowException($exception);
  142. }
  143.  
  144. $fbManager = $this->getMockBuilder(FacebookAuthManager::class)
  145. ->disableOriginalConstructor()
  146. ->setMethods(['initFacebook'])
  147. ->getMock();
  148. $fbManager->expects($this->any())
  149. ->method('initFacebook')
  150. ->willReturn($mockFacebook);
  151.  
  152. return $fbManager;
  153. }
  154. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement