<?php

/**
 * TEST: Timer test.
 */

declare(strict_types=1);
use Tester\Assert;
require(__DIR__ . '/../bootstrap.php');

$loop = React\EventLoop\Factory::create();

$callbackCount = 0;
$promiseCount = 0;

$timer = new Awaited\Timer($loop, .05, true, function(Awaited\Timer $timer) use(&$callbackCount): void {

	if (++$callbackCount % 5 === 0) {
		$timer->stop();
	}

}, true);

Assert::same(0.05, $timer->getInterval());
Assert::true($timer->isRunning());
Assert::true($timer->isPeriodic());
Assert::false($timer->elapsed());

$timer->await()
	->then(function(Awaited\Timer $timer) use(&$promiseCount): void {
		Assert::true($timer->elapsed());
		$promiseCount++;
	});

$loop->run();

Assert::true($timer->elapsed());
Assert::false($timer->isRunning());
Assert::false($timer->isPaused());
Assert::same(5, $callbackCount);
Assert::same(1, $promiseCount);