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( |
|
|
|
|
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' ); |
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
|
|
|
|
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
$myArray
is initialized the first time when the foreach loop is entered. You can also see that the value of thebar
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.