|
| 1 | +<?php |
| 2 | + |
| 3 | +declare( strict_types = 1 ); |
| 4 | + |
| 5 | +namespace WMDE\FunValidators\Tests\Unit; |
| 6 | + |
| 7 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 8 | +use PHPUnit\Framework\TestCase; |
| 9 | +use WMDE\FunValidators\ConstraintViolation; |
| 10 | + |
| 11 | +#[CoversClass( ConstraintViolation::class )] |
| 12 | +class ConstraintViolationTest extends TestCase { |
| 13 | + public function testConstructorSetsProperties(): void { |
| 14 | + $constraintViolation = new ConstraintViolation( "1nval1d", "no_mix_of_numbers_and_letters", "username" ); |
| 15 | + |
| 16 | + $this->assertSame( 'no_mix_of_numbers_and_letters', $constraintViolation->getMessageIdentifier() ); |
| 17 | + $this->assertSame( '1nval1d', $constraintViolation->getValue() ); |
| 18 | + $this->assertSame( 'username', $constraintViolation->getSource() ); |
| 19 | + } |
| 20 | + |
| 21 | + public function testSetSource(): void { |
| 22 | + $constraintViolation = new ConstraintViolation( "1nval1d", "no_mix_of_numbers_and_letters" ); |
| 23 | + |
| 24 | + $constraintViolation->setSource( 'access_code' ); |
| 25 | + |
| 26 | + $this->assertSame( 'access_code', $constraintViolation->getSource() ); |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * We'll change this to an exception check when our other libraries don't trigger deprecations any more |
| 31 | + */ |
| 32 | + public function testSetSourceDeprecatesOverridingExistingSource(): void { |
| 33 | + $constraintViolation = new ConstraintViolation( "1nval1d", "no_mix_of_numbers_and_letters", 'username' ); |
| 34 | + $hasTriggeredDeprecation = false; |
| 35 | + // @phpstan-ignore-next-line argument.type |
| 36 | + set_error_handler( static function ()use( &$hasTriggeredDeprecation ){ |
| 37 | + $hasTriggeredDeprecation = true; |
| 38 | + }, E_USER_DEPRECATED ); |
| 39 | + |
| 40 | + $constraintViolation->setSource( 'access_code' ); |
| 41 | + |
| 42 | + restore_error_handler(); |
| 43 | + $this->assertTrue( $hasTriggeredDeprecation ); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * We'll change this to an exception check when our other libraries don't trigger deprecations any more |
| 48 | + */ |
| 49 | + public function testSetSourceAllowsOverridingWhenSourceNameMatches(): void { |
| 50 | + $constraintViolation = new ConstraintViolation( "1nval1d", "no_mix_of_numbers_and_letters", 'username' ); |
| 51 | + $hasTriggeredDeprecation = false; |
| 52 | + // @phpstan-ignore-next-line argument.type |
| 53 | + set_error_handler( static function ()use( &$hasTriggeredDeprecation ){ |
| 54 | + $hasTriggeredDeprecation = true; |
| 55 | + }, E_USER_DEPRECATED ); |
| 56 | + |
| 57 | + $constraintViolation->setSource( 'username' ); |
| 58 | + |
| 59 | + restore_error_handler(); |
| 60 | + $this->assertFalse( $hasTriggeredDeprecation ); |
| 61 | + } |
| 62 | + |
| 63 | +} |
0 commit comments