Autoloader.php

<?php

namespace Liaison\Test\Compo\Autoloader;


class Autoloader extends \Taeluf\Tester {

    public function testCustomAutoloader(){
        $this->setup($lia,$loader,$dir,$compo);
        $class = 'Test\\Custom\\Autoloader';
        $this->catch('Exception')
            ->containing("Test autoloader was successful for '{$class}'");
        try {
            new $class;
        } catch (\Exception $e){
            $this->throw($e);
        }

        return true;
    }

    public function testInvokerLoadsNSClass(){
        $this->setup($lia,$loader,$dir);
        $cn = $this->makeClass('food\\industry\\istoxic');
        $parts = explode('\\',$cn);
        array_pop($parts);
        $ns = implode('\\',$parts);

        $lia->autoload($dir, [$ns]);
        $e = null;
        try {
            $obj = new $cn;
            $success = true
            && $this->compare($cn, $obj->getName())
            ;
        } catch (\Error $e){

        }

        $this->removeClass($cn);
        if ($e!=null)throw $e;
        return $success;
    }

    public function testInvokerLoadsClass(){
        $this->setup($lia,$loader,$dir);
        $cn = $this->makeClass();
        $lia->autoload($dir);
        $e = null;
        try {
            $obj = new $cn;
            $success = true
            && $this->compare($cn, $obj->getName())
            ;
        } catch (\Error $e){

        }

        $this->removeClass($cn);
        if ($e!=null)throw $e;
        return $success;
    }

    public function testAutoloadMethodIsAavailable(){
        echo "There will be no printed return values, but also no errors, if this test is passing.\n\n";
        $this->setup($lia,$loader);
        return true
            &&  $this->test('method')
                    ->compare(  $loader->autoload('fakedir'), 
                                $lia->autoload('fakedir')
                    )
            &&  $this->test('apiCall')
                     ->compare( $lia->autoload('fakedir'),
                                $lia->api('lia:autoload.addDir','fakedir')
                     ) 
        ;
    }

    protected function setup(&$lia,&$loader,&$genDir=null,&$compo=null){
        $lia = new \Liaison(['bare'=>true]);
        $package = new \Liaison\Test\Mock\Package($lia);
        $loader = new \Lia\Compo\Autoloader($package);
        $compo = new \Liaison\Test\Sample\Compo($package);

        $package->onPackageReady();
        $package->scanForPrefixesInCompos();
        $genDir = $this->genDir();
    }
    protected function genDir(){
        return __DIR__.'/../extra/Autoloader-gen/';
    }

    protected function removeClass($fqcn){
        $fn = array_slice(explode('\\',$fqcn),-1)[0].'.php';
        unlink($this->genDir().$fn);
    }
    protected function makeClass($ns=''){
        $dir = $this->genDir();
        $bytes = random_bytes(15);
        $cn = 'a'.bin2hex($bytes);
        $nsDeclaration = $ns ? 'namespace '.$ns.';' : '';
        $code = 
        <<<PHP
        <?php
        {$nsDeclaration}
        class {$cn} {
            public function getName(){
                return '\\\\'.get_class(\$this);
            }
        }
        PHP;
        echo "We create a file with a generic class, try the autoloader, then delete the file.";
        echo "\nThe file is '{$cn}.php'\n";
        file_put_contents($file=$dir.'/'.$cn.'.php',$code);

        if ($ns!='')$ns = '\\'.$ns;
        return $ns.'\\'.$cn;
    }
}

Autoloader::runAll();