Passed
Push — master ( 4f9e3a...139be2 )
by Alexander
23:24
created

ElementAttributesTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 49
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 6
lcom 1
cbo 2
dl 0
loc 49
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Horat1us\Tests;
4
5
use Horat1us\XmlConvertible;
6
use Horat1us\XmlConvertibleInterface;
7
use PHPUnit\Framework\TestCase;
8
9
class ElementAttributesTest extends TestCase implements XmlConvertibleInterface
10
{
11
    use XmlConvertible {
12
        getXmlProperties as traitGetXmlProperties;
13
    }
14
15
    public $attributes = [];
16
17
    public function testWithout()
18
    {
19
        $xml = $this->toXml();
20
        $this->assertEquals(0, $xml->attributes->length);
21
    }
22
23
    public function testFew()
24
    {
25
        $this->attributes = [
26
            'a' => 1,
27
            'b' => 2,
28
            // This attribute must be ignored
29
            'array' => [1,2,3],
30
            'null' => null,
31
            'object' => (object)[1,2,3],
32
         ];
33
        $xml = $this->toXml();
34
        $this->assertEquals(2, $xml->attributes->length);
35
36
        // Removing not-convertible attribute
37
        $attributes = array_splice($this->attributes, 0, 2);
38
        foreach ($attributes as $name => $value) {
39
            $this->assertEquals($value, $xml->getAttribute($name));
40
        }
41
    }
42
43
44
    public function getXmlProperties(?array $properties = null): array
45
    {
46
        if (empty($this->attributes)) {
47
            return $this->traitGetXmlProperties();
48
        }
49
        return array_keys($this->attributes);
50
    }
51
52
    public function __get($name)
53
    {
54
        $this->assertArrayHasKey($name, $this->attributes);
55
        return $this->attributes[$name];
56
    }
57
}
58