Added ability to test with fixtures

- Now we can specify exact timestamp
- Current fixture example.xls is readable
This commit is contained in:
Alexey Kopytko 2016-06-15 18:50:04 +09:00
parent 3b766dc161
commit 923008d208
4 changed files with 71 additions and 6 deletions

View File

@ -178,6 +178,9 @@ class Spreadsheet_Excel_Writer_Workbook extends Spreadsheet_Excel_Writer_BIFFwri
/** @var array */ /** @var array */
public $blockSize; public $blockSize;
/** @var int */
public $timestamp;
/** /**
* Class constructor * Class constructor
* *
@ -213,6 +216,8 @@ class Spreadsheet_Excel_Writer_Workbook extends Spreadsheet_Excel_Writer_BIFFwri
$this->totalStringLength = 0; $this->totalStringLength = 0;
$this->uniqueString = 0; $this->uniqueString = 0;
$this->tableOfStrings = array(); $this->tableOfStrings = array();
$this->timestamp = time();
$this->setPaletteXl97(); $this->setPaletteXl97();
} }
@ -636,7 +641,7 @@ class Spreadsheet_Excel_Writer_Workbook extends Spreadsheet_Excel_Writer_BIFFwri
} }
} }
$root = new OLE_PPS_Root(time(), time(), array($OLE)); $root = new OLE_PPS_Root($this->timestamp, $this->timestamp, array($OLE));
if ($this->temporaryDirectory != '') { if ($this->temporaryDirectory != '') {
$root->setTempDir($this->temporaryDirectory); $root->setTempDir($this->temporaryDirectory);

View File

@ -6,12 +6,50 @@
*/ */
class Test_Spreadsheet_Excel_WriterTestCase extends PHPUnit_Framework_TestCase class Test_Spreadsheet_Excel_WriterTestCase extends PHPUnit_Framework_TestCase
{ {
const FIXTURES_PATH = 'test/fixture/';
/** /**
* @param string $fileName * @param string $filename
* @return Spreadsheet_Excel_Writer_Workbook * @return Spreadsheet_Excel_Writer
*/ */
protected function getNewWorkbook($fileName = 'my_workbook') protected function getNewWorkbook($filename = '')
{ {
return new Spreadsheet_Excel_Writer_Workbook($fileName); // we're writing to the standard output by defaulr
return new Spreadsheet_Excel_Writer($filename);
}
protected function assertSameAsInFixture($filename, Spreadsheet_Excel_Writer $workbook)
{
$this->assertEmpty($workbook->fileName, "Testing with fixtures works only for standard output");
// we have to fix timestamp for fixtures to work
$workbook->timestamp = 1000000000; // somewhere in 2001
ob_start();
$workbook->close();
$data = ob_get_clean();
$fullPath = self::FIXTURES_PATH.$filename;
if ($this->shouldUpdateFixtures()) {
file_put_contents($fullPath, $data);
}
if (!is_file($fullPath)) {
$this->fail("Fixture $filename not found");
}
// TODO: should we save data for future analysis?
//file_put_contents("{$fullPath}.work", $data);
$this->assertEquals(file_get_contents($fullPath), $data, "Output differs for $filename");
}
/**
* We should update golden files
*/
private function shouldUpdateFixtures()
{
return isset($_SERVER['GOLDEN']);
} }
} }

View File

@ -10,6 +10,28 @@ class Test_Spreadsheet_Excel_Writer_WorkbookTest extends Test_Spreadsheet_Excel_
{ {
$workbook = $this->getNewWorkbook(); $workbook = $this->getNewWorkbook();
$before = get_object_vars($workbook);
$workbook->setVersion(1);
$this->assertEquals($before, get_object_vars($workbook), "Version 1 should not change internal state");
$workbook->setVersion(8); $workbook->setVersion(8);
$this->assertNotEquals($before, get_object_vars($workbook), "Version 8 should change internal state");
return $workbook;
} }
}
/**
* @depends testSetVersion
*/
public function testWriteSingleCell(Spreadsheet_Excel_Writer $workbook)
{
$sheet = $workbook->addWorksheet("Example");
$sheet->write(0, 0, "Example");
$this->assertSameAsInFixture('example.xls', $workbook);
}
}

BIN
test/fixture/example.xls Normal file

Binary file not shown.