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

assertParserHtmlOutputForCase()   B

Complexity

Conditions 4
Paths 5

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 8.7972
c 0
b 0
f 0
cc 4
eloc 13
nc 5
nop 1
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