. namespace hoplite\test; use \hoplite\base as base; require_once HOPLITE_ROOT . '/base/weak_interface.php'; interface AllOptional { public function DoSomething(); public function DoSomething1(array $a); } class AllOptionalImp { public $do_something = FALSE; public function DoSomething() { $this->do_something = TRUE; return 'foo'; } } class AllOptionalImpBad { public function DoSomething1($a, $b) {} } interface OneRequired { public function DoSomething(); /** @required */ public function DoRequired(); } class OneRequiredImp { public $do_required = FALSE; public function DoRequired() { $this->do_required = TRUE; } } class WeakInterfaceTest extends \PHPUnit_Framework_TestCase { public function testAllOptional() { $delegate = new base\WeakInterface('hoplite\test\AllOptional'); $delegate->Bind(new AllOptionalImp); $this->assertEquals('foo', $delegate->DoSomething()); $this->assertTrue($delegate->get()->do_something); $delegate->DoSomething1(); } public function testOneRequired() { $delegate = new base\WeakInterface('hoplite\test\OneRequired'); $delegate->Bind(new OneRequiredImp); $delegate->DoRequired(); $this->assertTrue($delegate->get()->do_required); $delegate->DoSomething(); } public function testRequirements() { $this->setExpectedException('hoplite\base\WeakInterfaceException'); $delegate = new base\WeakInterface('hoplite\test\OneRequired'); $delegate->Bind(new AllOptionalImp); } public function testNull() { $delegate = new base\WeakInterface('hoplite\test\AllOptional'); $delegate->DoSomething(); } public function testNullAllowed() { $this->setExpectedException('hoplite\base\WeakInterfaceException'); $delegate = new base\WeakInterface('hoplite\test\AllOptional'); $delegate->set_null_allowed(FALSE); $delegate->DoSomething(); } public function testMethodSignatures() { $this->setExpectedException('hoplite\base\WeakInterfaceException'); $delegate = new base\WeakInterface('hoplite\test\AllOptional'); $delegate->Bind(new AllOptionalImpBad); } public function testTiming() { $delegate = new base\WeakInterface('hoplite\test\AllOptional'); $imp = new AllOptionalImp; $delegate->Bind($imp); $mt_s = microtime(TRUE); $delegate->DoSomething(); $mt_e = microtime(TRUE); print 'WeakInterface: ' . ($mt_e - $mt_s) . 'µs' . "\n"; $mt_s = microtime(TRUE); $imp->DoSomething(); $mt_e = microtime(TRUE); print 'Straight Call: ' . ($mt_e - $mt_s) . 'µs' . "\n"; } }