AttributeTest   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 180
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 8
lcom 1
cbo 2
dl 0
loc 180
rs 10
c 0
b 0
f 0

8 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 4 1
A testConstruct() 0 10 1
A testKey() 0 18 1
A testValue() 0 18 1
A testIsValueInHtml() 0 14 1
A testToString() 0 30 1
A testToStringWithSpecials() 0 23 1
A testIsValueContainingSpecials() 0 8 1
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * phpDocumentor
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 *
11
 * @link      http://phpdoc.org
12
 */
13
14
namespace phpDocumentor\GraphViz\Test;
15
16
use phpDocumentor\GraphViz\Attribute;
17
use PHPUnit\Framework\TestCase;
18
19
/**
20
 * Test for the the class representing a GraphViz attribute.
21
 */
22
class AttributeTest extends TestCase
23
{
24
    /** @var Attribute */
25
    protected $fixture = null;
26
27
    /**
28
     * Initializes the fixture for this test.
29
     */
30
    protected function setUp() : void
31
    {
32
        $this->fixture = new Attribute('a', '1');
33
    }
34
35
    /**
36
     * Tests the construct method
37
     *
38
     * @covers \phpDocumentor\GraphViz\Attribute::__construct
39
     * @returnn void
40
     */
41
    public function testConstruct() : void
42
    {
43
        $fixture = new Attribute('MyKey', 'MyValue');
44
        $this->assertInstanceOf(
45
            Attribute::class,
46
            $fixture
47
        );
48
        $this->assertSame('MyKey', $fixture->getKey());
49
        $this->assertSame('MyValue', $fixture->getValue());
50
    }
51
52
    /**
53
     * Tests the getting and setting of the key.
54
     *
55
     * @covers \phpDocumentor\GraphViz\Attribute::getKey
56
     * @covers \phpDocumentor\GraphViz\Attribute::setKey
57
     */
58
    public function testKey() : void
59
    {
60
        $this->assertSame(
61
            $this->fixture->getKey(),
62
            'a',
63
            'Expecting the key to match the initial state'
64
        );
65
        $this->assertSame(
66
            $this->fixture,
67
            $this->fixture->setKey('b'),
68
            'Expecting a fluent interface'
69
        );
70
        $this->assertSame(
71
            $this->fixture->getKey(),
72
            'b',
73
            'Expecting the key to contain the new value'
74
        );
75
    }
76
77
    /**
78
     * Tests the getting and setting of the value.
79
     *
80
     * @covers \phpDocumentor\GraphViz\Attribute::getValue
81
     * @covers \phpDocumentor\GraphViz\Attribute::setValue
82
     */
83
    public function testValue() : void
84
    {
85
        $this->assertSame(
86
            $this->fixture->getValue(),
87
            '1',
88
            'Expecting the value to match the initial state'
89
        );
90
        $this->assertSame(
91
            $this->fixture,
92
            $this->fixture->setValue('2'),
93
            'Expecting a fluent interface'
94
        );
95
        $this->assertSame(
96
            $this->fixture->getValue(),
97
            '2',
98
            'Expecting the value to contain the new value'
99
        );
100
    }
101
102
    /**
103
     * Tests whether a string starting with a < is recognized as HTML.
104
     *
105
     * @covers \phpDocumentor\GraphViz\Attribute::isValueInHtml
106
     */
107
    public function testIsValueInHtml() : void
108
    {
109
        $this->fixture->setValue('a');
110
        $this->assertFalse(
111
            $this->fixture->isValueInHtml(),
112
            'Expected value to not be a HTML code'
113
        );
114
115
        $this->fixture->setValue('<a>test</a>');
116
        $this->assertTrue(
117
            $this->fixture->isValueInHtml(),
118
            'Expected value to be recognized as a HTML code'
119
        );
120
    }
121
122
    /**
123
     * Tests whether the toString provides a valid GraphViz attribute string.
124
     *
125
     * @covers \phpDocumentor\GraphViz\Attribute::__toString
126
     */
127
    public function testToString() : void
128
    {
129
        $this->fixture = new Attribute('a', 'b');
130
        $this->assertSame(
131
            'a="b"',
132
            (string) $this->fixture,
133
            'Strings should be surrounded with quotes'
134
        );
135
136
        $this->fixture->setValue('a"a');
137
        $this->assertSame(
138
            'a="a\"a"',
139
            (string) $this->fixture,
140
            'Strings should be surrounded with quotes'
141
        );
142
143
        $this->fixture->setKey('url');
144
        $this->assertSame(
145
            'URL="a\"a"',
146
            (string) $this->fixture,
147
            'The key named URL must be uppercased'
148
        );
149
150
        $this->fixture->setValue('<a>test</a>');
151
        $this->assertSame(
152
            'URL=<a>test</a>',
153
            (string) $this->fixture,
154
            'HTML strings should not be surrounded with quotes'
155
        );
156
    }
157
158
    /**
159
     * Tests whether the toString provides a valid GraphViz attribute string.
160
     *
161
     * @covers \phpDocumentor\GraphViz\Attribute::__toString
162
     * @covers \phpDocumentor\GraphViz\Attribute::encodeSpecials
163
     */
164
    public function testToStringWithSpecials() : void
165
    {
166
        $this->fixture = new Attribute('a', 'b');
167
168
        $this->fixture->setValue('a\la');
169
        $this->assertSame(
170
            'a="a\la"',
171
            (string) $this->fixture,
172
            'Specials should not be escaped'
173
        );
174
        $this->fixture->setValue('a\l"a');
175
        $this->assertSame(
176
            'a="a\l\"a"',
177
            (string) $this->fixture,
178
            'Specials should not be escaped, but quotes should'
179
        );
180
        $this->fixture->setValue('a\\\\l"a');
181
        $this->assertSame(
182
            'a="a\\\\l\"a"',
183
            (string) $this->fixture,
184
            'Double backslashes should stay the same'
185
        );
186
    }
187
188
    /**
189
     * Tests whether the isValueContainingSpecials function
190
     *
191
     * @covers \phpDocumentor\GraphViz\Attribute::isValueContainingSpecials
192
     */
193
    public function testIsValueContainingSpecials() : void
194
    {
195
        $this->fixture->setValue('+ name : string\l+ home_country : string\l');
196
        $this->assertTrue($this->fixture->isValueContainingSpecials());
197
198
        $this->fixture->setValue('+ ship(): boolean');
199
        $this->assertFalse($this->fixture->isValueContainingSpecials());
200
    }
201
}
202