3 // Copyright (c) 2011 Blue Static
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.
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
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/>.
17 namespace hoplite
\test
;
18 use \hoplite
\base
as base
;
20 require_once HOPLITE_ROOT
. '/base/weak_interface.php';
24 public function DoSomething();
25 public function DoSomething1(array $a);
30 public $do_something = FALSE
;
32 public function DoSomething()
34 $this->do_something
= TRUE
;
39 class AllOptionalImpBad
41 public function DoSomething1($a, $b) {}
46 public function DoSomething();
49 public function DoRequired();
54 public $do_required = FALSE
;
56 public function DoRequired()
58 $this->do_required
= TRUE
;
62 class WeakInterfaceTest
extends \PHPUnit_Framework_TestCase
64 public function testAllOptional()
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();
73 public function testOneRequired()
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();
82 public function testRequirements()
84 $this->setExpectedException('hoplite\base\WeakInterfaceException');
85 $delegate = new base\
WeakInterface('hoplite\test\OneRequired');
86 $delegate->Bind(new AllOptionalImp
);
89 public function testNull()
91 $delegate = new base\
WeakInterface('hoplite\test\AllOptional');
92 $delegate->DoSomething();
95 public function testNullAllowed()
97 $this->setExpectedException('hoplite\base\WeakInterfaceException');
98 $delegate = new base\
WeakInterface('hoplite\test\AllOptional');
99 $delegate->set_null_allowed(FALSE
);
100 $delegate->DoSomething();
103 public function testMethodSignatures()
105 $this->setExpectedException('hoplite\base\WeakInterfaceException');
106 $delegate = new base\
WeakInterface('hoplite\test\AllOptional');
107 $delegate->Bind(new AllOptionalImpBad
);
110 public function testTiming()
112 $delegate = new base\
WeakInterface('hoplite\test\AllOptional');
113 $imp = new AllOptionalImp
;
114 $delegate->Bind($imp);
116 $mt_s = microtime(TRUE
);
117 $delegate->DoSomething();
118 $mt_e = microtime(TRUE
);
119 print 'WeakInterface: ' . ($mt_e - $mt_s) . 'µs' . "\n";
121 $mt_s = microtime(TRUE
);
123 $mt_e = microtime(TRUE
);
124 print 'Straight Call: ' . ($mt_e - $mt_s) . 'µs' . "\n";