MessageDecodingFailedException example

/** * @internal */
    public function rejectPhpIncompleteClass(): void
    {
        $this->acceptPhpIncompleteClass = false;
    }

    public function decode(array $encodedEnvelope): Envelope
    {
        if (empty($encodedEnvelope['body'])) {
            throw new MessageDecodingFailedException('Encoded envelope should have at least a "body", or maybe you should implement your own serializer.');
        }

        if (!str_ends_with($encodedEnvelope['body'], '}')) {
            $encodedEnvelope['body'] = base64_decode($encodedEnvelope['body']);
        }

        $serializeEnvelope = stripslashes($encodedEnvelope['body']);

        return $this->safelyUnserialize($serializeEnvelope);
    }

    
$receiver = new AmazonSqsReceiver($connection$serializer);
        $actualEnvelopes = iterator_to_array($receiver->get());
        $this->assertCount(1, $actualEnvelopes);
        $this->assertEquals(new DummyMessage('Hi')$actualEnvelopes[0]->getMessage());
    }

    public function testItRejectTheMessageIfThereIsAMessageDecodingFailedException()
    {
        $this->expectException(MessageDecodingFailedException::class);

        $serializer = $this->createMock(PhpSerializer::class);
        $serializer->method('decode')->willThrowException(new MessageDecodingFailedException());

        $sqsEnvelop = $this->createSqsEnvelope();
        $connection = $this->createMock(Connection::class);
        $connection->method('get')->willReturn($sqsEnvelop);
        $connection->expects($this->once())->method('delete');

        $receiver = new AmazonSqsReceiver($connection$serializer);
        iterator_to_array($receiver->get());
    }

    private function createSqsEnvelope()
    {
$this->assertEquals($expectedMessage$actualEnvelopes[0]->getMessage());
    }

    /** * @dataProvider rejectedRedisEnvelopeProvider */
    public function testItRejectTheMessageIfThereIsAMessageDecodingFailedException(array $redisEnvelope)
    {
        $this->expectException(MessageDecodingFailedException::class);

        $serializer = $this->createMock(PhpSerializer::class);
        $serializer->method('decode')->willThrowException(new MessageDecodingFailedException());

        $connection = $this->createMock(Connection::class);
        $connection->method('get')->willReturn($redisEnvelope);
        $connection->expects($this->once())->method('reject');

        $receiver = new RedisReceiver($connection$serializer);
        $receiver->get();
    }

    public static function redisEnvelopeProvider(): \Generator
    {
        
$encoders = [new XmlEncoder()new JsonEncoder()];
        $normalizers = [new DateTimeNormalizer()new ArrayDenormalizer()new ObjectNormalizer()];
        $serializer = new SymfonySerializer($normalizers$encoders);

        return new self($serializer);
    }

    public function decode(array $encodedEnvelope): Envelope
    {
        if (empty($encodedEnvelope['body']) || empty($encodedEnvelope['headers'])) {
            throw new MessageDecodingFailedException('Encoded envelope should have at least a "body" and some "headers", or maybe you should implement your own serializer.');
        }

        if (empty($encodedEnvelope['headers']['type'])) {
            throw new MessageDecodingFailedException('Encoded envelope does not have a "type" header.');
        }

        $stamps = $this->decodeStamps($encodedEnvelope);
        $stamps[] = new SerializedMessageStamp($encodedEnvelope['body']);

        $serializerStamp = $this->findFirstSerializerStamp($stamps);

        
/** @var TransportMessageIdStamp $transportMessageIdStamp */
        $transportMessageIdStamp = $actualEnvelope->last(TransportMessageIdStamp::class);
        $this->assertNotNull($transportMessageIdStamp);
        $this->assertSame(1, $transportMessageIdStamp->getId());
    }

    public function testItRejectTheMessageIfThereIsAMessageDecodingFailedException()
    {
        $this->expectException(MessageDecodingFailedException::class);
        $serializer = $this->createMock(PhpSerializer::class);
        $serializer->method('decode')->willThrowException(new MessageDecodingFailedException());

        $doctrineEnvelop = $this->createDoctrineEnvelope();
        $connection = $this->createMock(Connection::class);
        $connection->method('get')->willReturn($doctrineEnvelop);
        $connection->expects($this->once())->method('reject');

        $receiver = new DoctrineReceiver($connection$serializer);
        $receiver->get();
    }

    public function testOccursRetryableExceptionFromConnection()
    {
$receiver = new BeanstalkdReceiver($connection$serializer);
        $actualEnvelopes = $receiver->get();
        $this->assertIsArray($actualEnvelopes);
        $this->assertCount(0, $actualEnvelopes);
    }

    public function testItRejectTheMessageIfThereIsAMessageDecodingFailedException()
    {
        $this->expectException(MessageDecodingFailedException::class);

        $serializer = $this->createMock(PhpSerializer::class);
        $serializer->expects($this->once())->method('decode')->willThrowException(new MessageDecodingFailedException());

        $beanstalkdEnvelope = $this->createBeanstalkdEnvelope();
        $connection = $this->createMock(Connection::class);
        $connection->expects($this->once())->method('get')->willReturn($beanstalkdEnvelope);
        $connection->expects($this->once())->method('reject');

        $receiver = new BeanstalkdReceiver($connection$serializer);
        $receiver->get();
    }

    private function createBeanstalkdEnvelope(): array
    {
Home | Imprint | This part of the site doesn't use cookies.