/Document/tests/document_rst_visitor_docbook_test.php

https://github.com/F5/zetacomponents · PHP · 159 lines · 95 code · 21 blank · 43 comment · 2 complexity · d2af694eaf6c9c4373f71f3c4667dc3c MD5 · raw file

  1. <?php
  2. /**
  3. * ezcDocumentRstParserTests
  4. *
  5. * Licensed to the Apache Software Foundation (ASF) under one
  6. * or more contributor license agreements. See the NOTICE file
  7. * distributed with this work for additional information
  8. * regarding copyright ownership. The ASF licenses this file
  9. * to you under the Apache License, Version 2.0 (the
  10. * "License"); you may not use this file except in compliance
  11. * with the License. You may obtain a copy of the License at
  12. *
  13. * http://www.apache.org/licenses/LICENSE-2.0
  14. *
  15. * Unless required by applicable law or agreed to in writing,
  16. * software distributed under the License is distributed on an
  17. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  18. * KIND, either express or implied. See the License for the
  19. * specific language governing permissions and limitations
  20. * under the License.
  21. *
  22. * @package Document
  23. * @version //autogen//
  24. * @subpackage Tests
  25. * @license http://www.apache.org/licenses/LICENSE-2.0 Apache License, Version 2.0
  26. */
  27. require_once 'helper/rst_dummy_directives.php';
  28. require_once 'helper/rst_paragraph_directive.php';
  29. /**
  30. * Test suite for class.
  31. *
  32. * @package Document
  33. * @subpackage Tests
  34. */
  35. class ezcDocumentRstDocbookVisitorTests extends ezcTestCase
  36. {
  37. protected static $testDocuments = null;
  38. public static function suite()
  39. {
  40. return new PHPUnit_Framework_TestSuite( __CLASS__ );
  41. }
  42. public static function getTestDocuments()
  43. {
  44. if ( self::$testDocuments === null )
  45. {
  46. // Get a list of all test files from the respektive folder
  47. $testFiles = glob( dirname( __FILE__ ) . '/files/rst/docbook/s_*.txt' );
  48. // Create array with the test file and the expected result file
  49. foreach ( $testFiles as $file )
  50. {
  51. self::$testDocuments[] = array(
  52. $file,
  53. substr( $file, 0, -3 ) . 'xml'
  54. );
  55. }
  56. }
  57. return self::$testDocuments;
  58. return array_slice( self::$testDocuments, -2, 1 );
  59. }
  60. /**
  61. * @dataProvider getTestDocuments
  62. */
  63. public function testParseRstFile( $from, $to )
  64. {
  65. if ( !is_file( $to ) )
  66. {
  67. $this->markTestSkipped( "Comparision file '$to' not yet defined." );
  68. }
  69. $document = new ezcDocumentRst();
  70. $document->options->errorReporting = E_PARSE | E_ERROR | E_WARNING;
  71. $document->registerDirective( 'my_custom_directive', 'ezcDocumentTestDummyDirective' );
  72. $document->registerDirective( 'user', 'ezcDocumentTestDummyDirective' );
  73. $document->registerDirective( 'book', 'ezcDocumentTestDummyDirective' );
  74. $document->registerDirective( 'function', 'ezcDocumentTestDummyDirective' );
  75. $document->registerDirective( 'replace', 'ezcDocumentTestDummyDirective' );
  76. $document->registerDirective( 'paragraph', 'ezcDocumentTestParagraphDirective' );
  77. $document->registerRole( 'my_role', 'ezcDocumentTestDummyRole' );
  78. $document->loadFile( $from );
  79. $docbook = $document->getAsDocbook();
  80. $xml = $docbook->save();
  81. // Store test file, to have something to compare on failure
  82. $tempDir = $this->createTempDir( 'docbook_' ) . '/';
  83. file_put_contents( $tempDir . basename( $to ), $xml );
  84. // Validate generated docbook
  85. $this->assertTrue( $docbook->validateString( $xml ) );
  86. $this->assertEquals(
  87. file_get_contents( $to ),
  88. $xml,
  89. 'Document not visited as expected.'
  90. );
  91. // Remove tempdir, when nothing failed.
  92. $this->removeTempDir();
  93. }
  94. public static function getErroneousTestDocuments()
  95. {
  96. // return array();
  97. return array(
  98. array(
  99. dirname( __FILE__ ) . '/files/rst/docbook/e_001_missing_directive.txt',
  100. 'Visitor error: Warning: \'No directive handler registered for directive \'missing_directive_dclaration\'.\' in line 7 at position 1.',
  101. ),
  102. array(
  103. dirname( __FILE__ ) . '/files/rst/docbook/e_001_missing_role.txt',
  104. 'Visitor error: Warning: \'No text role handler registered for text role \'no-handler-registered\'.\' in line 4 at position 45.',
  105. ),
  106. );
  107. }
  108. /**
  109. * @dataProvider getErroneousTestDocuments
  110. */
  111. public function testParseErroneousRstFile( $file, $message )
  112. {
  113. try
  114. {
  115. $document = new ezcDocumentRst();
  116. $document->options->errorReporting = E_PARSE | E_ERROR | E_WARNING;
  117. $document->registerDirective( 'my_custom_directive', 'ezcDocumentTestDummyDirective' );
  118. $document->registerDirective( 'user', 'ezcDocumentTestDummyDirective' );
  119. $document->registerDirective( 'book', 'ezcDocumentTestDummyDirective' );
  120. $document->registerDirective( 'function', 'ezcDocumentTestDummyDirective' );
  121. $document->registerDirective( 'replace', 'ezcDocumentTestDummyDirective' );
  122. $document->loadFile( $file );
  123. $docbook = $document->getAsDocbook();
  124. $xml = $docbook->save();
  125. $document = $parser->parse( $tokenizer->tokenizeFile( $file ) );
  126. $this->fail( 'Expected some exception.' );
  127. }
  128. catch ( ezcDocumentException $e )
  129. {
  130. $this->assertSame(
  131. $message,
  132. $e->getMessage(),
  133. 'Different parse error expected.'
  134. );
  135. }
  136. }
  137. }
  138. ?>