summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/tests/phpunit/Unit/PropertyAnnotators/SchemaPropertyAnnotatorTest.php
blob: d2b23d1ce682105fb6c08c56fa14004512fb4543 (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
70
71
72
73
74
75
76
77
78
79
80
81
<?php

namespace SMW\Tests\PropertyAnnotators;

use SMW\DataItemFactory;
use SMW\PropertyAnnotators\NullPropertyAnnotator;
use SMW\PropertyAnnotators\SchemaPropertyAnnotator;
use SMW\Schema\SchemaDefinition;
use SMW\Tests\TestEnvironment;

/**
 * @covers \SMW\PropertyAnnotators\SchemaPropertyAnnotator
 * @group semantic-mediawiki
 *
 * @license GNU GPL v2+
 * @since 3.0
 *
 * @author mwjames
 */
class SchemaPropertyAnnotatorTest extends \PHPUnit_Framework_TestCase {

	private $semanticDataFactory;
	private $semanticDataValidator;
	private $dataItemFactory;

	protected function setUp() {
		parent::setUp();

		$testEnvironment = new TestEnvironment();
		$this->semanticDataFactory = $testEnvironment->getUtilityFactory()->newSemanticDataFactory();
		$this->semanticDataValidator = $testEnvironment->newValidatorFactory()->newSemanticDataValidator();
		$this->dataItemFactory = new DataItemFactory();
	}

	public function testCanConstruct() {

		$semanticData = $this->getMockBuilder( '\SMW\SemanticData' )
			->disableOriginalConstructor()
			->getMock();

		$instance = new SchemaPropertyAnnotator(
			new NullPropertyAnnotator( $semanticData ),
			new SchemaDefinition( 'foo', [] )
		);

		$this->assertInstanceOf(
			SchemaPropertyAnnotator::class,
			$instance
		);
	}

	public function testAddAnnotation() {

		$def = [
			SchemaDefinition::SCHEMA_TYPE => 'bar',
			SchemaDefinition::SCHEMA_DESCRIPTION => '...',
			SchemaDefinition::SCHEMA_TAG => [ 'foobar' ],
		];

		$semanticData = $this->semanticDataFactory->newEmptySemanticData( __METHOD__ );

		$instance = new SchemaPropertyAnnotator(
			new NullPropertyAnnotator( $semanticData ),
			new SchemaDefinition( 'foo', $def )
		);

		$instance->addAnnotation();

		$expected = [
			'propertyCount'  => 4,
			'propertyKeys'   => [ '_SCHEMA_TYPE', '_SCHEMA_DEF', '_SCHEMA_DESC', '_SCHEMA_TAG' ],
			'propertyValues' => [ 'bar', '...', 'foobar', '{"type":"bar","description":"...","tags":["foobar"]}' ],
		];

		$this->semanticDataValidator->assertThatPropertiesAreSet(
			$expected,
			$instance->getSemanticData()
		);
	}

}