Testing SumOperator
All these classes are used by RxPHP to test its own code. Now we'll use them to test our own Observables and operators as well.
For testing purposes, we're going to write a simple operator that calculates the sum of all the integers it receives. When an onComplete
arrives, it emits a single onNext
with the sum of all numbers. It also emits onError
when a non-integer value arrives:
// SumOperator.php class SumOperator implements OperatorInterface { private $sum = 0; function __invoke($observable, $observer, $scheduler=null) { $observable->subscribe(new CallbackObserver( function($value) use ($observer) { if (is_int($value)) { $this->sum += $value; } else { $observer->onError(new Exception()); } }, [$observer, 'onError'], function() use ($observer) { $observer->onNext($this->sum); ...