Completed
Push — master ( 8089d5...e1eb7b )
by
unknown
02:03
created

tests/phpDocumentor/GraphViz/Test/GraphTest.php (7 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
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 InvalidArgumentException;
17
use Mockery as m;
18
use phpDocumentor\GraphViz\AttributeNotFound;
19
use phpDocumentor\GraphViz\Edge;
20
use phpDocumentor\GraphViz\Exception;
21
use phpDocumentor\GraphViz\Graph;
22
use phpDocumentor\GraphViz\Node;
23
use PHPUnit\Framework\TestCase;
24
use RuntimeException;
25
use const PHP_EOL;
26
use function is_readable;
27
use function preg_replace;
28
use function sys_get_temp_dir;
29
use function tempnam;
30
31
/**
32
 * Test for the the class representing a GraphViz graph.
33
 */
34
class GraphTest extends TestCase
35
{
36
    /** @var Graph */
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() : void
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() : void
53
    {
54
        m::close();
55
    }
56
57
    /**
58
     * @covers \phpDocumentor\GraphViz\Graph::create
59
     */
60
    public function testCreate() : void
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() : void
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() : void
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() : void
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() : void
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() : void
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() : void
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() : void
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() : void
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() : void
206
    {
207
        $this->assertNull($this->fixture->MyMethod());
208
        $this->assertSame($this->fixture, $this->fixture->setBgColor('black'));
209
        $this->assertSame('black', $this->fixture->getBgColor()->getValue());
210
    }
211
212
    /**
213
     * @covers \phpDocumentor\GraphViz\Graph::getAttribute
214
     * @covers \phpDocumentor\GraphViz\AttributeNotFound::__construct
215
     */
216
    public function testGetNonExistingAttributeThrowsAttributeNotFound() : void
217
    {
218
        $this->expectException(AttributeNotFound::class);
219
        $this->expectExceptionMessage('Attribute with name "notexisting" was not found');
220
221
        $this->fixture->getNotExisting();
222
    }
223
224
    /**
225
     * @covers \phpDocumentor\GraphViz\Graph::addGraph
226
     */
227
    public function testAddGraph() : void
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)
0 ignored issues
show
$mock is of type object<Mockery\LegacyMockInterface>, but the function expects a object<self>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
236
        );
237
    }
238
239
    /**
240
     * @covers \phpDocumentor\GraphViz\Graph::hasGraph
241
     */
242
    public function testHasGraph() : void
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);
0 ignored issues
show
$mock is of type object<Mockery\LegacyMockInterface>, but the function expects a object<self>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
250
        $this->assertTrue($this->fixture->hasGraph('MyName'));
251
    }
252
253
    /**
254
     * @covers \phpDocumentor\GraphViz\Graph::getGraph
255
     */
256
    public function testGetGraph() : void
257
    {
258
        $mock = m::mock(Graph::class);
259
        $mock->expects('setType');
260
        $mock->expects('getName')->andReturn('MyName');
261
262
        $this->fixture->addGraph($mock);
0 ignored issues
show
$mock is of type object<Mockery\LegacyMockInterface>, but the function expects a object<self>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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() : void
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)
0 ignored issues
show
$mock is of type object<Mockery\LegacyMockInterface>, but the function expects a object<phpDocumentor\GraphViz\Node>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
280
        );
281
    }
282
283
    /**
284
     * @covers \phpDocumentor\GraphViz\Graph::findNode
285
     */
286
    public function testFindNode() : void
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);
0 ignored issues
show
$mock is of type object<Mockery\LegacyMockInterface>, but the function expects a object<phpDocumentor\GraphViz\Node>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
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() : void
316
    {
317
        $mock = m::mock(Node::class);
318
319
        $this->assertSame(
320
            $this->fixture,
321
            $this->fixture->__set('myNode', $mock)
0 ignored issues
show
$mock is of type object<Mockery\LegacyMockInterface>, but the function expects a object<phpDocumentor\GraphViz\Node>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
322
        );
323
    }
324
325
    /**
326
     * @covers \phpDocumentor\GraphViz\Graph::__get
327
     */
328
    public function test__get() : void
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() : void
343
    {
344
        $mock = m::mock(Edge::class);
345
346
        $this->assertSame(
347
            $this->fixture,
348
            $this->fixture->link($mock)
0 ignored issues
show
$mock is of type object<Mockery\LegacyMockInterface>, but the function expects a object<phpDocumentor\GraphViz\Edge>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
349
        );
350
    }
351
352
    /**
353
     * @covers \phpDocumentor\GraphViz\Graph::export
354
     */
355
    public function testExportException() : void
356
    {
357
        $graph    = Graph::create('My First Graph');
358
        $filename = tempnam(sys_get_temp_dir(), 'tst');
359
360
        if ($filename === false) {
361
            $this->assertFalse('Failed to create destination file');
362
            return;
363
        }
364
365
        $this->expectException(Exception::class);
366
        $graph->export('fpd', $filename);
367
    }
368
369
    /**
370
     * @covers \phpDocumentor\GraphViz\Graph::export
371
     */
372
    public function testExport() : void
373
    {
374
        $graph    = Graph::create('My First Graph');
375
        $filename = tempnam(sys_get_temp_dir(), 'tst');
376
377
        if ($filename === false) {
378
            $this->assertFalse('Failed to create destination file');
379
            return;
380
        }
381
382
        $this->assertSame(
383
            $graph,
384
            $graph->export('pdf', $filename)
385
        );
386
        $this->assertTrue(is_readable($filename));
387
    }
388
389
    /**
390
     * @covers \phpDocumentor\GraphViz\Graph::__toString
391
     */
392
    public function test__toString() : void
393
    {
394
        $graph = Graph::create('My First Graph');
395
        $this->assertSame(
396
            $this->normalizeLineEndings((string) $graph),
397
            $this->normalizeLineEndings(('digraph "My First Graph" {' . PHP_EOL . PHP_EOL . '}'))
398
        );
399
400
        $graph->setLabel('PigeonPost');
401
        $this->assertSame(
402
            $this->normalizeLineEndings((string) $graph),
403
            $this->normalizeLineEndings(('digraph "My First Graph" {' . PHP_EOL . 'label="PigeonPost"' . PHP_EOL . '}'))
404
        );
405
406
        $graph->setStrict(true);
407
        $this->assertSame(
408
            $this->normalizeLineEndings((string) $graph),
409
            $this->normalizeLineEndings(
410
                ('strict digraph "My First Graph" {' . PHP_EOL . 'label="PigeonPost"' . PHP_EOL . '}')
411
            )
412
        );
413
    }
414
415
    /**
416
     * Help avoid issue of "#Warning: Strings contain different line endings!" on Windows.
417
     */
418
    private function normalizeLineEndings(string $string) : string
419
    {
420
        $result = preg_replace('~\R~u', "\r\n", $string);
421
        if ($result === null) {
422
            throw new RuntimeException('Normalize line endings failed');
423
        }
424
425
        return $result;
426
    }
427
}
428