public function testApiDestroy_CallsStopAndResolvesPromise_WhenInvokedFromStateOtherThanDestroyedAndStopSucceeded()
{
$states = [Runtime::STATE_CREATED, Runtime::STATE_STARTED, Runtime::STATE_STOPPED];
foreach ($states as $state) {
$loop = $this->getMock(Loop::class, [], [], '', false);
$loop->expects($this->once())->method('onTick')->will($this->returnCallback(function ($callable) {
$callable();
}));
$events = [];
$emitter = $this->getMock(EventEmitter::class, [], [], '', false);
$emitter->expects($this->exactly(3))->method('emit')->will($this->returnCallback(function ($event) use(&$events) {
$events[] = $event;
}));
$runtimes = ['A', 'B'];
$manager = $this->getMock(RuntimeManager::class, [], [], '', false);
$manager->expects($this->once())->method('getRuntimes')->will($this->returnValue($runtimes));
$manager->expects($this->once())->method('destroyRuntimes')->with($runtimes, Runtime::DESTROY_FORCE)->will($this->returnValue(true));
$runtime = $this->createModel([], ['getLoop', 'stop', 'getRuntimeManager', 'getEventEmitter', 'setLoopState', 'stopLoop']);
$runtime->expects($this->once())->method('getLoop')->will($this->returnValue($loop));
$runtime->expects($this->once())->method('stop')->will($this->returnValue(new PromiseFulfilled()));
$runtime->expects($this->atLeastOnce())->method('getRuntimeManager')->will($this->returnValue($manager));
$runtime->expects($this->atLeastOnce())->method('getEventEmitter')->will($this->returnValue($emitter));
$runtime->expects($this->once())->method('setLoopState');
$runtime->expects($this->once())->method('stopLoop');
$runtime->setState($state);
$callable = $this->createCallableMock();
$callable->expects($this->once())->method('__invoke')->with($this->isType('string'));
$runtime->destroy()->then($callable, $this->expectCallableNever());
$this->assertSame(['beforeDestroy', 'destroy', 'afterDestroy'], $events);
}
}