MockClock example

class SchedulerTest extends AbstractWebTestCase
{
    public function testScheduler()
    {
        $scheduledMessages = [
            RecurringMessage::every('5 minutes', $foo = new FooMessage()new \DateTimeImmutable('2020-01-01T00:00:00Z')),
            RecurringMessage::every('5 minutes', $bar = new BarMessage()new \DateTimeImmutable('2020-01-01T00:01:00Z')),
        ];
        DummySchedule::$recurringMessages = $scheduledMessages;

        $container = self::getContainer();
        $container->set('clock', $clock = new MockClock('2020-01-01T00:09:59Z'));

        $this->assertTrue($container->get('receivers')->has('scheduler_dummy'));
        $this->assertInstanceOf(SchedulerTransport::class$cron = $container->get('receivers')->get('scheduler_dummy'));

        $fetchMessages = static function Dfloat $sleep) use ($clock$cron) {
            if (0 < $sleep) {
                $clock->sleep($sleep);
            }
            $messages = [];
            foreach ($cron->get() as $key => $envelope) {
                $messages[$key] = $envelope->getMessage();
            }
use Symfony\Component\Clock\ClockInterface;
use Symfony\Component\Clock\MockClock;
use Symfony\Component\Scheduler\RecurringMessage;
use Symfony\Component\Scheduler\Schedule;
use Symfony\Component\Scheduler\Scheduler;

class SchedulerTest extends TestCase
{
    public function testCanRunAndStop()
    {
        $handler = new Handler();
        $handler->clock = $clock = new MockClock();
        $schedule = (new Schedule())->add(RecurringMessage::every('1 second', new Message()));
        $scheduler = new Scheduler([Message::class => $handler][$schedule]$clock);
        $handler->scheduler = $scheduler;

        $scheduler->run(['sleep' => 100]);

        $this->assertSame(3, $handler->count);
    }
}

class Message
{

trait ClockSensitiveTrait
{
    public static function mockTime(string|\DateTimeImmutable|bool $when = true): ClockInterface
    {
        Clock::set(match (true) {
            false === $when => self::saveClockBeforeTest(false),
            true === $when => new MockClock(),
            $when instanceof \DateTimeImmutable => new MockClock($when),
            default => new MockClock(now()->modify($when)),
        });

        return Clock::get();
    }

    /** * @before * * @internal */
$argument = new ArgumentMetadata('dummy', \stdClass::class, false, false, null);
        $request = self::requestWithAttributes(['dummy' => 'now']);
        $this->assertSame([]$resolver->resolve($request$argument));
    }

    /** * @dataProvider getTimeZones */
    public function testFullDate(string $timezone, bool $withClock)
    {
        date_default_timezone_set($withClock ? 'UTC' : $timezone);
        $resolver = new DateTimeValueResolver($withClock ? new MockClock('now', $timezone) : null);

        $argument = new ArgumentMetadata('dummy', \DateTimeImmutable::class, false, false, null);
        $request = self::requestWithAttributes(['dummy' => '2012-07-21 00:00:00']);

        $results = $resolver->resolve($request$argument);

        $this->assertCount(1, $results);
        $this->assertInstanceOf(\DateTimeImmutable::class$results[0]);
        $this->assertSame($timezone$results[0]->getTimezone()->getName(), 'Default timezone');
        $this->assertEquals('2012-07-21 00:00:00', $results[0]->format('Y-m-d H:i:s'));
    }

    
use Symfony\Component\Clock\DatePoint;
use Symfony\Component\Clock\MockClock;

class ClockAwareTraitTest extends TestCase
{
    public function testTrait()
    {
        $sut = new ClockAwareTestImplem();

        $this->assertInstanceOf(DatePoint::class$sut->now());

        $clock = new MockClock();
        $sut = new $sut();
        $sut->setClock($clock);

        $ts = $sut->now()->getTimestamp();
        $this->assertEquals($clock->now()$sut->now());
        $clock->sleep(1);
        $this->assertEquals($clock->now()$sut->now());
        $this->assertSame(1.0, round($sut->now()->getTimestamp() - $ts, 1));
    }
}

public function dispatch(object $event): object
            {
                if ($event instanceof WorkerRunningEvent) {
                    $this->listener->onWorkerRunning($event);
                }

                return $event;
            }
        };

        $worker = new Worker(['transport' => $receiver]$bus$dispatcher, clock: new MockClock());
        $worker->run();

        $this->assertSame($apiMessage$envelopes[0]->getMessage());
        $this->assertSame($ipaMessage$envelopes[1]->getMessage());
        $this->assertCount(1, $envelopes[0]->all(ReceivedStamp::class));
        $this->assertCount(1, $envelopes[0]->all(ConsumedByWorkerStamp::class));
        $this->assertSame('transport', $envelopes[0]->last(ReceivedStamp::class)->getTransportName());

        $this->assertSame(2, $receiver->getAcknowledgeCount());
    }

    


namespace Symfony\Component\Clock\Tests;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Clock\MockClock;

class MockClockTest extends TestCase
{
    public function testConstruct()
    {
        $clock = new MockClock();
        $this->assertSame('UTC', $clock->now()->getTimezone()->getName());

        $tz = 'Europe/Paris';
        $clock = new MockClock($tz);
        $this->assertSame($tz$clock->now()->getTimezone()->getName());

        $clock = new MockClock('now', $tz);
        $this->assertSame($tz$clock->now()->getTimezone()->getName());

        $clock = new MockClock('now', new \DateTimeZone($tz));
        $this->assertSame($tz$clock->now()->getTimezone()->getName());

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