ExcelWriterTest.php 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. <?php
  2. use Mockery as m;
  3. use Maatwebsite\Excel\Facades\Excel;
  4. use Maatwebsite\Excel\Classes;
  5. use Maatwebsite\Excel\Writers\LaravelExcelWriter;
  6. class ExcelWriterTest extends TestCase {
  7. /**
  8. * Setup
  9. */
  10. public function setUp()
  11. {
  12. parent::setUp();
  13. // Set excel class
  14. $this->excel = App::make('phpexcel');
  15. // Set writer class
  16. $this->writer = App::make('excel.writer');
  17. $this->writer->injectExcel($this->excel);
  18. }
  19. /**
  20. * Test the excel injection
  21. * @return [type] [description]
  22. */
  23. public function testExcelInjection()
  24. {
  25. $this->assertEquals($this->excel, $this->writer->getExcel());
  26. }
  27. /**
  28. * Test setTitle()
  29. * @return [type] [description]
  30. */
  31. public function testSetTitle()
  32. {
  33. $title = 'Workbook Title';
  34. $titleSet = $this->writer->setTitle($title);
  35. $this->assertEquals($this->writer, $titleSet);
  36. // Test if title was really set
  37. $this->assertEquals($this->writer->getTitle(), $title);
  38. $this->assertEquals($this->writer->getProperties()->getTitle(), $title);
  39. }
  40. /**
  41. * Test setTitle()
  42. * @return [type] [description]
  43. */
  44. public function testSetFilename()
  45. {
  46. $filename = 'filename';
  47. $filenameSet = $this->writer->setFileName($filename);
  48. $this->assertEquals($this->writer, $filenameSet);
  49. // Test if title was really set
  50. $this->assertEquals($this->writer->getFileName(), $filename);
  51. }
  52. /**
  53. * Test the share view
  54. * @return [type] [description]
  55. */
  56. public function testShareView()
  57. {
  58. // Set params
  59. $view = 'excel';
  60. $data = array();
  61. $mergeData = array();
  62. $viewShared = $this->writer->shareView($view, $data, $mergeData);
  63. $this->assertEquals($this->writer, $viewShared);
  64. // Get the parser
  65. $parser = $this->writer->getParser();
  66. // Test if parse data was set
  67. $this->assertEquals($parser->getView(), $view);
  68. $this->assertEquals($parser->getData(), $data);
  69. $this->assertEquals($parser->getMergeData(), $mergeData);
  70. }
  71. /**
  72. * Test basic sheet creation
  73. * @return [type] [description]
  74. */
  75. public function testSheet()
  76. {
  77. $title = 'Worksheet Title';
  78. $sheetCreated = $this->writer->sheet($title);
  79. $this->assertEquals($this->writer, $sheetCreated);
  80. // Test if title was really set
  81. $this->assertEquals($this->writer->getSheet()->getTitle(), $title);
  82. }
  83. /**
  84. * Test sheet closure
  85. * @return [type] [description]
  86. */
  87. public function testSheetClosure()
  88. {
  89. $title = 'Worksheet Title';
  90. $closureTitle = 'Closure Title';
  91. $this->writer->sheet($title, function($sheet) use($closureTitle) {
  92. $sheet->setTitle($closureTitle);
  93. });
  94. // Test if title was really set
  95. $this->assertEquals($this->writer->getSheet()->getTitle(), $closureTitle);
  96. }
  97. /**
  98. * Test multiple sheet creation
  99. * @return [type] [description]
  100. */
  101. public function testMultipleSheets()
  102. {
  103. // Set sheet titles
  104. $sheets = array(
  105. 'Worksheet 1 title',
  106. 'Worksheet 2 title',
  107. 'Worksheet 3 title'
  108. );
  109. // Create the sheets
  110. foreach($sheets as $sheetTitle)
  111. {
  112. $this->writer->sheet($sheetTitle);
  113. }
  114. // Count amount of sheets
  115. $this->assertEquals(count($sheets), $this->writer->getSheetCount());
  116. // Test if all sheet titles where set correctly
  117. foreach($sheets as $sheetTitle)
  118. {
  119. $this->assertContains($sheetTitle, $this->writer->getSheetNames());
  120. }
  121. }
  122. /**
  123. * Test setting properties (creator, ...)
  124. * @return [type] [description]
  125. */
  126. public function testSetProperties()
  127. {
  128. // Get available properties
  129. $properties = $this->excel->getAllowedProperties();
  130. // Loop through them
  131. foreach($properties as $prop)
  132. {
  133. // Set a random value
  134. $originalValue = rand();
  135. // Set needed set/get methods
  136. $method = 'set' . ucfirst($prop);
  137. $getMethod = 'get' . ucfirst($prop);
  138. // Set the property with the random value
  139. call_user_func_array(array($this->writer, $method), array($originalValue));
  140. // Get the property back
  141. $returnedValue = call_user_func_array(array($this->writer->getProperties(), $getMethod), array());
  142. // Check if the properties matches
  143. $this->assertEquals($originalValue, $returnedValue, $prop . ' doesn\'t match');
  144. }
  145. }
  146. public function testCreateFromArray()
  147. {
  148. $info = Excel::create('test', function ($writer)
  149. {
  150. $writer->sheet('test', function ($sheet)
  151. {
  152. $sheet->fromArray([
  153. 'test data'
  154. ]);
  155. });
  156. })->store('csv', __DIR__ . '/exports', true);
  157. $this->assertTrue(file_exists($info['full']));
  158. }
  159. public function testNumberPrecision()
  160. {
  161. $info = Excel::create('numbers', function ($writer)
  162. {
  163. $writer->sheet('test', function ($sheet)
  164. {
  165. $sheet->fromArray([
  166. ['number' => '1234'],
  167. ['number' => '1234.020'],
  168. ['number' => '01234HelloWorld'],
  169. ['number' => '12345678901234567890'],
  170. ['number' => 1234],
  171. ['number' => 1234.02],
  172. ['number' => 0.0231231234423],
  173. ['number' => 4195.99253472222],
  174. ['number' => '= A6 + A6'],
  175. ]);
  176. });
  177. })->store('xls', __DIR__ . '/exports', true);
  178. $this->assertTrue(file_exists($info['full']));
  179. $results = Excel::load($info['full'], null, false, true)->calculate()->toArray();
  180. $this->assertEquals('1234', $results[0]['number']);
  181. $this->assertEquals('1234.020', $results[1]['number']);
  182. $this->assertEquals('01234HelloWorld', $results[2]['number']);
  183. $this->assertEquals('12345678901234567890', $results[3]['number']);
  184. $this->assertTrue(is_double($results[4]['number']));
  185. $this->assertEquals((double) 1234, $results[4]['number']);
  186. $this->assertTrue(is_double($results[5]['number']));
  187. $this->assertEquals('1234.02', $results[5]['number']);
  188. $this->assertTrue(is_double($results[6]['number']));
  189. $this->assertEquals('0.0231231234423', $results[6]['number']);
  190. $this->assertTrue(is_double($results[7]['number']));
  191. $this->assertEquals(4195.99253472222, $results[7]['number']);
  192. $this->assertEquals(1234 + 1234, $results[8]['number']);
  193. }
  194. }