Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 2
31.58% covered (danger)
31.58%
6 / 19
CRAP
23.81% covered (danger)
23.81%
20 / 84
TestFilesTrait
0.00% covered (danger)
0.00%
0 / 1
42.86% covered (danger)
42.86%
6 / 14
369.04
27.78% covered (danger)
27.78%
20 / 72
 getTestDataPath($fileName = false)
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
5 / 5
 getCommonTestDataPath($fileName = false)
100.00% covered (success)
100.00%
1 / 1
2
100.00% covered (success)
100.00%
4 / 4
 getCommonTestDataContent($fileName)
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 getTestDataContent($fileName)
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 1
 getClassDataPath($fileName = false)
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 getClassDataContent($fileName)
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
1 / 1
 assertAssocContainsAssoc($assoc1, $assoc2)
100.00% covered (success)
100.00%
1 / 1
5
100.00% covered (success)
100.00%
7 / 7
 assertTemplateContains($varName, $value)
0.00% covered (danger)
0.00%
0 / 1
6
0.00% covered (danger)
0.00%
0 / 3
 getMockTemplate()
0.00% covered (danger)
0.00%
0 / 1
6
0.00% covered (danger)
0.00%
0 / 4
 authenticatedViewControllerSetUp()
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 14
 userIsLoggedIn($status = true)
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 5
 assertConnection( $actionName, $actionVars, $templateVar, $resourceName, $functionName, $expectedFunctionArgs = NULL )
0.00% covered (danger)
0.00%
0 / 1
6
0.00% covered (danger)
0.00%
0 / 5
 assertArrayConnection( $actionName, $actionVars, $resourceName, $functionName, $expectedFunctionArgs = NULL )
0.00% covered (danger)
0.00%
0 / 1
6
0.00% covered (danger)
0.00%
0 / 6
 _assertConnectionHelper( $testValue, $actionName, $actionVars, $resourceName, $functionName, $expectedFunctionArgs = NULL )
0.00% covered (danger)
0.00%
0 / 1
56
0.00% covered (danger)
0.00%
0 / 14
MockTemplate
0.00% covered (danger)
0.00%
0 / 1
44.44% covered (danger)
44.44%
4 / 9
156
0.00% covered (danger)
0.00%
0 / 12
 offsetSet($offset, $value)
0.00% covered (danger)
0.00%
0 / 1
6
0.00% covered (danger)
0.00%
0 / 3
 offsetExists($offset)
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 1
 offsetUnset($offset)
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 2
 offsetGet($offset)
0.00% covered (danger)
0.00%
0 / 1
2
0.00% covered (danger)
0.00%
0 / 1
 arraySet($array)
0.00% covered (danger)
0.00%
0 / 1
12
0.00% covered (danger)
0.00%
0 / 5
 load($templateName = null)
100.00% covered (success)
100.00%
1 / 1
1  
 
 loadFromFile($path)
100.00% covered (success)
100.00%
1 / 1
1  
 
 setLocalizer($localizer)
100.00% covered (success)
100.00%
1 / 1
1  
 
 getBaseDir()
100.00% covered (success)
100.00%
1 / 1
1  
 
<?php
/**
 *
 */
namespace aae\unitTesting {
    /**
     * @author Axel Ancona Esselmann
     * @package aae\unitTesting
     */
    trait TestFilesTrait {
        private $_mockTemplate = NULL;
        protected function getTestDataPath($fileName = false) {
            $reflectedClass = new \ReflectionObject($this);
            $reflectedClassFileName = $reflectedClass->getFileName();
            $path = dirname($reflectedClassFileName)."/".(substr(strrchr(get_class($this), "\\"), 1))."Data";
            if (is_string($fileName)) $path .= "/".$fileName;
            return $path;
        }
        protected function getCommonTestDataPath($fileName = false) {
            $speciffic = $this->getTestDataPath($fileName);
            $path = dirname(dirname($speciffic)).DIRECTORY_SEPARATOR."TestData";
            if (is_string($fileName)) $path .= "/".$fileName;
            return $path;
        }
        protected function getCommonTestDataContent($fileName) {
            return file_get_contents($this->getCommonTestDataPath($fileName));
        }
        protected function getTestDataContent($fileName) {
            return file_get_contents($this->getTestDataPath($fileName));
        }
        protected function getClassDataPath($fileName = false) {
            $testDataPath = $this->getTestDataPath($fileName);
            return str_replace("Test", "", $testDataPath);
        }
        protected function getClassDataContent($fileName) {
            return file_get_contents($this->getClassDataPath($fileName));
        }
        public function assertAssocContainsAssoc($assoc1, $assoc2) {
            $errorMessage = "";
            foreach ($assoc2 as $key => $value) {
                if (!array_key_exists($key, $assoc1)) $errorMessage .= "The associative array does not contain key '$key'.\n";
                if ($value != $assoc1[$key]) $errorMessage .= "\"$key\"=>$value does not match expected \"$key\"=>{$assoc1[$key]}.\n";
            }
            if (strlen($errorMessage) > 0) $this->fail($errorMessage);
        }
        public function assertTemplateContains($varName, $value) {
            if (is_null($this->_mockTemplate)) throw new \Exception("Pass mock template to controller. Call getMockTemplate to get an instance.", 209151225);
            $this->assertEquals($value, $this->_mockTemplate[$varName]);
        }
        public function getMockTemplate() {
            if (is_null($this->_mockTemplate)) {
                $this->_mockTemplate = new MockTemplate();
            }
            return $this->_mockTemplate;
        }
        public function authenticatedViewControllerSetUp() {
            $this->_doc        = $this->getMockTemplate();
            $this->_user       = $this->getMockBuilder('\aae\app\User')
                ->disableOriginalConstructor()
                ->getMock();
            $this->_session    = $this->getMockBuilder('\aae\app\Session')
                ->disableOriginalConstructor()
                ->getMock();
            $className = strstr(get_class($this), "Test", true);
            $this->sut = new $className(
                $this->_doc,
                $this->_user,
                $this->_session
            );
        }
        public function userIsLoggedIn($status = true) {
            $this->_user
                ->expects($this->once())
                ->method('isLoggedIn')
                ->willReturn($status);
        }
        public function assertConnection(
            $actionName,
            $actionVars,
            $templateVar,
            $resourceName,
            $functionName,
            $expectedFunctionArgs = NULL
        ) {
            $testValue = "aString1234";
            $this->_assertConnectionHelper($testValue, $actionName, $actionVars, $resourceName, $functionName, $expectedFunctionArgs);
            if ($testValue !== $this->_mockTemplate[$templateVar]) $this->fail("Template resource '$templateVar' received no value.");
            $this->assertEquals($testValue, $this->_mockTemplate[$templateVar]);
        }
        public function assertArrayConnection(
            $actionName,
            $actionVars,
            $resourceName,
            $functionName,
            $expectedFunctionArgs = NULL
        ) {
            $templateVar = "var";
            $testValue = [$templateVar => "aString1234"];
            $this->_assertConnectionHelper($testValue, $actionName, $actionVars, $resourceName, $functionName, $expectedFunctionArgs);
            if ($testValue[$templateVar] !== $this->_mockTemplate[$templateVar]) $this->fail("Template resource '$templateVar' received no value.");
            $this->assertEquals($testValue[$templateVar], $this->_mockTemplate[$templateVar]);
        }
        private function _assertConnectionHelper(
            $testValue,
            $actionName,
            $actionVars,
            $resourceName,
            $functionName,
            $expectedFunctionArgs = NULL
        ) {
            $resource = $this->$resourceName;
            if (is_null($resource)) $this->fail("Resource '$resourceName' does not exist.");
            if (!method_exists($resource, $functionName)) $this->fail("Resource '$resourceName' has no function named '$functionName.'");
            $method = $resource
                ->expects($this->once())
                ->method($functionName);
            if (!is_null($expectedFunctionArgs)) {
                if (!is_array($expectedFunctionArgs)) $expectedFunctionArgs = [$expectedFunctionArgs];
                call_user_func_array(array($method, "with"), $expectedFunctionArgs);
            }
            $method->willReturn($testValue);
            if (count($actionVars) < 1) $this->sut->$actionName();
            else call_user_func_array(array($this->sut, $actionName), $actionVars);
            if (is_null($this->_mockTemplate)) throw new \Exception("Pass mock template to controller. Call getMockTemplate to get an instance.", 209151225);
        }
    }
    class MockTemplate implements \ArrayAccess, \aae\ui\TemplateInterface {
        private $_outputAssoc = [];
        public function offsetSet($offset, $value) {
            if (is_null($offset)) throw new \Exception("Provide an offset add to the template.", 1208141630);
            $this->_outputAssoc[$offset] = $value;
        }
        public function offsetExists($offset) {
            return isset($this->_outputAssoc[$offset]);
        }
        public function offsetUnset($offset) {
            unset($this->_outputAssoc[$offset]);
        }
        public function offsetGet($offset) {
            return $this->_outputAssoc[$offset];
        }
        public function arraySet($array) {
            if (!is_array($array)) throw new \Exception("arraySet expects an array of arguments", 204151756);
            foreach ($array as $key => $value) {
                $this[$key] = $value;
            }
        }
        public function load($templateName = null) {}
        public function loadFromFile($path) {}
        public function setLocalizer($localizer) {}
        public function getBaseDir() {}
    }
}