diff --git a/Test/FilePropertyTest.php b/Test/FilePropertyTest.php new file mode 100644 index 000000000..57c45ef63 --- /dev/null +++ b/Test/FilePropertyTest.php @@ -0,0 +1,60 @@ +setAuthor($expected_author); + + $xlsx_properties = $writer->getFileProperties(); + + $this->assertEquals($expected_author, $xlsx_properties["author"]); + } + + public function testTitle() { + $expected_title = "My Spreadsheet"; + $writer = new \XLSXWriter(); + $writer->setTitle($expected_title); + + $xlsx_properties = $writer->getFileProperties(); + + $this->assertEquals($expected_title, $xlsx_properties["title"]); + } + + public function testSubject() { + $expected_subject = "My Spreadsheet is Wonderful"; + $writer = new \XLSXWriter(); + $writer->setSubject($expected_subject); + + $xlsx_properties = $writer->getFileProperties(); + + $this->assertEquals($expected_subject, $xlsx_properties["subject"]); + } + + public function testCompany() { + $expected_company = "EBANX"; + $writer = new \XLSXWriter(); + $writer->setCompany($expected_company); + + $xlsx_properties = $writer->getFileProperties(); + + $this->assertEquals($expected_company, $xlsx_properties["company"]); + } + + public function testKeywords() { + $expected_keywords = ["spreadsheet", "php", "EBANX"]; + $writer = new \XLSXWriter(); + $writer->setKeywords($expected_keywords); + + $xlsx_properties = $writer->getFileProperties(); + + $this->assertEquals($expected_keywords, $xlsx_properties["keywords"]); + } + +} diff --git a/Test/XLSXWriterTest.php b/Test/XLSXWriterTest.php new file mode 100644 index 000000000..5e9dced07 --- /dev/null +++ b/Test/XLSXWriterTest.php @@ -0,0 +1,115 @@ +writeCell($file_writer, 0, 0, '0123', 'string'); + $file_writer->close(); + $cell_xml = file_get_contents($filename); + $this->assertNotEquals('123', $cell_xml); + $this->assertEquals('0', $cell_xml);//0123 should be the 0th index of the shared string array + @unlink($filename); + } + + /** + * @covers XLSXWriter::writeToFile + */ + public function testWriteToFile() { + $filename = tempnam("/tmp", "xlsx_writer"); + + $header = ['0'=>'string','1'=>'string','2'=>'string','3'=>'string']; + $sheet = [ + ['55','66','77','88'], + ['10','11','12','13'], + ]; + + $xlsx_writer = new XLSXWriter(); + $xlsx_writer->writeSheet($sheet,'mysheet',$header); + $xlsx_writer->writeToFile($filename); + + $zip = new ZipArchive(); + $r = $zip->open($filename); + $this->assertTrue($r); + + $this->assertNotEmpty(($zip->numFiles)); + + $out_sheet = []; + for($z=0; $z < $zip->numFiles; $z++) { + $inside_zip_filename = $zip->getNameIndex($z); + + if (preg_match("/sheet(\d+).xml/", basename($inside_zip_filename))) { + $out_sheet = $this->stripCellsFromSheetXML($zip->getFromName($inside_zip_filename)); + array_shift($out_sheet); + $out_sheet = array_values($out_sheet); + } + } + + $zip->close(); + @unlink($filename); + + $r1 = self::array_diff_assoc_recursive($out_sheet, $sheet); + $r2 = self::array_diff_assoc_recursive($sheet, $out_sheet); + $this->assertEmpty($r1); + $this->assertEmpty($r2); + } + + private function stripCellsFromSheetXML($sheet_xml) { + $output = []; + + $xml = new SimpleXMLElement($sheet_xml); + + for ($i = 0; $i < count($xml->sheetData->row); $i++) { + $row = $xml->sheetData->row[$i]; + for ($j = 0; $j < count($row->c); $j ++) { + $output[$i][$j] = (string)$row->c[$j]->v; + } + } + + return $output; + } + + public static function array_diff_assoc_recursive($array1, $array2) { + $difference = []; + foreach($array1 as $key => $value) { + if(is_array($value)) { + if(!isset($array2[$key]) || !is_array($array2[$key])) { + $difference[$key] = $value; + } else { + $new_diff = self::array_diff_assoc_recursive($value, $array2[$key]); + if(!empty($new_diff)) { + $difference[$key] = $new_diff; + } + } + } else if(!isset($array2[$key]) || $array2[$key] != $value) { + $difference[$key] = $value; + } + } + + return empty($difference) ? [] : $difference; + } +} diff --git a/Test/XlsxWriterTest.php b/Test/XlsxWriterTest.php deleted file mode 100644 index bb72d69a3..000000000 --- a/Test/XlsxWriterTest.php +++ /dev/null @@ -1,20 +0,0 @@ -setAuthor($expected_author); - - $xlsx_properties = $writer->getFileProperties(); - - $this->assertEquals($expected_author, $xlsx_properties["author"]); - } - -} diff --git a/testbench/xlsxwriter.class.Test.php b/testbench/xlsxwriter.class.Test.php index 6660e6b87..c6ea5efc4 100644 --- a/testbench/xlsxwriter.class.Test.php +++ b/testbench/xlsxwriter.class.Test.php @@ -2,6 +2,8 @@ include_once __DIR__.'/../vendor/autoload.php'; +use PHPUnit\Framework\TestCase; + //TODO test double:writeSheetHeader //TODO test invalid UTF8 //TODO test outoforder writeSheetRow('Sheet1',()); @@ -9,12 +11,12 @@ class _XLSXWriter_ extends XLSXWriter { public function writeCell(XLSXWriter_BuffererWriter &$file, $row_number, $column_number, $value, $cell_format) { - return call_user_func_array('parent::writeCell', func_get_args()); + return call_user_func_array('parent::writeCell', [&$file, $row_number, $column_number, $value, $cell_format]); } } //Just a simple test, by no means comprehensive -class XLSXWriterTest extends PHPUnit_Framework_TestCase +class XLSXWriterTest extends TestCase { /** * @covers XLSXWriter::writeCell diff --git a/xlsxwriter.class.php b/xlsxwriter.class.php index 27fd37791..41727ca1d 100644 --- a/xlsxwriter.class.php +++ b/xlsxwriter.class.php @@ -20,6 +20,12 @@ class XLSXWriter protected $current_sheet = ''; + protected $title; + protected $subject; + protected $company; + protected $description; + protected $keywords = []; + public function __construct() { if(!ini_get('date.timezone')) @@ -30,10 +36,20 @@ public function __construct() } public function setAuthor($author='') { $this->author=$author; } + public function setTitle($title='') { $this->title=$title; } + public function setSubject($subject='') { $this->subject=$subject; } + public function setCompany($company='') { $this->company=$company; } + public function setKeywords($keywords=[]) { $this->keywords=$keywords; } + public function setDescription($description='') { $this->description=$description; } public function getFileProperties(): array { return [ "author" => $this->author, + "title" => $this->title, + "subject" => $this->subject, + "company" => $this->company, + "keywords" => $this->keywords, + "description" => $this->description, ]; } @@ -415,7 +431,10 @@ protected function buildAppXML() { $app_xml=""; $app_xml.=''."\n"; - $app_xml.='0'; + $app_xml.=''; + $app_xml.='0'; + $app_xml.=''.self::xmlspecialchars($this->company).''; + $app_xml.=''; return $app_xml; } @@ -425,7 +444,13 @@ protected function buildCoreXML() $core_xml.=''."\n"; $core_xml.=''; $core_xml.=''.date("Y-m-d\TH:i:s.00\Z").'';//$date_time = '2014-10-25T15:54:37.00Z'; + $core_xml.=''.self::xmlspecialchars($this->title).''; + $core_xml.=''.self::xmlspecialchars($this->subject).''; $core_xml.=''.self::xmlspecialchars($this->author).''; + if (!empty($this->keywords)) { + $core_xml.=''.self::xmlspecialchars(implode (", ", (array)$this->keywords)).''; + } + $core_xml.=''.self::xmlspecialchars($this->description).''; $core_xml.='0'; $core_xml.=''; return $core_xml;