Testing RxPHP code
Since Chapter 2, Reactive Programming with RxPHP, where we introduced Schedulers, we've been using them via ImmediateScheduler and EventLoopScheduler. Internally, EventLoopScheduler extends another Scheduler, called VirtualTimeScheduler, which is also used internally by TestScheduler, which we'll use for testing in a moment. But before we do that, let's see what's so interesting about VirtualTimeScheduler.
Introducing VirtualTimeScheduler
With ImmediateScheduler, everything is executed immediately. The VirtualTimeScheduler keeps a priority queue of actions to be executed and gives us control over the order they're called.
In this example, we'll make an instance of VirtualTimeScheduler and stack a couple of actions that will be executed with different delays using the schedule($actionCallable, $delay) method:
// virtual_time_scheduler_01.php
use Rx\Scheduler\VirtualTimeScheduler;
$scheduler = new VirtualTimeScheduler(0, function($a, $b) {
return...