1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @file |
5
|
|
|
* Grafizzi\Graph\Tests\IG12913Test: a component of the Grafizzi library. |
6
|
|
|
* |
7
|
|
|
* (c) 2012 Frédéric G. MARAND <[email protected]> |
8
|
|
|
* |
9
|
|
|
* Grafizzi is free software: you can redistribute it and/or modify it under the |
10
|
|
|
* terms of the GNU Lesser General Public License as published by the Free |
11
|
|
|
* Software Foundation, either version 3 of the License, or (at your option) any |
12
|
|
|
* later version. |
13
|
|
|
* |
14
|
|
|
* Grafizzi is distributed in the hope that it will be useful, but WITHOUT ANY |
15
|
|
|
* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
16
|
|
|
* A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more |
17
|
|
|
* details. |
18
|
|
|
* |
19
|
|
|
* You should have received a copy of the GNU Lesser General Public License |
20
|
|
|
* along with Grafizzi, in the COPYING.LESSER.txt file. If not, see |
21
|
|
|
* <http://www.gnu.org/licenses/> |
22
|
|
|
*/ |
23
|
|
|
|
24
|
|
|
namespace Grafizzi\Graph\Tests; |
25
|
|
|
|
26
|
|
|
use Grafizzi\Graph\Attribute; |
27
|
|
|
use Grafizzi\Graph\Cluster; |
28
|
|
|
use Grafizzi\Graph\Filter\DotFilter; |
29
|
|
|
use Grafizzi\Graph\Filter\FilterInterface; |
30
|
|
|
use Grafizzi\Graph\Node; |
31
|
|
|
use Pimple\Container; |
32
|
|
|
|
33
|
|
|
require 'vendor/autoload.php'; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* A recreation of Image_GraphViz bug_12913.phpt |
37
|
|
|
* |
38
|
|
|
* Image_GraphViz version author: Philippe Jausions <[email protected]> |
39
|
|
|
* |
40
|
|
|
* Request 12913: "PEAR_Error on failure" |
41
|
|
|
* |
42
|
|
|
* Since Grafizzi is not a PEAR component, it can throw exceptions instead of |
43
|
|
|
* using PEAR errors, so the test applies to exceptions. |
44
|
|
|
*/ |
45
|
|
|
class IG12913Test extends BaseGraphTest { |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* |
49
|
|
|
* @var \Grafizzi\Graph\Graph |
50
|
|
|
*/ |
51
|
|
|
public $Graph2; |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @var \Pimple\Container |
55
|
|
|
*/ |
56
|
|
|
public $dic2; |
57
|
|
|
|
58
|
1 |
|
public function setUp($name = 'G', $attributes = array()) { |
59
|
1 |
|
parent::setUp('G'); |
60
|
1 |
|
$this->Graph2 = $this->Graph; |
61
|
1 |
|
$this->dic2 = $this->dic; |
62
|
1 |
|
$this->dic2['use_exceptions'] = false; |
63
|
1 |
|
unset($this->dic, $this->Graph); |
64
|
|
|
|
65
|
1 |
|
parent::setUp('G'); |
66
|
1 |
|
$this->dic['use_exceptions'] = true; |
67
|
|
|
|
68
|
1 |
|
$this->Graph->addChild($cluster1 = new Cluster($this->dic, 1)); |
69
|
1 |
|
$cluster1->addChild($node1 = new Node($this->dic, 'Node1', array( |
70
|
1 |
|
new Attribute($this->dic, 'label', 'Node1'), |
71
|
1 |
|
))); |
72
|
|
|
|
73
|
1 |
|
$this->Graph2->addChild($cluster2 = new Cluster($this->dic2, 2)); |
74
|
1 |
|
$cluster2->addChild($node2 = new Node($this->dic2, 'Node2', array( |
75
|
1 |
|
new Attribute($this->dic2, 'label', 'Node2'), |
76
|
1 |
|
))); |
77
|
1 |
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param FilterInterface $filter |
81
|
|
|
* @param string $format |
82
|
|
|
*/ |
83
|
1 |
View Code Duplication |
protected function withDicException(FilterInterface $filter, $format) { |
|
|
|
|
84
|
1 |
|
$dic = new Container(array('use_exceptions' => true)); |
85
|
1 |
|
$filter->setDic($dic); |
|
|
|
|
86
|
|
|
try { |
87
|
1 |
|
$filter->image($format); |
|
|
|
|
88
|
|
|
$this->fail('Invalid format image did not throw an exception.'); |
89
|
|
|
} |
90
|
1 |
|
catch (\InvalidArgumentException $e) { |
91
|
1 |
|
$this->assertInstanceOf('InvalidArgumentException', $e, 'Invalid argument for invalid format.'); |
92
|
|
|
} |
93
|
1 |
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @param FilterInterface $filter |
97
|
|
|
* @param string $format |
98
|
|
|
*/ |
99
|
1 |
View Code Duplication |
protected function withDicNoException(FilterInterface $filter, $format) { |
|
|
|
|
100
|
1 |
|
$dic = new Container(array('use_exceptions' => false)); |
101
|
1 |
|
$filter->setDic($dic); |
|
|
|
|
102
|
|
|
try { |
103
|
1 |
|
$result = $filter->image($format); |
|
|
|
|
104
|
1 |
|
$this->assertFalse($result, 'Unavailable format image.'); |
105
|
|
|
} |
106
|
1 |
|
catch (\InvalidArgumentException $e) { |
107
|
|
|
$this->fail('Invalid format image threw an exception.'); |
108
|
1 |
|
} } |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Tests Graph->image() |
112
|
|
|
*/ |
113
|
1 |
|
public function testImage() { |
114
|
1 |
|
$dotFilter = new DotFilter(); |
115
|
1 |
|
$format = DotFilterTest::INVALID_FORMAT; |
116
|
1 |
|
$this->withDicException($dotFilter, $format); |
117
|
1 |
|
$this->withDicNoException($dotFilter, $format); |
118
|
1 |
|
} |
119
|
|
|
} |
120
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.