Completed
Push — master ( df6049...d068c5 )
by Antonio
07:08 queued 04:48
created

AbstractMailObjectTest::testMagicMethods()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 16
Code Lines 11

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 16
rs 9.4286
cc 1
eloc 11
nc 1
nop 0
1
<?php
2
namespace Da\Mailer\Test\Model;
3
4
use Da\Mailer\Model\AbstractMailObject;
5
use PHPUnit_Framework_TestCase;
6
7
class AbstractMailObjectTest extends PHPUnit_Framework_TestCase
8
{
9
    private $object;
10
11
    protected function setUp()
12
    {
13
        $this->object = new TestMailObject(['property' => 'Darth Vader']);
14
    }
15
16
    public function testMagicMethods()
17
    {
18
        $value = 'Anakin Skywalker';
19
        $object = new TestMailObject(['property' => $value]);
20
21
        $this->assertTrue(isset($object->property));
0 ignored issues
show
Documentation introduced by
The property $property is declared private in Da\Mailer\Test\Model\TestMailObject. 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...
22
        $this->assertEquals($value, $object->property);
0 ignored issues
show
Documentation introduced by
The property $property is declared private in Da\Mailer\Test\Model\TestMailObject. 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...
23
        unset($object->property);
24
        $this->assertTrue(isset($object->property) === false);
0 ignored issues
show
Documentation introduced by
The property $property is declared private in Da\Mailer\Test\Model\TestMailObject. 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...
25
26
        $this->assertTrue(isset($object->unkownProperty) === false);
27
28
        $value = 'Princess Leia';
29
        $object->property = $value;
0 ignored issues
show
Documentation introduced by
The property $property is declared private in Da\Mailer\Test\Model\TestMailObject. 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...
30
        $this->assertEquals($value, $object->property);
0 ignored issues
show
Documentation introduced by
The property $property is declared private in Da\Mailer\Test\Model\TestMailObject. 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...
31
    }
32
33
    /**
34
     * @expectedException \Da\Mailer\Exception\InvalidCallException
35
     */
36
    public function testUnsetInvalidCallException()
37
    {
38
        unset($this->object->getterOnlyProperty);
39
    }
40
41
    /**
42
     * @expectedException \Da\Mailer\Exception\InvalidCallException
43
     */
44
    public function testGetInvalidCallException()
45
    {
46
        $test = $this->object->setterOnlyProperty;
0 ignored issues
show
Bug introduced by
The property setterOnlyProperty does not seem to exist. Did you mean property?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
Unused Code introduced by
$test 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...
47
    }
48
49
    /**
50
     * @expectedException \Da\Mailer\Exception\UnknownPropertyException
51
     */
52
    public function testGetUnknownPropertyException()
53
    {
54
        $test = $this->object->unkownProperty;
0 ignored issues
show
Bug introduced by
The property unkownProperty does not seem to exist. Did you mean property?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
Unused Code introduced by
$test 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...
55
    }
56
57
    /**
58
     * @expectedException \Da\Mailer\Exception\InvalidCallException
59
     */
60
    public function testSetInvalidCallException()
61
    {
62
        $this->object->getterOnlyProperty = 'I am your father!';
0 ignored issues
show
Bug introduced by
The property getterOnlyProperty does not seem to exist. Did you mean property?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
63
    }
64
65
    /**
66
     * @expectedException \Da\Mailer\Exception\UnknownPropertyException
67
     */
68
    public function testSetUnknownPropertyException()
69
    {
70
        $this->object->lukeResponse = 'Nooooooooo!';
0 ignored issues
show
Documentation introduced by
The property lukeResponse does not exist on object<Da\Mailer\Test\Model\TestMailObject>. 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...
71
    }
72
73
    public function testFromArrayMethod()
74
    {
75
        $config = ['property' => 'Anakin Skywalker'];
76
        $object = new TestMailObject($config);
77
        $objectFromArray = TestMailObject::fromArray($config);
78
79
        $this->assertEquals($object, $objectFromArray);
80
    }
81
}
82
83
class TestMailObject extends AbstractMailObject
84
{
85
86
    private $property;
87
88
    public function __construct(array $config = []) {
89
        parent::__construct($config);
90
    }
91
92
    public function getProperty()
93
    {
94
        return $this->property;
95
    }
96
97
    public function setProperty($value)
98
    {
99
        $this->property = $value;
100
    }
101
102
    public function setSetterOnlyProperty()
103
    {
104
105
    }
106
107
    public function getGetterOnlyProperty()
108
    {
109
110
    }
111
}
112