PhpSpreadsheet/tests/PhpSpreadsheetTests/CellTest.php

304 lines
8.3 KiB
PHP
Raw Normal View History

<?php
namespace PhpOffice\PhpSpreadsheetTests;
use PhpOffice\PhpSpreadsheet\Cell;
use PhpOffice\PhpSpreadsheet\Exception;
use PHPUnit_Framework_TestCase;
class CellTest extends PHPUnit_Framework_TestCase
{
/**
* @dataProvider providerColumnString
*
* @param mixed $expectedResult
*/
public function testColumnIndexFromString($expectedResult, ...$args)
2015-05-17 13:00:02 +00:00
{
$result = Cell::columnIndexFromString(...$args);
2015-05-17 13:00:02 +00:00
$this->assertEquals($expectedResult, $result);
}
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';
try {
Cell::columnIndexFromString($cellAddress);
} catch (\Exception $e) {
$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');
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 = '';
try {
Cell::columnIndexFromString($cellAddress);
} catch (\Exception $e) {
$this->assertInstanceOf(Exception::class, $e);
2015-05-17 13:00:02 +00:00
$this->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
*/
public function testStringFromColumnIndex($expectedResult, ...$args)
2015-05-17 13:00:02 +00:00
{
$result = Cell::stringFromColumnIndex(...$args);
2015-05-17 13:00:02 +00:00
$this->assertEquals($expectedResult, $result);
}
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 = Cell::coordinateFromString(...$args);
2015-05-17 13:00:02 +00:00
$this->assertEquals($expectedResult, $result);
}
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';
try {
Cell::coordinateFromString($cellAddress);
} catch (\Exception $e) {
$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');
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 = '';
try {
Cell::coordinateFromString($cellAddress);
} catch (\Exception $e) {
$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');
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';
try {
Cell::coordinateFromString($cellAddress);
} catch (\Exception $e) {
$this->assertInstanceOf(Exception::class, $e);
$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.');
}
/**
* @dataProvider providerAbsoluteCoordinates
*
* @param mixed $expectedResult
*/
public function testAbsoluteCoordinateFromString($expectedResult, ...$args)
2015-05-17 13:00:02 +00:00
{
$result = Cell::absoluteCoordinate(...$args);
2015-05-17 13:00:02 +00:00
$this->assertEquals($expectedResult, $result);
}
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';
try {
Cell::absoluteCoordinate($cellAddress);
} catch (\Exception $e) {
$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');
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 = Cell::absoluteReference(...$args);
2015-05-17 13:00:02 +00:00
$this->assertEquals($expectedResult, $result);
}
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';
try {
Cell::absoluteReference($cellAddress);
} catch (\Exception $e) {
$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');
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 = 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);
}
}
}
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 = Cell::buildRange(...$args);
2015-05-17 13:00:02 +00:00
$this->assertEquals($expectedResult, $result);
}
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 = '';
Cell::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 = Cell::rangeBoundaries(...$args);
2015-05-17 13:00:02 +00:00
$this->assertEquals($expectedResult, $result);
}
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 = Cell::rangeDimension(...$args);
2015-05-17 13:00:02 +00:00
$this->assertEquals($expectedResult, $result);
}
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 = Cell::getRangeBoundaries(...$args);
2015-05-17 13:00:02 +00:00
$this->assertEquals($expectedResult, $result);
}
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 = Cell::extractAllCellReferencesInRange(...$args);
2015-05-17 13:00:02 +00:00
$this->assertEquals($expectedResult, $result);
}
public function providerExtractAllCellReferencesInRange()
{
return require 'data/CellExtractAllCellReferencesInRange.php';
2015-05-17 13:00:02 +00:00
}
}