|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace SRF\Tests\Unit\Formats; |
|
4
|
|
|
|
|
5
|
|
|
use SMW\Test\QueryPrinterRegistryTestCase; |
|
6
|
|
|
use SRF\Graph\GraphNode; |
|
7
|
|
|
|
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Tests for the SRF\Graph class. |
|
11
|
|
|
* |
|
12
|
|
|
* @file |
|
13
|
|
|
* @since 1.8 |
|
14
|
|
|
* |
|
15
|
|
|
* @ingroup SemanticResultFormats |
|
16
|
|
|
* @ingroup Test |
|
17
|
|
|
* |
|
18
|
|
|
* @group SRF |
|
19
|
|
|
* @group SMWExtension |
|
20
|
|
|
* @group ResultPrinters |
|
21
|
|
|
* |
|
22
|
|
|
* @licence GNU GPL v2+ |
|
23
|
|
|
* @author Jeroen De Dauw < [email protected] > |
|
24
|
|
|
*/ |
|
25
|
|
|
class GraphTest extends QueryPrinterRegistryTestCase { |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @see QueryPrinterRegistryTestCase::getFormats |
|
29
|
|
|
* |
|
30
|
|
|
* @since 1.8 |
|
31
|
|
|
* |
|
32
|
|
|
* @return array |
|
33
|
|
|
*/ |
|
34
|
|
|
public function getFormats() { |
|
35
|
|
|
return [ 'graph' ]; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @see QueryPrinterRegistryTestCase::getClass |
|
40
|
|
|
* |
|
41
|
|
|
* @since 1.8 |
|
42
|
|
|
* |
|
43
|
|
|
* @return string |
|
44
|
|
|
*/ |
|
45
|
|
|
public function getClass() { |
|
46
|
|
|
return 'SRF\Graph\GraphResultPrinter'; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Testing class GraphNode |
|
51
|
|
|
* |
|
52
|
|
|
* @since 3.0 |
|
53
|
|
|
* |
|
54
|
|
|
*/ |
|
55
|
|
|
public function testGraphNode(){ |
|
56
|
|
|
|
|
57
|
|
|
//can create GraphNode |
|
58
|
|
|
$this->assertInstanceOf( |
|
59
|
|
|
GraphNode::class, |
|
60
|
|
|
$node = new GraphNode( 'Team:Beta' ) |
|
61
|
|
|
); |
|
62
|
|
|
|
|
63
|
|
|
$this->assertEquals( 'Team:Beta', $node->getID() ); |
|
64
|
|
|
|
|
65
|
|
|
$node->addLabel( 1, "Fossil Power Generation" ); |
|
66
|
|
|
$this->assertEquals( "Fossil Power Generation", $node->getLabel(1) ); |
|
67
|
|
|
|
|
68
|
|
|
$node->addLabel( 2, "Gonzo the Great" ); |
|
69
|
|
|
$this->assertEquals( "Gonzo the Great\l", $node->getLabel(2) ); |
|
70
|
|
|
|
|
71
|
|
|
$node->addLabel( 3, "Miss Piggy" ); |
|
72
|
|
|
$node->addLabel( 3, "Rowlf the Dog" ); |
|
73
|
|
|
$this->assertEquals( "Miss Piggy\lRowlf the Dog\l", $node->getLabel(3) ); |
|
74
|
|
|
|
|
75
|
|
|
$mockParentNode1[] = [ |
|
|
|
|
|
|
76
|
|
|
"predicate" => 'Part Of Team', |
|
77
|
|
|
"object" => 'Alpha Team' |
|
78
|
|
|
]; |
|
79
|
|
|
|
|
80
|
|
|
$node->addParentNode( 'Part Of Team', 'Alpha Team' ); |
|
81
|
|
|
$this->assertEquals( $mockParentNode1, $node->getParentNode() ); |
|
82
|
|
|
|
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
} |
|
86
|
|
|
|
Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.
Let’s take a look at an example:
As you can see in this example, the array
$myArrayis initialized the first time when the foreach loop is entered. You can also see that the value of thebarkey is only written conditionally; thus, its value might result from a previous iteration.This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.