Completed
Push — master ( f6e07a...607751 )
by Stephan
02:50
created

ParserHtmlTestCaseProcessor   A

Complexity

Total Complexity 15

Size/Duplication

Total Lines 108
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 0
Metric Value
wmc 15
lcom 1
cbo 4
dl 0
loc 108
rs 10
c 0
b 0
f 0

5 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 7 1
A process() 0 12 3
B assertParserHtmlOutputForCase() 0 23 4
B getOutputText() 0 25 4
A isSetAndTrueish() 0 4 3
1
<?php
2
3
namespace SRF\Tests\Integration\JSONScript;
4
5
use SMW\DIWikiPage;
6
use SMW\Store;
7
use SMW\Tests\Utils\UtilityFactory;
8
9
/**
10
 * @group semantic-result-formats
11
 * @group medium
12
 *
13
 * @license GNU GPL v2+
14
 * @since 2.5
15
 *
16
 * @author Stephan Gambke
17
 */
18
class ParserHtmlTestCaseProcessor extends \PHPUnit_Framework_TestCase {
19
20
	/**
21
	 * @var HtmlValidator
22
	 */
23
	private $htmlValidator;
24
	/**
25
	 * @var Store
26
	 */
27
	private $store;
28
29
	/**
30
	 * ParserHtmlTestCaseProcessor constructor.
31
	 * @param Store $store
32
	 * @param HtmlValidator $htmlValidator
33
	 */
34
	public function __construct( Store $store, HtmlValidator $htmlValidator ) {
35
36
		parent::__construct();
37
38
		$this->htmlValidator = $htmlValidator;
39
		$this->store = $store;
40
	}
41
42
	/**
43
	 * @param array $case
44
	 */
45
	public function process( array $case ) {
46
47
		if ( !isset( $case[ 'subject' ] ) ) {
48
			return;
49
		}
50
51
		if ( isset( $case[ 'about' ] ) ) {
52
			$this->setName( $case[ 'about' ] );
53
		}
54
55
		$this->assertParserHtmlOutputForCase( $case );
56
	}
57
58
	/**
59
	 * @param $case
60
	 */
61
	private function assertParserHtmlOutputForCase( $case ) {
62
63
		if ( !isset( $case[ 'assert-output' ] ) ) {
64
			return;
65
		}
66
67
		$outputText = $this->getOutputText( $case );
68
69
		if ( $this->isSetAndTrueish( $case[ 'assert-output' ], 'to-be-valid-html' ) ) {
70
			$this->htmlValidator->assertThatHtmlIsValid(
71
				$outputText,
72
				$case[ 'about' ]
73
			);
74
		}
75
76
		if ( $this->isSetAndTrueish( $case[ 'assert-output' ], 'to-contain' ) ) {
77
			$this->htmlValidator->assertThatHtmlContains(
78
				$case[ 'assert-output' ][ 'to-contain' ],
79
				$outputText,
80
				$case[ 'about' ]
81
			);
82
		}
83
	}
84
85
	/**
86
	 * @param array $case
87
	 * @return string
88
	 */
89
	private function getOutputText( $case ) {
90
		$subject = DIWikiPage::newFromText(
91
			$case[ 'subject' ],
92
			isset( $case[ 'namespace' ] ) ? constant( $case[ 'namespace' ] ) : NS_MAIN
93
		);
94
95
		$parserOutput = UtilityFactory::getInstance()->newPageReader()->getEditInfo( $subject->getTitle() )->output;
0 ignored issues
show
Bug introduced by
It seems like $subject->getTitle() can be null; however, getEditInfo() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
96
97
		if ( !$this->isSetAndTrueish( $case[ 'assert-output' ], [ 'withOutputPageContext', 'onPageView' ] ) ) {
98
			return $parserOutput->getText();
99
		}
100
101
		$context = new \RequestContext();
102
		$context->setTitle( $subject->getTitle() );
103
104
		if ( $this->isSetAndTrueish( $case[ 'assert-output' ], 'withOutputPageContext' ) ) {
105
			// Ensures the OutputPageBeforeHTML hook is run
106
			$context->getOutput()->addParserOutput( $parserOutput );
107
		} else {
108
			\Article::newFromTitle( $subject->getTitle(), $context )->view();
109
		}
110
111
		return $context->getOutput()->getHTML();
112
113
	}
114
115
	/**
116
	 * @param $array
117
	 * @param string | string[] $keys
118
	 * @return bool
119
	 */
120
	private function isSetAndTrueish( $array, $keys ) {
121
		$keys = (array)$keys;
122
		return array_reduce( $keys, function ( $carry, $key ) use ( $array ) { return $carry || isset( $array[ $key ] ) && $array[ $key ]; }, false );
123
	}
124
125
}
126