Passed
Branch master (8fa86f)
by Alexander
04:20
created

AttributesTest::testAttributes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 15
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 15
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 10
nc 1
nop 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: horat1us
5
 * Date: 5/2/17
6
 * Time: 4:41 PM
7
 */
8
9
namespace Horat1us\Tests;
10
11
12
use Horat1us\Examples\Person;
13
use Horat1us\XmlConvertibleObject;
14
15
class AttributesTest extends \PHPUnit_Framework_TestCase
16
{
17
    public function testWrongAttribute()
18
    {
19
        $xml = '<?xml version="1.0"?>
20
<Person name="Alexander" surname="Letnikow" middlename="Alexandrovich"><Head size="big" mind="small"/></Person>
21
';
22
23
        $document = new \DOMDocument();
24
        $document->loadXML($xml);
25
26
        $this->expectException(\UnexpectedValueException::class);
27
        $this->expectExceptionCode(4);
28
29
        Person::fromXml($document);
30
    }
31
32
    public function testAttributes()
33
    {
34
        $name = 'Alexander';
35
        $surname = 'Letnikow';
36
        $xml = '<?xml version="1.0"?>
37
<Person name="' . $name . '" surname="' . $surname . '" ><Head size="big" mind="small"/></Person>
38
';
39
40
        $document = new \DOMDocument();
41
        $document->loadXML($xml);
42
43
        $person = Person::fromXml($document);
44
        $this->assertEquals($name, $person->name);
45
        $this->assertEquals($surname, $person->surname);
46
    }
47
48
    public function testWithDefaultObject()
49
    {
50
        $name = 'Alexander';
51
        $surname = 'Letnikow';
52
        $xml = '<?xml version="1.0"?>
53
<Person name="' . $name . '" surname="' . $surname . '" ><Head size="big" mind="small"/></Person>
54
';
55
56
        $document = new \DOMDocument();
57
        $document->loadXML($xml);
58
59
        $person = XmlConvertibleObject::fromXml($document);
60
        $this->assertEquals($name, $person->name);
0 ignored issues
show
Bug introduced by
The property name does not seem to exist in Horat1us\XmlConvertibleObject.

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...
61
        $this->assertEquals($surname, $person->surname);
0 ignored issues
show
Bug introduced by
The property surname does not seem to exist in Horat1us\XmlConvertibleObject.

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...
62
    }
63
}