Completed
Push — master ( 7c0e42...8c347f )
by André
14:17
created

ObjectStateGroupTest::testNewClass()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 10

Duplication

Lines 16
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 10
nc 1
nop 0
dl 16
loc 16
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * This file is part of the eZ Publish Kernel package.
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\ObjectState;
10
11
use eZ\Publish\API\Repository\Tests\Values\ValueObjectTestTrait;
12
use eZ\Publish\Core\Repository\Values\ObjectState\ObjectStateGroup;
13
use PHPUnit_Framework_TestCase;
14
15
/**
16
 * Test internal integrity of @see \eZ\Publish\Core\Repository\Values\ObjectState\ObjectStateGroup ValueObject.
17
 */
18 View Code Duplication
class ObjectStateGroupTest extends PHPUnit_Framework_TestCase
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
19
{
20
    use ValueObjectTestTrait;
21
22
    /**
23
     * Test a new class and default values on properties.
24
     *
25
     * @covers \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup::__construct
26
     */
27
    public function testNewClass()
28
    {
29
        $objectStateGroup = new ObjectStateGroup();
30
31
        $this->assertPropertiesCorrect(
32
            [
33
                'id' => null,
34
                'identifier' => null,
35
                'defaultLanguageCode' => null,
36
                'languageCodes' => null,
37
                'names' => [],
38
                'descriptions' => [],
39
            ],
40
            $objectStateGroup
41
        );
42
    }
43
44
    /**
45
     * Test retrieving missing property.
46
     *
47
     * @covers \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup::__get
48
     * @expectedException \eZ\Publish\API\Repository\Exceptions\PropertyNotFoundException
49
     */
50
    public function testMissingProperty()
51
    {
52
        $objectStateGroup = new ObjectStateGroup();
53
        $value = $objectStateGroup->notDefined;
0 ignored issues
show
Documentation introduced by
The property notDefined does not exist on object<eZ\Publish\Core\R...State\ObjectStateGroup>. 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
        $this->fail('Succeeded getting non existing property');
55
    }
56
57
    /**
58
     * Test setting read only property.
59
     *
60
     * @covers \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup::__set
61
     * @expectedException \eZ\Publish\API\Repository\Exceptions\PropertyReadOnlyException
62
     */
63
    public function testReadOnlyProperty()
64
    {
65
        $objectStateGroup = new ObjectStateGroup();
66
        $objectStateGroup->id = 42;
67
        $this->fail('Succeeded setting read only property');
68
    }
69
70
    /**
71
     * Test if property exists.
72
     *
73
     * @covers \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup::__isset
74
     */
75
    public function testIsPropertySet()
76
    {
77
        $objectStateGroup = new ObjectStateGroup();
78
        $value = isset($objectStateGroup->notDefined);
79
        $this->assertEquals(false, $value);
80
81
        $value = isset($objectStateGroup->id);
82
        $this->assertEquals(true, $value);
83
    }
84
85
    /**
86
     * Test unsetting a property.
87
     *
88
     * @covers \eZ\Publish\API\Repository\Values\ObjectState\ObjectStateGroup::__unset
89
     * @expectedException \eZ\Publish\API\Repository\Exceptions\PropertyReadOnlyException
90
     */
91
    public function testUnsetProperty()
92
    {
93
        $objectStateGroup = new ObjectStateGroup(['id' => 2]);
94
        unset($objectStateGroup->id);
95
        $this->fail('Unsetting read-only property succeeded');
96
    }
97
}
98