1<?php
2
3namespace React\Promise;
4
5class CancellationQueueTest extends TestCase
6{
7    /** @test */
8    public function acceptsSimpleCancellableThenable()
9    {
10        $p = new SimpleTestCancellableThenable();
11
12        $cancellationQueue = new CancellationQueue();
13        $cancellationQueue->enqueue($p);
14
15        $cancellationQueue();
16
17        $this->assertTrue($p->cancelCalled);
18    }
19
20    /** @test */
21    public function ignoresSimpleCancellable()
22    {
23        $p = new SimpleTestCancellable();
24
25        $cancellationQueue = new CancellationQueue();
26        $cancellationQueue->enqueue($p);
27
28        $cancellationQueue();
29
30        $this->assertFalse($p->cancelCalled);
31    }
32
33    /** @test */
34    public function callsCancelOnPromisesEnqueuedBeforeStart()
35    {
36        $d1 = $this->getCancellableDeferred();
37        $d2 = $this->getCancellableDeferred();
38
39        $cancellationQueue = new CancellationQueue();
40        $cancellationQueue->enqueue($d1->promise());
41        $cancellationQueue->enqueue($d2->promise());
42
43        $cancellationQueue();
44    }
45
46    /** @test */
47    public function callsCancelOnPromisesEnqueuedAfterStart()
48    {
49        $d1 = $this->getCancellableDeferred();
50        $d2 = $this->getCancellableDeferred();
51
52        $cancellationQueue = new CancellationQueue();
53
54        $cancellationQueue();
55
56        $cancellationQueue->enqueue($d2->promise());
57        $cancellationQueue->enqueue($d1->promise());
58    }
59
60    /** @test */
61    public function doesNotCallCancelTwiceWhenStartedTwice()
62    {
63        $d = $this->getCancellableDeferred();
64
65        $cancellationQueue = new CancellationQueue();
66        $cancellationQueue->enqueue($d->promise());
67
68        $cancellationQueue();
69        $cancellationQueue();
70    }
71
72    /** @test */
73    public function rethrowsExceptionsThrownFromCancel()
74    {
75        $this->setExpectedException('\Exception', 'test');
76
77        $mock = $this
78            ->getMockBuilder('React\Promise\CancellablePromiseInterface')
79            ->getMock();
80        $mock
81            ->expects($this->once())
82            ->method('cancel')
83            ->will($this->throwException(new \Exception('test')));
84
85        $cancellationQueue = new CancellationQueue();
86        $cancellationQueue->enqueue($mock);
87
88        $cancellationQueue();
89    }
90
91    private function getCancellableDeferred()
92    {
93        $mock = $this->createCallableMock();
94        $mock
95            ->expects($this->once())
96            ->method('__invoke');
97
98        return new Deferred($mock);
99    }
100}
101