UserCurrentTest.php 5.83 KB
Newer Older
1
2
3
4
5
6
7
8
<?php
namespace UnicaenAuthTest\View\Helper;

use UnicaenAppTest\View\Helper\TestAsset\ArrayTranslatorLoader;
use UnicaenAuth\View\Helper\UserCurrent;
use Zend\I18n\Translator\Translator;

/**
9
 * Description of UserCurrentTest
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 *
 * @property UserCurrent $helper Description
 * @author Bertrand GAUTHIER <bertrand.gauthier at unicaen.fr>
 */
class UserCurrentTest extends AbstractTest
{
    protected $helperClass = 'UnicaenAuth\View\Helper\UserCurrent';
    protected $renderer;
    protected $authService;
    protected $userStatusHelper;
    protected $userProfileHelper;
    protected $userInfoHelper;
    
    /**
     * Sets up the fixture, for example, open a network connection.
     * This method is called before a test is executed.
     */
    protected function setUp()
    {
        parent::setUp();
        
31
32
33
34
        $this->userStatusHelper   = $userStatusHelper   = $this->getMock('UnicaenAuth\View\Helper\UserStatus', array('__invoke'));
        $this->userProfileHelper  = $userProfileHelper  = $this->getMock('UnicaenAuth\View\Helper\UserProfile', array('__toString'));
        $this->userInfoHelper     = $userInfoHelper     = $this->getMock('UnicaenAuth\View\Helper\UserInfo', array('__invoke'));
        $this->inlineScriptHelper = $inlineScriptHelper = $this->getMock('Zend\View\Helper\InlineScript', array('__invoke'));
35
36
37
38
39
40
41
42
43
44
45
46
47
        
        $this->userStatusHelper
                ->expects($this->any())
                ->method('__invoke')
                ->will($this->returnValue('User Status Helper Markup'));
        $this->userProfileHelper
                ->expects($this->any())
                ->method('__toString')
                ->will($this->returnValue('User Profile Helper Markup'));
        $this->userInfoHelper
                ->expects($this->any())
                ->method('__invoke')
                ->will($this->returnValue('User Info Helper Markup'));
48
49
50
51
        $this->inlineScriptHelper
                ->expects($this->any())
                ->method('__invoke')
                ->will($this->returnValue('InlineScript Helper Markup'));
52
53
54
55
        
        $this->renderer = $this->getMock('Zend\View\Renderer\PhpRenderer', array('plugin'));
        $this->renderer->expects($this->any())
                       ->method('plugin')
56
                       ->will($this->returnCallback(function ($helper) use ($userStatusHelper, $userProfileHelper, $userInfoHelper, $inlineScriptHelper) {
57
58
59
60
61
62
63
64
65
                           if ('userstatus' === strtolower($helper)) {
                               return $userStatusHelper;
                           }
                           if ('userprofile' === strtolower($helper)) {
                               return $userProfileHelper;
                           }
                           if ('userinfo' === strtolower($helper)) {
                               return $userInfoHelper;
                           }
66
67
68
                           if ('inlinescript' === strtolower($helper)) {
                               return $inlineScriptHelper;
                           }
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
                           return null;
                       }));
        
        $this->authService = $this->getMock('Zend\Authentication\AuthenticationService', array('hasIdentity', 'getIdentity'));
        
        $this->helper->setAuthService($this->authService)
                     ->setView($this->renderer)
                     ->setTranslator(new Translator());
    }
    
    public function testCanConstructWithAuthService()
    {
        $helper = new UserCurrent($this->authService);
        $this->assertSame($this->authService, $helper->getAuthService());
    }
    
    public function testEntryPointReturnsSelfInstance()
    {
        $this->assertSame($this->helper, $this->helper->__invoke());
    }
    
    public function testEntryPointCanSetArgs()
    {
        $this->helper->__invoke($flag = true);
        $this->assertSame($flag, $this->helper->getAffectationFineSiDispo());
    }
    
    public function testCanRenderIfNoIdentityAvailable()
    {
        $this->authService->expects($this->any())
                          ->method('hasIdentity')
                          ->will($this->returnValue(false));
        
        $markup = (string) $this->helper;
        $this->assertEquals($this->getExpected('user_current/logged-out.phtml'), $markup);
        
        // traduction 
        $this->helper->setTranslator($this->_getTranslator());
        $markup = (string) $this->helper;
        $this->assertEquals($this->getExpected('user_current/logged-out-translated.phtml'), $markup);
    }
    
    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'));
                
        $markup = (string) $this->helper;
        $this->assertEquals($this->getExpected('user_current/logged-in.phtml'), $markup);
        
        // traduction
        $this->helper->setTranslator($this->_getTranslator());
        $markup = (string) $this->helper;
        $this->assertEquals($this->getExpected('user_current/logged-in-translated.phtml'), $markup);
    }

    /**
     * Returns translator
     *
     * @return Translator
     */
    protected function _getTranslator()
    {
        $loader = new ArrayTranslatorLoader();
        $loader->translations = array(
            "Utilisateur connecté à l'application" => "Auth user",
            "Aucun"                                => "None",
        );

        $translator = new Translator();
        $translator->getPluginManager()->setService('default', $loader);
        $translator->addTranslationFile('default', null);
        
        return $translator;
    }
}