LiaisonApi.php
<?php
namespace Liaison\Test;
/**
* Test core api functions likes api() and mediate()
*/
class LiaisonApi 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);
$this->compare($target, $lia->api($apiKey, $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);
$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);
$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', 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);
}
}
public function testApiMediatorWildcard(){
$lia = $this->bare();
$lia->addMediator('*',
function($funcName, $oldFunc, $newFunc){
if ($funcName!='api:t.hello')return null;
return $newFunc;
}
);
$lia->addApi('t.hello',[$this,'hello']);
$lia->addApi('t.hello',[$this,'hola']);
$lia->addApiMethod('t.hello', 'hello');
$name = "Cat";
$target = $this->hola($name);
$this->compare($target,$lia->hello($name));
}
public function testMediateApi(){
$lia = $this->bare();
$lia->addMediator('api:t.hello',
function($funcName, $old, $new){
return $new;
}
);
$lia->addApiMethod('t.hello', 'hello');
$lia->addApi('t.hello',[$this,'hello']);
$lia->addApi('t.hello',[$this,'hola']);
$name = "Cat";
$target = $this->hola($name);
$this->compare($target,$lia->hello($name));
}
public function testAddRemoveAddApi(){
$lia = $this->bare();
$lia->addApi('t.hello', [$this,'hello']);
$lia->addApiMethod('t.hello', 'hello');
$lia->removeApi('t.hello');
$lia->addApi('t.hello', [$this, 'hola']);
$lia->removeApi('t.hello', 'method');
$lia->addApi('t.hello', [$this, 'hola']);
$lia->addApiMethod('t.hello', 'hola');
$name = "Cat";
$target = $this->hola($name);
$this->compare($target, $lia->api('t.hello',$name));
$this->compare($target, $lia->hello($name));
$this->compare($target, $lia->hola($name));
}
}