UserStatusTest.php 6.96 KB
Newer Older
1
2
3
4
5
6
7
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
33
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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
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
<?php
namespace UnicaenAuthTest\View\Helper;

use UnicaenApp\Entity\Ldap\People as LdapPeopleEntity;
use UnicaenAppTest\Entity\Ldap\TestAsset\People as LdapPeopleTestAsset;
use UnicaenAppTest\View\Helper\TestAsset\ArrayTranslatorLoader;
use UnicaenAuth\View\Helper\UserStatus;
use Zend\I18n\Translator\Translator;

/**
 * Description of UserProfileTest
 *
 * @property UserStatus $helper Description
 * @author Bertrand GAUTHIER <bertrand.gauthier at unicaen.fr>
 */
class UserStatusTest extends AbstractTest
{
    protected $helperClass = 'UnicaenAuth\View\Helper\UserStatus';
    
    /**
     * Sets up the fixture, for example, open a network connection.
     * This method is called before a test is executed.
     */
    protected function setUp()
    {
        parent::setUp();
        
        $this->authService = $this->getMock('Zend\Authentication\AuthenticationService', array('hasIdentity', 'getIdentity'));
        
        $userConnectionHelper = $this->getMock('UnicaenAuth\View\Helper\UserConnection', array('__toString'));
        $userConnectionHelper->expects($this->any())
                             ->method('__toString')
                             ->will($this->returnValue('UserConnection Helper Markup'));
        
        $this->helper->getView()->getHelperPluginManager()->setService('userConnection', $userConnectionHelper);
        
        $this->helper->setDisplayConnectionLink()
                     ->setAuthService($this->authService);
    }
    
    public function testEntryPointReturnsSelfInstance()
    {
        $this->assertSame($this->helper, $this->helper->__invoke());
    }
    
    public function testEntryPointCanSetArgs()
    {
        $this->helper->__invoke($flag = true);
        $this->assertSame($flag, $this->helper->getDisplayConnectionLink());
    }
    
    public function testRenderingWithoutConnectionLinkReturnsNoneIfNoIdentityAvailable()
    {
        $this->authService->expects($this->any())
                          ->method('hasIdentity')
                          ->will($this->returnValue(false));
        
        $this->helper->setDisplayConnectionLink(false);
        
        $markup = (string) $this->helper;
        $this->assertEquals($this->getExpected('user_status/no-identity-without-link.phtml'), $markup);
        
        // traduction
        $this->helper->setTranslator($this->_getTranslator());
        $markup = (string) $this->helper;
        $this->assertEquals($this->getExpected('user_status/no-identity-without-link-translated.phtml'), $markup);
    }
    
    public function getIdentityAndExpectedScript()
    {
        return array(
            'identity-to-string' => array(
                new IdentityTestAsset1(),
                'user_status/identity-without-link.phtml',
                'user_status/identity-with-link.phtml',
            ),
            'identity-get-displayname' => array(
                new IdentityTestAsset2(),
                'user_status/identity-without-link.phtml',
                'user_status/identity-with-link.phtml',
            ),
            'identity-get-username' => array(
                new IdentityTestAsset3(),
                'user_status/identity-without-link.phtml',
                'user_status/identity-with-link.phtml',
            ),
            'identity-get-id' => array(
                new IdentityTestAsset4(),
                'user_status/identity-without-link.phtml',
                'user_status/identity-with-link.phtml',
            ),
            'unexpected-identity' => array(
                new \DateTime(),
                'user_status/unexpected-identity-without-link.phtml',
                'user_status/unexpected-identity-with-link.phtml',
            ),
        );
    }
    
    /**
     * @dataProvider getIdentityAndExpectedScript
     * @param mixed $identity
     * @param string $expectedScriptWithoutLink
     */
    public function testRenderingWithoutConnectLinkReturnsCorrectMarkupIfIdentityAvailable(
            $identity, 
            $expectedScriptWithoutLink)
    {
        $this->authService->expects($this->any())
                          ->method('hasIdentity')
                          ->will($this->returnValue(true));
        $this->authService->expects($this->any())
                          ->method('getIdentity')
                          ->will($this->returnValue($identity));
        
        $this->helper->setDisplayConnectionLink(false);
        
        $markup = (string) $this->helper;
        $this->assertEquals($this->getExpected($expectedScriptWithoutLink), $markup);
    }
    
    public function testRenderingWithConnectionLinkReturnsNoneIfNoIdentityAvailable()
    {
        $this->authService->expects($this->any())
                          ->method('hasIdentity')
                          ->will($this->returnValue(false));
        
        $this->helper->setDisplayConnectionLink(true);
        
        $markup = (string) $this->helper;
        $this->assertEquals($this->getExpected('user_status/no-identity-with-link.phtml'), $markup);
        
        // traduction
        $this->helper->setTranslator($this->_getTranslator());
        $markup = (string) $this->helper;
        $this->assertEquals($this->getExpected('user_status/no-identity-with-link-translated.phtml'), $markup);
    }
    
    /**
     * @dataProvider getIdentityAndExpectedScript
     * @param mixed $identity
     * @param string $expectedScriptWithoutLink
     * @param string $expectedScriptWithLink
     */
    public function testRenderingWithConnectLinkReturnsCorrectMarkupIfIdentityAvailable(
            $identity, 
            $expectedScriptWithoutLink, 
            $expectedScriptWithLink)
    {
        $this->authService->expects($this->any())
                          ->method('hasIdentity')
                          ->will($this->returnValue(true));
        $this->authService->expects($this->any())
                          ->method('getIdentity')
                          ->will($this->returnValue($identity));
        
        $this->helper->setDisplayConnectionLink(true);
        
        $markup = (string) $this->helper;
        $this->assertEquals($this->getExpected($expectedScriptWithLink), $markup);
    }

    /**
     * Returns translator
     *
     * @return Translator
     */
    protected function _getTranslator()
    {
        $loader = new ArrayTranslatorLoader();
        $loader->translations = array(
            "Aucun" => "None",
        );

        $translator = new Translator();
        $translator->getPluginManager()->setService('default', $loader);
        $translator->addTranslationFile('default', null);
        
        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';
    }
}