summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/SemanticMediaWiki/tests/phpunit/Utils/Validators/IncomingSemanticDataValidator.php
blob: ed7fc6f5699d6c7cb316af74778d3b28774f4390 (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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
<?php

namespace SMW\Tests\Utils\Validators;

use SMW\DIWikiPage;
use SMW\Store;

/**
 * @group semantic-mediawiki
 *
 * @license GNU GPL v2+
 * @since 2.5
 *
 * @author mwjames
 */
class IncomingSemanticDataValidator extends \PHPUnit_Framework_Assert {

	/**
	 * @var Store
	 */
	private $store;

	/**
	 * @since 2.5
	 *
	 * @param Store $store
	 */
	public function __construct( Store $store ) {
		$this->store = $store;
	}

	/**
	 * @since 2.5
	 *
	 * @param array $incomingSemanticData [description]
	 * @param DIWikiPage $subject
	 * @param string $message
	 */
	public function assertThatIncomingDataAreSet( array $incomingSemanticData, DIWikiPage $subject, $message ) {

		if ( !isset( $incomingSemanticData['propertyKeys'] ) ) {
			return;
		}

		$incomingProperties = $this->store->getInProperties( $subject );

		$this->assertCount(
			count( $incomingSemanticData['propertyKeys'] ),
			$incomingProperties,
			"Failed asserting count for incoming `propertyKeys` on " . $message . '"'
		);

		$this->doAssertPropertiesAndValues(
			$incomingSemanticData,
			$incomingProperties,
			$subject,
			$message
		);
	}

	private function doAssertPropertiesAndValues( $incomingSemanticData, $incomingProperties, $subject, $message ) {

		$incomingPropertyValues = [];

		foreach ( $incomingProperties as $property ) {

			$key = $property->getKey();

			$this->assertContains(
				$key,
				$incomingSemanticData['propertyKeys'],
				$this->createMessage( 'propertyKeys', $key, $message, $incomingSemanticData['propertyKeys'] )
			);

			if ( !isset( $incomingSemanticData['propertyValues'] ) ) {
				continue;
			}

			$propertySubjects = $this->store->getPropertySubjects( $property, $subject );

			foreach ( $propertySubjects as $propertySubject ) {
				$incomingPropertyValues[] = $propertySubject->getSerialization();
			}
		}

		foreach ( $incomingPropertyValues as $value ) {
			$this->assertContains(
				$value,
				$incomingSemanticData['propertyValues'],
				$this->createMessage( 'propertyValues', $value, $message, $incomingSemanticData['propertyValues'] )
			);
		}
	}

	private function createMessage( $section, $key, $message, array $data ) {
		return "Failed asserting that '{$key}' for incoming `$section` on \"" . $message . '" is listed in [ ' . implode( ',', $data ) . ' ]';
	}

}