Completed
Push — master ( 43acf6...5d1976 )
by mw
12s
created

HashBuilderTest::testCreateFromSemanticData()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 1
eloc 6
nc 1
nop 0
1
<?php
2
3
namespace SMW\Tests;
4
5
use SMW\DIProperty;
6
use SMW\DIWikiPage;
7
use SMW\HashBuilder;
8
use SMW\SemanticData;
9
use Title;
10
11
/**
12
 * @covers \SMW\HashBuilder
13
 * @group semantic-mediawiki
14
 *
15
 * @license GNU GPL v2+
16
 * @since 2.1
17
 *
18
 * @author mwjames
19
 */
20
class HashBuilderTest extends \PHPUnit_Framework_TestCase {
21
22
	/**
23
	 * @dataProvider segmentProvider
24
	 */
25
	public function testTitleRoundTrip( $namespace, $title, $interwiki , $fragment ) {
26
27
		$title = Title::makeTitle( $namespace, $title, $fragment, $interwiki );
28
29
		$this->assertEquals(
30
			$title,
31
			HashBuilder::newTitleFromHash(
32
				HashBuilder::getHashIdForTitle( $title )
33
			)
34
		);
35
	}
36
37
	/**
38
	 * @dataProvider segmentProvider
39
	 */
40
	public function testDiWikiPageRoundTrip( $namespace, $title, $interwiki, $subobjectName ) {
41
42
		$dataItem = new DIWikiPage( $title, $namespace, $interwiki, $subobjectName );
43
44
		$this->assertEquals(
45
			$dataItem,
46
			HashBuilder::newDiWikiPageFromHash(
47
				HashBuilder::getHashIdForDiWikiPage( $dataItem )
48
			)
49
		);
50
	}
51
52
	public function testPredefinedProperty() {
53
54
		$instance = new HashBuilder();
55
56
		$property = new DIProperty( '_MDAT' );
57
		$dataItem = $property->getDiWikiPage();
58
59
		$this->assertEquals(
60
			$dataItem,
61
			$instance->newDiWikiPageFromHash(
62
				$instance->getHashIdForDiWikiPage( $dataItem )
0 ignored issues
show
Bug introduced by
It seems like $dataItem defined by $property->getDiWikiPage() on line 57 can be null; however, SMW\HashBuilder::getHashIdForDiWikiPage() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
63
			)
64
		);
65
66
		$this->assertEquals(
67
			$dataItem,
68
			$instance->newDiWikiPageFromHash(
69
				$instance->createHashIdFromSegments( $property->getKey(), SMW_NS_PROPERTY )
0 ignored issues
show
Deprecated Code introduced by
The method SMW\HashBuilder::createHashIdFromSegments() has been deprecated with message: since 2.4, use Hash::createFromSegments

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
70
			)
71
		);
72
	}
73
74
	public function testContentHashId() {
75
76
		$hash = HashBuilder::createHashIdForContent( 'Foo' );
77
78
		$this->assertInternalType(
79
			'string',
80
			$hash
81
		);
82
83
		$this->assertSame(
84
			$hash,
85
			HashBuilder::createHashIdForContent( array( 'Foo' ) )
86
		);
87
88
		$this->assertContains(
89
			'Bar',
90
			HashBuilder::createHashIdForContent( array( 'Foo' ), 'Bar' )
91
		);
92
	}
93
94
	public function testCreateFromSemanticData() {
95
96
		$semanticData = new SemanticData(
97
			DIWikiPage::newFromText( __METHOD__ )
98
		);
99
100
		$this->assertInternalType(
101
			'string',
102
			HashBuilder::createFromSemanticData( $semanticData )
103
		);
104
	}
105
106
	public function segmentProvider() {
107
108
		$provider[] = array( NS_FILE, 'ichi', '', '' );
0 ignored issues
show
Coding Style Comprehensibility introduced by
$provider was never initialized. Although not strictly required by PHP, it is generally a good practice to add $provider = array(); before regardless.

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:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key 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.

Loading history...
109
		$provider[] = array( NS_HELP, 'ichi', 'ni', '' );
110
		$provider[] = array( NS_MAIN, 'ichi maru', 'ni', 'san' );
111
112
		return $provider;
113
	}
114
115
}
116