Completed
Push — master ( a906a9...6e20ec )
by Chuck
8s
created

GraphTest::test__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 1
Metric Value
c 3
b 0
f 1
dl 0
loc 20
rs 9.4285
cc 1
eloc 13
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
namespace phpDocumentor\GraphViz;
14
15
use phpDocumentor\GraphViz\Graph;
16
use phpDocumentor\GraphViz\Node;
17
use PHPUnit\Framework\TestCase;
18
19
/**
20
 * Generated by PHPUnit_SkeletonGenerator 1.2.0 on 2012-12-09 at 19:06:57.
21
 */
22
class GraphTest extends TestCase {
23
24
    /**
25
     * @var Graph
26
     */
27
    protected $fixture;
28
29
    /**
30
     * Sets up the fixture, for example, opens a network connection.
31
     * This method is called before a test is executed.
32
     */
33
    protected function setUp()
34
    {
35
        $this->fixture = new Graph;
36
    }
37
38
    /**
39
     * Tears down the fixture, for example, closes a network connection.
40
     * This method is called after a test is executed.
41
     */
42
    protected function tearDown()
43
    {
44
45
    }
46
47
    /**
48
     * @covers phpDocumentor\GraphViz\Graph::create
49
     */
50
    public function testCreate()
51
    {
52
        $fixture = Graph::create();
53
        $this->assertInstanceOf(
54
            'phpDocumentor\GraphViz\Graph',
55
            $fixture
56
        );
57
        $this->assertSame(
58
            'G',
59
            $fixture->getName()
60
        );
61
        $this->assertSame(
62
            'digraph',
63
            $fixture->getType()
64
        );
65
66
        $fixture = Graph::create('MyName', false);
67
        $this->assertSame(
68
            'MyName',
69
            $fixture->getName()
70
        );
71
        $this->assertSame(
72
            'graph',
73
            $fixture->getType()
74
        );
75
    }
76
77
    /**
78
     * @covers phpDocumentor\GraphViz\Graph::setName
79
     */
80
    public function testSetName()
81
    {
82
        $this->assertSame(
83
            $this->fixture, $this->fixture->setName('otherName'),
84
            'Expecting a fluent interface'
85
        );
86
    }
87
88
    /**
89
     * @covers phpDocumentor\GraphViz\Graph::getName
90
     */
91
    public function testGetName()
92
    {
93
        $this->assertSame(
94
            $this->fixture->getName(), 'G',
95
            'Expecting the name to match the initial state'
96
        );
97
        $this->fixture->setName('otherName');
98
        $this->assertSame(
99
            $this->fixture->getName(), 'otherName',
100
            'Expecting the name to contain the new value'
101
        );
102
    }
103
104
    /**
105
     * @covers phpDocumentor\GraphViz\Graph::setType
106
     */
107
    public function testSetType()
108
    {
109
        $this->assertSame(
110
            $this->fixture, $this->fixture->setType('digraph'),
111
            'Expecting a fluent interface'
112
        );
113
        $this->assertSame(
114
            $this->fixture, $this->fixture->setType('graph'),
115
            'Expecting a fluent interface'
116
        );
117
        $this->assertSame(
118
            $this->fixture, $this->fixture->setType('subgraph'),
119
            'Expecting a fluent interface'
120
        );
121
    }
122
123
    /**
124
     * @covers phpDocumentor\GraphViz\Graph::setType
125
     */
126
    public function testSetTypeException()
127
    {
128
        $this->expectException('\InvalidArgumentException');
129
        $this->fixture->setType('fakegraphg');
130
    }
131
132
    /**
133
     * @covers phpDocumentor\GraphViz\Graph::getType
134
     */
135
    public function testGetType()
136
    {
137
        $this->assertSame(
138
            $this->fixture->getType(),
139
            'digraph'
140
        );
141
        $this->fixture->setType('graph');
142
        $this->assertSame(
143
            $this->fixture->getType(),
144
            'graph'
145
        );
146
    }
147
148
    public function testSetStrict()
149
    {
150
        $this->assertSame(
151
            $this->fixture, $this->fixture->setStrict(true),
152
            'Expecting a fluent interface'
153
        );
154
        $this->assertSame(
155
            $this->fixture, $this->fixture->setStrict(false),
156
            'Expecting a fluent interface'
157
        );
158
    }
159
160
    public function testIsStrict()
161
    {
162
        $this->assertSame(
163
            $this->fixture->isStrict(),
164
            false
165
        );
166
        $this->fixture->setStrict(true);
167
        $this->assertSame(
168
            $this->fixture->isStrict(),
169
            true
170
        );
171
    }
172
173
    public function testSetPath()
174
    {
175
        $this->assertSame(
176
            $this->fixture, $this->fixture->setPath(__DIR__),
177
            'Expecting a fluent interface'
178
        );
179
    }
180
181
    /**
182
     * @covers phpDocumentor\GraphViz\Graph::__call
183
     */
184
    public function test__call()
185
    {
186
        $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...
187
        $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...
188
        $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...
189
    }
190
191
    /**
192
     * @covers phpDocumentor\GraphViz\Graph::addGraph
193
     */
194
    public function testAddGraph()
195
    {
196
        $this->markTestIncomplete('Need to redo mock technique.');
197
198
        $this->assertSame(
199
            $this->fixture,
200
            $this->fixture->addGraph($this->getMock('phpDocumentor\GraphViz\Graph'))
0 ignored issues
show
Bug introduced by
The method getMock() does not exist on phpDocumentor\GraphViz\GraphTest. Did you maybe mean getMockBuilder()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
201
        );
202
    }
203
204
    /**
205
     * @covers phpDocumentor\GraphViz\Graph::hasGraph
206
     */
207
    public function testHasGraph()
208
    {
209
        $this->markTestIncomplete('Need to redo mock technique.');
210
211
        $mock = $this->getMock('phpDocumentor\GraphViz\Graph');
0 ignored issues
show
Bug introduced by
The method getMock() does not exist on phpDocumentor\GraphViz\GraphTest. Did you maybe mean getMockBuilder()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
212
        $mock->expects($this->any())->method('getName')->will($this->returnValue('MyName'));
213
        $this->assertFalse($this->fixture->hasGraph('MyName'));
214
        $this->fixture->addGraph($mock);
215
        $this->assertTrue($this->fixture->hasGraph('MyName'));
216
    }
217
218
    /**
219
     * @covers phpDocumentor\GraphViz\Graph::getGraph
220
     */
221
    public function testGetGraph()
222
    {
223
        $this->markTestIncomplete('Need to redo mock technique.');
224
225
        $mock = $this->getMock('phpDocumentor\GraphViz\Graph');
0 ignored issues
show
Bug introduced by
The method getMock() does not exist on phpDocumentor\GraphViz\GraphTest. Did you maybe mean getMockBuilder()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
226
        $mock->expects($this->any())->method('getName')->will($this->returnValue('MyName'));
227
228
        $this->fixture->addGraph($mock);
229
        $this->assertSame(
230
            $mock,
231
            $this->fixture->getGraph('MyName')
232
        );
233
    }
234
235
    /**
236
     * @covers phpDocumentor\GraphViz\Graph::setNode
237
     */
238
    public function testSetNode()
239
    {
240
        $this->markTestIncomplete('Need to redo mock technique.');
241
242
        $mock = $this->getMock('phpDocumentor\GraphViz\Node', array(), array(), '', false);
0 ignored issues
show
Bug introduced by
The method getMock() does not exist on phpDocumentor\GraphViz\GraphTest. Did you maybe mean getMockBuilder()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
243
        $mock->expects($this->any())->method('getName')->will($this->returnValue('MyName'));
244
245
        $this->assertSame(
246
            $this->fixture,
247
            $this->fixture->setNode($mock)
248
        );
249
    }
250
251
    /**
252
     * @covers phpDocumentor\GraphViz\Graph::findNode
253
     */
254
    public function testFindNode() {
255
        $this->markTestIncomplete('Need to redo mock technique.');
256
257
        $this->assertNull($this->fixture->findNode('MyNode'));
258
259
        $mock = $this->getMock('phpDocumentor\GraphViz\Node', array(), array(), '', false);
0 ignored issues
show
Bug introduced by
The method getMock() does not exist on phpDocumentor\GraphViz\GraphTest. Did you maybe mean getMockBuilder()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
260
        $mock->expects($this->any())->method('getName')->will($this->returnValue('MyName'));
261
        $this->fixture->setNode($mock);
262
        $this->assertSame(
263
            $mock,
264
            $this->fixture->findNode('MyName')
265
        );
266
267
        $subGraph = Graph::create();
268
        $mock2 = $this->getMock('phpDocumentor\GraphViz\Node', array(), array(), '', false);
0 ignored issues
show
Bug introduced by
The method getMock() does not exist on phpDocumentor\GraphViz\GraphTest. Did you maybe mean getMockBuilder()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
269
        $mock2->expects($this->any())->method('getName')->will($this->returnValue('MyName2'));
270
        $subGraph->setNode($mock2);
271
272
        $this->fixture->addGraph($subGraph);
273
        $this->assertSame(
274
            $mock2,
275
            $this->fixture->findNode('MyName2')
276
        );
277
278
    }
279
280
    /**
281
     * @covers phpDocumentor\GraphViz\Graph::__set
282
     * @todo   Implement test__set().
283
     */
284
    public function test__set() {
285
        $this->markTestIncomplete('Need to redo mock technique.');
286
287
        $mock = $this->getMock('phpDocumentor\GraphViz\Node', array(), array(), '', false);
0 ignored issues
show
Bug introduced by
The method getMock() does not exist on phpDocumentor\GraphViz\GraphTest. Did you maybe mean getMockBuilder()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
288
        $mock->expects($this->any())->method('getName')->will($this->returnValue('MyName'));
289
290
        $this->assertSame(
291
            $this->fixture,
292
            $this->fixture->__set('myNode', $mock)
293
        );
294
    }
295
296
    /**
297
     * @covers phpDocumentor\GraphViz\Graph::__get
298
     * @todo   Implement test__get().
299
     */
300
    public function test__get() {
301
        $this->markTestIncomplete('Need to redo mock technique.');
302
303
        $mock = $this->getMock('phpDocumentor\GraphViz\Node', array(), array(), '', false);
0 ignored issues
show
Bug introduced by
The method getMock() does not exist on phpDocumentor\GraphViz\GraphTest. Did you maybe mean getMockBuilder()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
304
        $mock->expects($this->any())->method('getName')->will($this->returnValue('MyName'));
305
306
        $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...
307
        $this->assertSame(
308
            $mock,
309
            $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...
310
        );
311
    }
312
313
    /**
314
     * @covers phpDocumentor\GraphViz\Graph::link
315
     * @todo   Implement testLink().
316
     */
317
    public function testLink() {
318
        $this->markTestIncomplete('Need to redo mock technique.');
319
320
        $mock = $this->getMock('phpDocumentor\GraphViz\Edge', array(), array(), '', false);
0 ignored issues
show
Bug introduced by
The method getMock() does not exist on phpDocumentor\GraphViz\GraphTest. Did you maybe mean getMockBuilder()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
321
322
        $this->assertSame(
323
            $this->fixture,
324
            $this->fixture->link($mock)
325
        );
326
    }
327
328
    /**
329
     * @covers phpDocumentor\GraphViz\Graph::export
330
     */
331
    public function testExportException() {
332
        $graph = Graph::create('My First Graph');
333
        $filename = tempnam(sys_get_temp_dir(), 'tst');
334
335
        $this->expectException('phpDocumentor\GraphViz\Exception');
336
        $graph->export('fpd', $filename);
337
338
    }
339
340
    /**
341
     * @covers phpDocumentor\GraphViz\Graph::export
342
     */
343
    public function testExport() {
344
        $graph = Graph::create('My First Graph');
345
        $filename = tempnam(sys_get_temp_dir(), 'tst');
346
347
        $this->assertSame(
348
            $graph,
349
            $graph->export('pdf', $filename)
350
        );
351
        $this->assertTrue(is_readable($filename));
352
    }
353
354
    /**
355
     * @covers phpDocumentor\GraphViz\Graph::__toString
356
     */
357
    public function test__toString()
358
    {
359
        $graph = Graph::create('My First Graph');
360
        $this->assertSame(
361
            $this->normalizeLineEndings( (string) $graph ),
362
            $this->normalizeLineEndings( ('digraph "My First Graph" {' . PHP_EOL . PHP_EOL . '}' ))
363
        );
364
365
        $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...
366
        $this->assertSame(
367
            $this->normalizeLineEndings( (string) $graph ),
368
            $this->normalizeLineEndings( ('digraph "My First Graph" {' . PHP_EOL . 'label="PigeonPost"' . PHP_EOL . '}' ))
369
        );
370
371
        $graph->setStrict(true);
372
        $this->assertSame(
373
            $this->normalizeLineEndings( (string) $graph ),
374
            $this->normalizeLineEndings( ('strict digraph "My First Graph" {' . PHP_EOL . 'label="PigeonPost"' . PHP_EOL . '}' ))
375
        );
376
    }
377
378
    /**
379
     * Help avoid issue of "#Warning: Strings contain different line endings!" on Windows.
380
     */
381
    private function normalizeLineEndings($string)
382
    {
383
        return preg_replace('~\R~u', "\r\n", $string);
384
    }
385
386
}
387