Test Failed
Push — master ( 7969f2...0db1fe )
by Jeroen De
09:42
created

TagHookTest::getHookDefinition()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 26
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 19
nc 1
nop 0
dl 0
loc 26
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace ParserHooks\Tests;
4
5
use ParamProcessor\ProcessedParam;
6
use ParamProcessor\ProcessingResult;
7
use Parser;
8
use ParserHooks\FunctionRunner;
9
use ParserHooks\HookDefinition;
10
use ParserHooks\HookRegistrant;
11
use ParserHooks\HookRunner;
12
use ParserOptions;
13
use Title;
14
use User;
15
16
/**
17
 * @group ParserHooks
18
 * @licence GNU GPL v2+
19
 * @author Jeroen De Dauw < [email protected] >
20
 */
21
class TagHookTest extends \PHPUnit_Framework_TestCase {
22
23
	const HOOK_NAME = 'systemtest_tagextension';
24
25
	/**
26
	 * @var Parser
27
	 */
28
	protected $parser;
29
30
	public function setUp() {
31
		$this->parser = $this->getSomeParser();
32
	}
33
34
	protected function getSomeParser() {
35
		global $wgParserConf;
36
		$parser = new Parser( $wgParserConf );
37
		return $parser;
38
	}
39
40
	public function testParserFunctionReceivesArguments() {
41
		$this->registerParserHook();
42
43
		$name = self::HOOK_NAME;
44
45
		$result = $this->getParsedText(
46
			"||<$name 1337=yes>Jeroen</$name>|||"
47
		);
48
49
		$this->assertInternalType( 'string', $result );
50
		$this->assertEquals(
51
			"||-Jeroen-|||",
52
			$result
53
		);
54
	}
55
56
	protected function getParsedText( $text ) {
0 ignored issues
show
Coding Style introduced by
getParsedText 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...
57
		return $this->parser->parse(
58
			$text,
59
			Title::newFromText( "Test" ),
60
			ParserOptions::newFromUserAndLang( new User(), $GLOBALS['wgContLang'] ),
61
			false
62
		)->getText();
63
	}
64
65
	protected function registerParserHook() {
66
		$runner = $this->getHookRunner();
67
68
		$registrant = new HookRegistrant( $this->parser );
69
		$registrant->registerHook( $runner );
70
	}
71
72
	protected function getHookRunner() {
73
		return new HookRunner(
74
			$this->getHookDefinition(),
75
			$this->getHookHandler()
76
		);
77
	}
78
79
	protected function getHookDefinition() {
80
		return new HookDefinition(
81
			self::HOOK_NAME,
82
			array(
0 ignored issues
show
Documentation introduced by
array(array('name' => 'n...', 'default' => false)) is of type array<integer,array,{"0"...efault\":\"false\"}>"}>, but the function expects a array<integer,object<Par...essor\ParamDefinition>>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
83
				array(
84
					'name' => 'name',
85
					'message' => 'abc',
86
				),
87
				array(
88
					'name' => 'awesomeness',
89
					'message' => 'abc',
90
					'type' => 'integer',
91
					'default' => 9001,
92
				),
93
				array(
94
					'name' => '1337',
95
					'message' => 'abc',
96
					'type' => 'boolean',
97
					'default' => false,
98
				),
99
			),
100
			array(
101
				'name'
102
			)
103
		);
104
	}
105
106
	protected function getHookHandler() {
107
		$hookHandler = $this->getMock( 'ParserHooks\HookHandler' );
108
109
		$hookHandler->expects( $this->once() )
110
			->method( 'handle' )
111
			->with(
112
				$this->isInstanceOf( 'Parser' ),
113
				$this->callback( function( $var ) {
114
					if ( !( $var instanceof ProcessingResult ) ) {
115
						return false;
116
					}
117
118
					$params = $var->getParameters();
119
					$expectedParams = array(
120
						'1337' => new ProcessedParam( '1337', true, false, '1337', 'yes' ),
121
						'awesomeness' => new ProcessedParam( 'awesomeness', 9001, true, null, null ),
122
						'name' => new ProcessedParam( 'name', 'Jeroen', false, 'name', 'Jeroen' ),
123
					);
124
125
					asort( $params );
126
					asort( $expectedParams );
127
128
					return $params == $expectedParams;
129
				} )
130
			)
131
			->will( $this->returnCallback( function( Parser $parser, ProcessingResult $result ) {
132
				$params = $result->getParameters();
133
				return '-' . $params['name']->getValue() . '-';
134
			} ) );
135
136
		return $hookHandler;
137
	}
138
139
140
141
}
142