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

UserGroupTest::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 UserGroupTest 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\UserGroup;
13
use PHPUnit_Framework_TestCase;
14
15
/**
16
 * Test internal integrity of @see \eZ\Publish\Core\Repository\Values\User\UserGroup.
17
 */
18
class UserGroupTest extends PHPUnit_Framework_TestCase
19
{
20
    use ValueObjectTestTrait;
21
22
    /**
23
     * @covers \eZ\Publish\Core\Repository\Values\User\UserGroup::__construct
24
     */
25
    public function testNewClass()
26
    {
27
        $group = new UserGroup();
28
        self::assertEquals(null, $group->parentId);
0 ignored issues
show
Documentation introduced by
The property $parentId is declared protected in eZ\Publish\API\Repository\Values\User\UserGroup. 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...
29
        self::assertEquals(null, $group->subGroupCount);
0 ignored issues
show
Documentation introduced by
The property $subGroupCount is declared protected in eZ\Publish\API\Repository\Values\User\UserGroup. 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...
Deprecated Code introduced by
The property eZ\Publish\API\Repositor...erGroup::$subGroupCount has been deprecated with message: As of eZ Publish 5.3.3, count can be obtained on demand using location service.

This property 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 property will be removed from the class and what other property to use instead.

Loading history...
30
31
        $this->assertPropertiesCorrect(
32
            [
33
                'parentId' => null,
34
                'subGroupCount' => null,
35
            ],
36
            $group
37
        );
38
    }
39
40
    /**
41
     * Test retrieving missing property.
42
     *
43
     * @covers \eZ\Publish\API\Repository\Values\User\UserGroup::__get
44
     * @expectedException \eZ\Publish\API\Repository\Exceptions\PropertyNotFoundException
45
     */
46
    public function testMissingProperty()
47
    {
48
        $userGroup = new UserGroup();
49
        $value = $userGroup->notDefined;
0 ignored issues
show
Documentation introduced by
The property notDefined does not exist on object<eZ\Publish\Core\R...\Values\User\UserGroup>. 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...
50
        self::fail('Succeeded getting non existing property');
51
    }
52
53
    /**
54
     * @covers \eZ\Publish\Core\Repository\Values\User\UserGroup::getProperties
55
     */
56 View Code Duplication
    public function testObjectProperties()
57
    {
58
        $object = new UserGroup();
59
        $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...
60
        self::assertNotContains('internalFields', $properties, 'Internal property found ');
61
        self::assertContains('id', $properties, 'Property not found ');
62
        self::assertContains('fields', $properties, 'Property not found ');
63
        self::assertContains('versionInfo', $properties, 'Property not found ');
64
        self::assertContains('contentInfo', $properties, 'Property not found ');
65
66
        // check for duplicates and double check existence of property
67
        $propertiesHash = [];
68
        foreach ($properties as $property) {
69
            if (isset($propertiesHash[$property])) {
70
                self::fail("Property '{$property}' exists several times in properties list");
71
            } elseif (!isset($object->$property)) {
72
                self::fail("Property '{$property}' does not exist on object, even though it was hinted to be there");
73
            }
74
            $propertiesHash[$property] = 1;
75
        }
76
    }
77
78
    /**
79
     * Test setting read only property.
80
     *
81
     * @covers \eZ\Publish\API\Repository\Values\User\UserGroup::__set
82
     * @expectedException \eZ\Publish\API\Repository\Exceptions\PropertyReadOnlyException
83
     */
84
    public function testReadOnlyProperty()
85
    {
86
        $userGroup = new UserGroup();
87
        $userGroup->parentId = 42;
0 ignored issues
show
Documentation introduced by
The property $parentId is declared protected in eZ\Publish\API\Repository\Values\User\UserGroup. 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...
88
        self::fail('Succeeded setting read only property');
89
    }
90
91
    /**
92
     * Test if property exists.
93
     *
94
     * @covers \eZ\Publish\API\Repository\Values\User\UserGroup::__isset
95
     */
96
    public function testIsPropertySet()
97
    {
98
        $userGroup = new UserGroup();
99
        $value = isset($userGroup->notDefined);
100
        self::assertEquals(false, $value);
101
102
        $value = isset($userGroup->parentId);
0 ignored issues
show
Documentation introduced by
The property $parentId is declared protected in eZ\Publish\API\Repository\Values\User\UserGroup. 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...
103
        self::assertEquals(true, $value);
104
    }
105
106
    /**
107
     * Test unsetting a property.
108
     *
109
     * @covers \eZ\Publish\API\Repository\Values\User\UserGroup::__unset
110
     * @expectedException \eZ\Publish\API\Repository\Exceptions\PropertyReadOnlyException
111
     */
112
    public function testUnsetProperty()
113
    {
114
        $userGroup = new UserGroup(['parentId' => 1]);
115
        unset($userGroup->parentId);
116
        self::fail('Unsetting read-only property succeeded');
117
    }
118
}
119