-
-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
239 additions
and
9 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
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
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
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
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,30 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Zenstruck\Foundry\Object; | ||
|
||
use Symfony\Component\Validator\Exception\ValidationFailedException; | ||
use Symfony\Component\Validator\Validator\ValidatorInterface; | ||
use Zenstruck\Foundry\Object\Event\AfterInstantiate; | ||
|
||
final class ValidationListener | ||
{ | ||
public function __construct( | ||
private readonly ValidatorInterface $validator | ||
) { | ||
} | ||
|
||
public function __invoke(AfterInstantiate $event): void | ||
{ | ||
if (!$event->factory->validationEnabled()) { | ||
return; | ||
} | ||
|
||
$violations = $this->validator->validate($event->object); | ||
|
||
if ($violations->count() > 0) { | ||
throw new ValidationFailedException($event->object, $violations); | ||
} | ||
} | ||
} |
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
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
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
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,29 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
/* | ||
* This file is part of the zenstruck/foundry package. | ||
* | ||
* (c) Kevin Bond <[email protected]> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
namespace Zenstruck\Foundry\Tests\Fixture\Entity; | ||
|
||
use Doctrine\ORM\Mapping as ORM; | ||
use Zenstruck\Foundry\Tests\Fixture\Model\Base; | ||
use Symfony\Component\Validator\Constraints as Assert; | ||
|
||
#[ORM\Entity] | ||
class EntityForValidation extends Base | ||
{ | ||
public function __construct( | ||
#[ORM\Column()] | ||
#[Assert\NotBlank()] | ||
public string $name = '', | ||
) { | ||
} | ||
} |
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,2 @@ | ||
framework: | ||
validation: true |
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,6 @@ | ||
framework: | ||
validation: true | ||
|
||
zenstruck_foundry: | ||
instantiator: | ||
validation: true |
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,3 @@ | ||
zenstruck_foundry: | ||
instantiator: | ||
validation: true |
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,70 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace Zenstruck\Foundry\Tests\Integration; | ||
|
||
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase; | ||
use Symfony\Component\Validator\Exception\ValidationFailedException; | ||
use Zenstruck\Foundry\Test\Factories; | ||
use Zenstruck\Foundry\Tests\Fixture\Entity\EntityForValidation; | ||
|
||
use function Zenstruck\Foundry\factory; | ||
use function Zenstruck\Foundry\object; | ||
|
||
final class ValidationTest extends KernelTestCase | ||
{ | ||
use Factories; | ||
|
||
public function test_it_does_not_validate_object_if_validation_not_enabled(): void | ||
{ | ||
self::expectNotToPerformAssertions(); | ||
|
||
object(EntityForValidation::class); | ||
} | ||
|
||
public function test_it_throws_if_trying_to_validate_with_validation_not_available(): void | ||
{ | ||
self::expectException(\LogicException::class); | ||
self::expectExceptionMessage('Validation is not available.'); | ||
|
||
factory(EntityForValidation::class)->withValidation()->create(); | ||
} | ||
|
||
public function test_it_throws_if_validation_enabled_in_foundry_but_disabled_in_symfony(): void | ||
{ | ||
self::expectException(\LogicException::class); | ||
self::expectExceptionMessage('Validation support cannot be enabled'); | ||
|
||
self::bootKernel(['environment' => 'validation_not_available']); | ||
|
||
object(EntityForValidation::class); | ||
} | ||
|
||
public function test_it_validates_object_if_validation_forced(): void | ||
{ | ||
self::expectException(ValidationFailedException::class); | ||
|
||
self::bootKernel(['environment' => 'validation_available']); | ||
|
||
factory(EntityForValidation::class)->withValidation()->create(); | ||
} | ||
|
||
public function test_it_validates_object_if_validation_enabled_globally(): void | ||
{ | ||
self::expectException(ValidationFailedException::class); | ||
|
||
self::bootKernel(['environment' => 'validation_enabled']); | ||
|
||
object(EntityForValidation::class); | ||
} | ||
|
||
public function test_validation_can_be_disabled(): void | ||
{ | ||
self::expectNotToPerformAssertions(); | ||
|
||
self::bootKernel(['environment' => 'validation_enabled']); | ||
|
||
factory(EntityForValidation::class)->withoutValidation()->create(); | ||
} | ||
} |