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

TrashItemTest::testNewClass()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 18
nc 1
nop 0
dl 0
loc 25
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
/**
4
 * File containing the TrashItemTest 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\Content;
10
11
use eZ\Publish\API\Repository\Tests\Values\ValueObjectTestTrait;
12
use eZ\Publish\API\Repository\Values\Content\ContentInfo;
13
use eZ\Publish\Core\Repository\Values\Content\TrashItem;
14
use PHPUnit_Framework_TestCase;
15
16
/**
17
 *
18
 */
19
class TrashItemTest extends PHPUnit_Framework_TestCase
20
{
21
    use ValueObjectTestTrait;
22
23
    /**
24
     * @covers \eZ\Publish\Core\Repository\Values\Content\TrashItem::__construct
25
     */
26
    public function testNewClass()
27
    {
28
        // create ContentInfo to be able to retrieve the contentId property via magic method
29
        $contentInfo = new ContentInfo();
30
        $trashItem = new TrashItem(['contentInfo' => $contentInfo]);
31
32
        $this->assertPropertiesCorrect(
33
            [
34
                'contentInfo' => $contentInfo,
35
                'contentId' => null,
36
                'id' => null,
37
                'priority' => null,
38
                'hidden' => null,
39
                'invisible' => null,
40
                'remoteId' => null,
41
                'parentLocationId' => null,
42
                'pathString' => null,
43
                'path' => [],
44
                'depth' => null,
45
                'sortField' => null,
46
                'sortOrder' => null,
47
            ],
48
            $trashItem
49
        );
50
    }
51
52
    /**
53
     * Test retrieving missing property.
54
     *
55
     * @covers \eZ\Publish\API\Repository\Values\Content\TrashItem::__get
56
     * @expectedException \eZ\Publish\API\Repository\Exceptions\PropertyNotFoundException
57
     */
58
    public function testMissingProperty()
59
    {
60
        $trashItem = new TrashItem();
61
        $value = $trashItem->notDefined;
0 ignored issues
show
Documentation introduced by
The property notDefined does not exist on object<eZ\Publish\Core\R...lues\Content\TrashItem>. 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...
62
        self::fail('Succeeded getting non existing property');
63
    }
64
65
    /**
66
     * Test setting read only property.
67
     *
68
     * @covers \eZ\Publish\API\Repository\Values\Content\TrashItem::__set
69
     * @expectedException \eZ\Publish\API\Repository\Exceptions\PropertyReadOnlyException
70
     */
71
    public function testReadOnlyProperty()
72
    {
73
        $trashItem = new TrashItem();
74
        $trashItem->id = 42;
0 ignored issues
show
Documentation introduced by
The property $id is declared protected in eZ\Publish\API\Repository\Values\Content\Location. 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...
75
        self::fail('Succeeded setting read only property');
76
    }
77
78
    /**
79
     * Test if property exists.
80
     *
81
     * @covers \eZ\Publish\API\Repository\Values\Content\TrashItem::__isset
82
     */
83
    public function testIsPropertySet()
84
    {
85
        $trashItem = new TrashItem();
86
        $value = isset($trashItem->notDefined);
87
        self::assertEquals(false, $value);
88
89
        $value = isset($trashItem->id);
0 ignored issues
show
Documentation introduced by
The property $id is declared protected in eZ\Publish\API\Repository\Values\Content\Location. 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...
90
        self::assertEquals(true, $value);
91
    }
92
93
    /**
94
     * Test unsetting a property.
95
     *
96
     * @covers \eZ\Publish\API\Repository\Values\Content\TrashItem::__unset
97
     * @expectedException \eZ\Publish\API\Repository\Exceptions\PropertyReadOnlyException
98
     */
99
    public function testUnsetProperty()
100
    {
101
        $trashItem = new TrashItem(['id' => 2]);
102
        unset($trashItem->id);
103
        self::fail('Unsetting read-only property succeeded');
104
    }
105
}
106