Completed
Push — master ( c8702c...a22429 )
by Tobias
09:12
created

ParserFirstCallInitTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 133
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 7
c 1
b 0
f 0
lcom 0
cbo 2
dl 0
loc 133
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testCanConstruct() 0 20 1
B testHookParserFirstCallInit() 0 40 1
B testCanCreateParserHooks() 0 38 2
A doTestParserHook() 0 18 3
1
<?php
2
3
namespace BootstrapComponents\Tests\Unit\Hooks;
4
5
use BootstrapComponents\Hooks\ParserFirstCallInit as ParserFirstCallInit;
6
use BootstrapComponents\ComponentLibrary;
7
use \Parser;
8
use \PHPUnit_Framework_TestCase;
9
10
/**
11
 * @covers  \BootstrapComponents\Hooks\ParserFirstCallInit
12
 *
13
 * @ingroup Test
14
 *
15
 * @group   extension-bootstrap-components
16
 * @group   mediawiki-databaseless
17
 *
18
 * @license GNU GPL v3+
19
 *
20
 * @since   1.0
21
 * @author  Tobias Oetterer
22
 */
23
class ParserFirstCallInitTest extends PHPUnit_Framework_TestCase {
24
25
	public function testCanConstruct() {
26
27
		$parser = $this->getMockBuilder( 'Parser' )
28
			->disableOriginalConstructor()
29
			->getMock();
30
		$componentLibrary = $this->getMockBuilder( 'BootstrapComponents\ComponentLibrary' )
31
			->disableOriginalConstructor()
32
			->getMock();
33
		$nestingController = $this->getMockBuilder( 'BootstrapComponents\NestingController' )
34
			->disableOriginalConstructor()
35
			->getMock();
36
37
		/** @noinspection PhpParamsInspection */
38
		$instance = new ParserFirstCallInit( $parser, $componentLibrary, $nestingController );
39
40
		$this->assertInstanceOf(
41
			'BootstrapComponents\\Hooks\\ParserFirstCallInit',
42
			$instance
43
		);
44
	}
45
46
	/**
47
	 * @throws \ConfigException
48
	 */
49
	public function testHookParserFirstCallInit() {
50
		$prefix = ComponentLibrary::PARSER_HOOK_PREFIX;
51
		$observerParser = $this->getMockBuilder(Parser::class )
52
			->disableOriginalConstructor()
53
			->setMethods( [ 'setFunctionHook', 'setHook' ] )
54
			->getMock();
55
		$observerParser->expects( $this->exactly( 6 ) )
56
			->method( 'setFunctionHook' )
57
			->withConsecutive(
58
				[ $this->equalTo( $prefix . 'badge' ), $this->callback( 'is_callable' ) ],
59
				[ $this->equalTo( $prefix . 'button' ), $this->callback( 'is_callable' ) ],
60
				[ $this->equalTo( $prefix . 'carousel' ), $this->callback( 'is_callable' ) ],
61
				[ $this->equalTo( $prefix . 'icon' ), $this->callback( 'is_callable' ) ],
62
				[ $this->equalTo( $prefix . 'label' ), $this->callback( 'is_callable' ) ],
63
				[ $this->equalTo( $prefix . 'tooltip' ), $this->callback( 'is_callable' ) ]
64
			);
65
		$observerParser->expects( $this->exactly( 8 ) )
66
			->method( 'setHook' )
67
			->withConsecutive(
68
				[ $this->equalTo( $prefix . 'accordion' ), $this->callback( 'is_callable' ) ],
69
				[ $this->equalTo( $prefix . 'alert' ), $this->callback( 'is_callable' ) ],
70
				[ $this->equalTo( $prefix . 'collapse' ), $this->callback( 'is_callable' ) ],
71
				[ $this->equalTo( $prefix . 'jumbotron' ), $this->callback( 'is_callable' ) ],
72
				[ $this->equalTo( $prefix . 'modal' ), $this->callback( 'is_callable' ) ],
73
				[ $this->equalTo( $prefix . 'panel' ), $this->callback( 'is_callable' ) ],
74
				[ $this->equalTo( $prefix . 'popover' ), $this->callback( 'is_callable' ) ],
75
				[ $this->equalTo( $prefix . 'well' ), $this->callback( 'is_callable' ) ]
76
			);
77
		$componentLibrary = new ComponentLibrary( true );
78
		$nestingController = $this->getMockBuilder( 'BootstrapComponents\NestingController' )
79
			->disableOriginalConstructor()
80
			->getMock();
81
82
		/** @noinspection PhpParamsInspection */
83
		$instance = new ParserFirstCallInit( $observerParser, $componentLibrary, $nestingController );
84
85
		$this->assertTrue(
86
			$instance->process()
87
		);
88
	}
89
90
	/**
91
	 * @throws \ConfigException
92
	 */
93
	public function testCanCreateParserHooks() {
94
		$registeredParserHooks = [];
95
		$extractionParser = $this->getMockBuilder(Parser::class )
96
			->disableOriginalConstructor()
97
			->setMethods( [ 'setFunctionHook', 'setHook' ] )
98
			->getMock();
99
		$extractionParser->expects( $this->exactly( 6 ) )
100
			->method( 'setFunctionHook' )
101
			->will( $this->returnCallback( function( $parserHookString, $callBack ) use ( &$registeredParserHooks ) {
102
				$registeredParserHooks[$parserHookString] = [ $callBack, ComponentLibrary::HANDLER_TYPE_PARSER_FUNCTION ];
103
			} ) );
104
		$extractionParser->expects( $this->exactly( 8 ) )
105
			->method( 'setHook' )
106
			->will( $this->returnCallback( function( $parserHookString, $callBack ) use ( &$registeredParserHooks ) {
107
				$registeredParserHooks[$parserHookString] = [ $callBack, ComponentLibrary::HANDLER_TYPE_TAG_EXTENSION ];
108
			} ) );
109
110
		$componentLibrary = new ComponentLibrary( true );
111
		$nestingController = $this->getMockBuilder( 'BootstrapComponents\NestingController' )
112
			->disableOriginalConstructor()
113
			->getMock();
114
115
		/** @noinspection PhpParamsInspection */
116
		$instance = new ParserFirstCallInit( $extractionParser, $componentLibrary, $nestingController );
117
118
		$this->assertTrue(
119
			$instance->process()
120
		);
121
122
		$this->assertEquals(
123
			14,
124
			count( $registeredParserHooks )
125
		);
126
127
		foreach ( $registeredParserHooks as $registeredParserHook => $data ) {
128
			$this->doTestParserHook( $registeredParserHook, $data[0], $data[1] );
129
		}
130
	}
131
132
	/**
133
	 * @param string   $registeredParserHook
134
	 * @param \Closure $callback
135
	 * @param string   $handlerType
136
	 */
137
	private function doTestParserHook( $registeredParserHook, $callback, $handlerType ) {
138
		$parser = $this->getMockBuilder( 'Parser' )
139
			->disableOriginalConstructor()
140
			->getMock();
141
		$input = 'test';
142
		if ( $handlerType == ComponentLibrary::HANDLER_TYPE_TAG_EXTENSION ) {
143
			$ret = $callback( $input, [], $parser, null );
144
		} elseif ( $handlerType == ComponentLibrary::HANDLER_TYPE_PARSER_FUNCTION ) {
145
			$ret = $callback( $parser, $input );
146
		} else {
147
			$ret = false;
148
		}
149
		$this->assertInternalType(
150
			'string',
151
			$ret,
152
			'Failed testing parser hook for parser hook string ' . $registeredParserHook
153
		);
154
	}
155
}
156