Skip to content

Commit ef0ba8f

Browse files
Merge pull request #112 from nerijunior/fix/sanitization-accepting-nulls
Fix Sanitization class to deal with null values
2 parents f206424 + d57c8e9 commit ef0ba8f

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

src/validator-docs/Rules/Sanitization.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,6 @@ abstract class Sanitization
1010
{
1111
public function sanitize($value): string
1212
{
13-
return preg_replace('/[^\d]/', '', $value);
13+
return empty($value) ? "" : preg_replace('/[^\d]/', '', $value);
1414
}
1515
}

tests/SanitizationTestCase.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
namespace geekcom\ValidatorDocs\Tests;
4+
5+
use geekcom\ValidatorDocs\Rules\Sanitization;
6+
use Illuminate\Support\Facades\Validator;
7+
8+
final class SanitizationTestCase extends ValidatorTestCase
9+
{
10+
/** @test **/
11+
public function sanitizeRetornaVazioQuandoNuloOuVazio()
12+
{
13+
$mock = $this->getMockForAbstractClass(Sanitization::class);
14+
$this->assertEquals($mock->sanitize(null), "");
15+
}
16+
17+
/** @test **/
18+
public function sanitizeRetornaSomenteDigitos()
19+
{
20+
$mock = $this->getMockForAbstractClass(Sanitization::class);
21+
$this->assertEquals($mock->sanitize('a53.084.587/0001-20z'), "53084587000120");
22+
}
23+
}

0 commit comments

Comments
 (0)