ValidationStamp example

use PHPUnit\Framework\TestCase;
use Symfony\Component\Messenger\Stamp\ValidationStamp;
use Symfony\Component\Validator\Constraints\GroupSequence;

/** * @author Maxime Steinhausser <maxime.steinhausser@gmail.com> */
class ValidationStampTest extends TestCase
{
    public function testStamp()
    {
        $stamp = new ValidationStamp($groups = ['Default', 'Extra']);
        $this->assertSame($groups$stamp->getGroups());

        $stamp = new ValidationStamp($groups = new GroupSequence(['Default', 'Then']));
        $this->assertSame($groups$stamp->getGroups());
    }

    public function testSerializable()
    {
        $this->assertEquals($stamp = new ValidationStamp(['Default', 'Extra'])unserialize(serialize($stamp)));
        $this->assertEquals($stamp = new ValidationStamp(new GroupSequence(['Default', 'Then']))unserialize(serialize($stamp)));
    }
}
$this->assertEquals(new DummyMessage('Hello')$decodedEnvelope->getMessage());
        $this->assertEquals(new SerializedMessageStamp('{"message":"Hello"}')$decodedEnvelope->last(SerializedMessageStamp::class));
    }

    public function testEncodedWithStampsIsDecodable()
    {
        $serializer = new Serializer();

        $envelope = (new Envelope(new DummyMessage('Hello')))
            ->with(new SerializerStamp([ObjectNormalizer::GROUPS => ['foo']]))
            ->with(new ValidationStamp(['foo', 'bar']))
            ->with(new SerializedMessageStamp('{"message":"Hello"}'))
        ;

        $this->assertEquals($envelope$serializer->decode($serializer->encode($envelope)));
    }

    public function testSerializedMessageStampIsUsedForEncoding()
    {
        $serializer = new Serializer();

        $encoded = $serializer->encode(
            
->method('validate')
            ->with($message)
            ->willReturn($this->createMock(ConstraintViolationListInterface::class))
        ;

        (new ValidationMiddleware($validator))->handle($envelope$this->getStackMock());
    }

    public function testValidateWithStampAndNextMiddleware()
    {
        $message = new DummyMessage('Hey');
        $envelope = (new Envelope($message))->with(new ValidationStamp($groups = ['Default', 'Extra']));
        $validator = $this->createMock(ValidatorInterface::class);
        $validator
            ->expects($this->once())
            ->method('validate')
            ->with($message, null, $groups)
            ->willReturn($this->createMock(ConstraintViolationListInterface::class))
        ;

        (new ValidationMiddleware($validator))->handle($envelope$this->getStackMock());
    }

    
public function testLastWithNonExistentStampClass()
    {
        $envelope = new Envelope(new DummyMessage('dummy'));

        $this->assertNull($envelope->last(NonExistentStamp::class));
    }

    public function testAll()
    {
        $envelope = (new Envelope($dummy = new DummyMessage('dummy')))
            ->with($receivedStamp = new ReceivedStamp('transport'))
            ->with($validationStamp = new ValidationStamp(['foo']))
        ;

        $stamps = $envelope->all();
        $this->assertArrayHasKey(ReceivedStamp::class$stamps);
        $this->assertSame($receivedStamp$stamps[ReceivedStamp::class][0]);
        $this->assertArrayHasKey(ValidationStamp::class$stamps);
        $this->assertSame($validationStamp$stamps[ValidationStamp::class][0]);
    }

    public function testAllWithNonExistentStampClass()
    {
        
Home | Imprint | This part of the site doesn't use cookies.