summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/docs/technical/code-snippets/phpunit.test.property.md
blob: c20eb457845683aa8a0f3ea16b2000ed5032977e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
## Setup a "Text" type property

```php
$property = new DIProperty( 'Foo' );
$property->setPropertyTypeId( '_txt' );

// Using a dedicated property to created a DV
$dataValue = DataValueFactory::getInstance()->newDataValueByProperty(
	$property,
	'Some text'
);

$this->assertInstanceof(
	'\SMWStringValue',
	$dataValue
);

$this->assertEquals(
	'Some text',
	$dataValue->getDataItem()->getString()
);
```

## Using the MockBuilder

```php
$store = $this->getMockBuilder( '\SMW\Store' )
	->disableOriginalConstructor()
	->getMockForAbstractClass();

$store->expects( $this->at( 0 ) )
	->method( 'getPropertyValues' )
	->will( $this->returnValue( array(
		new DIWikiPage( 'SomePropertyOfTypeBlob', SMW_NS_PROPERTY ) ) ) );

$store->expects( $this->at( 1 ) )
	->method( 'getPropertyValues' )
	->with(
		$this->equalTo( new DIWikiPage( 'SomePropertyOfTypeBlob', SMW_NS_PROPERTY ) ),
		$this->anything(),
		$this->anything() )
	->will( $this->returnValue( array(
		\SMWDIUri::doUnserialize( 'http://semantic-mediawiki.org/swivt/1.0#_txt' ) ) ) );

// Inject the store as a mock object due to DIProperty::findPropertyTypeID finding the
// type dynamically when called without explicit declaration
ApplicationFactory::getInstance()->registerObject( 'Store', $store );

// Create a DV from a string value instead of using a dedicated typed
// property (used by #set, #subobject since the user input is a string and not
// a typed object)
$dataValue = DataValueFactory::getInstance()->newDataValueByText(
	'SomePropertyOfTypeBlob',
	'Some text'
);

$this->assertInstanceof(
	'\SMWStringValue',
	$dataValue
);

$this->assertEquals(
	'Some text',
	$dataValue->getDataItem()->getString()
);

// Reset instance to avoid issues with tests that follow hereafter
ApplicationFactory::getInstance()->clear();
```