正则表达式在 Laravel PHP 单元测试中的范围值失败



我尝试使用以下条件制作正则表达式:

-90 < latitude < 90
-180 < longitude < 180
Should have 6 decimal points.

以及我的正则表达式如下:

Latitude : /^-?(0|[0-9]|[1-8][0-9]|90).{1}d{6}$/
Longitude : /^-?(0|[0-9]|[1-9][0-9]|1[0-7][0-9]|180).{1}d{6}$/

通过此的最大测试。但是当我在 php 单元中尝试这个时

Latitude : 10.000000 , Longitude: 10.000000 // Got Failed
Latitude : 0.000001 , Longitude: 0.000001 // Got Failed
Latitude : 0.000000 , Longitude: 0.000000 // Got Failed

我还想包括这 3 个选项。我在幼虫 5.6 (PHP( 中使用此正则表达式。

同样,当我这样做时,它开始在单元测试中工作。

Latitude : "10.000000" , Longitude: "10.000000" // Got Succeed
Latitude : "0.000001" , Longitude: "0.000001" // Got Succeed
Latitude : "0.000000" , Longitude: "0.000000" // Got Succeed

如果我通过邮递员尝试,它适用于这两种情况。但是在进行Laravel PHP单元测试时,它不起作用。

我的验证规则是:

public static $fieldValidations = [
'serial'    => 'required|unique:panels|size:16|alpha_num',
'latitude'  => array('required','numeric','between:-90,90','regex:/^-?(0|[0-9]|[1-8][0-9]|90).{1}d{6}$/'),
'longitude'  => array('required','numeric','between:-180,180','regex:/^-?(0|[0-9]|[1-9][0-9]|1[0-7][0-9]|180).{1}d{6}$/'),
];

我的 php 单元测试代码是

public function testStoreFailureLatitudeLongitudeAllZeroDecimalCase()
{
$response = $this->json('POST', '/api/panels', [
'serial'    => 'AAAABBBBCCCC1234',
'longitude' => 10.000000,
'latitude'  => -20.000000
]);
$response->assertStatus(201);
}
public function testStoreFailurePrecisionFloatDecimalValueCase()
{
$response = $this->json('POST', '/api/panels', [
'serial'    => 'AAAABBBBCCCC1234',
'longitude' => 0.000001,
'latitude'  => 0.000001
]);
$response->assertStatus(201);
}
public function testStoreFailurePrecisionFloatDecimalValuewithZeroCase()
{
$response = $this->json('POST', '/api/panels', [
'serial'    => 'AAAABBBBCCCC1234',
'longitude' => 0.000000,
'latitude'  => 0.000000
]);
$response->assertStatus(201);
}

这些是它失败的 3 种情况,并且通过邮递员使用相同的值,它可以工作。

有什么帮助吗?

function validateLatitude($lat) {
return preg_match('/^(+|-)?(?:90(?:(?:.0{1,6})?)|(?:[0-9]|[1-8][0-9])(?:(?:.[0-9]{1,6})?))$/', $lat);
}
function validateLongitude($long) {
return preg_match('/^(+|-)?(?:180(?:(?:.0{1,6})?)|(?:[0-9]|[1-9][0-9]|1[0-7][0-9])(?:(?:.[0-9]{1,6})?))$/', $long);
}

对于 0.0001、0.00001、0.000001 失败

也许对于Latitude,你可以使用:

^-?(?:[1-8][0-9]|[0-9]|90).d{6}$

对于经度,您可以使用:

^-?(?:1[0-7][0-9]|[1-9][0-9]|[0-9]|180).d{6}$

请注意,您可以省略{1},因为0|[0-9]您可以只使用[0-9],如果您不引用捕获的组,则可以使用非捕获组(?:进行交替。

最新更新