diff options
Diffstat (limited to 'www/wiki/extensions/SemanticMediaWiki/docs/technical/code-snippets/phpunit.test.property.md')
-rw-r--r-- | www/wiki/extensions/SemanticMediaWiki/docs/technical/code-snippets/phpunit.test.property.md | 69 |
1 files changed, 69 insertions, 0 deletions
diff --git a/www/wiki/extensions/SemanticMediaWiki/docs/technical/code-snippets/phpunit.test.property.md b/www/wiki/extensions/SemanticMediaWiki/docs/technical/code-snippets/phpunit.test.property.md new file mode 100644 index 00000000..c20eb457 --- /dev/null +++ b/www/wiki/extensions/SemanticMediaWiki/docs/technical/code-snippets/phpunit.test.property.md @@ -0,0 +1,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(); +``` |