base.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. /**
  3. * ezcDocumentPdfTestCase
  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. /**
  28. * Base test suite for PDF tests, implementing an assertion on PDF
  29. * equality.
  30. *
  31. * @package Document
  32. * @subpackage Tests
  33. */
  34. abstract class ezcDocumentPdfTestCase extends ezcTestCase
  35. {
  36. protected $tempDir;
  37. protected $basePath;
  38. /**
  39. * Extension of generated files
  40. *
  41. * @var string
  42. */
  43. protected $extension = 'pdf';
  44. protected function setUp()
  45. {
  46. static $i = 0;
  47. $this->tempDir = $this->createTempDir( __CLASS__ . sprintf( '_%03d_', ++$i ) ) . '/';
  48. $this->basePath = dirname( __FILE__ ) . '/../files/pdf/';
  49. }
  50. protected function tearDown()
  51. {
  52. if ( !$this->hasFailed() )
  53. {
  54. $this->removeTempDir();
  55. }
  56. }
  57. /**
  58. * Assert that the given PDF document content is simlar to the
  59. * PDF document referenced by its test case name.
  60. *
  61. * @param string $content
  62. * @param string $name
  63. * @return void
  64. */
  65. protected function assertPdfDocumentsSimilar( $content, $name )
  66. {
  67. $baseName = str_replace( '::', '_', $name ) . '.' . $this->extension;
  68. // Normalize dates in generated PDF
  69. $content = preg_replace( '(([\\( ])D:\\d+(?:\\+\\d{2}\'\\d{2}\')?)', '$1D:20000101010000+02\'00\'', $content );
  70. // This damages the PDF files
  71. // $content = preg_replace( '((^| )(\\d+\\.\\d\\d)\\d+)', '$1$2', $content );
  72. // Store file for manual inspection if the test case fails
  73. file_put_contents( $this->tempDir . $baseName, $content );
  74. $this->assertFileExists( $compare = $this->basePath . 'driver/' . $baseName );
  75. $this->assertEquals(
  76. file_get_contents( $compare ),
  77. $content,
  78. 'Generated PDF document does not match expected document.'
  79. );
  80. }
  81. /**
  82. * Test rendering of a full document
  83. *
  84. * Test the rendering of a given full document with an
  85. * additional set of user configured styles.
  86. *
  87. * @param string $file
  88. * @param string $fileName
  89. * @param array $styles
  90. * @return void
  91. */
  92. protected function renderFullDocument( $file, $fileName, array $styles = array() )
  93. {
  94. $docbook = new ezcDocumentDocbook();
  95. $docbook->loadFile( $file );
  96. $style = new ezcDocumentPcssStyleInferencer();
  97. $style->appendStyleDirectives( array(
  98. new ezcDocumentPcssLayoutDirective(
  99. array( 'article' ),
  100. array(
  101. 'font-family' => 'serif',
  102. 'line-height' => '1',
  103. )
  104. ),
  105. new ezcDocumentPcssLayoutDirective(
  106. array( 'title' ),
  107. array(
  108. 'font-family' => 'sans-serif',
  109. )
  110. ),
  111. ) );
  112. $style->appendStyleDirectives( $styles );
  113. $renderer = new ezcDocumentPdfMainRenderer(
  114. new ezcDocumentPdfSvgDriver(),
  115. $style,
  116. new ezcDocumentPdfOptions()
  117. );
  118. $pdf = $renderer->render(
  119. $docbook,
  120. new ezcDocumentPdfDefaultHyphenator()
  121. );
  122. file_put_contents(
  123. $this->tempDir . $fileName,
  124. $pdf
  125. );
  126. $this->assertXmlFileEqualsXmlFile(
  127. $this->basePath . 'renderer/' . $fileName,
  128. $this->tempDir . $fileName
  129. );
  130. }
  131. }
  132. ?>