Completed
Pull Request — master (#19)
by Chuck
05:01
created

GraphTest::setUp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
/**
3
 * phpDocumentor
4
 *
5
 * PHP Version 5
6
 *
7
 * @package   phpDocumentor\GraphViz\Tests
8
 * @author    Danny van der Sluijs <[email protected]>
9
 * @copyright 2012 Danny van der Sluijs (http://www.fleppuhstein.com)
10
 * @license   http://www.opensource.org/licenses/mit-license.php MIT
11
 * @link      http://phpDocumentor-project.org
12
 */
13
14
namespace phpDocumentor\GraphViz\Test;
15
16
use Mockery as m;
17
use phpDocumentor\GraphViz\Edge;
18
use phpDocumentor\GraphViz\Graph;
19
use phpDocumentor\GraphViz\Node;
20
use PHPUnit\Framework\TestCase;
21
22
/**
23
 * Test for the the class representing a GraphViz graph.
24
 *
25
 * @package phpDocumentor\GraphViz\Tests
26
 * @author  Mike van Riel <[email protected]>
27
 * @license http://www.opensource.org/licenses/mit-license.php MIT
28
 * @link    http://phpDocumentor-project.org
29
 */
30
class GraphTest extends TestCase
31
{
32
    /**
33
     * @var Graph
34
     */
35
    protected $fixture;
36
37
    /**
38
     * Sets up the fixture, for example, opens a network connection.
39
     * This method is called before a test is executed.
40
     */
41
    protected function setUp()
42
    {
43
        $this->fixture = new Graph();
44
    }
45
46
    /**
47
     * Tears down the fixture, for example, closes a network connection.
48
     * This method is called after a test is executed.
49
     */
50
    protected function tearDown()
51
    {
52
        m::close();
53
    }
54
55
    /**
56
     * @covers phpDocumentor\GraphViz\Graph::create
57
     */
58
    public function testCreate()
59
    {
60
        $fixture = Graph::create();
61
        $this->assertInstanceOf(
62
            Graph::class,
63
            $fixture
64
        );
65
        $this->assertSame(
66
            'G',
67
            $fixture->getName()
68
        );
69
        $this->assertSame(
70
            'digraph',
71
            $fixture->getType()
72
        );
73
74
        $fixture = Graph::create('MyName', false);
75
        $this->assertSame(
76
            'MyName',
77
            $fixture->getName()
78
        );
79
        $this->assertSame(
80
            'graph',
81
            $fixture->getType()
82
        );
83
    }
84
85
    /**
86
     * @covers phpDocumentor\GraphViz\Graph::setName
87
     */
88
    public function testSetName()
89
    {
90
        $this->assertSame(
91
            $this->fixture,
92
            $this->fixture->setName('otherName'),
93
            'Expecting a fluent interface'
94
        );
95
    }
96
97
    /**
98
     * @covers phpDocumentor\GraphViz\Graph::getName
99
     */
100
    public function testGetName()
101
    {
102
        $this->assertSame(
103
            $this->fixture->getName(),
104
            'G',
105
            'Expecting the name to match the initial state'
106
        );
107
        $this->fixture->setName('otherName');
108
        $this->assertSame(
109
            $this->fixture->getName(),
110
            'otherName',
111
            'Expecting the name to contain the new value'
112
        );
113
    }
114
115
    /**
116
     * @covers phpDocumentor\GraphViz\Graph::setType
117
     */
118
    public function testSetType()
119
    {
120
        $this->assertSame(
121
            $this->fixture,
122
            $this->fixture->setType('digraph'),
123
            'Expecting a fluent interface'
124
        );
125
        $this->assertSame(
126
            $this->fixture,
127
            $this->fixture->setType('graph'),
128
            'Expecting a fluent interface'
129
        );
130
        $this->assertSame(
131
            $this->fixture,
132
            $this->fixture->setType('subgraph'),
133
            'Expecting a fluent interface'
134
        );
135
    }
136
137
    /**
138
     * @covers phpDocumentor\GraphViz\Graph::setType
139
     */
140
    public function testSetTypeException()
141
    {
142
        $this->expectException(\InvalidArgumentException::class);
143
        $this->fixture->setType('fakegraphg');
144
    }
145
146
    /**
147
     * @covers phpDocumentor\GraphViz\Graph::getType
148
     */
149
    public function testGetType()
150
    {
151
        $this->assertSame(
152
            $this->fixture->getType(),
153
            'digraph'
154
        );
155
        $this->fixture->setType('graph');
156
        $this->assertSame(
157
            $this->fixture->getType(),
158
            'graph'
159
        );
160
    }
161
162
    public function testSetStrict()
163
    {
164
        $this->assertSame(
165
            $this->fixture,
166
            $this->fixture->setStrict(true),
167
            'Expecting a fluent interface'
168
        );
169
        $this->assertSame(
170
            $this->fixture,
171
            $this->fixture->setStrict(false),
172
            'Expecting a fluent interface'
173
        );
174
    }
175
176
    public function testIsStrict()
177
    {
178
        $this->assertSame(
179
            $this->fixture->isStrict(),
180
            false
181
        );
182
        $this->fixture->setStrict(true);
183
        $this->assertSame(
184
            $this->fixture->isStrict(),
185
            true
186
        );
187
    }
188
189
    public function testSetPath()
190
    {
191
        $this->assertSame(
192
            $this->fixture,
193
            $this->fixture->setPath(__DIR__),
194
            'Expecting a fluent interface'
195
        );
196
    }
197
198
    /**
199
     * @covers phpDocumentor\GraphViz\Graph::__call
200
     */
201
    public function test__call()
202
    {
203
        $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...
204
        $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...
205
        $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...
206
    }
207
208
    /**
209
     * @covers phpDocumentor\GraphViz\Graph::addGraph
210
     */
211
    public function testAddGraph()
212
    {
213
        $mock = m::mock(Graph::class);
214
        $mock->expects('setType');
215
        $mock->expects('getName');
216
217
        $this->assertSame(
218
            $this->fixture,
219
            $this->fixture->addGraph($mock)
220
        );
221
    }
222
223
    /**
224
     * @covers phpDocumentor\GraphViz\Graph::hasGraph
225
     */
226
    public function testHasGraph()
227
    {
228
        $mock = m::mock(Graph::class);
229
        $mock->expects('getName')->andReturn('MyName');
230
        $mock->expects('setType');
231
232
        $this->assertFalse($this->fixture->hasGraph('MyName'));
233
        $this->fixture->addGraph($mock);
234
        $this->assertTrue($this->fixture->hasGraph('MyName'));
235
    }
236
237
    /**
238
     * @covers phpDocumentor\GraphViz\Graph::getGraph
239
     */
240
    public function testGetGraph()
241
    {
242
        $mock = m::mock(Graph::class);
243
        $mock->expects('setType');
244
        $mock->expects('getName')->andReturn('MyName');
245
246
        $this->fixture->addGraph($mock);
247
        $this->assertSame(
248
            $mock,
249
            $this->fixture->getGraph('MyName')
250
        );
251
    }
252
253
    /**
254
     * @covers phpDocumentor\GraphViz\Graph::setNode
255
     */
256
    public function testSetNode()
257
    {
258
        $mock = m::mock(Node::class);
259
        $mock->expects('getName')->andReturn('MyName');
260
261
        $this->assertSame(
262
            $this->fixture,
263
            $this->fixture->setNode($mock)
264
        );
265
    }
266
267
    /**
268
     * @covers phpDocumentor\GraphViz\Graph::findNode
269
     */
270
    public function testFindNode()
271
    {
272
        $this->assertNull($this->fixture->findNode('MyNode'));
273
274
        $mock = m::mock(Node::class);
275
        $mock->expects('getName')->andReturn('MyName');
276
277
        $this->fixture->setNode($mock);
278
        $this->assertSame(
279
            $mock,
280
            $this->fixture->findNode('MyName')
281
        );
282
283
        $subGraph = Graph::create();
284
        $mock2 = m::mock(Node::class);
285
        $mock2->expects('getName')->andReturn('MyName2');
286
287
        $subGraph->setNode($mock2);
288
289
        $this->fixture->addGraph($subGraph);
290
        $this->assertSame(
291
            $mock2,
292
            $this->fixture->findNode('MyName2')
293
        );
294
    }
295
296
    /**
297
     * @covers phpDocumentor\GraphViz\Graph::__set
298
     */
299
    public function test__set()
300
    {
301
        $mock = m::mock(Node::class);
302
303
        $this->assertSame(
304
            $this->fixture,
305
            $this->fixture->__set('myNode', $mock)
306
        );
307
    }
308
309
    /**
310
     * @covers phpDocumentor\GraphViz\Graph::__get
311
     */
312
    public function test__get()
313
    {
314
        $mock = m::mock(Node::class);
315
316
        $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...
317
        $this->assertSame(
318
            $mock,
319
            $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...
320
        );
321
    }
322
323
    /**
324
     * @covers phpDocumentor\GraphViz\Graph::link
325
     */
326
    public function testLink()
327
    {
328
        $mock = m::mock(Edge::class);
329
330
        $this->assertSame(
331
            $this->fixture,
332
            $this->fixture->link($mock)
333
        );
334
    }
335
336
    /**
337
     * @covers phpDocumentor\GraphViz\Graph::export
338
     */
339
    public function testExportException()
340
    {
341
        $graph = Graph::create('My First Graph');
342
        $filename = tempnam(sys_get_temp_dir(), 'tst');
343
344
        $this->expectException(\phpDocumentor\GraphViz\Exception::class);
345
        $graph->export('fpd', $filename);
346
    }
347
348
    /**
349
     * @covers phpDocumentor\GraphViz\Graph::export
350
     */
351
    public function testExport()
352
    {
353
        $graph = Graph::create('My First Graph');
354
        $filename = tempnam(sys_get_temp_dir(), 'tst');
355
356
        $this->assertSame(
357
            $graph,
358
            $graph->export('pdf', $filename)
359
        );
360
        $this->assertTrue(is_readable($filename));
361
    }
362
363
    /**
364
     * @covers phpDocumentor\GraphViz\Graph::__toString
365
     */
366
    public function test__toString()
367
    {
368
        $graph = Graph::create('My First Graph');
369
        $this->assertSame(
370
            $this->normalizeLineEndings((string) $graph),
371
            $this->normalizeLineEndings(('digraph "My First Graph" {' . PHP_EOL . PHP_EOL . '}'))
372
        );
373
374
        $graph->setLabel('PigeonPost');
0 ignored issues
show
Documentation Bug introduced by
The method setLabel 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...
375
        $this->assertSame(
376
            $this->normalizeLineEndings((string) $graph),
377
            $this->normalizeLineEndings(('digraph "My First Graph" {' . PHP_EOL . 'label="PigeonPost"' . PHP_EOL . '}'))
378
        );
379
380
        $graph->setStrict(true);
381
        $this->assertSame(
382
            $this->normalizeLineEndings((string) $graph),
383
            $this->normalizeLineEndings(('strict digraph "My First Graph" {' . PHP_EOL . 'label="PigeonPost"' . PHP_EOL . '}'))
384
        );
385
    }
386
387
    /**
388
     * Help avoid issue of "#Warning: Strings contain different line endings!" on Windows.
389
     */
390
    private function normalizeLineEndings($string)
391
    {
392
        return preg_replace('~\R~u', "\r\n", $string);
393
    }
394
}
395