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();
return true
&& $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', 'method', 14, 86))
&& $this->compare(12*13, $lia->callApi('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('on', $apiKey, 'method');
$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($prefix = 'whatev', $apiName='apiname.whatever');
$prefixList = $lia->getApiPrefixes();
unset($prefixList['api']);
$target = [
$prefix =>
[$apiName, $handlerName='prefix']
];
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;
public function __construct($liaison=null){
$this->lia = $liaison;
}
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 api_sum($a, $b){
return $a + $b;
}
public function api_math_mult_multiply($a, $b){
return $a * $b;
}
public function apiPrefix_math_func_mf_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;
}
}
Scanner::runAll();