forked from mk-j/PHP_XLSXWriter
-
Couldn't load subscription status.
- Fork 1
[t:DCWIbRRX] added properties to xlsx file #14
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I believe you're missing the
companyproperty that you've created, or is it intentional to not be here?