UserProfileTest.php 3.93 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
<?php
namespace UnicaenAuthTest\View\Helper;

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

/**
 * Description of UserProfileTest
 *
 * @property UserProfile $helper Description
 * @author Bertrand GAUTHIER <bertrand.gauthier at unicaen.fr>
 */
class UserProfileTest extends AbstractTest
{
    protected $helperClass = 'UnicaenAuth\View\Helper\UserProfile';
    protected $identityProvider;
    
    /**
     * 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->identityProvider = $this->getMockForAbstractClass('BjyAuthorize\Provider\Identity\ProviderInterface', array('getIdentityRoles'));
        
        $this->helper->setIdentityProvider($this->identityProvider)
                     ->setTranslator(new Translator());
    }
    
    public function testEntryPointReturnsSelfInstance()
    {
        $this->assertSame($this->helper, $this->helper->__invoke());
    }
    
    public function getIdentityRolesAndExpectedScript()
    {
        return array(
            'none' => array(
                array(),
                'user_profile/none.phtml',
                'user_profile/none-translated.phtml',
            ),
            'role' => array(
                array(new \Zend\Permissions\Acl\Role\GenericRole('Invité')),
                'user_profile/role.phtml',
                'user_profile/role-translated.phtml',
            ),
            'named-role' => array(
                array(new \UnicaenAuth\Acl\NamedRole('admin', null, "Administrateur")),
                'user_profile/named-role.phtml',
                'user_profile/named-role-translated.phtml',
            ),
            'stringable-role' => array(
                array('Opérateur'), // (string) 'Opérateur' renverra 'Opérateur'
                'user_profile/stringable-role.phtml',
                'user_profile/stringable-role-translated.phtml',
            ),
            'non-stringable-role' => array(
                array(new \stdClass()), // (string) new \stdClass() lèvera l'erreur "Object of class stdClass could not be converted to string"
                'user_profile/non-stringable-role.phtml',
                'user_profile/non-stringable-role-translated.phtml',
            ),
        );
    }
    
    /**
     * @dataProvider getIdentityRolesAndExpectedScript
     * @param array $roles
     * @param string $expectedScript
     * @param string $expectedScriptTranslated
     */
    public function testRenderingReturnsNoneIfIdentityProviderProvidesNoRole($roles, $expectedScript, $expectedScriptTranslated)
    {
        $this->identityProvider
                ->expects($this->any())
                ->method('getIdentityRoles')
                ->will($this->returnValue($roles));
        
        $markup = (string) $this->helper;
        $this->assertEquals($this->getExpected($expectedScript), $markup);
        
        // traduction
        $this->helper->setTranslator($this->_getTranslator());
        $markup = (string) $this->helper;
        $this->assertEquals($this->getExpected($expectedScriptTranslated), $markup);
    }

    /**
     * Returns translator
     *
     * @return Translator
     */
    protected function _getTranslator()
    {
        $loader = new ArrayTranslatorLoader();
        $loader->translations = array(
            "Profil utilisateur" => "User profile",
            "Inconnu"            => "Unknown",
            "Aucun"              => "None",
            "Invité"             => "Guest",
            "Administrateur"     => "Administrator",
            "Opérateur"          => "Operator"
        );

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