Completed
Push — master ( 11b69a...8cf83c )
by André
27:37 queued 12:55
created

UserGroupTest   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 120
Duplicated Lines 28.33 %

Coupling/Cohesion

Components 2
Dependencies 5

Importance

Changes 0
Metric Value
wmc 10
lcom 2
cbo 5
dl 34
loc 120
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A testNewClass() 0 14 1
A testGetName() 13 13 1
A testMissingProperty() 0 6 1
A testObjectProperties() 21 21 4
A testReadOnlyProperty() 0 6 1
A testIsPropertySet() 0 9 1
A testUnsetProperty() 0 6 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

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\API\Repository\Values\Content\Content;
13
use eZ\Publish\API\Repository\Values\Content\VersionInfo;
14
use eZ\Publish\Core\Repository\Values\User\UserGroup;
15
use Mockery;
16
use PHPUnit_Framework_TestCase;
17
18
/**
19
 * Test internal integrity of @see \eZ\Publish\Core\Repository\Values\User\UserGroup.
20
 */
21
class UserGroupTest extends PHPUnit_Framework_TestCase
22
{
23
    use ValueObjectTestTrait;
24
25
    /**
26
     * @covers \eZ\Publish\Core\Repository\Values\User\UserGroup::__construct
27
     */
28
    public function testNewClass()
29
    {
30
        $group = new UserGroup();
31
        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...
32
        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...
33
34
        $this->assertPropertiesCorrect(
35
            [
36
                'parentId' => null,
37
                'subGroupCount' => null,
38
            ],
39
            $group
40
        );
41
    }
42
43
    /**
44
     * Test getName method.
45
     *
46
     * @covers \eZ\Publish\Core\Repository\Values\User\UserGroup::getName
47
     */
48 View Code Duplication
    public function testGetName()
49
    {
50
        $name = 'Translated name';
51
        $contentMock = Mockery::mock(Content::class);
52
        $versionInfoMock = Mockery::mock(VersionInfo::class);
53
54
        $contentMock->shouldReceive('getVersionInfo')->andReturn($versionInfoMock);
55
        $versionInfoMock->shouldReceive('getName')->andReturn($name);
56
57
        $object = new UserGroup(['content' => $contentMock]);
58
59
        $this->assertEquals($name, $object->getName());
60
    }
61
62
    /**
63
     * Test retrieving missing property.
64
     *
65
     * @covers \eZ\Publish\API\Repository\Values\User\UserGroup::__get
66
     * @expectedException \eZ\Publish\API\Repository\Exceptions\PropertyNotFoundException
67
     */
68
    public function testMissingProperty()
69
    {
70
        $userGroup = new UserGroup();
71
        $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...
72
        self::fail('Succeeded getting non existing property');
73
    }
74
75
    /**
76
     * @covers \eZ\Publish\Core\Repository\Values\User\UserGroup::getProperties
77
     */
78 View Code Duplication
    public function testObjectProperties()
79
    {
80
        $object = new UserGroup();
81
        $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...
82
        self::assertNotContains('internalFields', $properties, 'Internal property found ');
83
        self::assertContains('id', $properties, 'Property not found ');
84
        self::assertContains('fields', $properties, 'Property not found ');
85
        self::assertContains('versionInfo', $properties, 'Property not found ');
86
        self::assertContains('contentInfo', $properties, 'Property not found ');
87
88
        // check for duplicates and double check existence of property
89
        $propertiesHash = [];
90
        foreach ($properties as $property) {
91
            if (isset($propertiesHash[$property])) {
92
                self::fail("Property '{$property}' exists several times in properties list");
93
            } elseif (!isset($object->$property)) {
94
                self::fail("Property '{$property}' does not exist on object, even though it was hinted to be there");
95
            }
96
            $propertiesHash[$property] = 1;
97
        }
98
    }
99
100
    /**
101
     * Test setting read only property.
102
     *
103
     * @covers \eZ\Publish\API\Repository\Values\User\UserGroup::__set
104
     * @expectedException \eZ\Publish\API\Repository\Exceptions\PropertyReadOnlyException
105
     */
106
    public function testReadOnlyProperty()
107
    {
108
        $userGroup = new UserGroup();
109
        $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...
110
        self::fail('Succeeded setting read only property');
111
    }
112
113
    /**
114
     * Test if property exists.
115
     *
116
     * @covers \eZ\Publish\API\Repository\Values\User\UserGroup::__isset
117
     */
118
    public function testIsPropertySet()
119
    {
120
        $userGroup = new UserGroup();
121
        $value = isset($userGroup->notDefined);
122
        self::assertEquals(false, $value);
123
124
        $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...
125
        self::assertEquals(true, $value);
126
    }
127
128
    /**
129
     * Test unsetting a property.
130
     *
131
     * @covers \eZ\Publish\API\Repository\Values\User\UserGroup::__unset
132
     * @expectedException \eZ\Publish\API\Repository\Exceptions\PropertyReadOnlyException
133
     */
134
    public function testUnsetProperty()
135
    {
136
        $userGroup = new UserGroup(['parentId' => 1]);
137
        unset($userGroup->parentId);
138
        self::fail('Unsetting read-only property succeeded');
139
    }
140
}
141