Completed
Pull Request — master (#26)
by Simon
02:52
created

GraphTest::testSetPath()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 8
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
/**
3
 * phpDocumentor
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 *
8
 * @package   phpDocumentor\GraphViz\Tests
9
 * @author    Danny van der Sluijs <[email protected]>
10
 * @copyright 2012-2018 Danny van der Sluijs (http://www.fleppuhstein.com)
11
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
12
 * @link      http://phpdoc.org
13
 */
14
15
namespace phpDocumentor\GraphViz\Test;
16
17
use Mockery as m;
18
use phpDocumentor\GraphViz\AttributeNotFound;
19
use phpDocumentor\GraphViz\Edge;
20
use phpDocumentor\GraphViz\Graph;
21
use phpDocumentor\GraphViz\Node;
22
use PHPUnit\Framework\TestCase;
23
24
/**
25
 * Test for the the class representing a GraphViz graph.
26
 *
27
 * @package phpDocumentor\GraphViz\Tests
28
 * @author  Mike van Riel <[email protected]>
29
 * @license http://www.opensource.org/licenses/mit-license.php MIT
30
 * @link    http://phpdoc.org
31
 */
32
class GraphTest extends TestCase
33
{
34
    /**
35
     * @var Graph
36
     */
37
    protected $fixture;
38
39
    /**
40
     * Sets up the fixture, for example, opens a network connection.
41
     * This method is called before a test is executed.
42
     */
43
    protected function setUp()
44
    {
45
        $this->fixture = new Graph();
46
    }
47
48
    /**
49
     * Tears down the fixture, for example, closes a network connection.
50
     * This method is called after a test is executed.
51
     */
52
    protected function tearDown()
53
    {
54
        m::close();
55
    }
56
57
    /**
58
     * @covers \phpDocumentor\GraphViz\Graph::create
59
     */
60
    public function testCreate()
61
    {
62
        $fixture = Graph::create();
63
        $this->assertInstanceOf(
64
            Graph::class,
65
            $fixture
66
        );
67
        $this->assertSame(
68
            'G',
69
            $fixture->getName()
70
        );
71
        $this->assertSame(
72
            'digraph',
73
            $fixture->getType()
74
        );
75
76
        $fixture = Graph::create('MyName', false);
77
        $this->assertSame(
78
            'MyName',
79
            $fixture->getName()
80
        );
81
        $this->assertSame(
82
            'graph',
83
            $fixture->getType()
84
        );
85
    }
86
87
    /**
88
     * @covers \phpDocumentor\GraphViz\Graph::setName
89
     */
90
    public function testSetName()
91
    {
92
        $this->assertSame(
93
            $this->fixture,
94
            $this->fixture->setName('otherName'),
95
            'Expecting a fluent interface'
96
        );
97
    }
98
99
    /**
100
     * @covers \phpDocumentor\GraphViz\Graph::getName
101
     */
102
    public function testGetName()
103
    {
104
        $this->assertSame(
105
            $this->fixture->getName(),
106
            'G',
107
            'Expecting the name to match the initial state'
108
        );
109
        $this->fixture->setName('otherName');
110
        $this->assertSame(
111
            $this->fixture->getName(),
112
            'otherName',
113
            'Expecting the name to contain the new value'
114
        );
115
    }
116
117
    /**
118
     * @covers \phpDocumentor\GraphViz\Graph::setType
119
     */
120
    public function testSetType()
121
    {
122
        $this->assertSame(
123
            $this->fixture,
124
            $this->fixture->setType('digraph'),
125
            'Expecting a fluent interface'
126
        );
127
        $this->assertSame(
128
            $this->fixture,
129
            $this->fixture->setType('graph'),
130
            'Expecting a fluent interface'
131
        );
132
        $this->assertSame(
133
            $this->fixture,
134
            $this->fixture->setType('subgraph'),
135
            'Expecting a fluent interface'
136
        );
137
    }
138
139
    /**
140
     * @covers \phpDocumentor\GraphViz\Graph::setType
141
     */
142
    public function testSetTypeException()
143
    {
144
        $this->expectException(\InvalidArgumentException::class);
145
        $this->fixture->setType('fakegraphg');
146
    }
147
148
    /**
149
     * @covers \phpDocumentor\GraphViz\Graph::getType
150
     */
151
    public function testGetType()
152
    {
153
        $this->assertSame(
154
            $this->fixture->getType(),
155
            'digraph'
156
        );
157
        $this->fixture->setType('graph');
158
        $this->assertSame(
159
            $this->fixture->getType(),
160
            'graph'
161
        );
162
    }
163
164
    public function testSetStrict()
165
    {
166
        $this->assertSame(
167
            $this->fixture,
168
            $this->fixture->setStrict(true),
169
            'Expecting a fluent interface'
170
        );
171
        $this->assertSame(
172
            $this->fixture,
173
            $this->fixture->setStrict(false),
174
            'Expecting a fluent interface'
175
        );
176
    }
177
178
    public function testIsStrict()
179
    {
180
        $this->assertSame(
181
            $this->fixture->isStrict(),
182
            false
183
        );
184
        $this->fixture->setStrict(true);
185
        $this->assertSame(
186
            $this->fixture->isStrict(),
187
            true
188
        );
189
    }
190
191
    public function testSetPath()
192
    {
193
        $this->assertSame(
194
            $this->fixture,
195
            $this->fixture->setPath(__DIR__),
196
            'Expecting a fluent interface'
197
        );
198
    }
199
200
    /**
201
     * @covers \phpDocumentor\GraphViz\Graph::__call
202
     * @covers \phpDocumentor\GraphViz\Graph::getAttribute
203
     * @covers \phpDocumentor\GraphViz\Graph::setAttribute
204
     */
205
    public function test__call()
206
    {
207
        $this->assertNull($this->fixture->MyMethod());
0 ignored issues
show
Documentation Bug introduced by
The method MyMethod does not exist on object<phpDocumentor\GraphViz\Graph>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
208
        $this->assertSame($this->fixture, $this->fixture->setColor('black'));
0 ignored issues
show
Documentation Bug introduced by
The method setColor does not exist on object<phpDocumentor\GraphViz\Graph>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
209
        $this->assertSame('black', $this->fixture->getColor()->getValue());
0 ignored issues
show
Documentation Bug introduced by
The method getColor does not exist on object<phpDocumentor\GraphViz\Graph>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
210
    }
211
212
    /**
213
     * @covers \phpDocumentor\GraphViz\Graph::getAttribute
214
     */
215
    public function testGetNonExistingAttributeThrowsAttributeNotFound()
216
    {
217
        $this->expectException(AttributeNotFound::class);
218
        $this->expectExceptionMessage('Attribute with name "color" was not found');
219
220
        $this->fixture->getColor();
0 ignored issues
show
Documentation Bug introduced by
The method getColor does not exist on object<phpDocumentor\GraphViz\Graph>? Since you implemented __call, maybe consider adding a @method annotation.

If you implement __call and you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.

This is often the case, when __call is implemented by a parent class and only the child class knows which methods exist:

class ParentClass {
    private $data = array();

    public function __call($method, array $args) {
        if (0 === strpos($method, 'get')) {
            return $this->data[strtolower(substr($method, 3))];
        }

        throw new \LogicException(sprintf('Unsupported method: %s', $method));
    }
}

/**
 * If this class knows which fields exist, you can specify the methods here:
 *
 * @method string getName()
 */
class SomeClass extends ParentClass { }
Loading history...
221
    }
222
223
    /**
224
     * @covers \phpDocumentor\GraphViz\Graph::addGraph
225
     */
226
    public function testAddGraph()
227
    {
228
        $mock = m::mock(Graph::class);
229
        $mock->expects('setType');
230
        $mock->expects('getName');
231
232
        $this->assertSame(
233
            $this->fixture,
234
            $this->fixture->addGraph($mock)
235
        );
236
    }
237
238
    /**
239
     * @covers \phpDocumentor\GraphViz\Graph::hasGraph
240
     */
241
    public function testHasGraph()
242
    {
243
        $mock = m::mock(Graph::class);
244
        $mock->expects('getName')->andReturn('MyName');
245
        $mock->expects('setType');
246
247
        $this->assertFalse($this->fixture->hasGraph('MyName'));
248
        $this->fixture->addGraph($mock);
249
        $this->assertTrue($this->fixture->hasGraph('MyName'));
250
    }
251
252
    /**
253
     * @covers \phpDocumentor\GraphViz\Graph::getGraph
254
     */
255
    public function testGetGraph()
256
    {
257
        $mock = m::mock(Graph::class);
258
        $mock->expects('setType');
259
        $mock->expects('getName')->andReturn('MyName');
260
261
        $this->fixture->addGraph($mock);
262
        $this->assertSame(
263
            $mock,
264
            $this->fixture->getGraph('MyName')
265
        );
266
    }
267
268
    /**
269
     * @covers \phpDocumentor\GraphViz\Graph::setNode
270
     */
271
    public function testSetNode()
272
    {
273
        $mock = m::mock(Node::class);
274
        $mock->expects('getName')->andReturn('MyName');
275
276
        $this->assertSame(
277
            $this->fixture,
278
            $this->fixture->setNode($mock)
279
        );
280
    }
281
282
    /**
283
     * @covers \phpDocumentor\GraphViz\Graph::findNode
284
     */
285
    public function testFindNode()
286
    {
287
        $this->assertNull($this->fixture->findNode('MyNode'));
288
289
        $mock = m::mock(Node::class);
290
        $mock->expects('getName')->andReturn('MyName');
291
292
        $this->fixture->setNode($mock);
293
        $this->assertSame(
294
            $mock,
295
            $this->fixture->findNode('MyName')
296
        );
297
298
        $subGraph = Graph::create();
299
        $mock2 = m::mock(Node::class);
300
        $mock2->expects('getName')->andReturn('MyName2');
301
302
        $subGraph->setNode($mock2);
303
304
        $this->fixture->addGraph($subGraph);
305
        $this->assertSame(
306
            $mock2,
307
            $this->fixture->findNode('MyName2')
308
        );
309
    }
310
311
    /**
312
     * @covers \phpDocumentor\GraphViz\Graph::__set
313
     */
314
    public function test__set()
315
    {
316
        $mock = m::mock(Node::class);
317
318
        $this->assertSame(
319
            $this->fixture,
320
            $this->fixture->__set('myNode', $mock)
321
        );
322
    }
323
324
    /**
325
     * @covers \phpDocumentor\GraphViz\Graph::__get
326
     */
327
    public function test__get()
328
    {
329
        $mock = m::mock(Node::class);
330
331
        $this->fixture->myNode = $mock;
0 ignored issues
show
Documentation introduced by
The property myNode does not exist on object<phpDocumentor\GraphViz\Graph>. 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...
332
        $this->assertSame(
333
            $mock,
334
            $this->fixture->myNode
0 ignored issues
show
Documentation introduced by
The property myNode does not exist on object<phpDocumentor\GraphViz\Graph>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
335
        );
336
    }
337
338
    /**
339
     * @covers \phpDocumentor\GraphViz\Graph::link
340
     */
341
    public function testLink()
342
    {
343
        $mock = m::mock(Edge::class);
344
345
        $this->assertSame(
346
            $this->fixture,
347
            $this->fixture->link($mock)
348
        );
349
    }
350
351
    /**
352
     * @covers \phpDocumentor\GraphViz\Graph::export
353
     */
354
    public function testExportException()
355
    {
356
        $graph = Graph::create('My First Graph');
357
        $filename = tempnam(sys_get_temp_dir(), 'tst');
358
359
        $this->expectException(\phpDocumentor\GraphViz\Exception::class);
360
        $graph->export('fpd', $filename);
361
    }
362
363
    /**
364
     * @covers \phpDocumentor\GraphViz\Graph::export
365
     */
366
    public function testExport()
367
    {
368
        $graph = Graph::create('My First Graph');
369
        $filename = tempnam(sys_get_temp_dir(), 'tst');
370
371
        $this->assertSame(
372
            $graph,
373
            $graph->export('pdf', $filename)
374
        );
375
        $this->assertTrue(is_readable($filename));
376
    }
377
378
    /**
379
     * @covers \phpDocumentor\GraphViz\Graph::__toString
380
     */
381
    public function test__toString()
382
    {
383
        $graph = Graph::create('My First Graph');
384
        $this->assertSame(
385
            $this->normalizeLineEndings((string) $graph),
386
            $this->normalizeLineEndings(('digraph "My First Graph" {' . PHP_EOL . PHP_EOL . '}'))
387
        );
388
389
        $graph->setLabel('PigeonPost');
390
        $this->assertSame(
391
            $this->normalizeLineEndings((string) $graph),
392
            $this->normalizeLineEndings(('digraph "My First Graph" {' . PHP_EOL . 'label="PigeonPost"' . PHP_EOL . '}'))
393
        );
394
395
        $graph->setStrict(true);
396
        $this->assertSame(
397
            $this->normalizeLineEndings((string) $graph),
398
            $this->normalizeLineEndings(('strict digraph "My First Graph" {' . PHP_EOL . 'label="PigeonPost"' . PHP_EOL . '}'))
399
        );
400
    }
401
402
    /**
403
     * Help avoid issue of "#Warning: Strings contain different line endings!" on Windows.
404
     * @param string $string
405
     * @return string
406
     */
407
    private function normalizeLineEndings($string)
408
    {
409
        return preg_replace('~\R~u', "\r\n", $string);
410
    }
411
}
412