UserConnectionTest.php 4.09 KB
Newer Older
1
2
3
4
5
<?php
namespace UnicaenAuthTest\View\Helper;

use UnicaenAppTest\View\Helper\TestAsset\ArrayTranslatorLoader;
use UnicaenAuth\View\Helper\UserConnection;
Bertrand Gauthier's avatar
Bertrand Gauthier committed
6
use Laminas\I18n\Translator\Translator;
7
8
9
10
11
12
13
14
15
16
17
18
19

/**
 * Description of AppConnectionTest
 *
 * @property UserConnection $helper Description
 * @author Bertrand GAUTHIER <bertrand.gauthier at unicaen.fr>
 */
class UserConnectionTest extends AbstractTest
{
    protected $helperClass = 'UnicaenAuth\View\Helper\UserConnection';
    protected $renderer;
    protected $authService;
    protected $urlHelper;
20

21
22
23
24
25
26
27
    /**
     * Sets up the fixture, for example, open a network connection.
     * This method is called before a test is executed.
     */
    protected function setUp()
    {
        parent::setUp();
28

Bertrand Gauthier's avatar
Bertrand Gauthier committed
29
        $this->urlHelper = $this->createMock('Laminas\View\Helper\Url'/*, ['__invoke']*/);
30

Bertrand Gauthier's avatar
Bertrand Gauthier committed
31
        $this->renderer = $this->createMock('Laminas\View\Renderer\PhpRenderer'/*, ['plugin']*/);
32
33
34
35
        $this->renderer->expects($this->any())
                       ->method('plugin')
                       ->with('url')
                       ->will($this->returnValue($this->urlHelper));
36

Bertrand Gauthier's avatar
Bertrand Gauthier committed
37
        $this->authService = $this->createMock('Laminas\Authentication\AuthenticationService'/*, ['hasIdentity', 'getIdentity']*/);
38

39
40
41
42
        $this->helper->setAuthService($this->authService)
                     ->setView($this->renderer)
                     ->setTranslator(new Translator());
    }
43

44
45
46
47
48
    public function testCanConstructWithAuthService()
    {
        $helper = new UserConnection($this->authService);
        $this->assertSame($this->authService, $helper->getAuthService());
    }
49

50
51
52
53
    public function testEntryPointReturnsSelfInstance()
    {
        $this->assertSame($this->helper, $this->helper->__invoke());
    }
54

55
56
57
58
59
    public function testCanRenderLoginLinkIfNoIdentityAvailable()
    {
        $this->authService->expects($this->any())
                          ->method('hasIdentity')
                          ->will($this->returnValue(false));
60

61
62
63
64
65
        $this->urlHelper
                ->expects($this->any())
                ->method('__invoke')
                ->with('zfcuser/login')
                ->will($this->returnValue('/appli/connexion'));
66

67
        $this->assertEquals($this->getExpected('user_connection/login.phtml'), (string) $this->helper);
68
69

        // traduction
70
71
72
        $this->helper->setTranslator($this->_getTranslator());
        $this->assertEquals($this->getExpected('user_connection/login-translated.phtml'), (string) $this->helper);
    }
73

74
75
76
77
78
79
80
81
    public function testCanRenderLogoutLinkIfIdentityAvailable()
    {
        $this->authService->expects($this->any())
                          ->method('hasIdentity')
                          ->will($this->returnValue(true));
        $this->authService->expects($this->any())
                          ->method('getIdentity')
                          ->will($this->returnValue($identity = 'Auth Service Identity'));
82

83
84
85
86
87
        $this->urlHelper
                ->expects($this->any())
                ->method('__invoke')
                ->with('zfcuser/logout')
                ->will($this->returnValue('/appli/deconnexion'));
88

89
        $this->assertEquals($this->getExpected('user_connection/logout.phtml'), (string) $this->helper);
90

91
92
93
94
95
96
97
98
99
100
101
102
103
        // traduction
        $this->helper->setTranslator($this->_getTranslator());
        $this->assertEquals($this->getExpected('user_connection/logout-translated.phtml'), (string) $this->helper);
    }

    /**
     * Returns translator
     *
     * @return Translator
     */
    protected function _getTranslator()
    {
        $loader = new ArrayTranslatorLoader();
104
        $loader->translations = [
105
106
107
108
            "Connexion"                                => 'Login',
            "Déconnexion"                              => 'Logout',
            "Affiche le formulaire d'authentification" => 'Display auth form',
            "Supprime les informations de connexion"   => 'Reset auth info',
109
110
        ];

111
112
113
        $translator = new Translator();
        $translator->getPluginManager()->setService('default', $loader);
        $translator->addTranslationFile('default', null);
114

115
116
117
        return $translator;
    }
}