Completed
Push — master ( 17dff3...f2d821 )
by mw
42:32 queued 07:38
created

ClassDescriptionTest::testGetFingerprint()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 38
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 19
nc 1
nop 0
dl 0
loc 38
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace SMW\Tests\Query\Language;
4
5
use SMW\DIWikiPage;
6
use SMW\Localizer;
7
use SMW\Query\Language\ClassDescription;
8
use SMW\Query\Language\ThingDescription;
9
10
/**
11
 * @covers \SMW\Query\Language\ClassDescription
12
 * @group semantic-mediawiki
13
 *
14
 * @license GNU GPL v2+
15
 * @since 2.1
16
 *
17
 * @author mwjames
18
 */
19
class ClassDescriptionTest extends \PHPUnit_Framework_TestCase {
20
21
	public function testCanConstruct() {
22
23
		$class = $this->getMockBuilder( '\SMW\DIWikiPage' )
24
			->disableOriginalConstructor()
25
			->getMock();
26
27
		$this->assertInstanceOf(
28
			'SMW\Query\Language\ClassDescription',
29
			new ClassDescription( $class )
30
		);
31
32
		// Legacy
33
		$this->assertInstanceOf(
34
			'SMW\Query\Language\ClassDescription',
35
			new \SMWClassDescription( $class )
36
		);
37
	}
38
39
	public function testConstructThrowsException() {
40
41
		$this->setExpectedException( 'Exception' );
42
43
		new ClassDescription( new \stdClass );
44
	}
45
46
	public function testCommonMethods() {
47
48
		$ns = Localizer::getInstance()->getNamespaceTextById( NS_CATEGORY );
49
50
		$class = new DIWikiPage( 'Foo', NS_CATEGORY );
51
		$instance = new ClassDescription( $class );
52
53
		$this->assertEquals( array( $class ), $instance->getCategories() );
54
55
		$this->assertEquals( "[[{$ns}:Foo]]", $instance->getQueryString() );
56
		$this->assertEquals( " <q>[[{$ns}:Foo]]</q> ", $instance->getQueryString( true ) );
57
58
		$this->assertEquals( false, $instance->isSingleton() );
59
		$this->assertEquals( array(), $instance->getPrintRequests() );
60
61
		$this->assertEquals( 1, $instance->getSize() );
62
		$this->assertEquals( 0, $instance->getDepth() );
63
		$this->assertEquals( 2, $instance->getQueryFeatures() );
64
	}
65
66
	public function testAddDescription() {
67
68
		$ns = Localizer::getInstance()->getNamespaceTextById( NS_CATEGORY );
69
70
		$instance = new ClassDescription( new DIWikiPage( 'Foo', NS_CATEGORY ) );
71
		$instance->addDescription( new ClassDescription( new DIWikiPage( 'Bar', NS_CATEGORY ) ) );
72
73
		$this->assertEquals(
74
			"[[{$ns}:Foo||Bar]]",
75
			$instance->getQueryString()
76
		);
77
78
		$this->assertEquals(
79
			" <q>[[{$ns}:Foo||Bar]]</q> ",
80
			$instance->getQueryString( true )
81
		);
82
	}
83
84
	public function testGetFingerprint() {
85
86
		$ns = Localizer::getInstance()->getNamespaceTextById( NS_CATEGORY );
0 ignored issues
show
Unused Code introduced by
$ns is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
87
88
		$instance = new ClassDescription(
89
			new DIWikiPage( 'Foo', NS_CATEGORY )
90
		);
91
92
		$instance->addDescription(
93
			new ClassDescription( new DIWikiPage( 'Bar', NS_CATEGORY ) )
94
		);
95
96
		$expected = $instance->getFingerprint();
97
98
		// Different position, same hash
99
		$instance = new ClassDescription(
100
			new DIWikiPage( 'Bar', NS_CATEGORY )
101
		);
102
103
		$instance->addDescription(
104
			new ClassDescription( new DIWikiPage( 'Foo', NS_CATEGORY ) )
105
		);
106
107
		$this->assertSame(
108
			$expected,
109
			$instance->getFingerprint()
110
		);
111
112
		// Adds extra description, changes hash
113
		$instance->addDescription(
114
			new ClassDescription( new DIWikiPage( 'Foobar', NS_CATEGORY ) )
115
		);
116
117
		$this->assertNotSame(
118
			$expected,
119
			$instance->getFingerprint()
120
		);
121
	}
122
123
	public function testPrune() {
124
125
		$instance = new ClassDescription( new DIWikiPage( 'Foo', NS_CATEGORY ) );
126
127
		$maxsize  = 1;
128
		$maxDepth = 1;
129
		$log      = array();
130
131
		$this->assertEquals(
132
			$instance,
133
			$instance->prune( $maxsize, $maxDepth, $log )
134
		);
135
136
		$maxsize  = 0;
137
		$maxDepth = 1;
138
		$log      = array();
139
140
		$this->assertEquals(
141
			new ThingDescription(),
142
			$instance->prune( $maxsize, $maxDepth, $log )
143
		);
144
	}
145
146
}
147