ZfcUserAuthenticationTest.php 3.8 KB
Newer Older
Bertrand Gauthier's avatar
Bertrand Gauthier committed
1
2
3
4
5
<?php

namespace ZfcUserTest\Controller\Plugin;

use ZfcUser\Controller\Plugin\ZfcUserAuthentication as Plugin;
Bertrand Gauthier's avatar
Bertrand Gauthier committed
6
7
use Laminas\Authentication\AuthenticationService;
use Laminas\Authentication\Adapter\AdapterInterface;
Bertrand Gauthier's avatar
Bertrand Gauthier committed
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
use ZfcUser\Authentication\Adapter\AdapterChain;

class ZfcUserAuthenticationTest extends \PHPUnit_Framework_TestCase
{
    /**
     *
     * @var Plugin
     */
    protected $SUT;

    /**
     *
     * @var AuthenticationService
     */
    protected $mockedAuthenticationService;

    /**
     *
     * @var AdapterChain
     */
    protected $mockedAuthenticationAdapter;

    public function setUp()
    {
        $this->SUT = new Plugin();
Bertrand Gauthier's avatar
Bertrand Gauthier committed
33
        $this->mockedAuthenticationService = $this->getMock('Laminas\Authentication\AuthenticationService');
Bertrand Gauthier's avatar
Bertrand Gauthier committed
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
        $this->mockedAuthenticationAdapter = $this->getMockForAbstractClass('\ZfcUser\Authentication\Adapter\AdapterChain');
    }


    /**
     * @covers ZfcUser\Controller\Plugin\ZfcUserAuthentication::hasIdentity
     * @covers ZfcUser\Controller\Plugin\ZfcUserAuthentication::getIdentity
     */
    public function testGetAndHasIdentity()
    {
        $this->SUT->setAuthService($this->mockedAuthenticationService);

        $callbackIndex = 0;
        $callback = function () use (&$callbackIndex) {
            $callbackIndex++;
            return (bool) ($callbackIndex % 2);
        };

        $this->mockedAuthenticationService->expects($this->any())
                                          ->method('hasIdentity')
                                          ->will($this->returnCallback($callback));

        $this->mockedAuthenticationService->expects($this->any())
                                          ->method('getIdentity')
                                          ->will($this->returnCallback($callback));

        $this->assertTrue($this->SUT->hasIdentity());
        $this->assertFalse($this->SUT->hasIdentity());
        $this->assertTrue($this->SUT->hasIdentity());

        $callbackIndex= 0;

        $this->assertTrue($this->SUT->getIdentity());
        $this->assertFalse($this->SUT->getIdentity());
        $this->assertTrue($this->SUT->getIdentity());
    }

    /**
     * @covers ZfcUser\Controller\Plugin\ZfcUserAuthentication::setAuthAdapter
     * @covers ZfcUser\Controller\Plugin\ZfcUserAuthentication::getAuthAdapter
     */
    public function testSetAndGetAuthAdapter()
    {
        $adapter1 = $this->mockedAuthenticationAdapter;
        $adapter2 = new AdapterChain();
        $this->SUT->setAuthAdapter($adapter1);

Bertrand Gauthier's avatar
Bertrand Gauthier committed
81
        $this->assertInstanceOf('\Laminas\Authentication\Adapter\AdapterInterface', $this->SUT->getAuthAdapter());
Bertrand Gauthier's avatar
Bertrand Gauthier committed
82
83
84
85
        $this->assertSame($adapter1, $this->SUT->getAuthAdapter());

        $this->SUT->setAuthAdapter($adapter2);

Bertrand Gauthier's avatar
Bertrand Gauthier committed
86
        $this->assertInstanceOf('\Laminas\Authentication\Adapter\AdapterInterface', $this->SUT->getAuthAdapter());
Bertrand Gauthier's avatar
Bertrand Gauthier committed
87
88
89
90
91
92
93
94
95
96
97
98
99
100
        $this->assertNotSame($adapter1, $this->SUT->getAuthAdapter());
        $this->assertSame($adapter2, $this->SUT->getAuthAdapter());
    }

    /**
     * @covers ZfcUser\Controller\Plugin\ZfcUserAuthentication::setAuthService
     * @covers ZfcUser\Controller\Plugin\ZfcUserAuthentication::getAuthService
     */
    public function testSetAndGetAuthService()
    {
        $service1 = new AuthenticationService();
        $service2 = new AuthenticationService();
        $this->SUT->setAuthService($service1);

Bertrand Gauthier's avatar
Bertrand Gauthier committed
101
        $this->assertInstanceOf('\Laminas\Authentication\AuthenticationService', $this->SUT->getAuthService());
Bertrand Gauthier's avatar
Bertrand Gauthier committed
102
103
104
105
        $this->assertSame($service1, $this->SUT->getAuthService());

        $this->SUT->setAuthService($service2);

Bertrand Gauthier's avatar
Bertrand Gauthier committed
106
        $this->assertInstanceOf('\Laminas\Authentication\AuthenticationService', $this->SUT->getAuthService());
Bertrand Gauthier's avatar
Bertrand Gauthier committed
107
108
109
110
        $this->assertNotSame($service1, $this->SUT->getAuthService());
        $this->assertSame($service2, $this->SUT->getAuthService());
    }
}