Completed
Push — master ( dd8cf8...395659 )
by Jaap
08:57
created

tests/phpDocumentor/GraphViz/Test/GraphTest.php (3 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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());
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
     * @covers \phpDocumentor\GraphViz\AttributeNotFound::__construct
215
     */
216
    public function testGetNonExistingAttributeThrowsAttributeNotFound()
217
    {
218
        $this->expectException(AttributeNotFound::class);
219
        $this->expectExceptionMessage('Attribute with name "color" was not found');
220
221
        $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...
222
    }
223
224
    /**
225
     * @covers \phpDocumentor\GraphViz\Graph::addGraph
226
     */
227
    public function testAddGraph()
228
    {
229
        $mock = m::mock(Graph::class);
230
        $mock->expects('setType');
231
        $mock->expects('getName');
232
233
        $this->assertSame(
234
            $this->fixture,
235
            $this->fixture->addGraph($mock)
236
        );
237
    }
238
239
    /**
240
     * @covers \phpDocumentor\GraphViz\Graph::hasGraph
241
     */
242
    public function testHasGraph()
243
    {
244
        $mock = m::mock(Graph::class);
245
        $mock->expects('getName')->andReturn('MyName');
246
        $mock->expects('setType');
247
248
        $this->assertFalse($this->fixture->hasGraph('MyName'));
249
        $this->fixture->addGraph($mock);
250
        $this->assertTrue($this->fixture->hasGraph('MyName'));
251
    }
252
253
    /**
254
     * @covers \phpDocumentor\GraphViz\Graph::getGraph
255
     */
256
    public function testGetGraph()
257
    {
258
        $mock = m::mock(Graph::class);
259
        $mock->expects('setType');
260
        $mock->expects('getName')->andReturn('MyName');
261
262
        $this->fixture->addGraph($mock);
263
        $this->assertSame(
264
            $mock,
265
            $this->fixture->getGraph('MyName')
266
        );
267
    }
268
269
    /**
270
     * @covers \phpDocumentor\GraphViz\Graph::setNode
271
     */
272
    public function testSetNode()
273
    {
274
        $mock = m::mock(Node::class);
275
        $mock->expects('getName')->andReturn('MyName');
276
277
        $this->assertSame(
278
            $this->fixture,
279
            $this->fixture->setNode($mock)
280
        );
281
    }
282
283
    /**
284
     * @covers \phpDocumentor\GraphViz\Graph::findNode
285
     */
286
    public function testFindNode()
287
    {
288
        $this->assertNull($this->fixture->findNode('MyNode'));
289
290
        $mock = m::mock(Node::class);
291
        $mock->expects('getName')->andReturn('MyName');
292
293
        $this->fixture->setNode($mock);
294
        $this->assertSame(
295
            $mock,
296
            $this->fixture->findNode('MyName')
297
        );
298
299
        $subGraph = Graph::create();
300
        $mock2 = m::mock(Node::class);
301
        $mock2->expects('getName')->andReturn('MyName2');
302
303
        $subGraph->setNode($mock2);
304
305
        $this->fixture->addGraph($subGraph);
306
        $this->assertSame(
307
            $mock2,
308
            $this->fixture->findNode('MyName2')
309
        );
310
    }
311
312
    /**
313
     * @covers \phpDocumentor\GraphViz\Graph::__set
314
     */
315
    public function test__set()
316
    {
317
        $mock = m::mock(Node::class);
318
319
        $this->assertSame(
320
            $this->fixture,
321
            $this->fixture->__set('myNode', $mock)
322
        );
323
    }
324
325
    /**
326
     * @covers \phpDocumentor\GraphViz\Graph::__get
327
     */
328
    public function test__get()
329
    {
330
        $mock = m::mock(Node::class);
331
332
        $this->fixture->myNode = $mock;
333
        $this->assertSame(
334
            $mock,
335
            $this->fixture->myNode
336
        );
337
    }
338
339
    /**
340
     * @covers \phpDocumentor\GraphViz\Graph::link
341
     */
342
    public function testLink()
343
    {
344
        $mock = m::mock(Edge::class);
345
346
        $this->assertSame(
347
            $this->fixture,
348
            $this->fixture->link($mock)
349
        );
350
    }
351
352
    /**
353
     * @covers \phpDocumentor\GraphViz\Graph::export
354
     */
355
    public function testExportException()
356
    {
357
        $graph = Graph::create('My First Graph');
358
        $filename = tempnam(sys_get_temp_dir(), 'tst');
359
360
        $this->expectException(\phpDocumentor\GraphViz\Exception::class);
361
        $graph->export('fpd', $filename);
362
    }
363
364
    /**
365
     * @covers \phpDocumentor\GraphViz\Graph::export
366
     */
367
    public function testExport()
368
    {
369
        $graph = Graph::create('My First Graph');
370
        $filename = tempnam(sys_get_temp_dir(), 'tst');
371
372
        $this->assertSame(
373
            $graph,
374
            $graph->export('pdf', $filename)
375
        );
376
        $this->assertTrue(is_readable($filename));
377
    }
378
379
    /**
380
     * @covers \phpDocumentor\GraphViz\Graph::__toString
381
     */
382
    public function test__toString()
383
    {
384
        $graph = Graph::create('My First Graph');
385
        $this->assertSame(
386
            $this->normalizeLineEndings((string) $graph),
387
            $this->normalizeLineEndings(('digraph "My First Graph" {' . PHP_EOL . PHP_EOL . '}'))
388
        );
389
390
        $graph->setLabel('PigeonPost');
391
        $this->assertSame(
392
            $this->normalizeLineEndings((string) $graph),
393
            $this->normalizeLineEndings(('digraph "My First Graph" {' . PHP_EOL . 'label="PigeonPost"' . PHP_EOL . '}'))
394
        );
395
396
        $graph->setStrict(true);
397
        $this->assertSame(
398
            $this->normalizeLineEndings((string) $graph),
399
            $this->normalizeLineEndings(('strict digraph "My First Graph" {' . PHP_EOL . 'label="PigeonPost"' . PHP_EOL . '}'))
400
        );
401
    }
402
403
    /**
404
     * Help avoid issue of "#Warning: Strings contain different line endings!" on Windows.
405
     * @param string $string
406
     * @return string
407
     */
408
    private function normalizeLineEndings($string)
409
    {
410
        return preg_replace('~\R~u', "\r\n", $string);
411
    }
412
}
413