diff options
Diffstat (limited to 'www/wiki/extensions/Mermaid/tests/phpunit')
3 files changed, 305 insertions, 0 deletions
diff --git a/www/wiki/extensions/Mermaid/tests/phpunit/Integration/I18nJsonFileIntegrityTest.php b/www/wiki/extensions/Mermaid/tests/phpunit/Integration/I18nJsonFileIntegrityTest.php new file mode 100644 index 00000000..c5a1c4ae --- /dev/null +++ b/www/wiki/extensions/Mermaid/tests/phpunit/Integration/I18nJsonFileIntegrityTest.php @@ -0,0 +1,77 @@ +<?php + +namespace Mermaid\Tests\Integration; + +/** + * @group mermaid + * @group medium + * + * @license GNU GPL v2+ + * @since 1.0 + * + * @author mwjames + */ +class I18nJsonFileIntegrityTest extends \PHPUnit_Framework_TestCase { + + /** + * @dataProvider i18nFileProvider + */ + public function testI18NJsonDecodeEncode( $file ) { + + $contents = file_get_contents( $file ); + + $this->assertInternalType( + 'array', + json_decode( $contents, true ), + 'Failed with ' . $this->getDescriptiveJsonError( json_last_error() ) . ' in ' . $file + ); + } + + public function i18nFileProvider() { + + $provider = array(); + + $files = $this->findFilesForExtension( + $GLOBALS['wgMessagesDirs']['Mermaid'], + 'json' + ); + + foreach ( $files as $file ) { + $provider[] = array( $file ); + } + + return $provider; + } + + private function findFilesForExtension( $path, $extension ) { + + $files = array(); + + $directoryIterator = new \RecursiveDirectoryIterator( + str_replace( array( '\\', '/' ), DIRECTORY_SEPARATOR, $path ) + ); + + foreach ( new \RecursiveIteratorIterator( $directoryIterator ) as $fileInfo ) { + if ( strtolower( substr( $fileInfo->getFilename(), -( strlen( $extension ) + 1 ) ) ) === ( '.' . $extension ) ) { + $files[$fileInfo->getFilename()] = $fileInfo->getPathname(); + } + } + + return $files; + } + + private function getDescriptiveJsonError( $errorCode ) { + + $errorMessages = array( + JSON_ERROR_NONE => '', + JSON_ERROR_STATE_MISMATCH => 'Underflow or the modes mismatch, malformed JSON', + JSON_ERROR_CTRL_CHAR => 'Unexpected control character found, possibly incorrectly encoded', + JSON_ERROR_SYNTAX => 'Syntax error, malformed JSON', + JSON_ERROR_UTF8 => 'Malformed UTF-8 characters, possibly incorrectly encoded', + JSON_ERROR_DEPTH => 'The maximum stack depth has been exceeded' + ); + + return $errorMessages[$errorCode]; + } + +} diff --git a/www/wiki/extensions/Mermaid/tests/phpunit/Unit/HookRegistryTest.php b/www/wiki/extensions/Mermaid/tests/phpunit/Unit/HookRegistryTest.php new file mode 100644 index 00000000..2c98737a --- /dev/null +++ b/www/wiki/extensions/Mermaid/tests/phpunit/Unit/HookRegistryTest.php @@ -0,0 +1,119 @@ +<?php + +namespace Mermaid\Tests; + +use Mermaid\HookRegistry; +use Title; + +/** + * @covers \Mermaid\HookRegistry + * @group mermaid + * + * @license GNU GPL v2+ + * @since 1.0 + * + * @author mwjames + */ +class HookRegistryTest extends \PHPUnit_Framework_TestCase { + + public function testCanConstruct() { + + $this->assertInstanceOf( + HookRegistry::class, + new HookRegistry() + ); + } + + public function testRegister() { + + $title = Title::newFromText( __METHOD__ ); + + $parserOutput = $this->getMockBuilder( '\ParserOutput' ) + ->disableOriginalConstructor() + ->getMock(); + + $parser = $this->getMockBuilder( '\Parser' ) + ->disableOriginalConstructor() + ->getMock(); + + $parser->expects( $this->any() ) + ->method( 'getTitle' ) + ->will( $this->returnValue( $title ) ); + + $parser->expects( $this->any() ) + ->method( 'getOutput' ) + ->will( $this->returnValue( $parserOutput ) ); + + $instance = new HookRegistry(); + $instance->deregister(); + $instance->register(); + + $this->doTestParserFirstCallInit( $instance, $parser ); + $this->doTestResourceLoaderGetConfigVars( $instance ); + $this->doTestOutputPageParserOutput( $instance ); + } + + public function doTestParserFirstCallInit( $instance, $parser ) { + + $handler = 'ParserFirstCallInit'; + + $this->assertTrue( + $instance->isRegistered( $handler ) + ); + + $this->assertThatHookIsExcutable( + $instance->getHandlerFor( $handler ), + array( &$parser ) + ); + } + + public function doTestResourceLoaderGetConfigVars( $instance ) { + + $handler = 'ResourceLoaderGetConfigVars'; + + $this->assertTrue( + $instance->isRegistered( $handler ) + ); + + $vars = array(); + + $this->assertThatHookIsExcutable( + $instance->getHandlerFor( $handler ), + array( &$vars ) + ); + } + + public function doTestOutputPageParserOutput( $instance ) { + + $parserOutput = $this->getMockBuilder( '\ParserOutput' ) + ->disableOriginalConstructor() + ->getMock(); + + $parserOutput->expects( $this->any() ) + ->method( 'getExtensionData' ) + ->will( $this->returnValue( true ) ); + + $outputPage = $this->getMockBuilder( '\OutputPage' ) + ->disableOriginalConstructor() + ->getMock(); + + $handler = 'OutputPageParserOutput'; + + $this->assertTrue( + $instance->isRegistered( $handler ) + ); + + $this->assertThatHookIsExcutable( + $instance->getHandlerFor( $handler ), + array( $outputPage, $parserOutput ) + ); + } + + private function assertThatHookIsExcutable( \Closure $handler, $arguments ) { + $this->assertInternalType( + 'boolean', + call_user_func_array( $handler, $arguments ) + ); + } + +} diff --git a/www/wiki/extensions/Mermaid/tests/phpunit/Unit/MermaidParserFunctionTest.php b/www/wiki/extensions/Mermaid/tests/phpunit/Unit/MermaidParserFunctionTest.php new file mode 100644 index 00000000..2a819c17 --- /dev/null +++ b/www/wiki/extensions/Mermaid/tests/phpunit/Unit/MermaidParserFunctionTest.php @@ -0,0 +1,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="{"content":"sequenceDiagram...","config":{"theme":"foo"}}"><div class="mermaid-dots"></div></div>' + ]; + + // [ ... ] + yield [ + [ 'sequenceDiagram id1["This is the (text) in the box"]', 'config.theme=foo' ], + 'data-mermaid="{"content":"sequenceDiagram id1[\"This is the (text) in the box\"]' + ]; + + // | + yield [ + [ 'A[Hard edge] -->|Link text| B(Round edge)' ], + 'data-mermaid="{"content":"A[Hard edge] -->|Link text| B(Round edge)"' + ]; + } + +} |