testTextValueWithEmbeddedLink()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 19
rs 9.6333
c 0
b 0
f 0
cc 1
nc 1
nop 0
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' );
0 ignored issues
show
Deprecated Code introduced by
The method SMW\DIProperty::setPropertyTypeId() has been deprecated with message: since 3.0, use DIProperty::setPropertyValueType

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...
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' );
0 ignored issues
show
Deprecated Code introduced by
The method SMW\DIProperty::setPropertyTypeId() has been deprecated with message: since 3.0, use DIProperty::setPropertyValueType

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...
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' );
0 ignored issues
show
Deprecated Code introduced by
The method SMW\DIProperty::setPropertyTypeId() has been deprecated with message: since 3.0, use DIProperty::setPropertyValueType

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...
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
	public function testTextValueWithEmbeddedLink() {
104
105
		$instance = new DataValueDeserializer( 'abc' );
106
107
		$property = new DIProperty( 'Bar' );
108
		$property->setPropertyTypeId( '_txt' );
0 ignored issues
show
Deprecated Code introduced by
The method SMW\DIProperty::setPropertyTypeId() has been deprecated with message: since 3.0, use DIProperty::setPropertyValueType

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...
109
110
		$dataValue = $instance->newDataValueFrom( $property, 'Foo [[42]] bar' );
111
112
		$this->assertInstanceOf(
113
			'\SMWStringValue',
114
			$dataValue
115
		);
116
117
		$this->assertEquals(
118
			'Foo [[abc:42|42]] bar',
119
			$dataValue->getDataItem()->getString()
120
		);
121
	}
122
123
}
124