Completed
Push — master ( 14d2bd...06e609 )
by mw
81:37 queued 59:24
created

SpecialAskTest::setupGlobals()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 18
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 1 Features 0
Metric Value
cc 3
eloc 10
nc 3
nop 1
dl 0
loc 18
rs 9.4285
c 1
b 1
f 0
1
<?php
2
3
namespace SMW\Tests\Integration;
4
5
use SMWAskPage;
6
use DOMDocument;
7
8
/**
9
 * @group semantic-mediawiki
10
 *
11
 * @license GNU GPL v2+
12
 * @since 2.4
13
 *
14
 * @author Stephan Gambke
15
 */
16
class SpecialAskTest extends \PHPUnit_Framework_TestCase {
17
18
	private $oldRequestValues;
19
	private $oldBodyText;
20
21
	/**
22
	 * @dataProvider provideTestData
23
	 * @param $params
24
	 */
25
	public function testProducesWellformedHtml( $params ) {
0 ignored issues
show
Coding Style introduced by
testProducesWellformedHtml uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
26
27
		$this->setupGlobals( $params );
28
29
		$special = new SMWAskPage();
30
		$special->execute( null );
31
32
		$html = $GLOBALS[ 'wgOut' ]->getHtml();
33
		$html = '<!DOCTYPE html><html><body>' . $html . '</body></html>';
34
35
		$document = new DOMDocument();
36
		$result = $document->loadHTML( $html );
37
		$this->assertTrue( $result );
38
39
		$result = $document->loadXML( $html );
40
		$this->assertTrue( $result );
41
42
		$this->restoreGlobals( $params );
43
	}
44
45
	/**
46
	 * @return array
47
	 */
48
	public function provideTestData() {
49
		return array(
50
			array( array( 'eq' => 'yes', 'q' => '' ) ),
51
			array( array( 'eq' => 'no', 'q' => '[[]]' ) ),
52
		);
53
	}
54
55
	/**
56
	 * @param array $params
57
	 */
58
	protected function setupGlobals( $params ) {
59
		global $wgOut, $wgRequest;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
60
61
		$this->oldRequestValues = array();
62
63
		foreach ( $params as $key => $value ) {
64
65
			$oldVal = $wgRequest->getText( $key, null );
66
			if ( $oldVal !== null ) {
67
				$this->oldRequestValues[ $key ] = $oldVal;
68
			}
69
70
			$wgRequest->setVal( $key, $value );
71
		}
72
73
		$this->oldBodyText = $wgOut->getHTML();
74
		$wgOut->clearHTML();
75
	}
76
77
	/**
78
	 * @param array $params
79
	 */
80
	protected function restoreGlobals( $params ) {
81
		global $wgOut, $wgRequest;
0 ignored issues
show
Compatibility Best Practice introduced by
Use of global functionality is not recommended; it makes your code harder to test, and less reusable.

Instead of relying on global state, we recommend one of these alternatives:

1. Pass all data via parameters

function myFunction($a, $b) {
    // Do something
}

2. Create a class that maintains your state

class MyClass {
    private $a;
    private $b;

    public function __construct($a, $b) {
        $this->a = $a;
        $this->b = $b;
    }

    public function myFunction() {
        // Do something
    }
}
Loading history...
82
83
		foreach ( $params as $key => $value ) {
84
			if ( method_exists( $wgRequest, 'unsetVal' ) ) {
85
				$wgRequest->unsetVal( $key );
86
			} else {
87
				$wgRequest->setVal( $key, null );
88
			}
89
		}
90
		foreach ( $this->oldRequestValues as $key => $value ) {
91
			$wgRequest->setVal( $key, $value );
92
		}
93
94
		$wgOut->clearHTML();
95
		$wgOut->addHTML( $this->oldBodyText );
96
	}
97
}
98