2017-02-18 12:36:08 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
namespace PhpOffice\PhpSpreadsheetTests\Reader;
|
|
|
|
|
|
|
|
use PhpOffice\PhpSpreadsheet\Cell\DataType;
|
2019-06-30 21:42:25 +00:00
|
|
|
use PhpOffice\PhpSpreadsheet\Document\Properties;
|
2017-02-18 12:36:08 +00:00
|
|
|
use PhpOffice\PhpSpreadsheet\Reader\Ods;
|
2017-05-17 22:02:17 +00:00
|
|
|
use PhpOffice\PhpSpreadsheet\Spreadsheet;
|
2017-02-18 12:36:08 +00:00
|
|
|
use PhpOffice\PhpSpreadsheet\Style\Font;
|
2017-11-08 15:48:01 +00:00
|
|
|
use PHPUnit\Framework\TestCase;
|
2017-02-18 12:36:08 +00:00
|
|
|
|
2017-03-06 05:40:27 +00:00
|
|
|
/**
|
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
|
|
|
*/
|
2017-11-08 15:48:01 +00:00
|
|
|
class OdsTest extends TestCase
|
2017-02-18 12:36:08 +00:00
|
|
|
{
|
|
|
|
/**
|
2017-05-17 22:02:17 +00:00
|
|
|
* @var Spreadsheet
|
2017-02-18 12:36:08 +00:00
|
|
|
*/
|
2017-12-30 10:07:22 +00:00
|
|
|
private $spreadsheetOdsTest;
|
2017-02-18 19:49:48 +00:00
|
|
|
|
|
|
|
/**
|
2017-05-17 22:02:17 +00:00
|
|
|
* @var Spreadsheet
|
2017-02-18 19:49:48 +00:00
|
|
|
*/
|
2017-03-06 05:40:27 +00:00
|
|
|
private $spreadsheetData;
|
2017-02-18 12:36:08 +00:00
|
|
|
|
|
|
|
/**
|
2017-05-17 22:02:17 +00:00
|
|
|
* @return Spreadsheet
|
2017-02-18 12:36:08 +00:00
|
|
|
*/
|
2017-12-30 10:07:22 +00:00
|
|
|
private function loadOdsTestFile()
|
2017-02-20 20:05:25 +00:00
|
|
|
{
|
2017-12-30 10:07:22 +00:00
|
|
|
if (!$this->spreadsheetOdsTest) {
|
2020-05-17 09:35:55 +00:00
|
|
|
$filename = 'samples/templates/OOCalcTest.ods';
|
2017-02-18 12:36:08 +00:00
|
|
|
|
2017-02-18 19:49:48 +00:00
|
|
|
// Load into this instance
|
|
|
|
$reader = new Ods();
|
2017-12-30 10:07:22 +00:00
|
|
|
$this->spreadsheetOdsTest = $reader->loadIntoExisting($filename, new Spreadsheet());
|
2017-02-18 19:49:48 +00:00
|
|
|
}
|
|
|
|
|
2017-12-30 10:07:22 +00:00
|
|
|
return $this->spreadsheetOdsTest;
|
2017-02-18 19:49:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2017-05-17 22:02:17 +00:00
|
|
|
* @return Spreadsheet
|
2017-02-18 19:49:48 +00:00
|
|
|
*/
|
2017-02-20 20:05:25 +00:00
|
|
|
protected function loadDataFile()
|
|
|
|
{
|
|
|
|
if (!$this->spreadsheetData) {
|
2020-05-17 09:35:55 +00:00
|
|
|
$filename = 'tests/data/Reader/Ods/data.ods';
|
2017-02-18 12:36:08 +00:00
|
|
|
|
|
|
|
// Load into this instance
|
|
|
|
$reader = new Ods();
|
2017-05-17 22:02:17 +00:00
|
|
|
$this->spreadsheetData = $reader->loadIntoExisting($filename, new 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
|
|
|
}
|
|
|
|
|
2020-05-18 04:49:57 +00:00
|
|
|
public function testReadFileProperties(): void
|
2017-02-20 20:02:04 +00:00
|
|
|
{
|
2020-05-17 09:35:55 +00:00
|
|
|
$filename = 'tests/data/Reader/Ods/data.ods';
|
2017-02-20 20:02:04 +00:00
|
|
|
|
|
|
|
// Load into this instance
|
|
|
|
$reader = new Ods();
|
|
|
|
|
|
|
|
// Test "listWorksheetNames" method
|
|
|
|
|
2017-09-20 05:55:42 +00:00
|
|
|
self::assertEquals([
|
2017-02-20 20:05:25 +00:00
|
|
|
'Sheet1',
|
|
|
|
'Second Sheet',
|
2017-02-20 20:02:04 +00:00
|
|
|
], $reader->listWorksheetNames($filename));
|
|
|
|
}
|
|
|
|
|
2020-05-18 04:49:57 +00:00
|
|
|
public function testLoadWorksheets(): void
|
2017-02-18 12:55:22 +00:00
|
|
|
{
|
2017-02-18 19:49:48 +00:00
|
|
|
$spreadsheet = $this->loadDataFile();
|
2017-02-18 12:55:22 +00:00
|
|
|
|
2017-09-20 05:55:42 +00:00
|
|
|
self::assertInstanceOf('PhpOffice\PhpSpreadsheet\Spreadsheet', $spreadsheet);
|
2017-02-18 12:55:22 +00:00
|
|
|
|
2017-09-20 05:55:42 +00:00
|
|
|
self::assertEquals(2, $spreadsheet->getSheetCount());
|
2017-02-18 12:55:22 +00:00
|
|
|
|
|
|
|
$firstSheet = $spreadsheet->getSheet(0);
|
2017-10-29 08:39:42 +00:00
|
|
|
self::assertInstanceOf('PhpOffice\PhpSpreadsheet\Worksheet\Worksheet', $firstSheet);
|
2017-02-18 12:55:22 +00:00
|
|
|
|
|
|
|
$secondSheet = $spreadsheet->getSheet(1);
|
2017-10-29 08:39:42 +00:00
|
|
|
self::assertInstanceOf('PhpOffice\PhpSpreadsheet\Worksheet\Worksheet', $secondSheet);
|
2017-02-18 12:55:22 +00:00
|
|
|
}
|
|
|
|
|
2020-05-18 04:49:57 +00:00
|
|
|
public function testReadValueAndComments(): void
|
2017-02-20 20:05:25 +00:00
|
|
|
{
|
2017-12-30 10:07:22 +00:00
|
|
|
$spreadsheet = $this->loadOdsTestFile();
|
2017-02-18 12:36:08 +00:00
|
|
|
|
|
|
|
$firstSheet = $spreadsheet->getSheet(0);
|
|
|
|
|
2017-09-20 05:55:42 +00:00
|
|
|
self::assertEquals(29, $firstSheet->getHighestRow());
|
|
|
|
self::assertEquals('N', $firstSheet->getHighestColumn());
|
2017-02-18 12:36:08 +00:00
|
|
|
|
|
|
|
// Simple cell value
|
2017-09-20 05:55:42 +00:00
|
|
|
self::assertEquals('Test String 1', $firstSheet->getCell('A1')->getValue());
|
2017-02-18 12:36:08 +00:00
|
|
|
|
|
|
|
// Merged cell
|
2017-09-20 05:55:42 +00:00
|
|
|
self::assertEquals('BOX', $firstSheet->getCell('B18')->getValue());
|
2017-02-18 12:36:08 +00:00
|
|
|
|
|
|
|
// Comments/Annotations
|
2017-09-20 05:55:42 +00:00
|
|
|
self::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-09-20 05:55:42 +00:00
|
|
|
self::assertEquals(DataType::TYPE_STRING, $firstSheet->getCell('A1')->getDataType());
|
|
|
|
self::assertEquals(DataType::TYPE_NUMERIC, $firstSheet->getCell('B1')->getDataType()); // Int
|
2017-02-18 12:36:08 +00:00
|
|
|
|
2017-09-20 05:55:42 +00:00
|
|
|
self::assertEquals(DataType::TYPE_NUMERIC, $firstSheet->getCell('B6')->getDataType()); // Float
|
|
|
|
self::assertEquals(1.23, $firstSheet->getCell('B6')->getValue());
|
|
|
|
self::assertEquals(0, $firstSheet->getCell('G10')->getValue());
|
2017-02-18 12:36:08 +00:00
|
|
|
|
2017-09-20 05:55:42 +00:00
|
|
|
self::assertEquals(DataType::TYPE_NUMERIC, $firstSheet->getCell('A10')->getDataType()); // Date
|
|
|
|
self::assertEquals(22269.0, $firstSheet->getCell('A10')->getValue());
|
2017-02-18 12:36:08 +00:00
|
|
|
|
2017-09-20 05:55:42 +00:00
|
|
|
self::assertEquals(DataType::TYPE_NUMERIC, $firstSheet->getCell('A13')->getDataType()); // Time
|
|
|
|
self::assertEquals(25569.0625, $firstSheet->getCell('A13')->getValue());
|
2017-02-18 12:36:08 +00:00
|
|
|
|
2017-09-20 05:55:42 +00:00
|
|
|
self::assertEquals(DataType::TYPE_NUMERIC, $firstSheet->getCell('A15')->getDataType()); // Date + Time
|
|
|
|
self::assertEquals(22269.0625, $firstSheet->getCell('A15')->getValue());
|
2017-02-18 12:36:08 +00:00
|
|
|
|
2017-09-20 05:55:42 +00:00
|
|
|
self::assertEquals(DataType::TYPE_NUMERIC, $firstSheet->getCell('A11')->getDataType()); // Fraction
|
2017-02-18 12:36:08 +00:00
|
|
|
|
2017-09-20 05:55:42 +00:00
|
|
|
self::assertEquals(DataType::TYPE_BOOL, $firstSheet->getCell('D6')->getDataType());
|
|
|
|
self::assertTrue($firstSheet->getCell('D6')->getValue());
|
2017-02-18 12:36:08 +00:00
|
|
|
|
2017-09-20 05:55:42 +00:00
|
|
|
self::assertEquals(DataType::TYPE_FORMULA, $firstSheet->getCell('C6')->getDataType()); // Formula
|
|
|
|
self::assertEquals('=TRUE()', $firstSheet->getCell('C6')->getValue()); // Formula
|
2017-02-18 12:36:08 +00:00
|
|
|
|
2017-10-01 11:07:04 +00:00
|
|
|
// Percentage, Currency
|
2017-02-18 12:36:08 +00:00
|
|
|
|
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-09-20 05:55:42 +00:00
|
|
|
self::assertEquals(DataType::TYPE_NUMERIC, $firstSheet->getCell('A1')->getDataType()); // Percentage (10%)
|
|
|
|
self::assertEquals(0.1, $firstSheet->getCell('A1')->getValue());
|
2017-02-18 12:36:08 +00:00
|
|
|
|
2017-09-20 05:55:42 +00:00
|
|
|
self::assertEquals(DataType::TYPE_NUMERIC, $firstSheet->getCell('A2')->getDataType()); // Percentage (10.00%)
|
|
|
|
self::assertEquals(0.1, $firstSheet->getCell('A2')->getValue());
|
2017-02-18 12:36:08 +00:00
|
|
|
|
2017-09-20 05:55:42 +00:00
|
|
|
self::assertEquals(DataType::TYPE_NUMERIC, $firstSheet->getCell('A4')->getDataType()); // Currency (€10.00)
|
|
|
|
self::assertEquals(10, $firstSheet->getCell('A4')->getValue());
|
2017-02-18 12:36:08 +00:00
|
|
|
|
2017-09-20 05:55:42 +00:00
|
|
|
self::assertEquals(DataType::TYPE_NUMERIC, $firstSheet->getCell('A5')->getDataType()); // Currency ($20)
|
|
|
|
self::assertEquals(20, $firstSheet->getCell('A5')->getValue());
|
2017-02-18 12:36:08 +00:00
|
|
|
}
|
|
|
|
|
2020-05-18 04:49:57 +00:00
|
|
|
public function testReadColors(): void
|
2017-02-18 12:36:08 +00:00
|
|
|
{
|
2017-12-30 10:07:22 +00:00
|
|
|
$spreadsheet = $this->loadOdsTestFile();
|
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-09-20 05:55:42 +00:00
|
|
|
self::assertEquals('none', $style->getFill()->getFillType());
|
|
|
|
self::assertEquals('FFFFFFFF', $style->getFill()->getStartColor()->getARGB());
|
|
|
|
self::assertEquals('FF000000', $style->getFill()->getEndColor()->getARGB());
|
2017-02-18 12:36:08 +00:00
|
|
|
}
|
|
|
|
|
2020-05-18 04:49:57 +00:00
|
|
|
public function testReadRichText(): void
|
2017-02-20 20:05:25 +00:00
|
|
|
{
|
2017-12-30 10:07:22 +00:00
|
|
|
$spreadsheet = $this->loadOdsTestFile();
|
2017-02-18 19:49:48 +00:00
|
|
|
$firstSheet = $spreadsheet->getSheet(0);
|
|
|
|
|
2017-09-20 05:55:42 +00:00
|
|
|
self::assertEquals(
|
2017-02-18 19:49:48 +00:00
|
|
|
"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
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-05-18 04:49:57 +00:00
|
|
|
public function testReadCellsWithRepeatedSpaces(): void
|
2017-02-20 20:05:25 +00:00
|
|
|
{
|
2017-02-18 19:49:48 +00:00
|
|
|
$spreadsheet = $this->loadDataFile();
|
|
|
|
$firstSheet = $spreadsheet->getSheet(0);
|
|
|
|
|
2017-09-20 05:55:42 +00:00
|
|
|
self::assertEquals('This has 4 spaces before and 2 after ', $firstSheet->getCell('A8')->getValue());
|
|
|
|
self::assertEquals('This only one after ', $firstSheet->getCell('A9')->getValue());
|
|
|
|
self::assertEquals('Test with DIFFERENT styles and multiple spaces: ', $firstSheet->getCell('A10')->getValue());
|
|
|
|
self::assertEquals("test with new \nLines", $firstSheet->getCell('A11')->getValue());
|
2017-02-18 19:49:48 +00:00
|
|
|
}
|
|
|
|
|
2020-05-18 04:49:57 +00:00
|
|
|
public function testReadHyperlinks(): void
|
2017-02-20 20:05:25 +00:00
|
|
|
{
|
2017-12-30 10:07:22 +00:00
|
|
|
$spreadsheet = $this->loadOdsTestFile();
|
2017-02-18 19:59:14 +00:00
|
|
|
$firstSheet = $spreadsheet->getSheet(0);
|
|
|
|
|
2017-02-20 20:05:25 +00:00
|
|
|
$hyperlink = $firstSheet->getCell('A29');
|
2017-02-18 19:59:14 +00:00
|
|
|
|
2017-09-20 05:55:42 +00:00
|
|
|
self::assertEquals(DataType::TYPE_STRING, $hyperlink->getDataType());
|
2017-12-30 10:07:22 +00:00
|
|
|
self::assertEquals('PhpSpreadsheet', $hyperlink->getValue());
|
|
|
|
self::assertEquals('https://github.com/PHPOffice/phpspreadsheet', $hyperlink->getHyperlink()->getUrl());
|
2017-02-18 19:59:14 +00:00
|
|
|
}
|
|
|
|
|
2017-10-01 11:07:04 +00:00
|
|
|
// Below some test for features not implemented yet
|
2017-02-18 12:36:08 +00:00
|
|
|
|
2020-05-18 04:49:57 +00:00
|
|
|
public function testReadBoldItalicUnderline(): void
|
2017-02-18 12:36:08 +00:00
|
|
|
{
|
2020-05-18 04:49:57 +00:00
|
|
|
self::markTestIncomplete('Features not implemented yet');
|
2017-02-18 12:36:08 +00:00
|
|
|
|
2017-12-30 10:07:22 +00:00
|
|
|
$spreadsheet = $this->loadOdsTestFile();
|
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();
|
2017-09-20 05:55:42 +00:00
|
|
|
self::assertEquals('FF000000', $style->getFont()->getColor()->getARGB());
|
|
|
|
self::assertEquals(11, $style->getFont()->getSize());
|
|
|
|
self::assertEquals(Font::UNDERLINE_NONE, $style->getFont()->getUnderline());
|
2017-02-18 12:36:08 +00:00
|
|
|
|
2017-02-20 20:05:25 +00:00
|
|
|
$style = $firstSheet->getCell('E3')->getStyle();
|
2017-09-20 05:55:42 +00:00
|
|
|
self::assertEquals(Font::UNDERLINE_SINGLE, $style->getFont()->getUnderline());
|
2017-02-18 12:36:08 +00:00
|
|
|
|
2017-02-20 20:05:25 +00:00
|
|
|
$style = $firstSheet->getCell('E1')->getStyle();
|
2017-09-20 05:55:42 +00:00
|
|
|
self::assertTrue($style->getFont()->getBold());
|
|
|
|
self::assertTrue($style->getFont()->getItalic());
|
2017-02-18 12:36:08 +00:00
|
|
|
}
|
2019-06-30 21:42:25 +00:00
|
|
|
|
2020-05-18 04:49:57 +00:00
|
|
|
public function testLoadOdsWorkbookProperties(): void
|
2019-06-30 21:42:25 +00:00
|
|
|
{
|
|
|
|
$customPropertySet = [
|
|
|
|
'Owner' => ['type' => Properties::PROPERTY_TYPE_STRING, 'value' => 'PHPOffice'],
|
|
|
|
'Tested' => ['type' => Properties::PROPERTY_TYPE_BOOLEAN, 'value' => true],
|
|
|
|
'Counter' => ['type' => Properties::PROPERTY_TYPE_FLOAT, 'value' => 10.0],
|
|
|
|
'TestDate' => ['type' => Properties::PROPERTY_TYPE_DATE, 'value' => '2019-06-30'],
|
|
|
|
'HereAndNow' => ['type' => Properties::PROPERTY_TYPE_DATE, 'value' => '2019-06-30'],
|
|
|
|
];
|
|
|
|
|
2020-05-17 09:35:55 +00:00
|
|
|
$filename = 'tests/data/Reader/Ods/propertyTest.ods';
|
2019-06-30 21:42:25 +00:00
|
|
|
$reader = new Ods();
|
|
|
|
$spreadsheet = $reader->load($filename);
|
|
|
|
|
|
|
|
$properties = $spreadsheet->getProperties();
|
|
|
|
// Core Properties
|
2019-07-25 17:15:53 +00:00
|
|
|
// self::assertSame('Mark Baker', $properties->getCreator());
|
|
|
|
self::assertSame('Property Test File', $properties->getTitle());
|
|
|
|
self::assertSame('Testing for Properties', $properties->getSubject());
|
|
|
|
self::assertSame('TEST ODS PHPSpreadsheet', $properties->getKeywords());
|
2019-06-30 21:42:25 +00:00
|
|
|
|
|
|
|
// Extended Properties
|
2019-07-25 17:15:53 +00:00
|
|
|
// self::assertSame('PHPOffice', $properties->getCompany());
|
|
|
|
// self::assertSame('The Big Boss', $properties->getManager());
|
2019-06-30 21:42:25 +00:00
|
|
|
|
|
|
|
// Custom Properties
|
|
|
|
$customProperties = $properties->getCustomProperties();
|
2019-07-25 17:15:53 +00:00
|
|
|
self::assertIsArray($customProperties);
|
2019-06-30 21:42:25 +00:00
|
|
|
$customProperties = array_flip($customProperties);
|
2019-07-25 17:15:53 +00:00
|
|
|
self::assertArrayHasKey('TestDate', $customProperties);
|
2019-06-30 21:42:25 +00:00
|
|
|
|
|
|
|
foreach ($customPropertySet as $propertyName => $testData) {
|
2019-07-25 17:15:53 +00:00
|
|
|
self::assertTrue($properties->isCustomPropertySet($propertyName));
|
|
|
|
self::assertSame($testData['type'], $properties->getCustomPropertyType($propertyName));
|
2019-06-30 21:42:25 +00:00
|
|
|
if ($properties->getCustomPropertyType($propertyName) == Properties::PROPERTY_TYPE_DATE) {
|
2019-07-25 17:15:53 +00:00
|
|
|
self::assertSame($testData['value'], date('Y-m-d', $properties->getCustomPropertyValue($propertyName)));
|
2019-06-30 21:42:25 +00:00
|
|
|
} else {
|
2019-07-25 17:15:53 +00:00
|
|
|
self::assertSame($testData['value'], $properties->getCustomPropertyValue($propertyName));
|
2019-06-30 21:42:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-02-18 12:36:08 +00:00
|
|
|
}
|