Completed
Push — api_content_name_shorthand ( e0a75f )
by André
17:01
created

UserGroupTest::testGetName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 13
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 8
nc 1
nop 0
dl 13
loc 13
rs 9.4285
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 Mockery;
14
use PHPUnit_Framework_TestCase;
15
16
/**
17
 * Test internal integrity of @see \eZ\Publish\Core\Repository\Values\User\UserGroup.
18
 */
19
class UserGroupTest extends PHPUnit_Framework_TestCase
20
{
21
    use ValueObjectTestTrait;
22
23
    /**
24
     * @covers \eZ\Publish\Core\Repository\Values\User\UserGroup::__construct
25
     */
26
    public function testNewClass()
27
    {
28
        $group = new UserGroup();
29
        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...
30
        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...
31
32
        $this->assertPropertiesCorrect(
33
            [
34
                'parentId' => null,
35
                'subGroupCount' => null,
36
            ],
37
            $group
38
        );
39
    }
40
41
    /**
42
     * Test a new class and default values on properties.
43
     *
44
     * @covers \eZ\Publish\API\Repository\Values\User\User::__construct
45
     */
46 View Code Duplication
    public function testGetName()
47
    {
48
        $name = 'Translated name';
49
        $contentMock = Mockery::mock('eZ\Publish\API\Repository\Values\Content\Content');
50
        $versionInfoMock = Mockery::mock('eZ\Publish\API\Repository\Values\Content\VersionInfo');
51
52
        $contentMock->shouldReceive('getVersionInfo')->andReturn($versionInfoMock);
53
        $versionInfoMock->shouldReceive('getName')->andReturn($name);
54
55
        $object = new UserGroup(['content' => $contentMock]);
56
57
        $this->assertEquals($name, $object->getName());
58
    }
59
60
    /**
61
     * Test retrieving missing property.
62
     *
63
     * @covers \eZ\Publish\API\Repository\Values\User\UserGroup::__get
64
     * @expectedException \eZ\Publish\API\Repository\Exceptions\PropertyNotFoundException
65
     */
66
    public function testMissingProperty()
67
    {
68
        $userGroup = new UserGroup();
69
        $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...
70
        self::fail('Succeeded getting non existing property');
71
    }
72
73
    /**
74
     * Test setting read only property.
75
     *
76
     * @covers \eZ\Publish\API\Repository\Values\User\UserGroup::__set
77
     * @expectedException \eZ\Publish\API\Repository\Exceptions\PropertyReadOnlyException
78
     */
79
    public function testReadOnlyProperty()
80
    {
81
        $userGroup = new UserGroup();
82
        $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...
83
        self::fail('Succeeded setting read only property');
84
    }
85
86
    /**
87
     * Test if property exists.
88
     *
89
     * @covers \eZ\Publish\API\Repository\Values\User\UserGroup::__isset
90
     */
91
    public function testIsPropertySet()
92
    {
93
        $userGroup = new UserGroup();
94
        $value = isset($userGroup->notDefined);
95
        self::assertEquals(false, $value);
96
97
        $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...
98
        self::assertEquals(true, $value);
99
    }
100
101
    /**
102
     * Test unsetting a property.
103
     *
104
     * @covers \eZ\Publish\API\Repository\Values\User\UserGroup::__unset
105
     * @expectedException \eZ\Publish\API\Repository\Exceptions\PropertyReadOnlyException
106
     */
107
    public function testUnsetProperty()
108
    {
109
        $userGroup = new UserGroup(['parentId' => 1]);
110
        unset($userGroup->parentId);
111
        self::fail('Unsetting read-only property succeeded');
112
    }
113
}
114