8000 [Validator] Add additional versions (`*_NO_PUBLIC`, `*_ONLY_PRIV` & `*_ONLY_RES`) in IP address constraint by Ninos · Pull Request #51777 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Validator] Add additional versions (*_NO_PUBLIC, *_ONLY_PRIV & *_ONLY_RES) in IP address constraint #51777

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 32 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
a98f828
Enhancement: Add *_ONLY_PRIV & *_ONLY_RES as possible versions in IP …
Ninos Sep 28, 2023
9551dc3
Updated: Changelog
Ninos Sep 28, 2023
2b1e888
Updated: UPGRADE-6.4.md
Ninos Sep 28, 2023
24c73a3
CS
Ninos Sep 28, 2023
2200cd8
Added: Tests for `Mac` constraint
Ninos Sep 28, 2023
3cb3119
Fixed: Tests for `Mac` constraint
Ninos Sep 28, 2023
99e03ea
Added: OnlyPrivate & OnlyReserved tests
Ninos Sep 28, 2023
c11c261
Enhancement: OnlyPrivate & OnlyReserved tests
Ninos Sep 28, 2023
12f126e
CS
Ninos Sep 28, 2023
e4e6a24
Update src/Symfony/Component/Validator/Constraints/Mac.php
Ninos Oct 5, 2023
0e6cf5e
Update src/Symfony/Component/Validator/Constraints/Ip.php
Ninos Oct 5, 2023
0b7910f
Update src/Symfony/Component/Validator/Constraints/MacValidator.php
Ninos Oct 5, 2023
719be6c
Update src/Symfony/Component/Validator/Constraints/Mac.php
Ninos Oct 5, 2023
a6eb4f2
Update src/Symfony/Component/Validator/Constraints/Mac.php
Ninos Oct 5, 2023
4b4c5e4
Update src/Symfony/Component/Validator/Constraints/Mac.php
Ninos Oct 5, 2023
9cf46fc
Update src/Symfony/Component/Validator/Constraints/Mac.php
Ninos Oct 5, 2023
9775156
CS
Ninos Oct 5, 2023
646fd5f
Removed: Mac constraint
Ninos Oct 6, 2023
26e9f2a
[Validator] Remove `Mac` constraint
Ninos Oct 6, 2023
6e84494
[Validator] Fix IpTest
Ninos Oct 6, 2023
cfff49a
[Validator] Renamed constants to full-word names
Ninos Nov 5, 2023
3f58f6b
[Validator] Renamed constants to full-word names
Ninos Nov 5, 2023
a0419db
[Validator] Possibility to use all `Ip` constraint versions in `Cidr`…
Ninos Nov 6, 2023
ed09494
[Validator] Possibility to use all `Ip` constraint versions in `Cidr`…
Ninos Nov 6, 2023
65b3184
[Validator] Possibility to use all `Ip` constraint versions in `Cidr`…
Ninos Nov 6, 2023
b066087
[Validator] Possibility to use all `Ip` constraint versions in `Cidr`…
Ninos Nov 6, 2023
1991107
[Validator] Fixed tests & added `*_NO_PUBLIC`
Ninos Nov 6, 2023
cbdd81f
[Validator] Fixed tests
Ninos Nov 6, 2023
601809c
[Validator] Added normalizer to `Cidr` + better string support
Ninos Nov 7, 2023
cc930ef
Merge branch '7.1' into constraints-networking
Ninos Nov 20, 2023
0d12319
[Validator] Small fixes
Ninos Nov 20, 2023
b58bb03
[Validator] Small fixes
Ninos Nov 20, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Added: Tests for Mac constraint
Fixed: Test for `Ip` constraint
  • Loading branch information
Ninos committed Sep 28, 2023
commit 2200cd82911e63b513b6781d871fa8613d08f66f
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public function testAttributes()
self::assertTrue($loader->loadClassMetadata($metadata));

[$aConstraint] = $metadata->properties['a']->getConstraints();
self::assertSame(Ip::V4, $aConstraint->version);
self::assertSame(Ip::ALL, $aConstraint->version);

[$bConstraint] = $metadata->properties['b']->getConstraints();
self::assertSame(Ip::V6, $bConstraint->version);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ public function testValidIpV6WithWhitespacesNamed()
{
$this->validator->validate(
"\n\t2001:0db8:85a3:0000:0000:8a2e:0370:7334\r\n",
new Ip(version: \Symfony\Component\Validator\Constraints\Ip::V6, normalizer: 'trim')
new Ip(version: Ip::V6, normalizer: 'trim')
);

$this->assertNoViolation();
Expand Down
70 changes: 70 additions & 0 deletions src/Symfony/Component/Validator/Tests/Constraints/MacTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Validator\Tests\Constraints;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Validator\Constraints\Mac;
use Symfony\Component\Validator\Exception\InvalidArgumentException;
use Symfony\Component\Validator\Mapping\ClassMetadata;
use Symfony\Component\Validator\Mapping\Loader\AttributeLoader;

/**
* @author Ninos Ego <me@ninosego.de>
*/
class MacTest extends TestCase
{
public function testNormalizerCanBeSet()
{
$ip = new Mac(['normalizer' => 'trim']);

$this->assertEquals('trim', $ip->normalizer);
}

public function testInvalidNormalizerThrowsException()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('The "normalizer" option must be a valid callable ("string" given).');
new Mac(['normalizer' => 'Unknown Callable']);
}

public function testInvalidNormalizerObjectThrowsException()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('The "normalizer" option must be a valid callable ("stdClass" given).');
new Mac(['normalizer' => new \stdClass()]);
}

public function testAttributes()
{
$metadata = new ClassMetadata(IpDummy::class);
$loader = new AttributeLoader();
self::assertTrue($loader->loadClassMetadata($metadata));

[$aConstraint] = $metadata->properties['a']->getConstraints();
self::assertSame('myMessage', $aConstraint->message);
self::assertSame('trim', $aConstraint->normalizer);
self::assertSame(['Default', 'IpDummy'], $aConstraint->groups);

[$bConstraint] = $metadata->properties['b']->getConstraints();
self::assertSame(['my_group'], $bConstraint->groups);
self::assertSame('some attached data', $bConstraint->payload);
}
}

class MacDummy
{
#[Mac( message: 'myMessage', normalizer: 'trim')]
private $a;

#[Mac(groups: ['my_group'], payload: 'some attached data')]
private $b;
}
125 changes: 125 additions & 0 deletions src/Symfony/Component/Validator/Tests/Constraints/MacValidatorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Validator\Tests\Constraints;

use Symfony\Component\Validator\Constraints\Mac;
use Symfony\Component\Validator\Constraints\MacValidator;
use Symfony\Component\Validator\Exception\UnexpectedValueException;
use Symfony\Component\Validator\Test\ConstraintValidatorTestCase;

/**
* @author Ninos Ego <me@ninosego.de>
*/
class MacValidatorTest extends ConstraintValidatorTestCase
{
protected function createValidator(): MacValidator
{
return new MacValidator();
}

public function testNullIsValid()
{
$this->validator->validate(null, new Mac());

$this->assertNoViolation();
}

public function testEmptyStringIsValid()
{
$this->validator->validate('', new Mac());

$this->assertNoViolation();
}

public function testExpectsStringCompatibleType()
{
$this->expectException(UnexpectedValueException::class);
$this->validator->validate(new \stdClass(), new Mac());
}

/**
* @dataProvider getValidMacs
*/
public function testValidMac($mac)
{
$this->validator->validate($mac, new Mac());

$this->assertNoViolation();
}

public static function getValidMacs()
{
return [
['00:00:00:00:00:00'],
['00-00-00-00-00-00'],
['ff:ff:ff:ff:ff:ff'],
['ff-ff-ff-ff-ff-ff'],
['FF:FF:FF:FF:FF:FF'],
['FF-FF-FF-FF-FF-FF'],
];
}

/**
* @dataProvider getValidMacsWithWhitespaces
*/
public function testValidMacsWithWhitespaces($mac)
{
$this->validator->validate($mac, new Mac([
'normalizer' => 'trim',
]));

$this->assertNoViolation();
}

public static function getValidMacsWithWhitespaces()
{
return [
["\x2000:00:00:00:00:00"],
["\x09\x0900-00-00-00-00-00"],
["ff:ff:ff:ff:ff:ff\x0A"],
["ff-ff-ff-ff-ff-ff\x0D\x0D"],
["\x00FF:FF:FF:FF:FF:FF\x00"],
["\x0B\x0BFF-FF-FF-FF-FF-FF\x0B\x0B"],
];
}

/**
* @dataProvider getInvalidMacs
*/
public function testInvalidMacs($mac)
{
$constraint = new Mac([
'message' => 'myMessage',
]);

$this->validator->validate($mac, $constraint);

$this->buildViolation('myMessage')
->setParameter('{{ value }}', '"'.$mac.'"')
->setCode(Mac::INVALID_MAC_ERROR)
->assertRaised();
}

public static function getInvalidMacs()
{
return [
['0'],
['00:00'],
['00:00:00'],
['00:00:00:00'],
['00:00:00:00:00'],
['00:00:00:00:00:000'],
['-00:00:00:00:00:00'],
['foobar'],
];
}
}
0