UserCurrentTest.php 6.1 KB
Newer Older
1
<?php
2

3
4
namespace UnicaenAuthTest\View\Helper;

5
6
use BjyAuthorize\Acl\Role;
use PHPUnit_Framework_MockObject_MockObject;
7
use UnicaenAppTest\View\Helper\TestAsset\ArrayTranslatorLoader;
8
use UnicaenAuth\Service\UserContext;
9
use UnicaenAuth\View\Helper\UserCurrent;
10
11
12
use UnicaenAuth\View\Helper\UserInfo;
use UnicaenAuth\View\Helper\UserProfile;
use UnicaenAuth\View\Helper\UserStatus;
Bertrand Gauthier's avatar
Bertrand Gauthier committed
13
14
15
use Laminas\I18n\Translator\Translator;
use Laminas\View\Helper\InlineScript;
use Laminas\View\Renderer\PhpRenderer;
16
17

/**
18
 * Description of UserCurrentTest
19
20
21
22
23
24
 *
 * @property UserCurrent $helper Description
 * @author Bertrand GAUTHIER <bertrand.gauthier at unicaen.fr>
 */
class UserCurrentTest extends AbstractTest
{
25
26
27
28
29
    protected $helperClass = UserCurrent::class;

    /**
     * @var PhpRenderer|PHPUnit_Framework_MockObject_MockObject
     */
30
    protected $renderer;
31
32
33
34
35
36

    /**
     * @var UserContext|PHPUnit_Framework_MockObject_MockObject
     */
    protected $userContext;

37
38
39
    protected $userStatusHelper;
    protected $userProfileHelper;
    protected $userInfoHelper;
40
    protected $inlineScriptHelper;
41

42
43
44
45
46
47
48
    /**
     * Sets up the fixture, for example, open a network connection.
     * This method is called before a test is executed.
     */
    protected function setUp()
    {
        parent::setUp();
49

50
51
52
53
        $this->userStatusHelper   = $userStatusHelper   = $this->createMock(UserStatus::class/*, ['__invoke']*/);
        $this->userProfileHelper  = $userProfileHelper  = $this->createMock(UserProfile::class/*, ['__toString']*/);
        $this->userInfoHelper     = $userInfoHelper     = $this->createMock(UserInfo::class/*, ['__invoke']*/);
        $this->inlineScriptHelper = $inlineScriptHelper = $this->createMock(InlineScript::class/*, ['__invoke']*/);
54

55
56
57
58
59
60
61
62
63
64
65
66
        $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'));
67
68
69
70
        $this->inlineScriptHelper
                ->expects($this->any())
                ->method('__invoke')
                ->will($this->returnValue('InlineScript Helper Markup'));
71

72
        $this->renderer = $this->createMock(PhpRenderer::class/*, ['plugin']*/);
73
74
        $this->renderer->expects($this->any())
                       ->method('plugin')
75
                       ->will($this->returnCallback(function ($helper) use ($userStatusHelper, $userProfileHelper, $userInfoHelper, $inlineScriptHelper) {
76
77
78
79
80
81
82
83
84
                           if ('userstatus' === strtolower($helper)) {
                               return $userStatusHelper;
                           }
                           if ('userprofile' === strtolower($helper)) {
                               return $userProfileHelper;
                           }
                           if ('userinfo' === strtolower($helper)) {
                               return $userInfoHelper;
                           }
85
86
87
                           if ('inlinescript' === strtolower($helper)) {
                               return $inlineScriptHelper;
                           }
88
89
                           return null;
                       }));
90

91
        $this->userContext = $this->createMock(UserContext::class/*, ['hasIdentity', 'getIdentity']*/);
92

93
94
95
96
        $this->helper
            ->setUserContext($this->userContext)
            ->setTranslator(new Translator())
            ->setView($this->renderer);
97
    }
98

99
    public function testCanConstructWithUserContextService()
100
    {
101
102
        $helper = new UserCurrent($this->userContext);
        $this->assertSame($this->userContext, $helper->getUserContext());
103
    }
104

105
106
107
108
    public function testEntryPointReturnsSelfInstance()
    {
        $this->assertSame($this->helper, $this->helper->__invoke());
    }
109

110
111
112
113
114
    public function testEntryPointCanSetArgs()
    {
        $this->helper->__invoke($flag = true);
        $this->assertSame($flag, $this->helper->getAffectationFineSiDispo());
    }
115

116
117
    public function testCanRenderIfNoIdentityAvailable()
    {
118
119
120
        $this->userContext->expects($this->any())
                          ->method('getIdentity')
                          ->willReturn(false);
121

122
        $markup = $this->helper->__toString();
123
        $this->assertEquals($this->getExpected('user_current/logged-out.phtml'), $markup);
124
125

        // traduction
126
        $this->helper->setTranslator($this->_getTranslator());
127
        $markup = $this->helper->__toString();
128
129
        $this->assertEquals($this->getExpected('user_current/logged-out-translated.phtml'), $markup);
    }
130

131
132
    public function testCanRenderLogoutLinkIfIdentityAvailable()
    {
133
134
135
136
137
        $this->userContext
            ->expects($this->once())
            ->method('getSelectedIdentityRole')
            ->willReturn($role = new Role('role id'));
        $this->userContext->expects($this->once())
138
                          ->method('getIdentity')
139
                          ->willReturn($identity = 'Auth Service Identity');
140

141
        $markup = $this->helper->__toString();
142
        $this->assertEquals($this->getExpected('user_current/logged-in.phtml'), $markup);
143

144
145
        // traduction
        $this->helper->setTranslator($this->_getTranslator());
146
        $markup = $this->helper->__toString();
147
148
149
150
151
152
153
154
155
156
157
        $this->assertEquals($this->getExpected('user_current/logged-in-translated.phtml'), $markup);
    }

    /**
     * Returns translator
     *
     * @return Translator
     */
    protected function _getTranslator()
    {
        $loader = new ArrayTranslatorLoader();
158
        $loader->translations = [
159
160
            "Utilisateur connecté à l'application" => "Auth user",
            "Aucun"                                => "None",
161
        ];
162
163
164
165

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

167
168
169
        return $translator;
    }
}