More error detection for range to column

This commit is contained in:
Buddhism8003 2024-10-18 17:04:04 +02:00
parent 777ed5c746
commit 6f4fffc305
2 changed files with 19 additions and 1 deletions

View File

@ -10,7 +10,12 @@ class ConverterHelpers{
*/ */
public static function RangeToColumnArray(string $range): array{ public static function RangeToColumnArray(string $range): array{
$rangeArray = explode("-",$range); $rangeArray = explode("-",$range);
if(count($rangeArray) !== 2){ if(count($rangeArray) !== 2
|| strlen($rangeArray[0]) === 0
|| strlen($rangeArray[1]) === 0
|| !(strlen($rangeArray[0]) < strlen($rangeArray[1])
|| (strlen($rangeArray[0]) === strlen($rangeArray[1])
&& strcmp($rangeArray[0], $rangeArray[1]) <= 0))){
throw new MalformattedRangeStringException(); throw new MalformattedRangeStringException();
} }

View File

@ -1,5 +1,6 @@
<?php <?php
use CSO\Excel2Html\ConverterHelpers; use CSO\Excel2Html\ConverterHelpers;
use CSO\Excel2Html\Exceptions\MalformattedRangeStringException;
use PHPUnit\Framework\TestCase; use PHPUnit\Framework\TestCase;
final class ConverterHelpersTest extends TestCase{ final class ConverterHelpersTest extends TestCase{
@ -8,4 +9,16 @@ final class ConverterHelpersTest extends TestCase{
$res = implode("|", $columns); $res = implode("|", $columns);
$this->assertSame('Y|Z|AA|AB|AC', $res); $this->assertSame('Y|Z|AA|AB|AC', $res);
} }
public function testColumnRangeErrorOnWrongFormat(): void{
$this->expectException(MalformattedRangeStringException::class);
$columns = ConverterHelpers::RangeToColumnArray("-AC");
}
public function testColumnRangeErrorOnTwoLetterColumnFirst(): void{
$this->expectException(MalformattedRangeStringException::class);
$columns = ConverterHelpers::RangeToColumnArray("AC-Y");
}
public function testColumnRangeErrorOnHigherColumnFirst(): void{
$this->expectException(MalformattedRangeStringException::class);
$columns = ConverterHelpers::RangeToColumnArray("Z-B");
}
} }