PhpSpreadsheet/tests/PhpSpreadsheetTests/Cell/CoordinateTest.php

335 lines
9.3 KiB
PHP
Raw Normal View History

<?php
namespace PhpOffice\PhpSpreadsheetTests\Cell;
use PhpOffice\PhpSpreadsheet\Cell\Coordinate;
use PhpOffice\PhpSpreadsheet\Exception;
use PHPUnit\Framework\TestCase;
class CoordinateTest extends TestCase
{
/**
* @dataProvider providerColumnString
*
* @param mixed $expectedResult
* @param mixed $string
*/
public function testColumnIndexFromString($expectedResult, $string)
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 'data/ColumnString.php';
2015-05-17 13:00:02 +00:00
}
public function testColumnIndexFromStringTooLong()
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;
}
$this->fail('An expected exception has not been raised.');
}
public function testColumnIndexFromStringTooShort()
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;
}
$this->fail('An expected exception has not been raised.');
}
/**
* @dataProvider providerColumnIndex
*
* @param mixed $expectedResult
* @param int $columnIndex
*/
public function testStringFromColumnIndex($expectedResult, $columnIndex)
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 'data/ColumnIndex.php';
2015-05-17 13:00:02 +00:00
}
/**
* @dataProvider providerCoordinates
*
* @param mixed $expectedResult
*/
public function testCoordinateFromString($expectedResult, ...$args)
2015-05-17 13:00:02 +00:00
{
$result = Coordinate::coordinateFromString(...$args);
self::assertEquals($expectedResult, $result);
2015-05-17 13:00:02 +00:00
}
public function providerCoordinates()
{
return require 'data/CellCoordinates.php';
2015-05-17 13:00:02 +00:00
}
public function testCoordinateFromStringWithRangeAddress()
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;
}
$this->fail('An expected exception has not been raised.');
}
public function testCoordinateFromStringWithEmptyAddress()
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;
}
$this->fail('An expected exception has not been raised.');
}
public function testCoordinateFromStringWithInvalidAddress()
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;
}
$this->fail('An expected exception has not been raised.');
}
/**
* @dataProvider providerAbsoluteCoordinates
*
* @param mixed $expectedResult
*/
public function testAbsoluteCoordinateFromString($expectedResult, ...$args)
2015-05-17 13:00:02 +00:00
{
$result = Coordinate::absoluteCoordinate(...$args);
self::assertEquals($expectedResult, $result);
2015-05-17 13:00:02 +00:00
}
public function providerAbsoluteCoordinates()
{
return require 'data/CellAbsoluteCoordinate.php';
2015-05-17 13:00:02 +00:00
}
public function testAbsoluteCoordinateFromStringWithRangeAddress()
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;
}
$this->fail('An expected exception has not been raised.');
}
/**
* @dataProvider providerAbsoluteReferences
*
* @param mixed $expectedResult
*/
public function testAbsoluteReferenceFromString($expectedResult, ...$args)
2015-05-17 13:00:02 +00:00
{
$result = Coordinate::absoluteReference(...$args);
self::assertEquals($expectedResult, $result);
2015-05-17 13:00:02 +00:00
}
public function providerAbsoluteReferences()
{
return require 'data/CellAbsoluteReference.php';
2015-05-17 13:00:02 +00:00
}
public function testAbsoluteReferenceFromStringWithRangeAddress()
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;
}
$this->fail('An expected exception has not been raised.');
}
/**
* @dataProvider providerSplitRange
*
* @param mixed $expectedResult
*/
public function testSplitRange($expectedResult, ...$args)
2015-05-17 13:00:02 +00:00
{
$result = Coordinate::splitRange(...$args);
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 'data/CellSplitRange.php';
2015-05-17 13:00:02 +00:00
}
/**
* @dataProvider providerBuildRange
*
* @param mixed $expectedResult
*/
public function testBuildRange($expectedResult, ...$args)
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 'data/CellBuildRange.php';
2015-05-17 13:00:02 +00:00
}
/**
* @expectedException \TypeError
*/
public function testBuildRangeInvalid()
2015-05-17 13:00:02 +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
}
$cellRange = '';
Coordinate::buildRange($cellRange);
2015-05-17 13:00:02 +00:00
}
/**
* @dataProvider providerRangeBoundaries
*
* @param mixed $expectedResult
*/
public function testRangeBoundaries($expectedResult, ...$args)
2015-05-17 13:00:02 +00:00
{
$result = Coordinate::rangeBoundaries(...$args);
self::assertEquals($expectedResult, $result);
2015-05-17 13:00:02 +00:00
}
public function providerRangeBoundaries()
{
return require 'data/CellRangeBoundaries.php';
2015-05-17 13:00:02 +00:00
}
/**
* @dataProvider providerRangeDimension
*
* @param mixed $expectedResult
*/
public function testRangeDimension($expectedResult, ...$args)
2015-05-17 13:00:02 +00:00
{
$result = Coordinate::rangeDimension(...$args);
self::assertEquals($expectedResult, $result);
2015-05-17 13:00:02 +00:00
}
public function providerRangeDimension()
{
return require 'data/CellRangeDimension.php';
2015-05-17 13:00:02 +00:00
}
/**
* @dataProvider providerGetRangeBoundaries
*
* @param mixed $expectedResult
*/
public function testGetRangeBoundaries($expectedResult, ...$args)
2015-05-17 13:00:02 +00:00
{
$result = Coordinate::getRangeBoundaries(...$args);
self::assertEquals($expectedResult, $result);
2015-05-17 13:00:02 +00:00
}
public function providerGetRangeBoundaries()
{
return require 'data/CellGetRangeBoundaries.php';
2015-05-17 13:00:02 +00:00
}
/**
* @dataProvider providerExtractAllCellReferencesInRange
*
* @param mixed $expectedResult
*/
public function testExtractAllCellReferencesInRange($expectedResult, ...$args)
2015-05-17 13:00:02 +00:00
{
$result = Coordinate::extractAllCellReferencesInRange(...$args);
self::assertEquals($expectedResult, $result);
2015-05-17 13:00:02 +00:00
}
public function providerExtractAllCellReferencesInRange()
{
return require 'data/CellExtractAllCellReferencesInRange.php';
2015-05-17 13:00:02 +00:00
}
/**
* @dataProvider providerMergeRangesInCollection
*
* @param mixed $expectedResult
*/
public function testMergeRangesInCollection($expectedResult, ...$args)
{
$result = Coordinate::mergeRangesInCollection(...$args);
self::assertEquals($expectedResult, $result);
}
public function providerMergeRangesInCollection()
{
return require 'data/CellMergeRangesInCollection.php';
}
}