1: <?php
2:
3: namespace Alchemy\tests;
4: use Alchemy\util\promise\Waitable;
5: use Alchemy\util\promise\TypeException;
6: use Alchemy\util\promise\TimeoutException;
7:
8:
9: class WaitableTest extends BaseTest {
10: public function testResolution() {
11: $waitable = new Waitable();
12:
13: $this->assertFalse($waitable->check());
14: $this->assertSame(null, $waitable());
15:
16: $waitable->resolve(null);
17: $this->assertFalse($waitable->check());
18: $this->assertSame(null, $waitable());
19:
20:
21: $waitable->resolve($obj = new \Exception());
22: $this->assertTrue($waitable->check());
23: $this->assertSame($obj, $waitable());
24:
25: $waitable->resolve(5);
26: $this->assertTrue($waitable->check());
27: $this->assertSame(5, $waitable());
28:
29:
30: $waitable->resolve(null);
31: $this->assertFalse($waitable->check());
32: $this->assertSame(null, $waitable());
33: }
34:
35:
36: public function testTypeExceptions() {
37: $waitable = new Waitable("stdClass");
38:
39: $this->assertEquals("stdClass", $waitable->type());
40:
41:
42: $waitable->resolve($obj = new \stdClass());
43: $this->assertTrue($waitable->check());
44: $this->assertSame($obj, $waitable());
45:
46:
47: $waitable->resolve($obj = new \Alchemy\core\query\Scalar(4));
48: $this->assertTrue($waitable->check());
49: $this->assertInstanceOf("Alchemy\util\promise\TypeException", $waitable());
50:
51:
52: $waitable->resolve($obj = new \Exception());
53: $this->assertTrue($waitable->check());
54: $this->assertInstanceOf("Exception", $waitable());
55:
56: $waitable->resolve($obj = new Waitable());
57: $this->assertTrue($waitable->check());
58: $this->assertInstanceOf("Alchemy\util\promise\Waitable", $waitable());
59: }
60:
61:
62: public function testWaitResolve() {
63: $waitable = new Waitable();
64:
65:
66: $waitable->timeout();
67:
68:
69: $this->assertInstanceOf("Alchemy\util\promise\TimeoutException", $waitable->wait());
70: $this->assertThrows("Alchemy\util\promise\TimeoutException",
71: array($waitable, 'expect'));
72:
73:
74: $waitable->resolve($obj = new \Exception());
75: $this->assertThrows($obj, array($waitable, 'expect'));
76:
77:
78: $waitable->resolve(5);
79: $this->assertSame(5, $waitable->wait());
80: $this->assertSame(5, $waitable->expect());
81: }
82: }