mirror of
https://github.com/Respect/Validation.git
synced 2024-06-07 16:22:16 +02:00
Add MacAddress Validation with tests
This commit is contained in:
parent
30a1056600
commit
7a6020b25d
|
@ -0,0 +1,50 @@
|
|||
<?php
|
||||
|
||||
namespace Respect\Validation\Exceptions;
|
||||
|
||||
class MacAddressException extends ValidationException
|
||||
{
|
||||
|
||||
public static $defaultTemplates = array(
|
||||
self::MODE_DEFAULT => array(
|
||||
self::STANDARD => '{{name}} must be a valid mac address',
|
||||
),
|
||||
self::MODE_NEGATIVE => array(
|
||||
self::STANDARD => '{{name}} must not be a valid mac address',
|
||||
)
|
||||
);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* LICENSE
|
||||
*
|
||||
* Copyright (c) 2011, Fábio da Silva Ribeiro.
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* * Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* * Neither the name of Alexandre Gomes Gaigalas nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from this
|
||||
* software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
*/
|
49
library/Respect/Validation/Rules/MacAddress.php
Normal file
49
library/Respect/Validation/Rules/MacAddress.php
Normal file
|
@ -0,0 +1,49 @@
|
|||
<?php
|
||||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
class MacAddress extends AbstractRule
|
||||
{
|
||||
|
||||
public function validate($input)
|
||||
{
|
||||
if(empty($input))
|
||||
return false;
|
||||
|
||||
return (bool) preg_match('/^(([0-9a-fA-F]{2}-){5}|([0-9a-fA-F]{2}:){5})[0-9a-fA-F]{2}$/', $input);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* LICENSE
|
||||
*
|
||||
* Copyright (c) 2011, Fábio da Silva Ribeiro
|
||||
* All rights reserved.
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without modification,
|
||||
* are permitted provided that the following conditions are met:
|
||||
*
|
||||
* * Redistributions of source code must retain the above copyright notice,
|
||||
* this list of conditions and the following disclaimer.
|
||||
*
|
||||
* * Redistributions in binary form must reproduce the above copyright notice,
|
||||
* this list of conditions and the following disclaimer in the documentation
|
||||
* and/or other materials provided with the distribution.
|
||||
*
|
||||
* * Neither the name of Alexandre Gomes Gaigalas nor the names of its
|
||||
* contributors may be used to endorse or promote products derived from this
|
||||
* software without specific prior written permission.
|
||||
*
|
||||
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
|
||||
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
||||
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
|
||||
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
|
||||
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
||||
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
|
||||
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
||||
* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
||||
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
|
||||
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||
*
|
||||
*/
|
56
tests/library/Respect/Validation/Rules/MacAddressTest.php
Normal file
56
tests/library/Respect/Validation/Rules/MacAddressTest.php
Normal file
|
@ -0,0 +1,56 @@
|
|||
<?php
|
||||
|
||||
namespace Respect\Validation\Rules;
|
||||
|
||||
class MacAddressTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
|
||||
protected $macaddressValidator;
|
||||
|
||||
protected function setUp()
|
||||
{
|
||||
$this->macaddressValidator = new MacAddress;
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForMacAddress
|
||||
*
|
||||
*/
|
||||
public function test_valid_macaddresses_should_return_True($input)
|
||||
{
|
||||
$this->assertTrue($this->macaddressValidator->validate($input));
|
||||
$this->assertTrue($this->macaddressValidator->assert($input));
|
||||
$this->assertTrue($this->macaddressValidator->check($input));
|
||||
}
|
||||
|
||||
/**
|
||||
* @dataProvider providerForNotMacAddress
|
||||
* @expectedException Respect\Validation\Exceptions\MacAddressException
|
||||
*/
|
||||
public function test_invalid_macaddress_should_throw_MacAddressException($input)
|
||||
{
|
||||
$this->assertFalse($this->macaddressValidator->validate($input));
|
||||
$this->assertFalse($this->macaddressValidator->assert($input));
|
||||
}
|
||||
|
||||
public function providerForMacAddress()
|
||||
{
|
||||
return array(
|
||||
array('00:11:22:33:44:55'),
|
||||
array('66-77-88-99-aa-bb'),
|
||||
array('AF:0F:bd:12:44:ba'),
|
||||
array('90-bc-d3-1a-dd-cc'),
|
||||
);
|
||||
}
|
||||
|
||||
public function providerForNotMacAddress()
|
||||
{
|
||||
return array(
|
||||
array('00-1122:33:44:55'),
|
||||
array('66-77--99-jj-bb'),
|
||||
array('HH:0F-bd:12:44:ba'),
|
||||
array('90-bc-nk:1a-dd-cc'),
|
||||
);
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in a new issue