Scanner.php
<?php
namespace Liaison\Test\Traits;
class Scanner extends \Taeluf\Tester {
public function testApiPrefixAndCustomPrefixesAndBasicallyEverything(){
$lia = $this->lia();
$scannable = new Scannable($lia);
// $scannable->autoHandlePrefix('api');
// $scannable->autoHandlePrefix('mf');
$scannable->autoHandlePrefixedMethods();
$this->test('Api Prefix');
$this->compare(1+5, $lia->sum(1,5));
$this->compare(7*8, $lia->multiply(7,8));
$this->compare(14*86, $lia->api('math.mult', 14, 86));
$this->compare(12*13, $lia->api('math.mult', 12, 13));
$this->test('Custom Prefixes');
$this->compare(144 / 12, $lia->mathDivide(144, 12));
$this->compare(4**6, $lia->mathExp(4,6));
$this->compare(192 + 6, $lia->mathAddSix(192));
;
}
public function testAddPrefixPointingToMethodHandler(){
$lia = $this->lia();
$scannable = new Scannable($lia);
$list = new \stdClass;
$apiKey = $lia->addMethod('addEvent',
function($key, $callable) use (&$list){
$list->$key = $callable;
}
);
$lia->addApiPrefix($apiKey,'on');
$scannable->autoHandlePrefixedMethods();
$aList = array_keys((array)$list);
$tList = array_keys($scannable->getMethodsWithPrefix('on'));
sort($aList);
sort($tList);
return
$this->compare($scannable->on_Three(), ($list->_Three)())
&& $this->compare($tList, $aList);
;
}
public function testAutoAddPrefixedFunctions(){
$lia = $this->lia();
$scannable = new Scannable($lia);
$list = new \stdClass;
$apiKey = $lia->addPrefix('on',
function($key, $callable) use (&$list){
// if (substr($key,0,1)=='_')$key = substr($key,1);
$list->$key = $callable;
}
);
$scannable->autoHandlePrefixedMethods();
$aList = array_keys((array)$list);
$tList = array_keys($scannable->getMethodsWithPrefix('on'));
sort($aList);
sort($tList);
return
$this->compare($scannable->on_Three(), ($list->_Three)())
&& $this->compare($tList, $aList);
;
}
public function testGlobalPrefix(){
$lia = $this->lia();
$scannable = new Scannable($lia);
$lia->addApiPrefix($apiName='apiname.whatever.prefix', $prefix = 'whatev');
$prefixList = $lia->getApiPrefixes();
unset($prefixList['api']);
unset($prefixList['mf']);
$target = [
$prefix =>
$apiName
];
return
true
&& $this->compare($target, $prefixList)
;
}
public function testGetPrefixedMethods(){
$scannableClass = new Scannable();
$map = $scannableClass->getMethodsWithPrefix('on');
$actual = array_keys($map);
$target = ["One", '_two', '_Three', 'Four'];
sort($actual);
sort($target);
return $this->compare($target, $actual);
}
public function compo(){
$mockPackage = new \stdClass();
$mockPackage->lia = null;
$mockPackage->dir = null;
$compo = new \Lia\Route\Router($mockPackage);
return $compo;
}
protected function lia($debug=false){
$liaison = new \Liaison(['bare'=>true,'debug'=>$debug]);
return $liaison;
}
}
class Scannable {
use \Lia\CompoTrait\Scanner;
protected $lia;
public function __construct($liaison=null){
$this->lia = $liaison;
// $this->lia->addApi('sum',[$this,'sum']);
//
//
// $this->lia->addApi('math', [$this,'addMathFunction'], 'prefix');
// $this->lia->addApiPrefix('math','math','prefix');
//
if ($this->lia!=null){
$this->lia->addMethod('sum',[$this,'sum']);
$this->lia->addApi('math.func.prefix',[$this,'addMathFunction']);
$this->lia->addApi('math.mult',[$this,'multiply']);
$this->lia->addApiMethod('math.mult','multiply');
$this->lia->addApiPrefix('math.func.prefix', 'mf');
}
}
public function getApiPrefixes(){
return array_merge(
$this->Scanner_getApiPrefixes(),
// ['on'=>[''],
['newMath' => 'math.func.prefix'],
)
;
}
public function onOne(){}
public function on_two(){}
public function on_Three($extra=''){
return "Event 'Three' was called. Extra is '{$extra}'.";
}
public function onFour(){}
//these should NOT be caught by the scanner
public function on__nope(){}
public function onnooooo(){}
public function sum($a, $b){
return $a + $b;
}
public function multiply($a, $b){
return $a * $b;
}
public function addMathFunction($cleanName, $callable){
if (substr($cleanName, 0, 1)=='_')$cleanName = substr($cleanName,1);
$this->lia->addMethod('math'.ucfirst($cleanName), $callable);
}
public function mf_divide($a, $byB){
return $a / $byB;
}
public function mfExp($a, $toE){
return pow($a, $toE);
}
public function newMathAddSix($toA){
return $toA + 6;
}
}