Skip to content

Commit

Permalink
feat: not html rule and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
matmper committed Sep 28, 2023
1 parent 44cd23a commit 0e562be
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 27 deletions.
19 changes: 13 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,14 @@ $ php artisan vendor:publish --provider="Matmper\Providers\ValidationProvider"

# Usage

| Name | Validation | Description |
|---|---|---|
| DOCUMENT| document | validate CPF or CNPJ value, ignore non numeric char |
| - | document:cpf | validate CPF value, ignore non numeric char |
| - | document:cpf,mask | validate CPF value and format (000.000.000-00) |
| Validation | Description |
|---|---|
| cep | validates CEP value, ignore non numeric char |
| cep:mask | validates CEP value and format (00000-000) |
| document | validates CPF or CNPJ value, ignore non numeric char |
| document:cpf | validates CPF value, ignore non numeric char |
| document:cpf,mask | validates CPF value and format (000.000.000-00) |
| not_html | validates that the string does not contain html |

**Example**
```php
Expand All @@ -45,7 +48,7 @@ $ php artisan vendor:publish --provider="Matmper\Providers\ValidationProvider"
// 00000000000000 -> true
// 00.000.000/0000-00 -> true
return ['document_number' => 'document'];
return ['document_number' => 'document:cpf|cnpj'];
return ['document_number' => 'document:cpf.cnpj'];

// 00000000000 -> true
// 000.000.000-00 -> true
Expand All @@ -58,6 +61,10 @@ return ['cpf' => 'document:cpf'];
// 00000000000000 -> false
// 00.000.000/0000-00 -> true
return ['cnpj' => 'document:cnpj,mask'];

// hello world -> true
// <p>hello world</p> -> false
return ['content' => 'string|not_html'];
```

| laravel-brazil-validation version | Laravel versions |
Expand Down
13 changes: 6 additions & 7 deletions src/Exceptions/InvalidDocumentTypeException.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,17 @@

class InvalidDocumentTypeException extends Exception
{
/**
* @param string|null $message
* @param int $code
* @param Throwable|null $previous
*/
/**
* @param string|null $message
* @param int $code
* @param Throwable|null $previous
*/
public function __construct(
string $message = null,
int $code = 0,
int $code = Response::HTTP_NOT_FOUND,
Throwable $previous = null,
) {
$message = "Invalid document type for validation: {$message}";
$code = $code ? $code : Response::HTTP_NOT_FOUND;

parent::__construct($message, $code, $previous);
}
Expand Down
13 changes: 6 additions & 7 deletions src/Exceptions/InvalidValidationParameterException.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,17 @@

class InvalidValidationParameterException extends Exception
{
/**
* @param string|null $message
* @param int $code
* @param Throwable|null $previous
*/
/**
* @param string|null $message
* @param int $code
* @param Throwable|null $previous
*/
public function __construct(
string $message = null,
int $code = 0,
int $code = Response::HTTP_NOT_ACCEPTABLE,
Throwable $previous = null,
) {
$message = "Validation parameter is invalid: {$message}";
$code = $code ? $code : Response::HTTP_NOT_ACCEPTABLE;

parent::__construct($message, $code, $previous);
}
Expand Down
27 changes: 27 additions & 0 deletions src/Exceptions/ValueIsNotStringException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Matmper\Exceptions;

use Exception;
use Illuminate\Http\Response;
use Throwable;

class ValueIsNotStringException extends Exception
{
/**
* @param string|null $message
* @param int $code
* @param Throwable|null $previous
*/
public function __construct(
string $message = null,
int $code = Response::HTTP_BAD_REQUEST,
Throwable $previous = null,
) {
$message = "Parameter value is not a valid string: {$message}";

parent::__construct($message, $code, $previous);
}
}
6 changes: 3 additions & 3 deletions src/Rules/DocumentNumberRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ class DocumentNumberRule implements Rule
*/
public function params(array $params): self
{
$this->setDocuments($params[0] ?? 'cpf|cnpj');
$this->setDocuments($params[0] ?? 'cpf.cnpj');
$this->setMask($params[1] ?? 'value');

return $this;
Expand Down Expand Up @@ -71,12 +71,12 @@ public function message()
/**
* Validate document list and set in $this->documents
*
* @param string $documents cpf|cnpj
* @param string $documents cpf.cnpj
* @return void
*/
private function setDocuments(string $documents): void
{
$this->documents = explode('|', $documents);
$this->documents = explode('.', $documents);

$validDocumentTypes = [DocumentType::CPF, DocumentType::CNPJ];

Expand Down
8 changes: 7 additions & 1 deletion src/Rules/NotHtmlRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,13 @@ public function params(array $params): self
*/
public function passes($attribute, $value): bool
{
// if is html, return false
if (!is_string($value)) {
throw new \Matmper\Exceptions\ValueIsNotStringException($attribute);
}

if (preg_match('/<\s?[^\>]*\/?\s?>/i', $value)) {
return false;
}

return true;
}
Expand Down
6 changes: 3 additions & 3 deletions tests/Unit/DocumentNumberRuleTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ public function test_document_number(string $type, string $mask, string $documen
public function testDocumentNumberDataProvider(): array
{
return [
'cpf_cnpj_true_cpf' => ['cpf|cnpj', 'value', FakeDocumentHelper::cpf(), true],
'cpf_cnpj_true_cnpj' => ['cpf|cnpj', 'value', FakeDocumentHelper::cnpj(), true],
'cpf_cnpj_false' => ['cpf|cnpj', 'value', fake()->word(), false],
'cpf_cnpj_true_cpf' => ['cpf.cnpj', 'value', FakeDocumentHelper::cpf(), true],
'cpf_cnpj_true_cnpj' => ['cpf.cnpj', 'value', FakeDocumentHelper::cnpj(), true],
'cpf_cnpj_false' => ['cpf.cnpj', 'value', fake()->word(), false],

'cpf_true' => ['cpf', 'value', FakeDocumentHelper::cpf(), true],
'cpf_true_mask' => ['cpf', 'mask', FakeDocumentHelper::cpf(true), true],
Expand Down
62 changes: 62 additions & 0 deletions tests/Unit/NotHtmlRuleTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php declare(strict_types=1);

namespace Tests;

class NotHtmlRuleTest extends TestCase
{
/**
* @test
* @dataProvider testNotHtmlDataProvider
*/
public function test_not_html(mixed $value, bool $assert): void
{
$rule = app(\Matmper\Rules\NotHtmlRule::class);
$passes = $rule->passes(fake()->word(), $value);

$this->assertEquals($passes, $assert);
}

/**
* Data Provider: test_not_html
*
* @return array
*/
public function testNotHtmlDataProvider(): array
{
return [
'true_string' => [$value = fake()->text(100), true],
'false_string_01' => ["<div>{$value}</div>", false],
'false_string_02' => ["<div>", false],
'false_string_03' => ["</div>", false],
'false_string_04' => ["<div><p></p></div>", false],
'false_string_05' => ["$value<div></div>", false],
'false_string_05' => ["<div></div>$value", false],
];
}

/**
* @test
* @dataProvider testNotHtmlExceptionDataProvider
*/
public function test_not_html_string_exception(mixed $value): void
{
$this->expectException(\Matmper\Exceptions\ValueIsNotStringException::class);

$rule = app(\Matmper\Rules\NotHtmlRule::class);
$rule->passes(fake()->word(), $value);
}

/**
* Data Provider: test_not_html_string_exception
*
* @return array
*/
public function testNotHtmlExceptionDataProvider(): array
{
return [
'false_int' => [fake()->randomDigitNotZero()],
'false_float' => [fake()->randomFloat(2)],
'false_array' => [[1, 2, 3]],
];
}
}

0 comments on commit 0e562be

Please sign in to comment.