-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Stub for Trait to make phpstan happy
- it's not advised to test traits isolated from the code that actually uses the trait sebastianbergmann/phpunit#5958 sebastianbergmann/phpunit#5244 - lots of code around mocking traits for that matter got deprecated in phpunit because of that - that's why we try to use a stub class that uses the trait artificially to test its private method
- Loading branch information
Showing
3 changed files
with
62 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,8 +5,8 @@ | |
namespace WMDE\FunValidators; | ||
|
||
/** | ||
* @license GPL-2.0-or-later | ||
* @author Gabriel Birke < [email protected] > | ||
* @deprecated this trait is used in other repositories and cannot be tested here properly, | ||
* thus it should be turned into a helper class instead or get the behaviour extracted in another way | ||
*/ | ||
trait CanValidateField { | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
declare( strict_types = 1 ); | ||
|
||
namespace WMDE\FunValidators\Tests\Unit; | ||
|
||
use PHPUnit\Framework\Attributes\CoversClass; | ||
use PHPUnit\Framework\TestCase; | ||
use WMDE\FunValidators\CanValidateField; | ||
use WMDE\FunValidators\ConstraintViolation; | ||
use WMDE\FunValidators\ValidationResult; | ||
|
||
#[CoversClass( CanValidateField::class )] | ||
#[CoversClass( CanValidateFieldTraitStub::class )] | ||
class CanValidateFieldTest extends TestCase { | ||
|
||
public function testGivenValidResult_returnsNoValidations(): void { | ||
$canValidateFieldTraitStub = new CanValidateFieldTraitStub(); | ||
|
||
$validValidationResult = new ValidationResult(); | ||
|
||
$this->assertNull( | ||
$canValidateFieldTraitStub->publicWrapperMethodForGetFieldViolationMethod( $validValidationResult ) | ||
); | ||
} | ||
|
||
public function testGivenResultWithConstraints_returnsConstraintViolations(): void { | ||
$canValidateFieldTraitStub = new CanValidateFieldTraitStub(); | ||
|
||
$constraintViolation = new ConstraintViolation( "a", "b" ); | ||
$validValidationResult = new ValidationResult( $constraintViolation ); | ||
|
||
$this->assertEquals( | ||
$constraintViolation, | ||
$canValidateFieldTraitStub->publicWrapperMethodForGetFieldViolationMethod( $validValidationResult ) | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
<?php | ||
|
||
declare( strict_types = 1 ); | ||
|
||
namespace WMDE\FunValidators\Tests\Unit; | ||
|
||
use WMDE\FunValidators\CanValidateField; | ||
use WMDE\FunValidators\ConstraintViolation; | ||
use WMDE\FunValidators\ValidationResult; | ||
|
||
class CanValidateFieldTraitStub { | ||
|
||
use CanValidateField; | ||
|
||
public function publicWrapperMethodForGetFieldViolationMethod( ValidationResult $validationResult ): ?ConstraintViolation { | ||
return $this->getFieldViolation( | ||
$validationResult, | ||
"a field name" | ||
); | ||
} | ||
|
||
} |