DebugTraitTest.php

<?php

namespace Liaison\Test\Traits;

class Debug extends \Taeluf\Tester {

    public function hello(){}
    public function hola(){}
    public function mediate(...$args){return [$this,'mediate'];}
    public function brokenMediate(...$args){return null;}

    //@TODO test for debugging of remaining debuggable items

    public function testDebugStack(){
        $lia = $this->bare($debug=true);

        $lia->mediate('two',[$this,'mediate']);
        $lia->mediate('two',[$this,'mediate']);
        $lia->addGlobalFunction('two',[$this,'hello']);
        $lia->addGlobalFunction('two',[$this,'hola']);
        $lia->two('Tired');
        $lia->mediate('three',[$this,'brokenMediate']);
        $lia->addGlobalFunction('three',[$this,'hello']);
        try {
            $lia->addGlobalFunction('three',[$this,'hola']);
        } catch (\Lia\Exception\BadReturn $e){
            //don't actually do anything...
        }

        $target = [
            ['FUNCTIONS','mapAction','deriveGlobalFunction'],
            ['PREFIX','addGlobalPrefix',''],
            ['MEDIATORS','register','two'],
            ['MEDIATORS','replace','two'],
            ['FUNCTIONS','register','two'],
            ['FUNCTIONS','register','two'],
            ['MEDIATORS','mediate','two'],
            ['FUNCTIONS','call', 'two'],
            ['MEDIATORS','register','three'],
            ['FUNCTIONS','register','three'],
            ['FUNCTIONS','register','three'],
            ['MEDIATORS','mediate','three'],
            ['MEDIATORS','bad return', 'three']
        ];

        $debug = $lia->debug(\Liaison::STACK);
        // print_r($debug);
        $actual = [];
        foreach ($debug as $item){
            // echo "\n\n";
            // echo $item['group']."\n\n";
            $stack = [$item['group'], $item['action'], $item['args']['function']??''];
            $actual[] = $stack;
        }

        return $this->compare($target,$actual);
    }
    public function testDebugMediators(){
        $lia = $this->bare($debug=true);

        $lia->mediate('two',[$this,'mediate']);
        $lia->mediate('two',[$this,'mediate']);
        $lia->addGlobalFunction('two',[$this,'hello']);
        $lia->addGlobalFunction('two',[$this,'hola']);
        $lia->two('Tired');
        $lia->mediate('three',[$this,'mediate']);

        //@TODO test for substack as well
        $target = [
            ['register','two'],
            ['replace','two'],
            ['mediate', 'two'],
            ['register','three']
        ];

        $debug = $lia->debug(\Liaison::MEDIATORS);
        $actual = [];
        foreach ($debug as $item){
            $stack = [$item['action'], $item['args']['function']];
            $actual[] = $stack;
        }

        return $this->compare($target, $actual);
    }

    public function testDebugFunctions(){
        $lia = $this->bare($debug=true);

        $lia->addGlobalFunction('one',[$this,'hello']);
        $lia->removeGlobalFunction('one');
        try {
            $lia->one("nonexistent person");
        } catch (\Lia\Exception\MagicCallNotFound $e){
            //don't need to do anything... 
        }
        $lia->addGlobalFunction('two',[$this,'hello']);
        $lia->replaceGlobalFunction('two', [$this, 'hola']);
        $lia->two("tired person");
        $lia->addGlobalFunction('three',[$this,'hello']);
        $lia->addGlobalFunction('six',[$this,'hello']);

        $target = [
            ['mapAction', 'deriveGlobalFunction'],
            ['register','one'],
            ['unRegister','one'],
            ['callFail', 'one'],
            ['register','two'],
            ['reRegister','two',
                [
                    ['unRegister','two'],
                    ['register','two']
                ]
            ],
            ['call','two'],
            ['register','three'],
            ['register','six'],
        ];

        $debug = $lia->debug(\Liaison::FUNCTIONS);

        $actual = [];
        foreach ($debug as $item){
            $stack = [$item['action'], $item['args']['function']];
            if (isset($item['args']['substack'])){
                $subStack = [];
                foreach ($item['args']['substack'] as $subItem){
                    $subStack[] = [$subItem['action'], $subItem['args']['function']];
                }
                $stack[] = $subStack;
            }
            $actual[] = $stack;
        }

        return $this->compare($target, $actual);
    }

    public function testInspectMediators(){
        $lia = $this->bare();

        $lia->mediate('one',[$this,'mediate']);
        $lia->mediate('two',[$this,'mediate']);
        $lia->mediate('three',[$this,'mediate']);
        $lia->mediate('four',[$this,'mediate']);
        $lia->mediate('five',[$this,'mediate']);
        $lia->mediate('six',[$this,'mediate']);

        $actual = $lia->inspect(\Liaison::MEDIATORS);
        $actual = $actual['items'];
        $target = ['one','two','three','four','five','six'];
        sort($actual);
        sort($target);
        return $this->compare($target,$actual);
    }

    public function testInspectFunctions(){
        $lia = $this->bare();

        $lia->addGlobalFunction('one',[$this,'hello']);
        $lia->addGlobalFunction('two',[$this,'hello']);
        $lia->addGlobalFunction('three',[$this,'hello']);
        $lia->addGlobalFunction('four',[$this,'hello']);
        $lia->addGlobalFunction('five',[$this,'hello']);
        $lia->addGlobalFunction('six',[$this,'hello']);

        $actual = $lia->inspect(\Liaison::FUNCTIONS);
        $actual = $actual['items'];
        $target = [
            'one','two','three','four','five','six'
        ];
        sort($actual);
        sort($target);
        return $this->compare($target,$actual);
    }

    public function testInspectRaw(){
        $lia = $this->bare();

        $inspectables = $lia->inspect();
        $items = $inspectables['items'];
        $constants = (new \ReflectionClass(\Liaison::class))->getConstants();
        sort($items);
        sort($constants);

        return $this->compare($items,$constants);
    }


    protected function bare($debug=false){
        $liaison = new \Liaison(['bare'=>true,'debug'=>$debug]);
        return $liaison;
    }
}

Debug::runAll();