/Tests/Security/EntryPoint/FacebookAuthenticationEntryPointTest.php
PHP | 105 lines | 73 code | 15 blank | 17 comment | 0 complexity | e98c1f5db18e1a9d90c0f802bd57618c MD5 | raw file
1<?php 2 3/* 4 * This file is part of the FOSFacebookBundle package. 5 * 6 * (c) FriendsOfSymfony <http://friendsofsymfony.github.com/> 7 * 8 * For the full copyright and license information, please view the LICENSE 9 * file that was distributed with this source code. 10 */ 11 12namespace FOS\FacebookBundle\Tests\Security\EntryPoint; 13 14use FOS\FacebookBundle\Security\EntryPoint\FacebookAuthenticationEntryPoint; 15 16class FacebookAuthenticationEntryPointTest extends \PHPUnit_Framework_TestCase 17{ 18 /** 19 * @covers FOS\FacebookBundle\Security\EntryPoint\FacebookAuthenticationEntryPoint::start 20 */ 21 public function testThatRedirectResponseWithFacebookLoginUrlIsCreated() 22 { 23 $requestMock = $this->getMock('Symfony\Component\HttpFoundation\Request', array('getUriForPath')); 24 $requestMock->expects($this->once()) 25 ->method('getUriForPath') 26 ->with($this->equalTo('/index')) 27 ->will($this->returnValue('http://localhost/index')); 28 29 $options = array('check_path' => '/index', 'redirect_to_facebook_login' => true); 30 $facebookMock = $this->getMockBuilder('FOS\FacebookBundle\Facebook\FacebookSessionPersistence') 31 ->disableOriginalConstructor() 32 ->setMethods(array('getLoginUrl')) 33 ->getMock(); 34 $facebookMock->expects($this->once()) 35 ->method('getLoginUrl') 36 ->with($this->equalTo(array( 37 'display' => 'page', 38 'scope' => 'email,user_website', 39 'redirect_uri' => 'http://localhost/index' 40 ))) 41 ->will($this->returnValue('http://localhost/facebook-redirect/index')); 42 43 $facebookAuthentication = new FacebookAuthenticationEntryPoint($facebookMock, $options, array('email', 'user_website')); 44 $response = $facebookAuthentication->start($requestMock); 45 46 $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response, 'RedirectResponse is returned'); 47 $this->assertEquals($response->headers->get('location'), 'http://localhost/facebook-redirect/index', 'RedirectResponse has defined expected location'); 48 } 49 50 /** 51 * @covers FOS\FacebookBundle\Security\EntryPoint\FacebookAuthenticationEntryPoint::start 52 */ 53 public function testThatRedirectResponseWithoutFacebookLoginUrlIsCreated() 54 { 55 $requestMock = $this->getMock('Symfony\Component\HttpFoundation\Request', array('getUriForPath')); 56 $requestMock->expects($this->never()) 57 ->method('getUriForPath'); 58 59 $options = array( 60 'check_path' => '/index', 61 'login_path' => '/login', 62 'redirect_to_facebook_login' => false 63 ); 64 $facebookMock = $this->getMockBuilder('FOS\FacebookBundle\Facebook\FacebookSessionPersistence') 65 ->disableOriginalConstructor() 66 ->setMethods(array('getLoginUrl')) 67 ->getMock(); 68 $facebookMock->expects($this->never()) 69 ->method('getLoginUrl'); 70 71 $facebookAuthentication = new FacebookAuthenticationEntryPoint($facebookMock, $options, array('email', 'user_website')); 72 $response = $facebookAuthentication->start($requestMock); 73 74 $this->assertInstanceOf('Symfony\Component\HttpFoundation\RedirectResponse', $response, 'RedirectResponse is returned'); 75 $this->assertEquals($response->headers->get('location'), '/login', 'RedirectResponse has defined expected location'); 76 } 77 78 /** 79 * @covers FOS\FacebookBundle\Security\EntryPoint\FacebookAuthenticationEntryPoint::start 80 */ 81 public function testThatRedirectionToFacebookLoginUrlIsCreated() 82 { 83 $requestMock = $this->getMock('Symfony\Component\HttpFoundation\Request', array('getUriForPath')); 84 85 $options = array( 86 'check_path' => '/index', 87 'server_url' => 'http://server.url', 88 'app_url' => 'http://app.url', 89 'redirect_to_facebook_login' => true 90 ); 91 $facebookMock = $this->getMockBuilder('FOS\FacebookBundle\Facebook\FacebookSessionPersistence') 92 ->disableOriginalConstructor() 93 ->setMethods(array('getLoginUrl')) 94 ->getMock(); 95 $facebookMock->expects($this->once()) 96 ->method('getLoginUrl') 97 ->will($this->returnValue('http://localhost/facebook-redirect/index')); 98 99 $facebookAuthentication = new FacebookAuthenticationEntryPoint($facebookMock, $options, array()); 100 $response = $facebookAuthentication->start($requestMock); 101 102 $this->assertInstanceOf('Symfony\Component\HttpFoundation\Response', $response, 'Response is returned'); 103 $this->assertRegExp('/location\.href="http:\/\/localhost\/facebook-redirect\/index/', $response->getContent(), 'Javascript redirection is in response'); 104 } 105}