These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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
|
|||
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 1. Pass all data via parametersfunction myFunction($a, $b) {
// Do something
}
2. Create a class that maintains your stateclass 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 1. Pass all data via parametersfunction myFunction($a, $b) {
// Do something
}
2. Create a class that maintains your stateclass 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 |
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: