在Laravel PHP单元测试中,正则表达式的范围值失败
我尝试使用以下条件制作正则表达式:在Laravel PHP单元测试中,正则表达式的范围值失败,php,laravel,phpunit,Php,Laravel,Phpunit,我尝试使用以下条件制作正则表达式: -90 < latitude < 90 -180 < longitude < 180 Should have 6 decimal points. 通过此测试的最大测试数。但是当我在Php单元中尝试这个时 Latitude : 10.000000 , Longitude: 10.000000 // Got Failed Latitude : 0.000001 , Longitude: 0.000001 // Got Failed
-90 < latitude < 90
-180 < longitude < 180
Should have 6 decimal points.
通过此测试的最大测试数。但是当我在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时失败,也许对于纬度,您可以使用:
^-?(?:[1-8][0-9]|[0-9]|90)\.\d{6}$
对于经度,您可以使用:
^-?(?:1[0-7][0-9]|[1-9][0-9]|[0-9]|[0-9]|180)\.\d{6}$
请注意,您可以省略
{1}
,对于0 |[0-9]
,您可以只使用[0-9]
,如果您不是指捕获的组,则可以使用非捕获组(?:
用于替换。我正在分别测试纬度和经度。你能为两者编写单独的正则表达式吗。在0.000001Nope上也失败了。在所有3种情况下都失败了。通过省略{1}也不起作用。因此,当您将值括在双引号之间时,您的测试通过了?如果我没有弄错的话,问题是如何让您的单元测试工作?因为您的正则表达式与您想要匹配的匹配。您可以将您的代码添加到您如何编写测试的问题中。我没有使用Laravel进行单元测试的经验,but是否可以指定numeric,而使用正则表达式则需要字符串?
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);
}