ExcelTest.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. /**
  7. * Magento_Convert Test Case for \Magento\Framework\Convert\Excel Export
  8. */
  9. namespace Magento\Framework\Convert\Test\Unit;
  10. class ExcelTest extends \PHPUnit\Framework\TestCase
  11. {
  12. /**
  13. * Test data
  14. *
  15. * @var array
  16. */
  17. private $_testData = [
  18. [
  19. 'ID', 'Name', 'Email', 'Group', 'Telephone', '+Telephone', 'ZIP', '0ZIP', 'Country', 'State/Province',
  20. 'Symbol=', 'Symbol-', 'Symbol+'
  21. ],
  22. [
  23. 1, 'Jon Doe', 'jon.doe@magento.com', 'General', '310-111-1111', '+310-111-1111', 90232, '090232',
  24. 'United States', 'California', '=', '-', '+'
  25. ],
  26. ];
  27. protected $_testHeader = [
  28. 'HeaderID', 'HeaderName', 'HeaderEmail', 'HeaderGroup', 'HeaderPhone', 'Header+Phone', 'HeaderZIP',
  29. 'Header0ZIP', 'HeaderCountry', 'HeaderRegion', 'HeaderSymbol=', 'HeaderSymbol-', 'HeaderSymbol+'
  30. ];
  31. protected $_testFooter = [
  32. 'FooterID', 'FooterName', 'FooterEmail', 'FooterGroup', 'FooterPhone', 'Footer+Phone', 'FooterZIP',
  33. 'Footer0ZIP', 'FooterCountry', 'FooterRegion', 'FooterSymbol=', 'FooterSymbol-', 'FooterSymbol+'
  34. ];
  35. /**
  36. * Path for Sample File
  37. *
  38. * @return string
  39. */
  40. protected function _getSampleOutputFile()
  41. {
  42. return __DIR__ . '/_files/sample.xml';
  43. }
  44. /**
  45. * Callback method
  46. *
  47. * @param array $row
  48. * @return array
  49. */
  50. public function callbackMethod($row)
  51. {
  52. $data = [];
  53. foreach ($row as $value) {
  54. $data[] = $value . '_TRUE_';
  55. }
  56. return $data;
  57. }
  58. /**
  59. * Test \Magento\Framework\Convert\Excel->convert()
  60. * \Magento\Framework\Convert\Excel($iterator)
  61. *
  62. * @return void
  63. */
  64. public function testConvert()
  65. {
  66. $convert = new \Magento\Framework\Convert\Excel(new \ArrayIterator($this->_testData));
  67. $convert->setDataHeader($this->_testHeader);
  68. $convert->setDataFooter($this->_testFooter);
  69. $this->assertXmlStringEqualsXmlString(
  70. file_get_contents($this->_getSampleOutputFile()),
  71. $convert->convert()
  72. );
  73. }
  74. /**
  75. * Test \Magento\Framework\Convert\Excel->convert()
  76. * \Magento\Framework\Convert\Excel($iterator, $callbackMethod)
  77. *
  78. * @return void
  79. */
  80. public function testConvertCallback()
  81. {
  82. $convert = new \Magento\Framework\Convert\Excel(
  83. new \ArrayIterator($this->_testData),
  84. [$this, 'callbackMethod']
  85. );
  86. $this->assertContains('_TRUE_', $convert->convert(), 'Failed asserting that callback method is called.');
  87. }
  88. /**
  89. * Write Data into File
  90. *
  91. * @param bool $callback
  92. * @return string
  93. */
  94. protected function _writeFile($callback = false)
  95. {
  96. $name = md5(microtime());
  97. $file = TESTS_TEMP_DIR . '/' . $name . '.xml';
  98. $stream = new \Magento\Framework\Filesystem\File\Write(
  99. $file,
  100. new \Magento\Framework\Filesystem\Driver\File(),
  101. 'w+'
  102. );
  103. $stream->lock();
  104. if (!$callback) {
  105. $convert = new \Magento\Framework\Convert\Excel(new \ArrayIterator($this->_testData));
  106. $convert->setDataHeader($this->_testHeader);
  107. $convert->setDataFooter($this->_testFooter);
  108. } else {
  109. $convert = new \Magento\Framework\Convert\Excel(
  110. new \ArrayIterator($this->_testData),
  111. [$this, 'callbackMethod']
  112. );
  113. }
  114. $convert->write($stream);
  115. $stream->unlock();
  116. $stream->close();
  117. return $file;
  118. }
  119. /**
  120. * Test \Magento\Framework\Convert\Excel->write()
  121. * \Magento\Framework\Convert\Excel($iterator)
  122. *
  123. * @return void
  124. */
  125. public function testWrite()
  126. {
  127. $file = $this->_writeFile();
  128. $this->assertXmlStringEqualsXmlString(
  129. file_get_contents($this->_getSampleOutputFile()),
  130. file_get_contents($file)
  131. );
  132. }
  133. /**
  134. * Test \Magento\Framework\Convert\Excel->write()
  135. * \Magento\Framework\Convert\Excel($iterator, $callbackMethod)
  136. *
  137. * @return void
  138. */
  139. public function testWriteCallback()
  140. {
  141. $file = $this->_writeFile(true);
  142. $this->assertContains('_TRUE_', file_get_contents($file), 'Failed asserting that callback method is called.');
  143. }
  144. }