UserStatusTest.php 6.8 KB
Newer Older
1
<?php
2

3
4
5
namespace UnicaenAuthTest\View\Helper;

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

/**
 * Description of UserProfileTest
 *
 * @property UserStatus $helper Description
 * @author Bertrand GAUTHIER <bertrand.gauthier at unicaen.fr>
 */
class UserStatusTest extends AbstractTest
{
18
19
20
21
22
    /**
     * @var UserContext|\PHPUnit_Framework_MockObject_MockObject
     */
    protected $userContext;

23
    protected $helperClass = 'UnicaenAuth\View\Helper\UserStatus';
24

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

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

35
        $userConnectionHelper = $this->createMock('UnicaenAuth\View\Helper\UserConnection'/*, ['__toString']*/);
36
37
38
        $userConnectionHelper->expects($this->any())
                             ->method('__toString')
                             ->will($this->returnValue('UserConnection Helper Markup'));
39

40
        $this->helper->getView()->getHelperPluginManager()->setService('userConnection', $userConnectionHelper);
41

42
        $this->helper->setDisplayConnectionLink()
43
                     ->setUserContext($this->userContext);
44
    }
45

46
47
48
49
    public function testEntryPointReturnsSelfInstance()
    {
        $this->assertSame($this->helper, $this->helper->__invoke());
    }
50

51
52
53
54
55
    public function testEntryPointCanSetArgs()
    {
        $this->helper->__invoke($flag = true);
        $this->assertSame($flag, $this->helper->getDisplayConnectionLink());
    }
56

57
58
    public function testRenderingWithoutConnectionLinkReturnsNoneIfNoIdentityAvailable()
    {
59
        $this->userContext->expects($this->any())
60
61
                          ->method('hasIdentity')
                          ->will($this->returnValue(false));
62

63
        $this->helper->setDisplayConnectionLink(false);
64

65
66
        $markup = (string) $this->helper;
        $this->assertEquals($this->getExpected('user_status/no-identity-without-link.phtml'), $markup);
67

68
69
70
71
72
        // traduction
        $this->helper->setTranslator($this->_getTranslator());
        $markup = (string) $this->helper;
        $this->assertEquals($this->getExpected('user_status/no-identity-without-link-translated.phtml'), $markup);
    }
73

74
75
    public function getIdentityAndExpectedScript()
    {
76
77
        return [
            'identity-to-string' => [
78
79
80
                new IdentityTestAsset1(),
                'user_status/identity-without-link.phtml',
                'user_status/identity-with-link.phtml',
81
82
            ],
            'identity-get-displayname' => [
83
84
85
                new IdentityTestAsset2(),
                'user_status/identity-without-link.phtml',
                'user_status/identity-with-link.phtml',
86
87
            ],
            'identity-get-username' => [
88
89
90
                new IdentityTestAsset3(),
                'user_status/identity-without-link.phtml',
                'user_status/identity-with-link.phtml',
91
92
            ],
            'identity-get-id' => [
93
94
95
                new IdentityTestAsset4(),
                'user_status/identity-without-link.phtml',
                'user_status/identity-with-link.phtml',
96
97
            ],
            'unexpected-identity' => [
98
99
100
                new \DateTime(),
                'user_status/unexpected-identity-without-link.phtml',
                'user_status/unexpected-identity-with-link.phtml',
101
102
            ],
        ];
103
    }
104

105
106
107
108
109
110
    /**
     * @dataProvider getIdentityAndExpectedScript
     * @param mixed $identity
     * @param string $expectedScriptWithoutLink
     */
    public function testRenderingWithoutConnectLinkReturnsCorrectMarkupIfIdentityAvailable(
111
            $identity,
112
113
            $expectedScriptWithoutLink)
    {
114
        $this->userContext->expects($this->any())
115
116
                          ->method('hasIdentity')
                          ->will($this->returnValue(true));
117
        $this->userContext->expects($this->any())
118
119
                          ->method('getIdentity')
                          ->will($this->returnValue($identity));
120

121
        $this->helper->setDisplayConnectionLink(false);
122

123
124
125
        $markup = (string) $this->helper;
        $this->assertEquals($this->getExpected($expectedScriptWithoutLink), $markup);
    }
126

127
128
    public function testRenderingWithConnectionLinkReturnsNoneIfNoIdentityAvailable()
    {
129
        $this->userContext->expects($this->any())
130
131
                          ->method('hasIdentity')
                          ->will($this->returnValue(false));
132

133
        $this->helper->setDisplayConnectionLink(true);
134

135
136
        $markup = (string) $this->helper;
        $this->assertEquals($this->getExpected('user_status/no-identity-with-link.phtml'), $markup);
137

138
139
140
141
142
        // traduction
        $this->helper->setTranslator($this->_getTranslator());
        $markup = (string) $this->helper;
        $this->assertEquals($this->getExpected('user_status/no-identity-with-link-translated.phtml'), $markup);
    }
143

144
145
146
147
148
149
150
    /**
     * @dataProvider getIdentityAndExpectedScript
     * @param mixed $identity
     * @param string $expectedScriptWithoutLink
     * @param string $expectedScriptWithLink
     */
    public function testRenderingWithConnectLinkReturnsCorrectMarkupIfIdentityAvailable(
151
152
            $identity,
            $expectedScriptWithoutLink,
153
154
            $expectedScriptWithLink)
    {
155
        $this->userContext->expects($this->any())
156
157
                          ->method('hasIdentity')
                          ->will($this->returnValue(true));
158
        $this->userContext->expects($this->any())
159
160
                          ->method('getIdentity')
                          ->will($this->returnValue($identity));
161

162
        $this->helper->setDisplayConnectionLink(true);
163

164
165
166
167
168
169
170
171
172
173
174
175
        $markup = (string) $this->helper;
        $this->assertEquals($this->getExpected($expectedScriptWithLink), $markup);
    }

    /**
     * Returns translator
     *
     * @return Translator
     */
    protected function _getTranslator()
    {
        $loader = new ArrayTranslatorLoader();
176
        $loader->translations = [
177
            "Vous n'êtes pas connecté(e)" => "None",
178
        ];
179
180
181
182

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

184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
        return $translator;
    }
}

class IdentityTestAsset1
{
    public function __toString()
    {
        return 'User identity';
    }
}

class IdentityTestAsset2
{
    public function getDisplayName()
    {
        return 'User identity';
    }
}

class IdentityTestAsset3
{
    public function getUsername()
    {
        return 'User identity';
    }
}

class IdentityTestAsset4
{
    public function getId()
    {
        return 'User identity';
    }
}