Completed
Push — master ( dd7c4d...5ce00d )
by mw
02:33
created

DataValueDeserializerTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 85
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 4

Importance

Changes 4
Bugs 1 Features 0
Metric Value
wmc 6
c 4
b 1
f 0
lcom 0
cbo 4
dl 0
loc 85
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
A testCanConstruct() 0 7 1
A testNewDiWikiPage() 0 14 1
A testTryNewDiWikiPageForInvalidSeralization() 0 8 1
A testNewTimeValueForOutOfRangeTimestamp() 0 12 1
A testNewTimeValueForRawTimeFromat() 0 12 1
A testNewRecordValue() 0 23 1
1
<?php
2
3
namespace SEQL\Tests;
4
5
use SEQL\DataValueDeserializer;
6
use SMW\DIWikiPage;
7
use SMW\DIProperty;
8
use SMWDITime as DITime;
9
10
/**
11
 * @covers \SEQL\DataValueDeserializer
12
 * @group semantic-external-query-lookup
13
 *
14
 * @license GNU GPL v2+
15
 * @since 1.0
16
 *
17
 * @author mwjames
18
 */
19
class DataValueDeserializerTest extends \PHPUnit_Framework_TestCase {
20
21
	public function testCanConstruct() {
22
23
		$this->assertInstanceOf(
24
			'\SEQL\DataValueDeserializer',
25
			new DataValueDeserializer( 'foo' )
26
		);
27
	}
28
29
	public function testNewDiWikiPage() {
30
31
		$instance = new DataValueDeserializer( 'foo' );
32
33
		$value = array(
34
			'namespace' => NS_MAIN,
35
			'fulltext'  => 'abc def'
36
		);
37
38
		$this->assertEquals(
39
			new DIWikiPage( 'Foo:abc_def', NS_MAIN ),
40
			$instance->newDiWikiPage( $value )
41
		);
42
	}
43
44
	public function testTryNewDiWikiPageForInvalidSeralization() {
45
46
		$instance = new DataValueDeserializer( 'foo' );
47
48
		$this->assertFalse(
49
			$instance->newDiWikiPage( array( 'Foo' ) )
50
		);
51
	}
52
53
	public function testNewTimeValueForOutOfRangeTimestamp() {
54
55
		$instance = new DataValueDeserializer( 'foo' );
56
57
		$property = new DIProperty( 'Bar' );
58
		$property->setPropertyTypeId( '_dat' );
59
60
		$this->assertNotEquals(
61
			DITime::doUnserialize( '2/-200' ),
62
			$instance->newDataValueFrom( $property, '-2000101000000' )
63
		);
64
	}
65
66
	public function testNewTimeValueForRawTimeFromat() {
67
68
		$instance = new DataValueDeserializer( 'foo' );
69
70
		$property = new DIProperty( 'Bar' );
71
		$property->setPropertyTypeId( '_dat' );
72
73
		$this->assertEquals(
74
			DITime::doUnserialize( '2/-200' ),
75
			$instance->newDataValueFrom( $property, array( 'raw' => '2/-200' ) )->getDataItem()
76
		);
77
	}
78
79
	public function testNewRecordValue() {
80
81
		$instance = new DataValueDeserializer( 'foo' );
82
83
		$property = new DIProperty( 'Foo' );
84
		$property->setPropertyTypeId( '_rec' );
85
86
		$item = array(
87
			'namespace' => NS_MAIN,
88
			'fulltext'  => 'abc def'
89
		);
90
91
		$record[] = array(
0 ignored issues
show
Coding Style Comprehensibility introduced by
$record was never initialized. Although not strictly required by PHP, it is generally a good practice to add $record = 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...
92
			'label'  => 'Foo',
93
			'typeid' => '_wpg',
94
			'item'   => array( $item )
95
		);
96
97
		$this->assertInstanceOf(
98
			'\SMWRecordValue',
99
			$instance->newDataValueFrom( $property, $record )
100
		);
101
	}
102
103
}
104