Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
ExcelTest.php
Go to the documentation of this file.
1 <?php
11 
12 class ExcelTest extends \PHPUnit\Framework\TestCase
13 {
19  private $_testData = [
20  [
21  'ID', 'Name', 'Email', 'Group', 'Telephone', '+Telephone', 'ZIP', '0ZIP', 'Country', 'State/Province',
22  'Symbol=', 'Symbol-', 'Symbol+'
23  ],
24  [
25  1, 'Jon Doe', '[email protected]', 'General', '310-111-1111', '+310-111-1111', 90232, '090232',
26  'United States', 'California', '=', '-', '+'
27  ],
28  ];
29 
30  protected $_testHeader = [
31  'HeaderID', 'HeaderName', 'HeaderEmail', 'HeaderGroup', 'HeaderPhone', 'Header+Phone', 'HeaderZIP',
32  'Header0ZIP', 'HeaderCountry', 'HeaderRegion', 'HeaderSymbol=', 'HeaderSymbol-', 'HeaderSymbol+'
33  ];
34 
35  protected $_testFooter = [
36  'FooterID', 'FooterName', 'FooterEmail', 'FooterGroup', 'FooterPhone', 'Footer+Phone', 'FooterZIP',
37  'Footer0ZIP', 'FooterCountry', 'FooterRegion', 'FooterSymbol=', 'FooterSymbol-', 'FooterSymbol+'
38  ];
39 
45  protected function _getSampleOutputFile()
46  {
47  return __DIR__ . '/_files/sample.xml';
48  }
49 
56  public function callbackMethod($row)
57  {
58  $data = [];
59  foreach ($row as $value) {
60  $data[] = $value . '_TRUE_';
61  }
62  return $data;
63  }
64 
71  public function testConvert()
72  {
73  $convert = new \Magento\Framework\Convert\Excel(new \ArrayIterator($this->_testData));
74  $convert->setDataHeader($this->_testHeader);
75  $convert->setDataFooter($this->_testFooter);
76  $this->assertXmlStringEqualsXmlString(
78  $convert->convert()
79  );
80  }
81 
88  public function testConvertCallback()
89  {
90  $convert = new \Magento\Framework\Convert\Excel(
91  new \ArrayIterator($this->_testData),
92  [$this, 'callbackMethod']
93  );
94  $this->assertContains('_TRUE_', $convert->convert(), 'Failed asserting that callback method is called.');
95  }
96 
103  protected function _writeFile($callback = false)
104  {
105  $name = md5(microtime());
106  $file = TESTS_TEMP_DIR . '/' . $name . '.xml';
107 
108  $stream = new \Magento\Framework\Filesystem\File\Write(
109  $file,
110  new \Magento\Framework\Filesystem\Driver\File(),
111  'w+'
112  );
113  $stream->lock();
114 
115  if (!$callback) {
116  $convert = new \Magento\Framework\Convert\Excel(new \ArrayIterator($this->_testData));
117  $convert->setDataHeader($this->_testHeader);
118  $convert->setDataFooter($this->_testFooter);
119  } else {
120  $convert = new \Magento\Framework\Convert\Excel(
121  new \ArrayIterator($this->_testData),
122  [$this, 'callbackMethod']
123  );
124  }
125 
126  $convert->write($stream);
127  $stream->unlock();
128  $stream->close();
129 
130  return $file;
131  }
132 
139  public function testWrite()
140  {
141  $file = $this->_writeFile();
142  $this->assertXmlStringEqualsXmlString(
144  file_get_contents($file)
145  );
146  }
147 
154  public function testWriteCallback()
155  {
156  $file = $this->_writeFile(true);
157  $this->assertContains('_TRUE_', file_get_contents($file), 'Failed asserting that callback method is called.');
158  }
159 }
defined('TESTS_BP')||define('TESTS_BP' __DIR__
Definition: _bootstrap.php:60
$value
Definition: gender.phtml:16
if(!isset($_GET['name'])) $name
Definition: log.php:14