Fix more bugs in TemplateLoader and a singleton helper
[hoplite.git] / testing / tests / base / weak_interface_test.php
1 <?php
2 // Hoplite
3 // Copyright (c) 2011 Blue Static
4 //
5 // This program is free software: you can redistribute it and/or modify it
6 // under the terms of the GNU General Public License as published by the Free
7 // Software Foundation, either version 3 of the License, or any later version.
8 //
9 // This program is distributed in the hope that it will be useful, but WITHOUT
10 // ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 // more details.
13 //
14 // You should have received a copy of the GNU General Public License along with
15 // this program. If not, see <http://www.gnu.org/licenses/>.
16
17 namespace hoplite\test;
18 use \hoplite\base as base;
19
20 require_once HOPLITE_ROOT . '/base/weak_interface.php';
21
22 interface AllOptional
23 {
24 public function DoSomething();
25 public function DoSomething1(array $a);
26 }
27
28 class AllOptionalImp
29 {
30 public $do_something = FALSE;
31
32 public function DoSomething()
33 {
34 $this->do_something = TRUE;
35 return 'foo';
36 }
37 }
38
39 class AllOptionalImpBad
40 {
41 public function DoSomething1($a, $b) {}
42 }
43
44 interface OneRequired
45 {
46 public function DoSomething();
47
48 /** @required */
49 public function DoRequired();
50 }
51
52 class OneRequiredImp
53 {
54 public $do_required = FALSE;
55
56 public function DoRequired()
57 {
58 $this->do_required = TRUE;
59 }
60 }
61
62 class WeakInterfaceTest extends \PHPUnit_Framework_TestCase
63 {
64 public function testAllOptional()
65 {
66 $delegate = new base\WeakInterface('hoplite\test\AllOptional');
67 $delegate->Bind(new AllOptionalImp);
68 $this->assertEquals('foo', $delegate->DoSomething());
69 $this->assertTrue($delegate->get()->do_something);
70 $delegate->DoSomething1();
71 }
72
73 public function testOneRequired()
74 {
75 $delegate = new base\WeakInterface('hoplite\test\OneRequired');
76 $delegate->Bind(new OneRequiredImp);
77 $delegate->DoRequired();
78 $this->assertTrue($delegate->get()->do_required);
79 $delegate->DoSomething();
80 }
81
82 public function testRequirements()
83 {
84 $this->setExpectedException('hoplite\base\WeakInterfaceException');
85 $delegate = new base\WeakInterface('hoplite\test\OneRequired');
86 $delegate->Bind(new AllOptionalImp);
87 }
88
89 public function testNull()
90 {
91 $delegate = new base\WeakInterface('hoplite\test\AllOptional');
92 $delegate->DoSomething();
93 }
94
95 public function testNullAllowed()
96 {
97 $this->setExpectedException('hoplite\base\WeakInterfaceException');
98 $delegate = new base\WeakInterface('hoplite\test\AllOptional');
99 $delegate->set_null_allowed(FALSE);
100 $delegate->DoSomething();
101 }
102
103 public function testMethodSignatures()
104 {
105 $this->setExpectedException('hoplite\base\WeakInterfaceException');
106 $delegate = new base\WeakInterface('hoplite\test\AllOptional');
107 $delegate->Bind(new AllOptionalImpBad);
108 }
109
110 public function testTiming()
111 {
112 $delegate = new base\WeakInterface('hoplite\test\AllOptional');
113 $imp = new AllOptionalImp;
114 $delegate->Bind($imp);
115
116 $mt_s = microtime(TRUE);
117 $delegate->DoSomething();
118 $mt_e = microtime(TRUE);
119 print 'WeakInterface: ' . ($mt_e - $mt_s) . 'µs' . "\n";
120
121 $mt_s = microtime(TRUE);
122 $imp->DoSomething();
123 $mt_e = microtime(TRUE);
124 print 'Straight Call: ' . ($mt_e - $mt_s) . 'µs' . "\n";
125 }
126 }