forked from mk-j/PHP_XLSXWriter
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[t:DCWIbRRX] added properties to xlsx file
- Loading branch information
1 parent
a1aa396
commit b53dc7a
Showing
5 changed files
with
201 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
namespace Test; | ||
|
||
|
||
use PHPUnit\Framework\TestCase; | ||
|
||
class FilePropertyTest extends TestCase { | ||
|
||
public function testAuthor() { | ||
$expected_author = "John Doe"; | ||
$writer = new \XLSXWriter(); | ||
$writer->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"]); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
<?php | ||
|
||
namespace Test; | ||
|
||
include_once __DIR__.'/../vendor/autoload.php'; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use SimpleXMLElement; | ||
use XLSXWriter; | ||
use XLSXWriter_BuffererWriter; | ||
use ZipArchive; | ||
|
||
class _XLSXWriter_ extends XLSXWriter | ||
{ | ||
public function writeCell(XLSXWriter_BuffererWriter &$file, $row_number, $column_number, $value, $cell_format) { | ||
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 TestCase | ||
{ | ||
/** | ||
* @covers XLSXWriter::writeCell | ||
*/ | ||
public function testWriteCell() { | ||
$filename = tempnam("/tmp", "xlsx_writer"); | ||
$file_writer = new XLSXWriter_BuffererWriter($filename); | ||
|
||
$xlsx_writer = new _XLSXWriter_(); | ||
$xlsx_writer->writeCell($file_writer, 0, 0, '0123', 'string'); | ||
$file_writer->close(); | ||
$cell_xml = file_get_contents($filename); | ||
$this->assertNotEquals('<c r="A1" s="0" t="n"><v>123</v></c>', $cell_xml); | ||
$this->assertEquals('<c r="A1" s="0" t="s"><v>0</v></c>', $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; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters