* * For the full copyright and license information, please view the "LICENSE.md" * file that was distributed with this source code. */ declare(strict_types=1); namespace Respect\Validation\Rules; use Respect\Validation\Test\TestCase; use stdClass; use Symfony\Component\Validator\Constraints\IsFalse; use Symfony\Component\Validator\Constraints\IsNull; use Symfony\Component\Validator\Validation; use Symfony\Component\Validator\Validator\TraceableValidator; use Symfony\Component\Validator\Validator\ValidatorInterface; /** * @group rule * * @covers \Respect\Validation\Rules\Sf * * @author Augusto Pascutti * @author Gabriel Caruso * @author Henrique Moody */ final class SfTest extends TestCase { /** * @test */ public function itShouldValidateWithDefinedConstraintAndValidator(): void { $sut = new Sf(new IsNull()); self::assertTrue($sut->validate(null)); } /** * @test */ public function itShouldInvalidateWithDefinedConstraintAndValidator(): void { $sut = new Sf(new IsFalse()); self::assertFalse($sut->validate(true)); } /** * @test */ public function itShouldHaveTheValidatorByDefault(): void { $sut = new Sf(new IsNull()); self::assertAttributeInstanceOf(ValidatorInterface::class, 'validator', $sut); } /** * @test */ public function itShouldUseTheDefinedValidatorToValidate(): void { if (!class_exists(TraceableValidator::class)) { self::markTestSkipped('The current version of Symfony Validator does not have '.TraceableValidator::class); } $input = new stdClass(); $validator = new TraceableValidator(Validation::createValidator()); $sut = new Sf(new IsNull(), $validator); $sut->validate($input); self::assertSame($input, $validator->getCollectedData()[0]['context']['value']); } }