Completed
Push — master ( d6b271...11b69a )
by André
66:29 queued 52:45
created

UserTest::testObjectProperties()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 21
Code Lines 15

Duplication

Lines 21
Ratio 100 %

Importance

Changes 0
Metric Value
cc 4
eloc 15
nc 4
nop 0
dl 21
loc 21
rs 9.0534
c 0
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\Core\Repository\Values\User\User;
13
use PHPUnit_Framework_TestCase;
14
15
/**
16
 * Test internal integrity of @see \eZ\Publish\Core\Repository\Values\User\User ValueObject.
17
 */
18
class UserTest extends PHPUnit_Framework_TestCase
19
{
20
    use ValueObjectTestTrait;
21
22
    /**
23
     * Test a new class and default values on properties.
24
     *
25
     * @covers \eZ\Publish\API\Repository\Values\User\User::__construct
26
     */
27
    public function testNewClass()
28
    {
29
        $user = new User();
30
31
        $this->assertPropertiesCorrect(
32
            [
33
                'login' => null,
34
                'email' => null,
35
                'passwordHash' => null,
36
                'hashAlgorithm' => null,
37
                'maxLogin' => null,
38
                'enabled' => false,
39
            ],
40
            $user
41
        );
42
    }
43
44
    /**
45
     * Test retrieving missing property.
46
     *
47
     * @covers \eZ\Publish\API\Repository\Values\User\User::__get
48
     * @expectedException \eZ\Publish\API\Repository\Exceptions\PropertyNotFoundException
49
     */
50
    public function testMissingProperty()
51
    {
52
        $user = new User();
53
        $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...
54
        self::fail('Succeeded getting non existing property');
55
    }
56
57
    /**
58
     * @covers \eZ\Publish\Core\Repository\Values\User\User::getProperties
59
     */
60 View Code Duplication
    public function testObjectProperties()
61
    {
62
        $object = new User();
63
        $properties = $object->attributes();
0 ignored issues
show
Deprecated Code introduced by
The method eZ\Publish\API\Repositor...lueObject::attributes() has been deprecated with message: Since 5.0, available purely for legacy eZTemplate compatibility

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
64
        self::assertNotContains('internalFields', $properties, 'Internal property found ');
65
        self::assertContains('id', $properties, 'Property not found ');
66
        self::assertContains('fields', $properties, 'Property not found ');
67
        self::assertContains('versionInfo', $properties, 'Property not found ');
68
        self::assertContains('contentInfo', $properties, 'Property not found ');
69
70
        // check for duplicates and double check existence of property
71
        $propertiesHash = [];
72
        foreach ($properties as $property) {
73
            if (isset($propertiesHash[$property])) {
74
                self::fail("Property '{$property}' exists several times in properties list");
75
            } elseif (!isset($object->$property)) {
76
                self::fail("Property '{$property}' does not exist on object, even though it was hinted to be there");
77
            }
78
            $propertiesHash[$property] = 1;
79
        }
80
    }
81
82
    /**
83
     * Test setting read only property.
84
     *
85
     * @covers \eZ\Publish\API\Repository\Values\User\User::__set
86
     * @expectedException \eZ\Publish\API\Repository\Exceptions\PropertyReadOnlyException
87
     */
88
    public function testReadOnlyProperty()
89
    {
90
        $user = new User();
91
        $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...
92
        self::fail('Succeeded setting read only property');
93
    }
94
95
    /**
96
     * Test if property exists.
97
     *
98
     * @covers \eZ\Publish\API\Repository\Values\User\User::__isset
99
     */
100
    public function testIsPropertySet()
101
    {
102
        $user = new User();
103
        $value = isset($user->notDefined);
104
        self::assertEquals(false, $value);
105
106
        $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...
107
        self::assertEquals(true, $value);
108
    }
109
110
    /**
111
     * Test unsetting a property.
112
     *
113
     * @covers \eZ\Publish\API\Repository\Values\User\User::__unset
114
     * @expectedException \eZ\Publish\API\Repository\Exceptions\PropertyReadOnlyException
115
     */
116
    public function testUnsetProperty()
117
    {
118
        $user = new User(['login' => 'admin']);
119
        unset($user->login);
120
        self::fail('Unsetting read-only property succeeded');
121
    }
122
}
123