document_ezxml_test.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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. /**
  29. * Test suite for class.
  30. *
  31. * @package Document
  32. * @subpackage Tests
  33. */
  34. class ezcDocumentEzXmlTests extends ezcTestCase
  35. {
  36. protected static $testDocuments = null;
  37. public static function suite()
  38. {
  39. return new PHPUnit_Framework_TestSuite( __CLASS__ );
  40. }
  41. public function testValidateValidDocument()
  42. {
  43. $doc = new ezcDocumentEzXml();
  44. $this->assertSame(
  45. $doc->validateFile( dirname( __FILE__ ) . '/files/ezxml/s_001_header.ezp' ),
  46. true
  47. );
  48. }
  49. public function testValidateInvalidDocument()
  50. {
  51. $invalid = dirname( __FILE__ ) . '/files/ezxml/e_000_invalid.ezp';
  52. $doc = new ezcDocumentEzXml();
  53. $this->assertEquals(
  54. count( $errors = $doc->validateFile( $invalid ) ),
  55. count( $doc->validateString( file_get_contents( $invalid ) ) )
  56. );
  57. $this->assertEquals(
  58. (string) $errors[0],
  59. 'Error in 6:0: Did not expect element unknown there.'
  60. );
  61. }
  62. public function testUnhandledLinkType()
  63. {
  64. $invalid = dirname( __FILE__ ) . '/files/ezxml/e_001_unhandled_link.ezp';
  65. $document = new ezcDocumentEzXml();
  66. $document->loadFile( $invalid );
  67. try
  68. {
  69. $docbook = $document->getAsDocbook();
  70. $this->fail( 'Expected ezcDocumentConversionException.' );
  71. }
  72. catch ( ezcDocumentConversionException $e )
  73. {
  74. $this->assertSame(
  75. $e->getMessage(),
  76. 'Conversion error: Warning: \'Unhandled link type.\'.'
  77. );
  78. }
  79. }
  80. public function testCreateFromDocbook()
  81. {
  82. $from = dirname( __FILE__ ) . '/files/docbook/ezxml/s_001_empty.xml';
  83. $to = dirname( __FILE__ ) . '/files/docbook/ezxml/s_001_empty.ezp';
  84. $docbook = new ezcDocumentDocbook();
  85. $docbook->loadFile( $from );
  86. $document = new ezcDocumentEzXml();
  87. $document->createFromDocbook( $docbook );
  88. // Store test file, to have something to compare on failure
  89. $tempDir = $this->createTempDir( 'docbook_ezxml_' ) . '/';
  90. file_put_contents( $tempDir . basename( $to ), $xml = $document->save() );
  91. $this->assertEquals(
  92. file_get_contents( $to ),
  93. $xml,
  94. 'Document not visited as expected.'
  95. );
  96. // Remove tempdir, when nothing failed.
  97. $this->removeTempDir();
  98. }
  99. public static function getEzXmlTestDocuments()
  100. {
  101. if ( self::$testDocuments === null )
  102. {
  103. // Get a list of all test files from the respektive folder
  104. $testFiles = glob( dirname( __FILE__ ) . '/files/ezxml/s_*.ezp' );
  105. // Create array with the test file and the expected result file
  106. foreach ( $testFiles as $file )
  107. {
  108. self::$testDocuments[] = array(
  109. $file,
  110. substr( $file, 0, -3 ) . 'xml'
  111. );
  112. }
  113. }
  114. return self::$testDocuments;
  115. return array_slice( self::$testDocuments, 6, 1 );
  116. }
  117. /**
  118. * @dataProvider getEzXmlTestDocuments
  119. */
  120. public function testConvertToDocbook( $from, $to )
  121. {
  122. if ( !is_file( $to ) )
  123. {
  124. $this->markTestSkipped( "Comparision file '$to' not yet defined." );
  125. }
  126. $document = new ezcDocumentEzXml();
  127. $document->loadFile( $from );
  128. $docbook = $document->getAsDocbook();
  129. $xml = $docbook->save();
  130. $this->assertTrue(
  131. $docbook instanceof ezcDocumentDocbook
  132. );
  133. // Store test file, to have something to compare on failure
  134. $tempDir = $this->createTempDir( 'ezxml_docbook_' ) . '/';
  135. file_put_contents( $tempDir . basename( $to ), $xml );
  136. // We need a proper XSD first, the current one does not accept legal
  137. // XML.
  138. // $this->checkDocbook( $docbook->getDomDocument() );
  139. $this->assertEquals(
  140. file_get_contents( $to ),
  141. $xml,
  142. 'Document not visited as expected.'
  143. );
  144. // Remove tempdir, when nothing failed.
  145. $this->removeTempDir();
  146. }
  147. }
  148. ?>