mirror of
https://github.com/Respect/Validation.git
synced 2024-06-01 13:22:24 +02:00
Apply contribution guidelines to "MacAddress" rule
This commit will also replace the usage of "mac" to "MAC" since it is an acronym. Co-authored-by: Henrique Moody <henriquemoody@gmail.com>
This commit is contained in:
parent
e2e9197f29
commit
907e24fb8f
|
@ -2,7 +2,7 @@
|
|||
|
||||
- `MacAddress()`
|
||||
|
||||
Validates a Mac Address.
|
||||
Validates whether the input is a valid MAC address.
|
||||
|
||||
```php
|
||||
v::macAddress()->validate('00:11:22:33:44:55'); // true
|
||||
|
|
|
@ -13,14 +13,22 @@ declare(strict_types=1);
|
|||
|
||||
namespace Respect\Validation\Exceptions;
|
||||
|
||||
class MacAddressException extends ValidationException
|
||||
/**
|
||||
* @author Danilo Correa <danilosilva87@gmail.com>
|
||||
* @author Fábio da Silva Ribeiro <fabiorphp@gmail.com>
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
*/
|
||||
final class MacAddressException extends ValidationException
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public static $defaultTemplates = [
|
||||
self::MODE_DEFAULT => [
|
||||
self::STANDARD => '{{name}} must be a valid mac address',
|
||||
self::STANDARD => '{{name}} must be a valid MAC address',
|
||||
],
|
||||
self::MODE_NEGATIVE => [
|
||||
self::STANDARD => '{{name}} must not be a valid mac address',
|
||||
self::STANDARD => '{{name}} must not be a valid MAC address',
|
||||
],
|
||||
];
|
||||
}
|
||||
|
|
|
@ -13,10 +13,28 @@ declare(strict_types=1);
|
|||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
class MacAddress extends AbstractRule
|
||||
use function is_string;
|
||||
use function preg_match;
|
||||
|
||||
/**
|
||||
* Validates whether the input is a valid MAC address.
|
||||
*
|
||||
* @author Alexandre Gomes Gaigalas <alexandre@gaigalas.net>
|
||||
* @author Danilo Correa <danilosilva87@gmail.com>
|
||||
* @author Fábio da Silva Ribeiro <fabiorphp@gmail.com>
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
*/
|
||||
final class MacAddress extends AbstractRule
|
||||
{
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function validate($input): bool
|
||||
{
|
||||
return !empty($input) && preg_match('/^(([0-9a-fA-F]{2}-){5}|([0-9a-fA-F]{2}:){5})[0-9a-fA-F]{2}$/', $input);
|
||||
if (!is_string($input)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return preg_match('/^(([0-9a-fA-F]{2}-){5}|([0-9a-fA-F]{2}:){5})[0-9a-fA-F]{2}$/', $input) > 0;
|
||||
}
|
||||
}
|
||||
|
|
37
tests/integration/rules/macAddress.phpt
Normal file
37
tests/integration/rules/macAddress.phpt
Normal file
|
@ -0,0 +1,37 @@
|
|||
--FILE--
|
||||
<?php
|
||||
require 'vendor/autoload.php';
|
||||
|
||||
use Respect\Validation\Exceptions\MacAddressException;
|
||||
use Respect\Validation\Exceptions\NestedValidationException;
|
||||
use Respect\Validation\Validator as v;
|
||||
|
||||
try {
|
||||
v::macAddress()->check('00-11222:33:44:55');
|
||||
} catch (MacAddressException $exception) {
|
||||
echo $exception->getMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::macAddress())->check('00:11:22:33:44:55');
|
||||
} catch (MacAddressException $exception) {
|
||||
echo $exception->getMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::macAddress()->assert('90-bc-nk:1a-dd-cc');
|
||||
} catch (NestedValidationException $exception) {
|
||||
echo $exception->getFullMessage().PHP_EOL;
|
||||
}
|
||||
|
||||
try {
|
||||
v::not(v::macAddress())->assert('AF:0F:bd:12:44:ba');
|
||||
} catch (NestedValidationException $exception) {
|
||||
echo $exception->getFullMessage().PHP_EOL;
|
||||
}
|
||||
?>
|
||||
--EXPECTF--
|
||||
"00-11222:33:44:55" must be a valid MAC address
|
||||
"00:11:22:33:44:55" must not be a valid MAC address
|
||||
- "90-bc-nk:1a-dd-cc" must be a valid MAC address
|
||||
- "AF:0F:bd:12:44:ba" must not be a valid MAC address
|
|
@ -13,64 +13,55 @@ declare(strict_types=1);
|
|||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Respect\Validation\Test\RuleTestCase;
|
||||
use const PHP_INT_MAX;
|
||||
use function random_int;
|
||||
use function tmpfile;
|
||||
|
||||
/**
|
||||
* @group rule
|
||||
* @covers \Respect\Validation\Exceptions\MacAddressException
|
||||
* @group rule
|
||||
*
|
||||
* @covers \Respect\Validation\Rules\MacAddress
|
||||
*
|
||||
* @author Danilo Correa <danilosilva87@gmail.com>
|
||||
* @author Fábio da Silva Ribeiro <fabiorphp@gmail.com>
|
||||
* @author Gabriel Caruso <carusogabriel34@gmail.com>
|
||||
* @author Henrique Moody <henriquemoody@gmail.com>
|
||||
*/
|
||||
class MacAddressTest extends TestCase
|
||||
final class MacAddressTest extends RuleTestCase
|
||||
{
|
||||
protected $macaddressValidator;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->macaddressValidator = new MacAddress();
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForMacAddress
|
||||
*
|
||||
* @test
|
||||
* {@inheritdoc}
|
||||
*/
|
||||
public function validMacaddressesShouldReturnTrue($input): void
|
||||
public function providerForValidInput(): array
|
||||
{
|
||||
self::assertTrue($this->macaddressValidator->__invoke($input));
|
||||
$this->macaddressValidator->assert($input);
|
||||
$this->macaddressValidator->check($input);
|
||||
}
|
||||
$sut = new MacAddress();
|
||||
|
||||
/**
|
||||
* @dataProvider providerForNotMacAddress
|
||||
* @expectedException \Respect\Validation\Exceptions\MacAddressException
|
||||
*
|
||||
* @test
|
||||
*/
|
||||
public function invalidMacaddressShouldThrowMacAddressException($input): void
|
||||
{
|
||||
self::assertFalse($this->macaddressValidator->__invoke($input));
|
||||
$this->macaddressValidator->assert($input);
|
||||
}
|
||||
|
||||
public function providerForMacAddress()
|
||||
{
|
||||
return [
|
||||
['00:11:22:33:44:55'],
|
||||
['66-77-88-99-aa-bb'],
|
||||
['AF:0F:bd:12:44:ba'],
|
||||
['90-bc-d3-1a-dd-cc'],
|
||||
[$sut, '00:11:22:33:44:55'],
|
||||
[$sut, '66-77-88-99-aa-bb'],
|
||||
[$sut, 'AF:0F:bd:12:44:ba'],
|
||||
[$sut, '90-bc-d3-1a-dd-cc'],
|
||||
];
|
||||
}
|
||||
|
||||
public function providerForNotMacAddress()
|
||||
/**
|
||||
* {@inheditdoc}
|
||||
*/
|
||||
public function providerForInvalidInput(): array
|
||||
{
|
||||
$sut = new MacAddress();
|
||||
|
||||
return [
|
||||
[''],
|
||||
['00-1122:33:44:55'],
|
||||
['66-77--99-jj-bb'],
|
||||
['HH:0F-bd:12:44:ba'],
|
||||
['90-bc-nk:1a-dd-cc'],
|
||||
'empty string' => [$sut, ''],
|
||||
'invalid MAC address' => [$sut, '00-1122:33:44:55'],
|
||||
'boolean' => [$sut, true],
|
||||
'array' => [$sut, ['90-bc-d3-1a-dd-cc']],
|
||||
'int' => [$sut, random_int(1, PHP_INT_MAX)],
|
||||
'float' => [$sut, random_int(1, 9) / 10],
|
||||
'null' => [$sut, null],
|
||||
'resource' => [$sut, tmpfile()],
|
||||
'callable' => [$sut, function (): void {}],
|
||||
];
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue