Api.php
<?php
namespace Liaison\Test\Traits;
class Api extends \Taeluf\Tester {
public function hello($name="Anonymous"){
return "Hello {$name}";
}
public function hola($name="Anonymous"){
return "Hola {$name}";
}
protected function bare($debug=false){
$liaison = new \Liaison(['bare'=>true,'debug'=>$debug]);
return $liaison;
}
public function testPrefixMediatorWildcard(){
$lia = $this->bare();
$lia->addMediator('*',
function($mediatorKey, $oldFunc, $newFunc){
if ($mediatorKey!='prefix.hi')return null;
return $newFunc;
}
);
$lia->addPrefix('hi',[$this,'hello']);
$apiKey = $lia->addPrefix('hi',[$this,'hola']);
$name = "Cat";
$target = $this->hola($name);
return true
&& $this->compare($target, $lia->api($apiKey, 'prefix', $name))
;
}
public function testMethodMediatorWildcard(){
$lia = $this->bare();
$lia->addMediator('*',
function($mediatorKey, $oldFunc, $newFunc){
if ($mediatorKey!='method.hello')return null;
return $newFunc;
}
);
$lia->addMethod('hello',[$this,'hello']);
$lia->addMethod('hello',[$this,'hola']);
$name = "Cat";
$target = $this->hola($name);
return true
&& $this->compare($target,$lia->hello($name))
;
}
public function testMethodMediator(){
$lia = $this->bare();
$lia->addMediator('method.hello',
function($funcName, $oldFunc, $newFunc){
return $newFunc;
}
);
$lia->addMethod('hello',[$this,'hello']);
$lia->addMethod('hello',[$this,'hola']);
$name = "Cat";
$target = $this->hola($name);
return true
&& $this->compare($target,$lia->hello($name))
;
}
public function testApiDuplicate(){
$lia = $this->bare();
$this->catch('Lia\\Exception\\Base')
->containing("No mediator found to handle 'api.t.hello.method', but a duplicate was added.");
$lia->addApi('t.hello',[$this,'hello']);
try {
$lia->addApi('t.hello',[$this,'hello']);
} catch (\Lia\Exception\Base $e){
$this->throw($e);
}
return true;
}
public function testApiMediatorWildcard(){
$lia = $this->bare();
$lia->addMediator('*',
function($funcName, $oldFunc, $newFunc){
if ($funcName!='api.t.hello.method')return null;
return $newFunc;
}
);
$lia->addApi('t.hello',[$this,'hello']);
$lia->addApi('t.hello',[$this,'hola']);
$lia->addApiMethod('hello', 't.hello');
$name = "Cat";
$target = $this->hola($name);
return true
&& $this->compare($target,$lia->hello($name))
;
}
public function testMediateApi(){
$lia = $this->bare();
$lia->addMediator('api.t.hello.method',
function($funcName, $old, $new){
return $new;
}
);
$lia->addApiMethod('hello', 't.hello');
$lia->addApi('t.hello',[$this,'hello']);
$lia->addApi('t.hello',[$this,'hola']);
$name = "Cat";
$target = $this->hola($name);
return true
&& $this->compare($target,$lia->hello($name))
;
}
public function testAddRemoveAddApi(){
$lia = $this->bare();
$lia->addApi('t.hello', [$this,'hello']);
$lia->addApiMethod('hello', 't.hello');
$lia->removeApi('t.hello');
$lia->addApi('t.hello', [$this, 'hola']);
$lia->removeApi('t.hello', 'method');
$lia->addApi('t.hello', [$this, 'hola']);
$lia->addApiMethod('hola', 't.hello');
$name = "Cat";
$target = $this->hola($name);
return true
&& $this->compare($target, $lia->api('t.hello', 'method',$name))
&& $this->compare($target, $lia->callApi('t.hello', $name))
&& $this->compare($target, $lia->hello($name))
&& $this->compare($target, $lia->hola($name))
;
}
}
Api::runAll();