-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Validate data provided in purchase form
- Loading branch information
Showing
9 changed files
with
160 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
namespace App\Http\Validation\Rules; | ||
|
||
use App\Exceptions\ValidationException; | ||
use App\Http\Validation\BaseRule; | ||
|
||
class FullNameRule extends RegexRule | ||
{ | ||
public function __construct(string $pattern = "/^\p{L}+\s\p{L}+$/u") | ||
{ | ||
parent::__construct($pattern); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
namespace App\Http\Validation\Rules; | ||
|
||
use App\Exceptions\ValidationException; | ||
use App\Http\Validation\BaseRule; | ||
|
||
class PostalCodeRule extends RegexRule | ||
{ | ||
public function __construct(string $pattern = "/^\d{2}-\d{3}$/") | ||
{ | ||
parent::__construct($pattern); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php | ||
namespace App\Http\Validation\Rules; | ||
|
||
use App\Exceptions\ValidationException; | ||
use App\Http\Validation\BaseRule; | ||
|
||
class RegexRule extends BaseRule | ||
{ | ||
private string $pattern; | ||
|
||
public function __construct(string $pattern) | ||
{ | ||
parent::__construct(); | ||
$this->pattern = $pattern; | ||
} | ||
|
||
public function validate($attribute, $value, array $data): void | ||
{ | ||
if (!preg_match($this->pattern, $value)) { | ||
throw new ValidationException($this->lang->t("field_regex_warn")); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
<?php | ||
|
||
namespace Tests\Unit\Http\Validation; | ||
|
||
use App\Exceptions\ValidationException; | ||
use App\Http\Validation\Rules\FullNameRule; | ||
use App\Http\Validation\Rules\PostalCodeRule; | ||
use Tests\Psr4\TestCases\UnitTestCase; | ||
|
||
class FullNameRuleTest extends UnitTestCase | ||
{ | ||
public function validFullNames(): array | ||
{ | ||
return [["John Wick"], ["John Johny"], ["Michał Nowak"], ["Zażółć GęśląJaźń"]]; | ||
} | ||
|
||
/** | ||
* @test | ||
* @dataProvider validFullNames | ||
*/ | ||
public function passes_validation(string $fullName) | ||
{ | ||
// given | ||
$postalCodeRule = new FullNameRule(); | ||
|
||
// when | ||
$postalCodeRule->validate("", $fullName, []); | ||
} | ||
|
||
public function invalidFullNames(): array | ||
{ | ||
return [["xyz"], ["to jestem ja"], [""], [null]]; | ||
} | ||
|
||
/** | ||
* @test | ||
* @dataProvider invalidFullNames | ||
*/ | ||
public function fails_validation(?string $fullName) | ||
{ | ||
// given | ||
$this->expectException(ValidationException::class); | ||
|
||
$postalCodeRule = new FullNameRule(); | ||
|
||
// when | ||
$postalCodeRule->validate("", $fullName, []); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
<?php | ||
|
||
namespace Tests\Unit\Http\Validation; | ||
|
||
use App\Exceptions\ValidationException; | ||
use App\Http\Validation\Rules\PostalCodeRule; | ||
use Tests\Psr4\TestCases\UnitTestCase; | ||
|
||
class PostalCodeRuleTest extends UnitTestCase | ||
{ | ||
public function validPostalCodes(): array | ||
{ | ||
return [["00-000"], ["01-960"]]; | ||
} | ||
|
||
/** | ||
* @test | ||
* @dataProvider validPostalCodes | ||
*/ | ||
public function passes_validation(string $postalCode) | ||
{ | ||
// given | ||
$postalCodeRule = new PostalCodeRule(); | ||
|
||
// when | ||
$postalCodeRule->validate("", $postalCode, []); | ||
} | ||
|
||
public function invalidPostalCodes(): array | ||
{ | ||
return [["abc"], ["0000"], [""], [null]]; | ||
} | ||
|
||
/** | ||
* @test | ||
* @dataProvider invalidPostalCodes | ||
*/ | ||
public function fails_validation(?string $postalCode) | ||
{ | ||
// given | ||
$this->expectException(ValidationException::class); | ||
|
||
$postalCodeRule = new PostalCodeRule(); | ||
|
||
// when | ||
$postalCodeRule->validate("", $postalCode, []); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters