initial checkin
[m6w6/hikke] / tests / hikke / ProxyTest.php
1 <?php
2
3 namespace hikke;
4
5 require_once __DIR__."/../../vendor/autoload.php";
6 require_once __DIR__."/TestObserver.php";
7
8 class ProxyTest extends \PHPUnit_Framework_TestCase
9 {
10 function setUp() {
11 TestObserver::reset();
12 }
13
14 function testBasic() {
15 $proxy = new Event\Proxy(["first","second"]);
16 $this->assertInstanceOf("hikke\\Event", $proxy->first);
17 $this->assertInstanceOf("hikke\\Event", $proxy->second);
18 }
19
20 function testAutoInsert() {
21 $proxy = new Event\Proxy;
22 $this->assertFalse(isset($proxy->event));
23 $this->assertInstanceOf("hikke\\Event", $proxy->event);
24 $this->assertTrue(isset($proxy->event));
25 unset($proxy->event);
26 $this->assertFalse(isset($proxy->event));
27 }
28
29 function testExplicitInsert() {
30 $proxy = new Event\Proxy;
31 $this->assertFalse(isset($proxy->event));
32 $proxy->event = 1.23;
33 $this->assertTrue(isset($proxy->event));
34 $this->assertInstanceOf("hikke\Event", $proxy->event);
35 $this->assertEquals(1.23, round($proxy->event->getPriority(),2));
36 }
37
38 function testProxy() {
39 $proxy = new Event\Proxy(["one", "two", "three"]);
40 $observer = new TestObserver("observer");
41 $proxy->attach($observer);
42 $proxy->notify();
43 $this->assertEquals(
44 "observer: one\n".
45 "observer: two\n".
46 "observer: three\n",
47 TestObserver::$logs);
48 }
49
50 function testProxyProxy() {
51 $proxied = new Event\Proxy(["one", "two", "three"]);
52 $observer = new TestObserver("observer");
53 $proxied->attach($observer);
54 $proxy = new Event\Proxy;
55 $proxy->attach($proxied);
56 $proxy->notify();
57 $this->assertEquals(
58 "observer: default:one\n".
59 "observer: default:two\n".
60 "observer: default:three\n",
61 TestObserver::$logs);
62 }
63
64 function testProxyCall() {
65 $proxy = new Event\Proxy;
66 $observer = new TestObserver("call");
67 $proxy->attach($observer);
68 $arg = (object) ["data" => null];
69 $proxy->callMe($arg);
70 $this->assertEquals("hikke\\TestObserver::callMe\n", $arg->data);
71 }
72
73 function testIterator() {
74 $proxy = new Event\Proxy;
75 $proxy->attach(new TestObserver("o1"));
76 $proxy->attach(new TestObserver("o2"));
77 $proxy->attach(new TestObserver("o3"));
78 $string = "";
79 foreach ($proxy as $event) {
80 $string .= $event;
81 foreach ($event as $observer) {
82 $string .= ":$observer";
83 }
84 }
85 $this->assertEquals("default:o1:o2:o3", $string);
86 }
87
88 function testCountAndDetach() {
89 $rcount = function($proxy) {
90 return array_sum(array_map(function($e) {
91 return count($e);
92 }, iterator_to_array($proxy)));
93 };
94 $proxy = new Event\Proxy(["e1","e2"]);
95 $observer1 = new TestObserver("o1");
96 $proxy->attach($observer1);
97 $this->assertEquals(2, count($proxy));
98 $observer2 = new TestObserver("o2");
99 $proxy->attach($observer2);
100 $this->assertEquals(4, $rcount($proxy));
101 $proxy->detach($observer1);
102 $this->assertEquals(2, $rcount($proxy));
103 $proxy->detach($observer2, "e2");
104 $this->assertEquals(1, $rcount($proxy));
105 }
106
107 function testAssignTwice() {
108 $proxy = new Event\Proxy;
109 $proxy->ev0 = new Event("ev0", 0);
110 $this->setExpectedException("UnexpectedValueException", "The event name 'ev0' is already in use");
111 $proxy->ev0 = 0;
112 }
113
114 function testAssignDifferent() {
115 $proxy = new Event\Proxy;
116 $this->setExpectedException("UnexpectedValueException", "The event names differ: 'ev3' <> 'ev4'");
117 $proxy->ev3 = new Event("ev4");
118 }
119 }