Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions Test/FilePropertyTest.php
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"]);
}

}
115 changes: 115 additions & 0 deletions Test/XLSXWriterTest.php
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;
}
}
20 changes: 0 additions & 20 deletions Test/XlsxWriterTest.php

This file was deleted.

6 changes: 4 additions & 2 deletions testbench/xlsxwriter.class.Test.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,21 @@

include_once __DIR__.'/../vendor/autoload.php';

use PHPUnit\Framework\TestCase;

//TODO test double:writeSheetHeader
//TODO test invalid UTF8
//TODO test outoforder writeSheetRow('Sheet1',());

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
Expand Down
27 changes: 26 additions & 1 deletion xlsxwriter.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand All @@ -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,
];
}

Expand Down Expand Up @@ -415,7 +431,10 @@ protected function buildAppXML()
{
$app_xml="";
$app_xml.='<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'."\n";
$app_xml.='<Properties xmlns="http://schemas.openxmlformats.org/officeDocument/2006/extended-properties" xmlns:vt="http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes"><TotalTime>0</TotalTime></Properties>';
$app_xml.='<Properties xmlns="http://schemas.openxmlformats.org/officeDocument/2006/extended-properties" xmlns:vt="http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes">';
$app_xml.='<TotalTime>0</TotalTime>';
$app_xml.='<Company>'.self::xmlspecialchars($this->company).'</Company>';
$app_xml.='</Properties>';
return $app_xml;
}

Expand All @@ -425,7 +444,13 @@ protected function buildCoreXML()
$core_xml.='<?xml version="1.0" encoding="UTF-8" standalone="yes"?>'."\n";
$core_xml.='<cp:coreProperties xmlns:cp="http://schemas.openxmlformats.org/package/2006/metadata/core-properties" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:dcmitype="http://purl.org/dc/dcmitype/" xmlns:dcterms="http://purl.org/dc/terms/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">';
$core_xml.='<dcterms:created xsi:type="dcterms:W3CDTF">'.date("Y-m-d\TH:i:s.00\Z").'</dcterms:created>';//$date_time = '2014-10-25T15:54:37.00Z';
$core_xml.='<dc:title>'.self::xmlspecialchars($this->title).'</dc:title>';
$core_xml.='<dc:subject>'.self::xmlspecialchars($this->subject).'</dc:subject>';
$core_xml.='<dc:creator>'.self::xmlspecialchars($this->author).'</dc:creator>';
if (!empty($this->keywords)) {
$core_xml.='<cp:keywords>'.self::xmlspecialchars(implode (", ", (array)$this->keywords)).'</cp:keywords>';
}
$core_xml.='<dc:description>'.self::xmlspecialchars($this->description).'</dc:description>';

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 company property that you've created, or is it intentional to not be here?

$core_xml.='<cp:revision>0</cp:revision>';
$core_xml.='</cp:coreProperties>';
return $core_xml;
Expand Down