Completed
Push — master ( a29d2d...4681dd )
by Hannes
10:53 queued 08:08
created

NodeSpec   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 0
Metric Value
wmc 11
lcom 0
cbo 2
dl 0
loc 78
rs 10
c 0
b 0
f 0
1
<?php
2
3
declare(strict_types = 1);
4
5
namespace spec\byrokrat\autogiro\Tree;
6
7
use byrokrat\autogiro\Tree\Node;
8
use byrokrat\autogiro\Visitor;
9
use byrokrat\autogiro\Exception\LogicException;
10
use PhpSpec\ObjectBehavior;
11
12
class NodeSpec extends ObjectBehavior
13
{
14
    function it_is_initializable()
15
    {
16
        $this->shouldHaveType(Node::CLASS);
17
    }
18
19
    function it_contains_a_type()
20
    {
21
        $this->getType()->shouldEqual('Node');
22
    }
23
24
    function it_contains_a_line_number()
25
    {
26
        $this->beConstructedWith(5);
27
        $this->getLineNr()->shouldEqual(5);
28
    }
29
30
    function it_contains_a_value()
31
    {
32
        $this->beConstructedWith(1, 'foobar');
33
        $this->getValue()->shouldEqual('foobar');
34
    }
35
36
    function it_accepts_a_visitor(Visitor $visitor, Node $node)
37
    {
38
        $this->setChild('node', $node);
39
40
        $this->accept($visitor);
41
42
        $visitor->visitBefore($this)->shouldHaveBeenCalled();
43
        $node->accept($visitor)->shouldHaveBeenCalled();
44
        $visitor->visitAfter($this)->shouldHaveBeenCalled();
45
    }
46
47
    function it_can_save_attributes()
48
    {
49
        $this->hasAttribute('key')->shouldEqual(false);
50
        $this->setAttribute('key', 'value');
51
        $this->hasAttribute('key')->shouldEqual(true);
52
        $this->getAttribute('key')->shouldEqual('value');
53
    }
54
55
    function it_defualts_attributes_to_null()
56
    {
57
        $this->getAttribute('does-not-exist')->shouldEqual(null);
58
    }
59
60
    function it_can_iterate_over_attributes()
61
    {
62
        $this->setAttribute('foo', 'bar');
63
        $this->setAttribute('bar', 'foo');
64
        $this->getAttributes()->shouldEqual([
65
            'foo' => 'bar',
66
            'bar' => 'foo'
67
        ]);
68
    }
69
70
    function it_can_save_children(Node $node)
71
    {
72
        $this->hasChild('key')->shouldEqual(false);
73
        $this->setChild('key', $node);
74
        $this->hasChild('key')->shouldEqual(true);
75
        $this->getChild('key')->shouldEqual($node);
76
    }
77
78
    function it_throws_exception_on_unknown_child()
79
    {
80
        $this->shouldThrow(LogicException::CLASS)->duringGetChild('does-not-exist');
81
    }
82
83
    function it_can_iterate_over_child_nodes(Node $node)
84
    {
85
        $this->setChild('node', $node);
86
        $this->getChildren()->shouldEqual([
87
            'node' => $node
88
        ]);
89
    }
90
}
91