PhpSpreadsheet/tests/PhpSpreadsheetTests/Cell/CoordinateTest.php

374 lines
11 KiB
PHP
Raw Normal View History

<?php
namespace PhpOffice\PhpSpreadsheetTests\Cell;
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use PhpOffice\PhpSpreadsheet\Exception;
use PHPUnit\Framework\TestCase;
2020-05-18 04:49:57 +00:00
use TypeError;
class CoordinateTest extends TestCase
{
/**
* @dataProvider providerColumnString
*
* @param mixed $expectedResult
* @param mixed $string
*/
2020-05-18 04:49:57 +00:00
public function testColumnIndexFromString($expectedResult, $string): void
2015-05-17 13:00:02 +00:00
{
$columnIndex = Coordinate::columnIndexFromString($string);
self::assertEquals($expectedResult, $columnIndex);
$stringBack = Coordinate::stringFromColumnIndex($columnIndex);
self::assertEquals($stringBack, $string, 'should be able to get the original input with opposite method');
2015-05-17 13:00:02 +00:00
}
public function providerColumnString()
{
return require 'tests/data/ColumnString.php';
2015-05-17 13:00:02 +00:00
}
2020-05-18 04:49:57 +00:00
public function testColumnIndexFromStringTooLong(): void
2015-05-17 13:00:02 +00:00
{
$cellAddress = 'ABCD';
2017-10-01 11:07:04 +00:00
2015-05-17 13:00:02 +00:00
try {
Coordinate::columnIndexFromString($cellAddress);
} catch (\Exception $e) {
self::assertInstanceOf(Exception::class, $e);
self::assertEquals($e->getMessage(), 'Column string index can not be longer than 3 characters');
2015-05-17 13:00:02 +00:00
return;
}
2020-05-18 04:49:57 +00:00
self::fail('An expected exception has not been raised.');
2015-05-17 13:00:02 +00:00
}
2020-05-18 04:49:57 +00:00
public function testColumnIndexFromStringTooShort(): void
2015-05-17 13:00:02 +00:00
{
$cellAddress = '';
2017-10-01 11:07:04 +00:00
2015-05-17 13:00:02 +00:00
try {
Coordinate::columnIndexFromString($cellAddress);
} catch (\Exception $e) {
self::assertInstanceOf(Exception::class, $e);
self::assertEquals($e->getMessage(), 'Column string index can not be empty');
2015-05-17 13:00:02 +00:00
return;
}
2020-05-18 04:49:57 +00:00
self::fail('An expected exception has not been raised.');
2015-05-17 13:00:02 +00:00
}
/**
* @dataProvider providerColumnIndex
*
* @param mixed $expectedResult
* @param int $columnIndex
*/
2020-05-18 04:49:57 +00:00
public function testStringFromColumnIndex($expectedResult, $columnIndex): void
2015-05-17 13:00:02 +00:00
{
$string = Coordinate::stringFromColumnIndex($columnIndex);
self::assertEquals($expectedResult, $string);
$columnIndexBack = Coordinate::columnIndexFromString($string);
self::assertEquals($columnIndexBack, $columnIndex, 'should be able to get the original input with opposite method');
2015-05-17 13:00:02 +00:00
}
public function providerColumnIndex()
{
return require 'tests/data/ColumnIndex.php';
2015-05-17 13:00:02 +00:00
}
/**
* @dataProvider providerCoordinates
*
* @param mixed $expectedResult
* @param string $rangeSet
*/
public function testCoordinateFromString($expectedResult, $rangeSet): void
2015-05-17 13:00:02 +00:00
{
$result = Coordinate::coordinateFromString($rangeSet);
self::assertEquals($expectedResult, $result);
2015-05-17 13:00:02 +00:00
}
public function providerCoordinates()
{
return require 'tests/data/CellCoordinates.php';
2015-05-17 13:00:02 +00:00
}
2020-05-18 04:49:57 +00:00
public function testCoordinateFromStringWithRangeAddress(): void
2015-05-17 13:00:02 +00:00
{
$cellAddress = 'A1:AI2012';
2017-10-01 11:07:04 +00:00
2015-05-17 13:00:02 +00:00
try {
Coordinate::coordinateFromString($cellAddress);
} catch (\Exception $e) {
self::assertInstanceOf(Exception::class, $e);
self::assertEquals($e->getMessage(), 'Cell coordinate string can not be a range of cells');
2015-05-17 13:00:02 +00:00
return;
}
2020-05-18 04:49:57 +00:00
self::fail('An expected exception has not been raised.');
2015-05-17 13:00:02 +00:00
}
2020-05-18 04:49:57 +00:00
public function testCoordinateFromStringWithEmptyAddress(): void
2015-05-17 13:00:02 +00:00
{
$cellAddress = '';
2017-10-01 11:07:04 +00:00
2015-05-17 13:00:02 +00:00
try {
Coordinate::coordinateFromString($cellAddress);
} catch (\Exception $e) {
self::assertInstanceOf(Exception::class, $e);
self::assertEquals($e->getMessage(), 'Cell coordinate can not be zero-length string');
2015-05-17 13:00:02 +00:00
return;
}
2020-05-18 04:49:57 +00:00
self::fail('An expected exception has not been raised.');
2015-05-17 13:00:02 +00:00
}
2020-05-18 04:49:57 +00:00
public function testCoordinateFromStringWithInvalidAddress(): void
2015-05-17 13:00:02 +00:00
{
$cellAddress = 'AI';
2017-10-01 11:07:04 +00:00
2015-05-17 13:00:02 +00:00
try {
Coordinate::coordinateFromString($cellAddress);
} catch (\Exception $e) {
self::assertInstanceOf(Exception::class, $e);
self::assertEquals($e->getMessage(), 'Invalid cell coordinate ' . $cellAddress);
2015-05-17 13:00:02 +00:00
return;
}
2020-05-18 04:49:57 +00:00
self::fail('An expected exception has not been raised.');
2015-05-17 13:00:02 +00:00
}
/**
* @dataProvider providerAbsoluteCoordinates
*
* @param string $expectedResult
* @param string $rangeSet
*/
public function testAbsoluteCoordinateFromString($expectedResult, $rangeSet): void
2015-05-17 13:00:02 +00:00
{
$result = Coordinate::absoluteCoordinate($rangeSet);
self::assertEquals($expectedResult, $result);
2015-05-17 13:00:02 +00:00
}
public function providerAbsoluteCoordinates()
{
return require 'tests/data/CellAbsoluteCoordinate.php';
2015-05-17 13:00:02 +00:00
}
2020-05-18 04:49:57 +00:00
public function testAbsoluteCoordinateFromStringWithRangeAddress(): void
2015-05-17 13:00:02 +00:00
{
$cellAddress = 'A1:AI2012';
2017-10-01 11:07:04 +00:00
2015-05-17 13:00:02 +00:00
try {
Coordinate::absoluteCoordinate($cellAddress);
} catch (\Exception $e) {
self::assertInstanceOf(Exception::class, $e);
self::assertEquals($e->getMessage(), 'Cell coordinate string can not be a range of cells');
2015-05-17 13:00:02 +00:00
return;
}
2020-05-18 04:49:57 +00:00
self::fail('An expected exception has not been raised.');
2015-05-17 13:00:02 +00:00
}
/**
* @dataProvider providerAbsoluteReferences
*
* @param mixed $expectedResult
* @param string $rangeSet
*/
public function testAbsoluteReferenceFromString($expectedResult, $rangeSet): void
2015-05-17 13:00:02 +00:00
{
$result = Coordinate::absoluteReference($rangeSet);
self::assertEquals($expectedResult, $result);
2015-05-17 13:00:02 +00:00
}
public function providerAbsoluteReferences()
{
return require 'tests/data/CellAbsoluteReference.php';
2015-05-17 13:00:02 +00:00
}
2020-05-18 04:49:57 +00:00
public function testAbsoluteReferenceFromStringWithRangeAddress(): void
2015-05-17 13:00:02 +00:00
{
$cellAddress = 'A1:AI2012';
2017-10-01 11:07:04 +00:00
2015-05-17 13:00:02 +00:00
try {
Coordinate::absoluteReference($cellAddress);
} catch (\Exception $e) {
self::assertInstanceOf(Exception::class, $e);
self::assertEquals($e->getMessage(), 'Cell coordinate string can not be a range of cells');
2015-05-17 13:00:02 +00:00
return;
}
2020-05-18 04:49:57 +00:00
self::fail('An expected exception has not been raised.');
2015-05-17 13:00:02 +00:00
}
/**
* @dataProvider providerSplitRange
*
* @param mixed $expectedResult
* @param string $rangeSet
*/
public function testSplitRange($expectedResult, $rangeSet): void
2015-05-17 13:00:02 +00:00
{
$result = Coordinate::splitRange($rangeSet);
2015-05-17 13:00:02 +00:00
foreach ($result as $key => $split) {
if (!is_array($expectedResult[$key])) {
self::assertEquals($expectedResult[$key], $split[0]);
2015-05-17 13:00:02 +00:00
} else {
self::assertEquals($expectedResult[$key], $split);
2015-05-17 13:00:02 +00:00
}
}
}
public function providerSplitRange()
{
return require 'tests/data/CellSplitRange.php';
2015-05-17 13:00:02 +00:00
}
/**
* @dataProvider providerBuildRange
*
* @param mixed $expectedResult
*/
2020-05-18 04:49:57 +00:00
public function testBuildRange($expectedResult, ...$args): void
2015-05-17 13:00:02 +00:00
{
$result = Coordinate::buildRange(...$args);
self::assertEquals($expectedResult, $result);
2015-05-17 13:00:02 +00:00
}
public function providerBuildRange()
{
return require 'tests/data/CellBuildRange.php';
2015-05-17 13:00:02 +00:00
}
2020-05-18 04:49:57 +00:00
public function testBuildRangeInvalid(): void
2015-05-17 13:00:02 +00:00
{
2020-05-18 04:49:57 +00:00
$this->expectException(TypeError::class);
2018-01-28 06:59:38 +00:00
$cellRange = '';
Coordinate::buildRange($cellRange);
2015-05-17 13:00:02 +00:00
}
/**
* @dataProvider providerRangeBoundaries
*
* @param mixed $expectedResult
* @param string $rangeSet
*/
public function testRangeBoundaries($expectedResult, $rangeSet): void
2015-05-17 13:00:02 +00:00
{
$result = Coordinate::rangeBoundaries($rangeSet);
self::assertEquals($expectedResult, $result);
2015-05-17 13:00:02 +00:00
}
public function providerRangeBoundaries()
{
return require 'tests/data/CellRangeBoundaries.php';
2015-05-17 13:00:02 +00:00
}
/**
* @dataProvider providerRangeDimension
*
* @param mixed $expectedResult
* @param string $rangeSet
*/
public function testRangeDimension($expectedResult, $rangeSet): void
2015-05-17 13:00:02 +00:00
{
$result = Coordinate::rangeDimension($rangeSet);
self::assertEquals($expectedResult, $result);
2015-05-17 13:00:02 +00:00
}
public function providerRangeDimension()
{
return require 'tests/data/CellRangeDimension.php';
2015-05-17 13:00:02 +00:00
}
/**
* @dataProvider providerGetRangeBoundaries
*
* @param mixed $expectedResult
* @param string $rangeSet
*/
public function testGetRangeBoundaries($expectedResult, $rangeSet): void
2015-05-17 13:00:02 +00:00
{
$result = Coordinate::getRangeBoundaries($rangeSet);
self::assertEquals($expectedResult, $result);
2015-05-17 13:00:02 +00:00
}
public function providerGetRangeBoundaries()
{
return require 'tests/data/CellGetRangeBoundaries.php';
2015-05-17 13:00:02 +00:00
}
/**
* @dataProvider providerExtractAllCellReferencesInRange
*
* @param array $expectedResult
* @param string $rangeSet
*/
public function testExtractAllCellReferencesInRange($expectedResult, $rangeSet): void
2015-05-17 13:00:02 +00:00
{
$result = Coordinate::extractAllCellReferencesInRange($rangeSet);
self::assertEquals($expectedResult, $result);
2015-05-17 13:00:02 +00:00
}
public function providerExtractAllCellReferencesInRange()
{
return require 'tests/data/CellExtractAllCellReferencesInRange.php';
2015-05-17 13:00:02 +00:00
}
/**
* @dataProvider providerInvalidRange
*
* @param string $range
*/
2020-05-18 04:49:57 +00:00
public function testExtractAllCellReferencesInRangeInvalidRange($range): void
{
$this->expectException(Exception::class);
$this->expectExceptionMessage('Invalid range: "' . $range . '"');
Coordinate::extractAllCellReferencesInRange($range);
}
public function providerInvalidRange()
{
return [['Z1:A1'], ['A4:A1'], ['B1:A1'], ['AA1:Z1']];
}
/**
* @dataProvider providerMergeRangesInCollection
*
* @param mixed $expectedResult
*/
2020-05-18 04:49:57 +00:00
public function testMergeRangesInCollection($expectedResult, ...$args): void
{
$result = Coordinate::mergeRangesInCollection(...$args);
self::assertEquals($expectedResult, $result);
}
public function providerMergeRangesInCollection()
{
return require 'tests/data/CellMergeRangesInCollection.php';
}
/**
* @dataProvider providerCoordinateIsRange
*
* @param mixed $expectedResult
* @param string $rangeSet
*/
public function testCoordinateIsRange($expectedResult, $rangeSet): void
{
$result = Coordinate::coordinateIsRange($rangeSet);
self::assertEquals($expectedResult, $result);
}
public function providerCoordinateIsRange()
{
return require 'tests/data/CoordinateIsRange.php';
}
}