2012-07-29 19:56:35 +00:00
|
|
|
<?php
|
|
|
|
|
2016-08-31 17:18:12 +00:00
|
|
|
namespace PhpOffice\PhpSpreadsheetTests;
|
2016-08-14 04:08:43 +00:00
|
|
|
|
2016-08-31 17:18:12 +00:00
|
|
|
use PhpOffice\PhpSpreadsheet\Cell;
|
|
|
|
use PhpOffice\PhpSpreadsheet\Exception;
|
2015-05-29 20:35:35 +00:00
|
|
|
|
|
|
|
class CellTest extends \PHPUnit_Framework_TestCase
|
2012-07-29 19:56:35 +00:00
|
|
|
{
|
|
|
|
/**
|
|
|
|
* @dataProvider providerColumnString
|
2017-01-23 05:49:10 +00:00
|
|
|
*
|
|
|
|
* @param mixed $expectedResult
|
2012-07-29 19:56:35 +00:00
|
|
|
*/
|
2017-01-23 05:49:10 +00:00
|
|
|
public function testColumnIndexFromString($expectedResult, ...$args)
|
2015-05-17 13:00:02 +00:00
|
|
|
{
|
2017-01-23 05:49:10 +00:00
|
|
|
$result = Cell::columnIndexFromString(...$args);
|
2015-05-17 13:00:02 +00:00
|
|
|
$this->assertEquals($expectedResult, $result);
|
|
|
|
}
|
2012-07-29 19:56:35 +00:00
|
|
|
|
|
|
|
public function providerColumnString()
|
|
|
|
{
|
2016-08-16 12:00:19 +00:00
|
|
|
return require 'data/ColumnString.php';
|
2015-05-17 13:00:02 +00:00
|
|
|
}
|
2012-07-29 19:56:35 +00:00
|
|
|
|
|
|
|
public function testColumnIndexFromStringTooLong()
|
2015-05-17 13:00:02 +00:00
|
|
|
{
|
|
|
|
$cellAddress = 'ABCD';
|
|
|
|
try {
|
2016-08-26 06:39:29 +00:00
|
|
|
Cell::columnIndexFromString($cellAddress);
|
2015-05-29 20:35:35 +00:00
|
|
|
} catch (\Exception $e) {
|
2016-08-14 04:08:43 +00:00
|
|
|
$this->assertInstanceOf(Exception::class, $e);
|
2015-05-17 13:00:02 +00:00
|
|
|
$this->assertEquals($e->getMessage(), 'Column string index can not be longer than 3 characters');
|
2016-08-16 15:33:57 +00:00
|
|
|
|
2015-05-17 13:00:02 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
$this->fail('An expected exception has not been raised.');
|
|
|
|
}
|
2012-07-29 19:56:35 +00:00
|
|
|
|
|
|
|
public function testColumnIndexFromStringTooShort()
|
2015-05-17 13:00:02 +00:00
|
|
|
{
|
|
|
|
$cellAddress = '';
|
|
|
|
try {
|
2016-08-26 06:39:29 +00:00
|
|
|
Cell::columnIndexFromString($cellAddress);
|
2015-05-29 20:35:35 +00:00
|
|
|
} catch (\Exception $e) {
|
2016-08-14 04:08:43 +00:00
|
|
|
$this->assertInstanceOf(Exception::class, $e);
|
2015-05-17 13:00:02 +00:00
|
|
|
$this->assertEquals($e->getMessage(), 'Column string index can not be empty');
|
2016-08-16 15:33:57 +00:00
|
|
|
|
2015-05-17 13:00:02 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
$this->fail('An expected exception has not been raised.');
|
|
|
|
}
|
2012-07-29 19:56:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider providerColumnIndex
|
2017-01-23 05:49:10 +00:00
|
|
|
*
|
|
|
|
* @param mixed $expectedResult
|
2012-07-29 19:56:35 +00:00
|
|
|
*/
|
2017-01-23 05:49:10 +00:00
|
|
|
public function testStringFromColumnIndex($expectedResult, ...$args)
|
2015-05-17 13:00:02 +00:00
|
|
|
{
|
2017-01-23 05:49:10 +00:00
|
|
|
$result = Cell::stringFromColumnIndex(...$args);
|
2015-05-17 13:00:02 +00:00
|
|
|
$this->assertEquals($expectedResult, $result);
|
|
|
|
}
|
2012-07-29 19:56:35 +00:00
|
|
|
|
|
|
|
public function providerColumnIndex()
|
|
|
|
{
|
2016-08-16 12:00:19 +00:00
|
|
|
return require 'data/ColumnIndex.php';
|
2015-05-17 13:00:02 +00:00
|
|
|
}
|
2012-07-29 19:56:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider providerCoordinates
|
2017-01-23 05:49:10 +00:00
|
|
|
*
|
|
|
|
* @param mixed $expectedResult
|
2012-07-29 19:56:35 +00:00
|
|
|
*/
|
2017-01-23 05:49:10 +00:00
|
|
|
public function testCoordinateFromString($expectedResult, ...$args)
|
2015-05-17 13:00:02 +00:00
|
|
|
{
|
2017-01-23 05:49:10 +00:00
|
|
|
$result = Cell::coordinateFromString(...$args);
|
2015-05-17 13:00:02 +00:00
|
|
|
$this->assertEquals($expectedResult, $result);
|
|
|
|
}
|
2012-07-29 19:56:35 +00:00
|
|
|
|
|
|
|
public function providerCoordinates()
|
|
|
|
{
|
2016-08-16 12:00:19 +00:00
|
|
|
return require 'data/CellCoordinates.php';
|
2015-05-17 13:00:02 +00:00
|
|
|
}
|
2012-07-29 19:56:35 +00:00
|
|
|
|
|
|
|
public function testCoordinateFromStringWithRangeAddress()
|
2015-05-17 13:00:02 +00:00
|
|
|
{
|
|
|
|
$cellAddress = 'A1:AI2012';
|
|
|
|
try {
|
2016-08-26 06:39:29 +00:00
|
|
|
Cell::coordinateFromString($cellAddress);
|
2015-05-29 20:35:35 +00:00
|
|
|
} catch (\Exception $e) {
|
2016-08-14 04:08:43 +00:00
|
|
|
$this->assertInstanceOf(Exception::class, $e);
|
2015-05-17 13:00:02 +00:00
|
|
|
$this->assertEquals($e->getMessage(), 'Cell coordinate string can not be a range of cells');
|
2016-08-16 15:33:57 +00:00
|
|
|
|
2015-05-17 13:00:02 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
$this->fail('An expected exception has not been raised.');
|
|
|
|
}
|
2012-07-29 19:56:35 +00:00
|
|
|
|
|
|
|
public function testCoordinateFromStringWithEmptyAddress()
|
2015-05-17 13:00:02 +00:00
|
|
|
{
|
|
|
|
$cellAddress = '';
|
|
|
|
try {
|
2016-08-26 06:39:29 +00:00
|
|
|
Cell::coordinateFromString($cellAddress);
|
2015-05-29 20:35:35 +00:00
|
|
|
} catch (\Exception $e) {
|
2016-08-14 04:08:43 +00:00
|
|
|
$this->assertInstanceOf(Exception::class, $e);
|
2015-05-17 13:00:02 +00:00
|
|
|
$this->assertEquals($e->getMessage(), 'Cell coordinate can not be zero-length string');
|
2016-08-16 15:33:57 +00:00
|
|
|
|
2015-05-17 13:00:02 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
$this->fail('An expected exception has not been raised.');
|
|
|
|
}
|
2012-07-29 19:56:35 +00:00
|
|
|
|
|
|
|
public function testCoordinateFromStringWithInvalidAddress()
|
2015-05-17 13:00:02 +00:00
|
|
|
{
|
|
|
|
$cellAddress = 'AI';
|
|
|
|
try {
|
2016-08-26 06:39:29 +00:00
|
|
|
Cell::coordinateFromString($cellAddress);
|
2015-05-29 20:35:35 +00:00
|
|
|
} catch (\Exception $e) {
|
2016-08-14 04:08:43 +00:00
|
|
|
$this->assertInstanceOf(Exception::class, $e);
|
2016-08-16 15:33:57 +00:00
|
|
|
$this->assertEquals($e->getMessage(), 'Invalid cell coordinate ' . $cellAddress);
|
|
|
|
|
2015-05-17 13:00:02 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
$this->fail('An expected exception has not been raised.');
|
|
|
|
}
|
2012-07-29 19:56:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider providerAbsoluteCoordinates
|
2017-01-23 05:49:10 +00:00
|
|
|
*
|
|
|
|
* @param mixed $expectedResult
|
2012-07-29 19:56:35 +00:00
|
|
|
*/
|
2017-01-23 05:49:10 +00:00
|
|
|
public function testAbsoluteCoordinateFromString($expectedResult, ...$args)
|
2015-05-17 13:00:02 +00:00
|
|
|
{
|
2017-01-23 05:49:10 +00:00
|
|
|
$result = Cell::absoluteCoordinate(...$args);
|
2015-05-17 13:00:02 +00:00
|
|
|
$this->assertEquals($expectedResult, $result);
|
|
|
|
}
|
2012-07-29 19:56:35 +00:00
|
|
|
|
|
|
|
public function providerAbsoluteCoordinates()
|
|
|
|
{
|
2016-08-16 12:00:19 +00:00
|
|
|
return require 'data/CellAbsoluteCoordinate.php';
|
2015-05-17 13:00:02 +00:00
|
|
|
}
|
2012-07-29 19:56:35 +00:00
|
|
|
|
|
|
|
public function testAbsoluteCoordinateFromStringWithRangeAddress()
|
2015-05-17 13:00:02 +00:00
|
|
|
{
|
|
|
|
$cellAddress = 'A1:AI2012';
|
|
|
|
try {
|
2016-08-26 06:39:29 +00:00
|
|
|
Cell::absoluteCoordinate($cellAddress);
|
2015-05-29 20:35:35 +00:00
|
|
|
} catch (\Exception $e) {
|
2016-08-14 04:08:43 +00:00
|
|
|
$this->assertInstanceOf(Exception::class, $e);
|
2015-05-17 13:00:02 +00:00
|
|
|
$this->assertEquals($e->getMessage(), 'Cell coordinate string can not be a range of cells');
|
2016-08-16 15:33:57 +00:00
|
|
|
|
2015-05-17 13:00:02 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
$this->fail('An expected exception has not been raised.');
|
|
|
|
}
|
2012-07-29 19:56:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider providerAbsoluteReferences
|
2017-01-23 05:49:10 +00:00
|
|
|
*
|
|
|
|
* @param mixed $expectedResult
|
2012-07-29 19:56:35 +00:00
|
|
|
*/
|
2017-01-23 05:49:10 +00:00
|
|
|
public function testAbsoluteReferenceFromString($expectedResult, ...$args)
|
2015-05-17 13:00:02 +00:00
|
|
|
{
|
2017-01-23 05:49:10 +00:00
|
|
|
$result = Cell::absoluteReference(...$args);
|
2015-05-17 13:00:02 +00:00
|
|
|
$this->assertEquals($expectedResult, $result);
|
|
|
|
}
|
2012-07-29 19:56:35 +00:00
|
|
|
|
|
|
|
public function providerAbsoluteReferences()
|
|
|
|
{
|
2016-08-16 12:00:19 +00:00
|
|
|
return require 'data/CellAbsoluteReference.php';
|
2015-05-17 13:00:02 +00:00
|
|
|
}
|
2012-07-29 19:56:35 +00:00
|
|
|
|
|
|
|
public function testAbsoluteReferenceFromStringWithRangeAddress()
|
2015-05-17 13:00:02 +00:00
|
|
|
{
|
|
|
|
$cellAddress = 'A1:AI2012';
|
|
|
|
try {
|
2016-08-26 06:39:29 +00:00
|
|
|
Cell::absoluteReference($cellAddress);
|
2015-05-29 20:35:35 +00:00
|
|
|
} catch (\Exception $e) {
|
2016-08-14 04:08:43 +00:00
|
|
|
$this->assertInstanceOf(Exception::class, $e);
|
2015-05-17 13:00:02 +00:00
|
|
|
$this->assertEquals($e->getMessage(), 'Cell coordinate string can not be a range of cells');
|
2016-08-16 15:33:57 +00:00
|
|
|
|
2015-05-17 13:00:02 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
$this->fail('An expected exception has not been raised.');
|
|
|
|
}
|
2012-07-29 19:56:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider providerSplitRange
|
2017-01-23 05:49:10 +00:00
|
|
|
*
|
|
|
|
* @param mixed $expectedResult
|
2012-07-29 19:56:35 +00:00
|
|
|
*/
|
2017-01-23 05:49:10 +00:00
|
|
|
public function testSplitRange($expectedResult, ...$args)
|
2015-05-17 13:00:02 +00:00
|
|
|
{
|
2017-01-23 05:49:10 +00:00
|
|
|
$result = Cell::splitRange(...$args);
|
2015-05-17 13:00:02 +00:00
|
|
|
foreach ($result as $key => $split) {
|
|
|
|
if (!is_array($expectedResult[$key])) {
|
|
|
|
$this->assertEquals($expectedResult[$key], $split[0]);
|
|
|
|
} else {
|
|
|
|
$this->assertEquals($expectedResult[$key], $split);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-07-29 19:56:35 +00:00
|
|
|
|
|
|
|
public function providerSplitRange()
|
|
|
|
{
|
2016-08-16 12:00:19 +00:00
|
|
|
return require 'data/CellSplitRange.php';
|
2015-05-17 13:00:02 +00:00
|
|
|
}
|
2012-07-29 19:56:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider providerBuildRange
|
2017-01-23 05:49:10 +00:00
|
|
|
*
|
|
|
|
* @param mixed $expectedResult
|
2012-07-29 19:56:35 +00:00
|
|
|
*/
|
2017-01-23 05:49:10 +00:00
|
|
|
public function testBuildRange($expectedResult, ...$args)
|
2015-05-17 13:00:02 +00:00
|
|
|
{
|
2017-01-23 05:49:10 +00:00
|
|
|
$result = Cell::buildRange(...$args);
|
2015-05-17 13:00:02 +00:00
|
|
|
$this->assertEquals($expectedResult, $result);
|
|
|
|
}
|
2012-07-29 19:56:35 +00:00
|
|
|
|
|
|
|
public function providerBuildRange()
|
|
|
|
{
|
2016-08-16 12:00:19 +00:00
|
|
|
return require 'data/CellBuildRange.php';
|
2015-05-17 13:00:02 +00:00
|
|
|
}
|
2012-07-29 19:56:35 +00:00
|
|
|
|
2017-04-16 04:15:04 +00:00
|
|
|
/**
|
|
|
|
* @expectedException \TypeError
|
|
|
|
*/
|
2012-07-29 19:56:35 +00:00
|
|
|
public function testBuildRangeInvalid()
|
2015-05-17 13:00:02 +00:00
|
|
|
{
|
2017-04-16 04:15:04 +00:00
|
|
|
if (PHP_MAJOR_VERSION < 7) {
|
|
|
|
$this->markTestSkipped('Cannot catch type hinting error with PHP 5.6');
|
2015-05-17 13:00:02 +00:00
|
|
|
}
|
2017-04-16 04:15:04 +00:00
|
|
|
|
|
|
|
$cellRange = '';
|
|
|
|
Cell::buildRange($cellRange);
|
2015-05-17 13:00:02 +00:00
|
|
|
}
|
2012-07-29 19:56:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider providerRangeBoundaries
|
2017-01-23 05:49:10 +00:00
|
|
|
*
|
|
|
|
* @param mixed $expectedResult
|
2012-07-29 19:56:35 +00:00
|
|
|
*/
|
2017-01-23 05:49:10 +00:00
|
|
|
public function testRangeBoundaries($expectedResult, ...$args)
|
2015-05-17 13:00:02 +00:00
|
|
|
{
|
2017-01-23 05:49:10 +00:00
|
|
|
$result = Cell::rangeBoundaries(...$args);
|
2015-05-17 13:00:02 +00:00
|
|
|
$this->assertEquals($expectedResult, $result);
|
|
|
|
}
|
2012-07-29 19:56:35 +00:00
|
|
|
|
|
|
|
public function providerRangeBoundaries()
|
|
|
|
{
|
2016-08-16 12:00:19 +00:00
|
|
|
return require 'data/CellRangeBoundaries.php';
|
2015-05-17 13:00:02 +00:00
|
|
|
}
|
2012-07-29 19:56:35 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider providerRangeDimension
|
2017-01-23 05:49:10 +00:00
|
|
|
*
|
|
|
|
* @param mixed $expectedResult
|
2012-07-29 19:56:35 +00:00
|
|
|
*/
|
2017-01-23 05:49:10 +00:00
|
|
|
public function testRangeDimension($expectedResult, ...$args)
|
2015-05-17 13:00:02 +00:00
|
|
|
{
|
2017-01-23 05:49:10 +00:00
|
|
|
$result = Cell::rangeDimension(...$args);
|
2015-05-17 13:00:02 +00:00
|
|
|
$this->assertEquals($expectedResult, $result);
|
|
|
|
}
|
2012-07-29 19:56:35 +00:00
|
|
|
|
|
|
|
public function providerRangeDimension()
|
|
|
|
{
|
2016-08-16 12:00:19 +00:00
|
|
|
return require 'data/CellRangeDimension.php';
|
2015-05-17 13:00:02 +00:00
|
|
|
}
|
2012-07-29 19:56:35 +00:00
|
|
|
|
2012-07-31 12:00:09 +00:00
|
|
|
/**
|
|
|
|
* @dataProvider providerGetRangeBoundaries
|
2017-01-23 05:49:10 +00:00
|
|
|
*
|
|
|
|
* @param mixed $expectedResult
|
2012-07-31 12:00:09 +00:00
|
|
|
*/
|
2017-01-23 05:49:10 +00:00
|
|
|
public function testGetRangeBoundaries($expectedResult, ...$args)
|
2015-05-17 13:00:02 +00:00
|
|
|
{
|
2017-01-23 05:49:10 +00:00
|
|
|
$result = Cell::getRangeBoundaries(...$args);
|
2015-05-17 13:00:02 +00:00
|
|
|
$this->assertEquals($expectedResult, $result);
|
|
|
|
}
|
2012-07-31 12:00:09 +00:00
|
|
|
|
|
|
|
public function providerGetRangeBoundaries()
|
|
|
|
{
|
2016-08-16 12:00:19 +00:00
|
|
|
return require 'data/CellGetRangeBoundaries.php';
|
2015-05-17 13:00:02 +00:00
|
|
|
}
|
2012-07-31 12:00:09 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider providerExtractAllCellReferencesInRange
|
2017-01-23 05:49:10 +00:00
|
|
|
*
|
|
|
|
* @param mixed $expectedResult
|
2012-07-31 12:00:09 +00:00
|
|
|
*/
|
2017-01-23 05:49:10 +00:00
|
|
|
public function testExtractAllCellReferencesInRange($expectedResult, ...$args)
|
2015-05-17 13:00:02 +00:00
|
|
|
{
|
2017-01-23 05:49:10 +00:00
|
|
|
$result = Cell::extractAllCellReferencesInRange(...$args);
|
2015-05-17 13:00:02 +00:00
|
|
|
$this->assertEquals($expectedResult, $result);
|
|
|
|
}
|
2012-07-31 12:00:09 +00:00
|
|
|
|
|
|
|
public function providerExtractAllCellReferencesInRange()
|
|
|
|
{
|
2016-08-16 12:00:19 +00:00
|
|
|
return require 'data/CellExtractAllCellReferencesInRange.php';
|
2015-05-17 13:00:02 +00:00
|
|
|
}
|
2012-07-29 19:56:35 +00:00
|
|
|
}
|