1<?php
2
3namespace React\Promise;
4
5use React\Promise\PromiseAdapter\CallbackPromiseAdapter;
6
7class FulfilledPromiseTest extends TestCase
8{
9    use PromiseTest\PromiseSettledTestTrait,
10        PromiseTest\PromiseFulfilledTestTrait;
11
12    public function getPromiseTestAdapter(callable $canceller = null)
13    {
14        $promise = null;
15
16        return new CallbackPromiseAdapter([
17            'promise' => function () use (&$promise) {
18                if (!$promise) {
19                    throw new \LogicException('FulfilledPromise must be resolved before obtaining the promise');
20                }
21
22                return $promise;
23            },
24            'resolve' => function ($value = null) use (&$promise) {
25                if (!$promise) {
26                    $promise = new FulfilledPromise($value);
27                }
28            },
29            'reject' => function () {
30                throw new \LogicException('You cannot call reject() for React\Promise\FulfilledPromise');
31            },
32            'notify' => function () {
33                // no-op
34            },
35            'settle' => function ($value = null) use (&$promise) {
36                if (!$promise) {
37                    $promise = new FulfilledPromise($value);
38                }
39            },
40        ]);
41    }
42
43    /** @test */
44    public function shouldThrowExceptionIfConstructedWithAPromise()
45    {
46        $this->setExpectedException('\InvalidArgumentException');
47
48        return new FulfilledPromise(new FulfilledPromise());
49    }
50
51    /** @test */
52    public function shouldNotLeaveGarbageCyclesWhenRemovingLastReferenceToFulfilledPromiseWithAlwaysFollowers()
53    {
54        gc_collect_cycles();
55        $promise = new FulfilledPromise(1);
56        $promise->always(function () {
57            throw new \RuntimeException();
58        });
59        unset($promise);
60
61        $this->assertSame(0, gc_collect_cycles());
62    }
63
64    /** @test */
65    public function shouldNotLeaveGarbageCyclesWhenRemovingLastReferenceToFulfilledPromiseWithThenFollowers()
66    {
67        gc_collect_cycles();
68        $promise = new FulfilledPromise(1);
69        $promise = $promise->then(function () {
70            throw new \RuntimeException();
71        });
72        unset($promise);
73
74        $this->assertSame(0, gc_collect_cycles());
75    }
76}
77