2017-02-18 12:36:08 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Reader;
|
|
|
|
|
|
|
|
use PhpOffice\PhpSpreadsheet\Cell\DataType;
|
|
|
|
use PhpOffice\PhpSpreadsheet\Reader\Ods;
|
|
|
|
use PhpOffice\PhpSpreadsheet\Style\Font;
|
|
|
|
|
|
|
|
/*
|
2017-02-20 20:02:04 +00:00
|
|
|
* @todo The class doesn't read the bold/italic/underline properties (rich text)
|
2017-02-18 12:36:08 +00:00
|
|
|
*/
|
|
|
|
class OdsTest extends \PHPUnit_Framework_TestCase
|
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @var \PhpOffice\PhpSpreadsheet\Spreadsheet
|
|
|
|
*/
|
2017-02-18 19:49:48 +00:00
|
|
|
public $spreadsheetOOCalcTest;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @var \PhpOffice\PhpSpreadsheet\Spreadsheet
|
|
|
|
*/
|
|
|
|
public $spreadsheetData;
|
2017-02-18 12:36:08 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @return \PhpOffice\PhpSpreadsheet\Spreadsheet
|
|
|
|
*/
|
2017-02-20 20:05:25 +00:00
|
|
|
protected function loadOOCalcTestFile()
|
|
|
|
{
|
|
|
|
if (!$this->spreadsheetOOCalcTest) {
|
2017-02-18 12:36:08 +00:00
|
|
|
$filename = __DIR__ . '/../../../samples/templates/OOCalcTest.ods';
|
|
|
|
|
2017-02-18 19:49:48 +00:00
|
|
|
// Load into this instance
|
|
|
|
$reader = new Ods();
|
|
|
|
$this->spreadsheetOOCalcTest = $reader->loadIntoExisting($filename, new \PhpOffice\PhpSpreadsheet\Spreadsheet());
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->spreadsheetOOCalcTest;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @return \PhpOffice\PhpSpreadsheet\Spreadsheet
|
|
|
|
*/
|
2017-02-20 20:05:25 +00:00
|
|
|
protected function loadDataFile()
|
|
|
|
{
|
|
|
|
if (!$this->spreadsheetData) {
|
2017-02-18 19:49:48 +00:00
|
|
|
$filename = __DIR__ . '/../../data/Reader/Ods/data.ods';
|
2017-02-18 12:36:08 +00:00
|
|
|
|
|
|
|
// Load into this instance
|
|
|
|
$reader = new Ods();
|
2017-02-18 19:49:48 +00:00
|
|
|
$this->spreadsheetData = $reader->loadIntoExisting($filename, new \PhpOffice\PhpSpreadsheet\Spreadsheet());
|
2017-02-18 12:36:08 +00:00
|
|
|
}
|
|
|
|
|
2017-02-18 19:49:48 +00:00
|
|
|
return $this->spreadsheetData;
|
2017-02-18 12:36:08 +00:00
|
|
|
}
|
|
|
|
|
2017-02-20 20:02:04 +00:00
|
|
|
public function testReadFileProperties()
|
|
|
|
{
|
|
|
|
$filename = __DIR__ . '/../../data/Reader/Ods/data.ods';
|
|
|
|
|
|
|
|
// Load into this instance
|
|
|
|
$reader = new Ods();
|
|
|
|
|
|
|
|
// Test "listWorksheetNames" method
|
|
|
|
|
|
|
|
$this->assertEquals([
|
2017-02-20 20:05:25 +00:00
|
|
|
'Sheet1',
|
|
|
|
'Second Sheet',
|
2017-02-20 20:02:04 +00:00
|
|
|
], $reader->listWorksheetNames($filename));
|
|
|
|
}
|
|
|
|
|
2017-02-18 12:55:22 +00:00
|
|
|
public function testLoadWorksheets()
|
|
|
|
{
|
2017-02-18 19:49:48 +00:00
|
|
|
$spreadsheet = $this->loadDataFile();
|
2017-02-18 12:55:22 +00:00
|
|
|
|
|
|
|
$this->assertInstanceOf('PhpOffice\PhpSpreadsheet\Spreadsheet', $spreadsheet);
|
|
|
|
|
2017-02-20 20:02:04 +00:00
|
|
|
$this->assertEquals(2, $spreadsheet->getSheetCount());
|
2017-02-18 12:55:22 +00:00
|
|
|
|
|
|
|
$firstSheet = $spreadsheet->getSheet(0);
|
|
|
|
$this->assertInstanceOf('PhpOffice\PhpSpreadsheet\Worksheet', $firstSheet);
|
|
|
|
|
|
|
|
$secondSheet = $spreadsheet->getSheet(1);
|
|
|
|
$this->assertInstanceOf('PhpOffice\PhpSpreadsheet\Worksheet', $secondSheet);
|
|
|
|
}
|
|
|
|
|
2017-02-20 20:05:25 +00:00
|
|
|
public function testReadValueAndComments()
|
|
|
|
{
|
2017-02-18 19:49:48 +00:00
|
|
|
$spreadsheet = $this->loadOOCalcTestFile();
|
2017-02-18 12:36:08 +00:00
|
|
|
|
|
|
|
$firstSheet = $spreadsheet->getSheet(0);
|
|
|
|
|
|
|
|
$this->assertEquals(29, $firstSheet->getHighestRow());
|
|
|
|
$this->assertEquals('N', $firstSheet->getHighestColumn());
|
|
|
|
|
|
|
|
// Simple cell value
|
2017-02-20 20:05:25 +00:00
|
|
|
$this->assertEquals('Test String 1', $firstSheet->getCell('A1')->getValue());
|
2017-02-18 12:36:08 +00:00
|
|
|
|
|
|
|
// Merged cell
|
2017-02-20 20:05:25 +00:00
|
|
|
$this->assertEquals('BOX', $firstSheet->getCell('B18')->getValue());
|
2017-02-18 12:36:08 +00:00
|
|
|
|
|
|
|
// Comments/Annotations
|
|
|
|
$this->assertEquals(
|
2017-02-20 20:05:25 +00:00
|
|
|
'Test for a simple colour-formatted string',
|
|
|
|
$firstSheet->getComment('A1')->getText()->getPlainText()
|
2017-02-18 12:36:08 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
// Data types
|
2017-02-20 20:05:25 +00:00
|
|
|
$this->assertEquals(DataType::TYPE_STRING, $firstSheet->getCell('A1')->getDataType());
|
|
|
|
$this->assertEquals(DataType::TYPE_NUMERIC, $firstSheet->getCell('B1')->getDataType()); // Int
|
2017-02-18 12:36:08 +00:00
|
|
|
|
2017-02-20 20:05:25 +00:00
|
|
|
$this->assertEquals(DataType::TYPE_NUMERIC, $firstSheet->getCell('B6')->getDataType()); // Float
|
|
|
|
$this->assertEquals(1.23, $firstSheet->getCell('B6')->getValue());
|
|
|
|
$this->assertEquals(0, $firstSheet->getCell('G10')->getValue());
|
2017-02-18 12:36:08 +00:00
|
|
|
|
2017-02-20 20:05:25 +00:00
|
|
|
$this->assertEquals(DataType::TYPE_NUMERIC, $firstSheet->getCell('A10')->getDataType()); // Date
|
|
|
|
$this->assertEquals(22269.0, $firstSheet->getCell('A10')->getValue());
|
2017-02-18 12:36:08 +00:00
|
|
|
|
2017-02-20 20:05:25 +00:00
|
|
|
$this->assertEquals(DataType::TYPE_NUMERIC, $firstSheet->getCell('A13')->getDataType()); // Time
|
|
|
|
$this->assertEquals(25569.0625, $firstSheet->getCell('A13')->getValue());
|
2017-02-18 12:36:08 +00:00
|
|
|
|
2017-02-20 20:05:25 +00:00
|
|
|
$this->assertEquals(DataType::TYPE_NUMERIC, $firstSheet->getCell('A15')->getDataType()); // Date + Time
|
|
|
|
$this->assertEquals(22269.0625, $firstSheet->getCell('A15')->getValue());
|
2017-02-18 12:36:08 +00:00
|
|
|
|
2017-02-20 20:05:25 +00:00
|
|
|
$this->assertEquals(DataType::TYPE_NUMERIC, $firstSheet->getCell('A11')->getDataType()); // Fraction
|
2017-02-18 12:36:08 +00:00
|
|
|
|
2017-02-20 20:05:25 +00:00
|
|
|
$this->assertEquals(DataType::TYPE_BOOL, $firstSheet->getCell('D6')->getDataType());
|
|
|
|
$this->assertTrue($firstSheet->getCell('D6')->getValue());
|
2017-02-18 12:36:08 +00:00
|
|
|
|
2017-02-20 20:05:25 +00:00
|
|
|
$this->assertEquals(DataType::TYPE_FORMULA, $firstSheet->getCell('C6')->getDataType()); // Formula
|
|
|
|
$this->assertEquals('=TRUE()', $firstSheet->getCell('C6')->getValue()); // Formula
|
2017-02-18 12:36:08 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Percentage, Currency
|
|
|
|
*/
|
|
|
|
|
2017-02-18 19:49:48 +00:00
|
|
|
$spreadsheet = $this->loadDataFile();
|
2017-02-18 12:36:08 +00:00
|
|
|
|
2017-02-18 12:55:22 +00:00
|
|
|
$firstSheet = $spreadsheet->getSheet(0);
|
2017-02-18 12:36:08 +00:00
|
|
|
|
2017-02-20 20:05:25 +00:00
|
|
|
$this->assertEquals(DataType::TYPE_NUMERIC, $firstSheet->getCell('A1')->getDataType()); // Percentage (10%)
|
|
|
|
$this->assertEquals(0.1, $firstSheet->getCell('A1')->getValue());
|
2017-02-18 12:36:08 +00:00
|
|
|
|
2017-02-20 20:05:25 +00:00
|
|
|
$this->assertEquals(DataType::TYPE_NUMERIC, $firstSheet->getCell('A2')->getDataType()); // Percentage (10.00%)
|
|
|
|
$this->assertEquals(0.1, $firstSheet->getCell('A2')->getValue());
|
2017-02-18 12:36:08 +00:00
|
|
|
|
2017-02-20 20:05:25 +00:00
|
|
|
$this->assertEquals(DataType::TYPE_NUMERIC, $firstSheet->getCell('A4')->getDataType()); // Currency (€10.00)
|
|
|
|
$this->assertEquals(10, $firstSheet->getCell('A4')->getValue());
|
2017-02-18 12:36:08 +00:00
|
|
|
|
2017-02-20 20:05:25 +00:00
|
|
|
$this->assertEquals(DataType::TYPE_NUMERIC, $firstSheet->getCell('A5')->getDataType()); // Currency ($20)
|
|
|
|
$this->assertEquals(20, $firstSheet->getCell('A5')->getValue());
|
2017-02-18 12:36:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testReadColors()
|
|
|
|
{
|
2017-02-18 19:49:48 +00:00
|
|
|
$spreadsheet = $this->loadOOCalcTestFile();
|
2017-02-18 12:36:08 +00:00
|
|
|
$firstSheet = $spreadsheet->getSheet(0);
|
|
|
|
|
|
|
|
// Background color
|
|
|
|
|
2017-02-20 20:05:25 +00:00
|
|
|
$style = $firstSheet->getCell('K3')->getStyle();
|
2017-02-18 12:36:08 +00:00
|
|
|
|
2017-02-20 20:05:25 +00:00
|
|
|
$this->assertEquals('none', $style->getFill()->getFillType());
|
|
|
|
$this->assertEquals('FFFFFFFF', $style->getFill()->getStartColor()->getARGB());
|
|
|
|
$this->assertEquals('FF000000', $style->getFill()->getEndColor()->getARGB());
|
2017-02-18 12:36:08 +00:00
|
|
|
}
|
|
|
|
|
2017-02-20 20:05:25 +00:00
|
|
|
public function testReadRichText()
|
|
|
|
{
|
2017-02-18 19:49:48 +00:00
|
|
|
$spreadsheet = $this->loadOOCalcTestFile();
|
|
|
|
$firstSheet = $spreadsheet->getSheet(0);
|
|
|
|
|
|
|
|
$this->assertEquals(
|
|
|
|
"I don't know if OOCalc supports Rich Text in the same way as Excel, " .
|
2017-02-20 20:05:25 +00:00
|
|
|
'And this row should be autofit height with text wrap',
|
|
|
|
$firstSheet->getCell('A28')->getValue()
|
2017-02-18 19:49:48 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2017-02-20 20:05:25 +00:00
|
|
|
public function testReadCellsWithRepeatedSpaces()
|
|
|
|
{
|
2017-02-18 19:49:48 +00:00
|
|
|
$spreadsheet = $this->loadDataFile();
|
|
|
|
$firstSheet = $spreadsheet->getSheet(0);
|
|
|
|
|
2017-02-20 20:05:25 +00:00
|
|
|
$this->assertEquals('This has 4 spaces before and 2 after ', $firstSheet->getCell('A8')->getValue());
|
|
|
|
$this->assertEquals('This only one after ', $firstSheet->getCell('A9')->getValue());
|
|
|
|
$this->assertEquals('Test with DIFFERENT styles and multiple spaces: ', $firstSheet->getCell('A10')->getValue());
|
|
|
|
$this->assertEquals("test with new \nLines", $firstSheet->getCell('A11')->getValue());
|
2017-02-18 19:49:48 +00:00
|
|
|
}
|
|
|
|
|
2017-02-20 20:05:25 +00:00
|
|
|
public function testReadHyperlinks()
|
|
|
|
{
|
2017-02-18 19:59:14 +00:00
|
|
|
$spreadsheet = $this->loadOOCalcTestFile();
|
|
|
|
$firstSheet = $spreadsheet->getSheet(0);
|
|
|
|
|
2017-02-20 20:05:25 +00:00
|
|
|
$hyperlink = $firstSheet->getCell('A29');
|
2017-02-18 19:59:14 +00:00
|
|
|
|
|
|
|
$this->assertEquals(DataType::TYPE_STRING, $hyperlink->getDataType());
|
2017-02-20 20:05:25 +00:00
|
|
|
$this->assertEquals('PHPExcel', $hyperlink->getValue());
|
|
|
|
$this->assertEquals('http://www.phpexcel.net/', $hyperlink->getHyperlink()->getUrl());
|
2017-02-18 19:59:14 +00:00
|
|
|
}
|
|
|
|
|
2017-02-18 12:36:08 +00:00
|
|
|
/*
|
|
|
|
* Below some test for features not implemented yet
|
|
|
|
*/
|
|
|
|
|
|
|
|
public function testReadBoldItalicUnderline()
|
|
|
|
{
|
2017-02-20 20:05:25 +00:00
|
|
|
$this->markTestSkipped('Features not implemented yet');
|
2017-02-18 12:36:08 +00:00
|
|
|
|
2017-02-18 19:49:48 +00:00
|
|
|
$spreadsheet = $this->loadOOCalcTestFile();
|
2017-02-18 12:36:08 +00:00
|
|
|
$firstSheet = $spreadsheet->getSheet(0);
|
|
|
|
|
|
|
|
// Font styles
|
|
|
|
|
2017-02-20 20:05:25 +00:00
|
|
|
$style = $firstSheet->getCell('A1')->getStyle();
|
|
|
|
$this->assertEquals('FF000000', $style->getFont()->getColor()->getARGB());
|
2017-02-18 12:36:08 +00:00
|
|
|
$this->assertEquals(11, $style->getFont()->getSize());
|
|
|
|
$this->assertEquals(Font::UNDERLINE_NONE, $style->getFont()->getUnderline());
|
|
|
|
|
2017-02-20 20:05:25 +00:00
|
|
|
$style = $firstSheet->getCell('E3')->getStyle();
|
2017-02-18 12:36:08 +00:00
|
|
|
$this->assertEquals(Font::UNDERLINE_SINGLE, $style->getFont()->getUnderline());
|
|
|
|
|
2017-02-20 20:05:25 +00:00
|
|
|
$style = $firstSheet->getCell('E1')->getStyle();
|
2017-02-18 12:36:08 +00:00
|
|
|
$this->assertTrue($style->getFont()->getBold());
|
|
|
|
$this->assertTrue($style->getFont()->getItalic());
|
|
|
|
}
|
|
|
|
}
|