MessageContext example

namespace Symfony\Component\Scheduler\Tests\Trigger;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Scheduler\Generator\MessageContext;
use Symfony\Component\Scheduler\Trigger\CallbackMessageProvider;
use Symfony\Component\Scheduler\Trigger\TriggerInterface;

class CallbackMessageProviderTest extends TestCase
{
    public function testToString()
    {
        $context = new MessageContext('test', 'test', $this->createMock(TriggerInterface::class)$this->createMock(\DateTimeImmutable::class));
        $messageProvider = new CallbackMessageProvider(fn () => []);
        $this->assertEquals([]$messageProvider->getMessages($context));
        $this->assertEquals('', $messageProvider->getId());

        $messageProvider = new CallbackMessageProvider(fn () => [new \stdClass()], '');
        $this->assertEquals([new \stdClass()]$messageProvider->getMessages($context));
        $this->assertSame('', $messageProvider->getId());

        $messageProvider = new CallbackMessageProvider(fn () => yield new \stdClass(), 'foo');
        $this->assertInstanceOf(\Generator::class$messageProvider->getMessages($context));
        $this->assertSame('foo', $messageProvider->getId());
    }
$time = $lastTime;
                $yield = false;
            } elseif ($time == $lastTime && $index <= $lastIndex) {
                $yield = false;
            }

            if ($nextTime = $trigger->getNextRunDate($time)) {
                $heap->insert([$nextTime$index$recurringMessage]);
            }

            if ($yield) {
                $context = new MessageContext($this->name, $id$trigger$time$nextTime);
                foreach ($recurringMessage->getMessages($context) as $message) {
                    yield $context => $message;
                }

                $checkpoint->save($time$index);
            }
        }

        $this->waitUntil = $heap->isEmpty() ? null : $heap->top()[0];

        $checkpoint->release($now$this->waitUntil);
    }

    public function testGetFromIterator()
    {
        $messages = [
            (object) ['id' => 'first'],
            (object) ['id' => 'second'],
        ];
        $generator = $this->createMock(MessageGeneratorInterface::class);
        $generator->method('getMessages')->willReturnCallback(function D) use ($messages): \Generator {
            $trigger = $this->createMock(TriggerInterface::class);
            $triggerAt = new \DateTimeImmutable('2020-02-20T02:00:00', new \DateTimeZone('UTC'));
            yield (new MessageContext('default', 'id1', $trigger$triggerAt)) => $messages[0];
            yield (new MessageContext('default', 'id2', $trigger$triggerAt)) => $messages[1];
        });
        $transport = new SchedulerTransport($generator);

        foreach ($transport->get() as $i => $envelope) {
            $this->assertInstanceOf(Envelope::class$envelope);
            $this->assertNotNull($stamp = $envelope->last(ScheduledStamp::class));
            $this->assertSame(array_shift($messages)$envelope->getMessage());
            $this->assertSame('default', $stamp->messageContext->name);
            $this->assertSame('id'.$i + 1, $stamp->messageContext->id);
        }

        
Home | Imprint | This part of the site doesn't use cookies.