Skip to content

Commit

Permalink
change validate and add tests data
Browse files Browse the repository at this point in the history
  • Loading branch information
AdrianBatista committed Nov 12, 2023
1 parent 8c23039 commit 419e17d
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 52 deletions.
36 changes: 13 additions & 23 deletions src/PhpSpreadsheet/Cell/Coordinate.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

use PhpOffice\PhpSpreadsheet\Exception;
use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet;
use Throwable;

/**
* Helper class to manipulate cell coordinates.
Expand All @@ -15,6 +14,7 @@
abstract class Coordinate
{
public const A1_COORDINATE_REGEX = '/^(?<col>\$?[A-Z]{1,3})(?<row>\$?\d{1,7})$/i';
public const FULL_REFERENCE_REGEX = '/^(?:(?<worksheet>[^!]*)!)?(?<localReference>(?<firstCoordinate>[$]?[A-Z]{1,3}[$]?\d{1,7})(?:\:(?<secondCoordinate>[$]?[A-Z]{1,3}[$]?\d{1,7}))?)$/i';

/**
* Default range variable constant.
Expand Down Expand Up @@ -269,24 +269,24 @@ public static function getRangeBoundaries(string $range)
*/
private static function validateReferenceAndGetData($reference): array
{
preg_match('/^(?:(?<worksheet>[^!]*)!)?(?<localReference>(?<firstCoordinate>[A-Z]{1,3}\d{1,7})(?:\:(?<secondCoordinate>[A-Z]{1,3}\d{1,7}))?)$/', $reference, $matches);
preg_match(self::FULL_REFERENCE_REGEX, $reference, $matches);
if (count($matches) === 0) {
throw new Exception('Invalid Cell or Range Reference');
return ['type' => 'invalid'];
}

if (isset($matches['secondCoordinate'])) {
$data['type'] = 'range';
$data['firstCoordinate'] = $matches['firstCoordinate'];
$data['secondCoordinate'] = $matches['secondCoordinate'];
$data['firstCoordinate'] = str_replace('$', '', $matches['firstCoordinate']);
$data['secondCoordinate'] = str_replace('$', '', $matches['secondCoordinate']);
} else {
$data['coordinate'] = $matches['firstCoordinate'];
$data['type'] = 'coordinate';
$data['coordinate'] = str_replace('$', '', $matches['firstCoordinate']);
}

if ($matches['worksheet'] !== '') {
$data['worksheet'] = $matches['worksheet'];
$data['worksheet'] = strtolower(str_replace('\'', '', $matches['worksheet']));
}
$data['localReference'] = $matches['localReference'];
$data['localReference'] = str_replace('$', '', $matches['localReference']);

return $data;
}
Expand All @@ -301,21 +301,13 @@ private static function validateReferenceAndGetData($reference): array
*/
public static function coordinateIsInsideRange(string $range, string $coordinate): bool
{
try {
$rangeData = self::validateReferenceAndGetData($range);
if ($rangeData['type'] !== 'range') {
throw new Exception();
}
} catch (Throwable $th) {
$rangeData = self::validateReferenceAndGetData($range);
if ($rangeData['type'] === 'invalid') {
throw new Exception('First argument needs to be a range');
}

try {
$coordinateData = self::validateReferenceAndGetData($coordinate);
if ($coordinateData['type'] !== 'coordinate') {
throw new Exception();
}
} catch (Throwable $th) {
$coordinateData = self::validateReferenceAndGetData($coordinate);
if ($coordinateData['type'] === 'invalid') {
throw new Exception('Second argument needs to be a single coordinate');
}

Expand All @@ -336,9 +328,7 @@ public static function coordinateIsInsideRange(string $range, string $coordinate
}

$boundaries = self::rangeBoundaries($range);

$coordinates = self::coordinateFromString($coordinate);
$coordinates[0] = self::columnIndexFromString($coordinates[0]);
$coordinates = self::indexesFromString($coordinate);

$columnIsInside = $boundaries[0][0] <= $coordinates[0] && $coordinates[0] <= $boundaries[1][0];
if (!$columnIsInside) {
Expand Down
45 changes: 16 additions & 29 deletions tests/PhpSpreadsheetTests/Cell/CoordinateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -300,52 +300,39 @@ public static function providerGetRangeBoundaries(): array
return require 'tests/data/CellGetRangeBoundaries.php';
}

public static function testCoordinateIsInsideRange(): void
/**
* @dataProvider providerCoordinateIsInsideRange
*/
public static function testCoordinateIsInsideRange(bool $expectedResult, string $range, string $coordinate): void
{
$cellRange = 'A1:D4';
$cellCordinate = 'B2';
$result = Coordinate::coordinateIsInsideRange($cellRange, $cellCordinate);
self::assertTrue($result);
$result = Coordinate::coordinateIsInsideRange($range, $coordinate);
self::assertEquals($result, $expectedResult);
}

public static function testCoordinateIsOutsideRange(): void
public static function providerCoordinateIsInsideRange(): array
{
$cellRange = 'A1:D4';
$cellCordinate = 'F6';
$result = Coordinate::coordinateIsInsideRange($cellRange, $cellCordinate);
self::assertFalse($result);
return require 'tests/data/coordinateIsInsideRange.php';

Check warning on line 314 in tests/PhpSpreadsheetTests/Cell/CoordinateTest.php

View workflow job for this annotation

GitHub Actions / PHP 8.0

require(tests/data/coordinateIsInsideRange.php): Failed to open stream: No such file or directory

Check warning on line 314 in tests/PhpSpreadsheetTests/Cell/CoordinateTest.php

View workflow job for this annotation

GitHub Actions / PHP 8.1

require(tests/data/coordinateIsInsideRange.php): Failed to open stream: No such file or directory

Check warning on line 314 in tests/PhpSpreadsheetTests/Cell/CoordinateTest.php

View workflow job for this annotation

GitHub Actions / PHP 8.2

require(tests/data/coordinateIsInsideRange.php): Failed to open stream: No such file or directory

Check warning on line 314 in tests/PhpSpreadsheetTests/Cell/CoordinateTest.php

View workflow job for this annotation

GitHub Actions / PHP nightly

require(tests/data/coordinateIsInsideRange.php): Failed to open stream: No such file or directory
}

public static function testCoordinateIsInsideRangeInvalidFirstArgument(): void
/**
* @dataProvider providerCoordinateIsInsideRangeException
*/
public static function testCoordinateIsInsideRangeException(string $expectedResult, string $range, string $coordinate): void
{
$cellRange = 'inavlidRange';
$cellCordinate = 'F6';

try {
Coordinate::coordinateIsInsideRange($cellRange, $cellCordinate);
Coordinate::coordinateIsInsideRange($range, $coordinate);
} catch (\Exception $e) {
self::assertInstanceOf(Exception::class, $e);
self::assertEquals($e->getMessage(), 'First argument needs to be a range');
self::assertEquals($e->getMessage(), $expectedResult);

return;
}
self::fail('An expected exception has not been raised.');
}

public static function testCoordinateIsInsideRangeInvalidSecondArgument(): void
public static function providerCoordinateIsInsideRangeException(): array
{
$cellRange = 'A1:D4';
$cellCordinate = 'invalidCoordinate';

try {
Coordinate::coordinateIsInsideRange($cellRange, $cellCordinate);
} catch (\Exception $e) {
self::assertInstanceOf(Exception::class, $e);
self::assertEquals($e->getMessage(), 'Second argument needs to be a single coordinate');

return;
}
self::fail('An expected exception has not been raised.');
return require 'tests/data/coordinateIsInsideRangeException.php';

Check warning on line 335 in tests/PhpSpreadsheetTests/Cell/CoordinateTest.php

View workflow job for this annotation

GitHub Actions / PHP 8.0

require(tests/data/coordinateIsInsideRangeException.php): Failed to open stream: No such file or directory

Check warning on line 335 in tests/PhpSpreadsheetTests/Cell/CoordinateTest.php

View workflow job for this annotation

GitHub Actions / PHP 8.1

require(tests/data/coordinateIsInsideRangeException.php): Failed to open stream: No such file or directory

Check warning on line 335 in tests/PhpSpreadsheetTests/Cell/CoordinateTest.php

View workflow job for this annotation

GitHub Actions / PHP 8.2

require(tests/data/coordinateIsInsideRangeException.php): Failed to open stream: No such file or directory

Check warning on line 335 in tests/PhpSpreadsheetTests/Cell/CoordinateTest.php

View workflow job for this annotation

GitHub Actions / PHP nightly

require(tests/data/coordinateIsInsideRangeException.php): Failed to open stream: No such file or directory
}

/**
Expand Down
26 changes: 26 additions & 0 deletions tests/data/Cell/coordinateIsInsideRange.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

declare(strict_types=1);

return [
[true, 'A1:E20', 'B4'],
[false, 'A1:E20', 'F36'],
[true, '$A$1:$E$20', '$B$4'],
[false, '$A$1:$E$20', '$F$36'],
[true, 'Sheet!A1:E20', 'Sheet!B4'],
[false, 'Sheet!A1:E20', 'Sheet!F36'],
[true, 'Sheet!$A$1:$E$20', 'Sheet!$B$4'],
[false, 'Sheet!$A$1:$E$20', 'Sheet!$F$36'],
[false, 'Sheet!A1:E20', 'B4'],
[false, 'Sheet!A1:E20', 'F36'],
[false, 'Sheet!$A$1:$E$20', '$B$4'],
[false, 'Sheet!$A$1:$E$20', '$F$36'],
[false, 'A1:E20', 'Sheet!B4'],
[false, 'A1:E20', 'Sheet!F36'],
[false, '$A$1:$E$20', 'Sheet!$B$4'],
[false, '$A$1:$E$20', 'Sheet!$F$36'],
[true, '\'Sheet space\'!A1:E20', '\'Sheet space\'!B4'],
[false, '\'Sheet space\'!A1:E20', '\'Sheet space\'!F36'],
[true, '\'Sheet space\'!$A$1:$E$20', '\'Sheet space\'!$B$4'],
[false, '\'Sheet space\'!$A$1:$E$20', '\'Sheet space\'!$F$36'],
];
8 changes: 8 additions & 0 deletions tests/data/Cell/coordinateIsInsideRangeException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

declare(strict_types=1);

return [
['First argument needs to be a range', 'invalidRange', 'B4'],
['Second argument needs to be a single coordinate', 'A1:E20', 'invalidCoordinate'],
];

0 comments on commit 419e17d

Please sign in to comment.