Completed
Push — 6.10 ( 0b123e...da1d28 )
by André
13:41
created

UserTest::testGetName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 13
Ratio 100 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 8
nc 1
nop 0
dl 13
loc 13
rs 9.4285
c 1
b 0
f 0
1
<?php
2
3
/**
4
 * File containing the UserTest class.
5
 *
6
 * @copyright Copyright (C) eZ Systems AS. All rights reserved.
7
 * @license For full copyright and license information view LICENSE file distributed with this source code.
8
 */
9
namespace eZ\Publish\Core\Repository\Tests\Values\User;
10
11
use eZ\Publish\API\Repository\Tests\Values\ValueObjectTestTrait;
12
use eZ\Publish\API\Repository\Values\Content\Content;
13
use eZ\Publish\API\Repository\Values\Content\VersionInfo;
14
use eZ\Publish\Core\Repository\Values\User\User;
15
use Mockery;
16
use PHPUnit_Framework_TestCase;
17
18
/**
19
 * Test internal integrity of @see \eZ\Publish\Core\Repository\Values\User\User ValueObject.
20
 */
21
class UserTest extends PHPUnit_Framework_TestCase
22
{
23
    use ValueObjectTestTrait;
24
25
    /**
26
     * Test a new class and default values on properties.
27
     *
28
     * @covers \eZ\Publish\Core\Repository\Values\User\User::__construct
29
     */
30
    public function testNewClass()
31
    {
32
        $user = new User();
33
34
        $this->assertPropertiesCorrect(
35
            [
36
                'login' => null,
37
                'email' => null,
38
                'passwordHash' => null,
39
                'hashAlgorithm' => null,
40
                'maxLogin' => null,
41
                'enabled' => false,
42
            ],
43
            $user
44
        );
45
    }
46
47
    /**
48
     * Test getName method.
49
     *
50
     * @covers \eZ\Publish\Core\Repository\Values\User\User::getName
51
     */
52 View Code Duplication
    public function testGetName()
53
    {
54
        $name = 'Translated name';
55
        $contentMock = Mockery::mock(Content::class);
56
        $versionInfoMock = Mockery::mock(VersionInfo::class);
57
58
        $contentMock->shouldReceive('getVersionInfo')->andReturn($versionInfoMock);
59
        $versionInfoMock->shouldReceive('getName')->andReturn($name);
60
61
        $object = new User(['content' => $contentMock]);
62
63
        $this->assertEquals($name, $object->getName());
64
    }
65
66
    /**
67
     * Test retrieving missing property.
68
     *
69
     * @covers \eZ\Publish\API\Repository\Values\User\User::__get
70
     * @expectedException \eZ\Publish\API\Repository\Exceptions\PropertyNotFoundException
71
     */
72
    public function testMissingProperty()
73
    {
74
        $user = new User();
75
        $value = $user->notDefined;
0 ignored issues
show
Documentation introduced by
The property notDefined does not exist on object<eZ\Publish\Core\R...itory\Values\User\User>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
Unused Code introduced by
$value is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
76
        self::fail('Succeeded getting non existing property');
77
    }
78
79
    /**
80
     * Test setting read only property.
81
     *
82
     * @covers \eZ\Publish\API\Repository\Values\User\User::__set
83
     * @expectedException \eZ\Publish\API\Repository\Exceptions\PropertyReadOnlyException
84
     */
85
    public function testReadOnlyProperty()
86
    {
87
        $user = new User();
88
        $user->login = 'user';
0 ignored issues
show
Documentation introduced by
The property $login is declared protected in eZ\Publish\API\Repository\Values\User\User. Since you implemented __set(), maybe consider adding a @property or @property-write annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
89
        self::fail('Succeeded setting read only property');
90
    }
91
92
    /**
93
     * Test if property exists.
94
     *
95
     * @covers \eZ\Publish\API\Repository\Values\User\User::__isset
96
     */
97
    public function testIsPropertySet()
98
    {
99
        $user = new User();
100
        $value = isset($user->notDefined);
101
        self::assertEquals(false, $value);
102
103
        $value = isset($user->login);
0 ignored issues
show
Documentation introduced by
The property $login is declared protected in eZ\Publish\API\Repository\Values\User\User. Since you implemented __get(), maybe consider adding a @property or @property-read annotation. This makes it easier for IDEs to provide auto-completion.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
104
        self::assertEquals(true, $value);
105
    }
106
107
    /**
108
     * Test unsetting a property.
109
     *
110
     * @covers \eZ\Publish\API\Repository\Values\User\User::__unset
111
     * @expectedException \eZ\Publish\API\Repository\Exceptions\PropertyReadOnlyException
112
     */
113
    public function testUnsetProperty()
114
    {
115
        $user = new User(['login' => 'admin']);
116
        unset($user->login);
117
        self::fail('Unsetting read-only property succeeded');
118
    }
119
}
120