Skip to content
Snippets Groups Projects
Select Git revision
  • cb8b152dcc049c15d2a55b5630e1462dffa65fe0
  • master default protected
  • 5.x
  • ll-php8-bs5
  • release_5_bs5
  • ll-php8
  • 4.x
  • laminas_migration
  • release_1.0.0.2
  • release_4.0.0
  • release_3.2.8
  • bootstrap4_migration
  • 1.0.0.3
  • 6.0.7
  • 6.0.6
  • 6.0.5
  • 6.0.4
  • 6.0.3
  • 6.0.2
  • 6.0.1
  • 5.1.1
  • 6.0.0
  • 5.1.0
  • 5.0.0
  • 4.0.2
  • 3.2.11
  • 4.0.1
  • 3.2.10
  • 4.0.0
  • 1.0.0.2
  • 3.2.9
  • 3.2.8
32 results

UserAbstractTest.php

  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    UserAbstractTest.php 5.78 KiB
    <?php
    namespace UnicaenAuthTest\View\Helper;
    
    use PHPUnit\Framework\TestCase;
    use UnicaenAuth\View\Helper\UserAbstract;
    
    /**
     * Description of AppConnectionTest
     *
     * @property UserAbstract $helper Description
     * @author Bertrand GAUTHIER <bertrand.gauthier at unicaen.fr>
     */
    class UserAbstractTest extends TestCase
    {
        protected $helper;
        protected $authService;
    
        /**
         * Sets up the fixture, for example, open a network connection.
         * This method is called before a test is executed.
         */
        protected function setUp()
        {
            $this->authService = $this->getMock('Zend\Authentication\AuthenticationService', ['hasIdentity', 'getIdentity']);
    
            $this->helper = $this->getMockForAbstractClass('UnicaenAuth\View\Helper\UserAbstract');
            $this->helper->setAuthService($this->authService);
        }
    
        public function testCanConstructWithAuthServiceSpecified()
        {
            $helper = $this->getMockForAbstractClass('UnicaenAuth\View\Helper\UserAbstract', [$this->authService]);
            $this->assertSame($this->authService, $helper->getAuthService());
        }
    
        public function testCanSetAuthService()
        {
            $this->assertSame($this->authService, $this->helper->getAuthService());
        }
    
        public function testGettingIdentityReturnsNullIfNoAuthServiceAvailable()
        {
            $this->helper->setAuthService(null);
            $this->assertNull($this->helper->getAuthService());
            $this->assertNull($this->helper->getIdentity());
        }
    
        public function testGettingIdentityReturnsNullIfAuthServiceHasNoIdentity()
        {
            $this->authService->expects($this->once())
                              ->method('hasIdentity')
                              ->will($this->returnValue(false));
            $this->assertNull($this->helper->getIdentity());
        }
    
        public function testGettingIdentityReturnsAuthServiceIdentity()
        {
            $this->authService->expects($this->once())
                              ->method('hasIdentity')
                              ->will($this->returnValue(true));
            $this->authService->expects($this->once())
                              ->method('getIdentity')
                              ->will($this->returnValue($identity = 'Auth Service Identity'));
            $this->assertEquals($identity, $this->helper->getIdentity());
        }
    
        public function provideValidArrayIdentity()
        {
            return [
                'db-only' => [
                    ['db' => 'Db Identity'],
                    'Db Identity',
                ],
                'ldap-only' => [
                    ['ldap' => 'Ldap Identity'],
                    'Ldap Identity',
                ],
                'db-ldap' => [
                    ['db'   => 'Db Identity', 'ldap' => 'Ldap Identity'],
                    'Ldap Identity',
                ],
            ];
        }
    
        /**
         * @dataProvider provideValidArrayIdentity
         * @param array $identity
         * @param string $expected
         */
        public function testGettingIdentityReturnsAuthServiceIdentityFromValidArrayIdentity($identity, $expected)
        {
            $this->authService->expects($this->once())
                              ->method('hasIdentity')
                              ->will($this->returnValue(true));
            $this->authService->expects($this->once())
                              ->method('getIdentity')
                              ->will($this->returnValue($identity));
            $this->assertEquals($expected, $this->helper->getIdentity());
        }
    
        public function provideValidArrayIdentityWithPreferedKey()
        {
            return [
                'db-only-ldap-prefered' => [
                    ['db' => 'Db Identity'],
                    'ldap', // clé absente
                    'Db Identity',
                ],
                'ldap-only-db-prefered' => [
                    ['ldap' => 'Ldap Identity'],
                    'db', // clé absente
                    'Ldap Identity',
                ],
                'db-ldap-none-prefered' => [
                    ['db' => 'Db Identity', 'ldap' => 'Ldap Identity'],
                    null, // équivaut à 'ldap'
                    'Ldap Identity',
                ],
                'db-ldap-ldap-prefered' => [
                    ['db' => 'Db Identity', 'ldap' => 'Ldap Identity'],
                    'ldap',
                    'Ldap Identity',
                ],
                'db-ldap-db-prefered' => [
                    ['db' => 'Db Identity', 'ldap' => 'Ldap Identity'],
                    'db',
                    'Db Identity',
                ],
            ];
        }
    
        /**
         * @dataProvider provideValidArrayIdentityWithPreferedKey
         * @param array $identity
         * @param string $preferedKey
         * @param string $expected
         */
        public function testGettingIdentityReturnsAuthServiceIdentityFromValidArrayIdentityWithPreferedKey($identity, $preferedKey, $expected)
        {
            $this->authService->expects($this->once())
                              ->method('hasIdentity')
                              ->will($this->returnValue(true));
            $this->authService->expects($this->once())
                              ->method('getIdentity')
                              ->will($this->returnValue($identity));
            $this->assertEquals($expected, $this->helper->getIdentity($preferedKey));
        }
    
        public function provideInvalidArrayIdentity()
        {
            return [
                [[]],
                [['other' => 'Other Identity']],
            ];
        }
    
        /**
         * @dataProvider provideInvalidArrayIdentity
         * @expectedException \InvalidArgumentException
         * @param array $identity
         */
        public function testGettingIdentityThrowsExceptionFromInvalidArrayIdentity($identity)
        {
            $this->authService->expects($this->once())
                              ->method('hasIdentity')
                              ->will($this->returnValue(true));
            $this->authService->expects($this->once())
                              ->method('getIdentity')
                              ->will($this->returnValue($identity));
            $this->helper->getIdentity();
        }
    }