summaryrefslogtreecommitdiff
path: root/www/wiki/extensions/Mermaid/tests/phpunit/Unit/MermaidParserFunctionTest.php
blob: 2a819c1726bf9c22da32ebb38acb21d4ed3ce9f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
<?php

namespace Mermaid\Tests;

use Mermaid\MermaidParserFunction;

/**
 * @covers \Mermaid\MermaidParserFunction
 * @group mermaid
 *
 * @license GNU GPL v2+
 * @since 1.0
 *
 * @author mwjames
 */
class MermaidParserFunctionTest extends \PHPUnit_Framework_TestCase {

	public function testCanConstruct() {

		$parser = $this->getMockBuilder( '\Parser' )
			->disableOriginalConstructor()
			->getMock();

		$this->assertInstanceOf(
			MermaidParserFunction::class,
			new MermaidParserFunction( $parser )
		);
	}

	public function testInitCallback() {

		$callback = MermaidParserFunction::newCallback( 'foo' );

		$this->assertInstanceOf(
			'\Closure',
			$callback
		);

		$parserOutput = $this->getMockBuilder( '\ParserOutput' )
			->disableOriginalConstructor()
			->getMock();

		$parser = $this->getMockBuilder( '\Parser' )
			->disableOriginalConstructor()
			->getMock();

		$parser->expects( $this->any() )
			->method( 'getOutput' )
			->will( $this->returnValue( $parserOutput ) );

		$this->assertNotEmpty(
			call_user_func_array( $callback, [ $parser ] )
		);
	}

	/**
	 * @dataProvider textProvider
	 */
	public function testParse( $text, $expected ) {

		$parserOutput = $this->getMockBuilder( '\ParserOutput' )
			->disableOriginalConstructor()
			->getMock();

		$parserOutput->expects( $this->once() )
			->method( 'setExtensionData' )
			->with(
				$this->equalTo( 'ext-mermaid' ),
				$this->equalTo( true ) );

		$parser = $this->getMockBuilder( '\Parser' )
			->disableOriginalConstructor()
			->getMock();

		$parser->expects( $this->any() )
			->method( 'getOutput' )
			->will( $this->returnValue( $parserOutput ) );

		$instance = new MermaidParserFunction(
			$parser
		);

		$this->assertContains(
			$expected,
			$instance->parse( $text )
		);
	}

	public function textProvider() {

		yield [
			[ 'sequenceDiagram...', 'config.theme=foo' ],
			'class="ext-mermaid" data-mermaid="{&quot;content&quot;:&quot;sequenceDiagram...&quot;,&quot;config&quot;:{&quot;theme&quot;:&quot;foo&quot;}}"><div class="mermaid-dots"></div></div>'
		];

		// [ ... ]
		yield [
			[ 'sequenceDiagram id1["This is the (text) in the box"]', 'config.theme=foo' ],
			'data-mermaid="{&quot;content&quot;:&quot;sequenceDiagram id1[\&quot;This is the (text) in the box\&quot;]'
		];

		// |
		yield [
			[ 'A[Hard edge] -->|Link text| B(Round edge)' ],
			'data-mermaid="{&quot;content&quot;:&quot;A[Hard edge] --&gt;|Link text| B(Round edge)&quot;'
		];
	}

}