Helper.php 4.34 KB
Newer Older
Ivan Chepurnyi's avatar
Ivan Chepurnyi committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
<?php

/**
 * Test Helpers Factory
 *
 */
class EcomDev_PHPUnit_Helper
{
    /**
     * Helpers container
     *
     * @var EcomDev_PHPunit_Helper_Interface[]
     */
    protected static $helpers = array();

    /**
     * Adds a new helper instance to helpers registry
     *
     * If $position is specified, it will use value
     * from before or after key as related helper
     *
     * @param EcomDev_PHPunit_Helper_Interface $helper
     * @param bool|array                       $position
     *
     * @throws RuntimeException
     */
    public static function add(EcomDev_PHPunit_Helper_Interface $helper, $position = false)
    {
        if ($position === false) {
            self::$helpers[] = $helper;
        } elseif (isset($position['after']) || isset($position['before'])) {
            $isBefore = isset($position['before']);
            $relatedHelper = $isBefore ? $position['before'] : $position['after'];

            if (is_string($relatedHelper)) {
                // Retrieving of helper by class name
                $relatedHelper = current(self::getHelpersByClass($relatedHelper));
            }

            $helperPosition = array_search($relatedHelper, self::$helpers, true);
            if ($helperPosition !== false) {
                array_splice(
                    self::$helpers,
                    $helperPosition + ($isBefore ? 0 : 1),
                    null,
                    array($helper)
                );
            }
        } else {
            throw new RuntimeException('Unknown position specified for helper addition');
        }
    }

    /**
     * Removes helper by instance
     *
     * @param EcomDev_PHPUnit_Helper_Interface $helper
     */
    public static function remove(EcomDev_PHPUnit_Helper_Interface $helper)
    {
        $helperPosition = array_search($helper, self::$helpers, true);
        if ($helperPosition !== false) {
            array_splice(self::$helpers, $helperPosition, 1);
        }
    }

    /**
     * Removes all helpers by class name from helpers array
     *
     * @param string $helperClass
     */
    public static function removeByClass($helperClass)
    {
        $helpersByClass = self::getHelpersByClass($helperClass);
        foreach ($helpersByClass as $helper) {
            self::remove($helper);
        }
    }

    /**
     * Returns helper by action,
     * if helper for action was not found it returns false
     *
     * @param $action
     * @return bool|EcomDev_PHPunit_Helper_Interface
     */
    public static function getByAction($action)
    {
        foreach (self::$helpers as $helper) {
            if ($helper->has($action)) {
                return $helper;
            }
        }

        return false;
    }

    /**
     * Checks existence of a helper for an action
     *
     * @param string $action
     * @return bool
     */
    public static function has($action)
    {
        return self::getByAction($action) !== false;
    }

    /**
     * Invokes a helper action with arguments as an array
     *
     * @param string $action
     * @param array  $args
     *
     * @throws RuntimeException
     * @return mixed
     */
    public static function invokeArgs($action, array $args)
    {
        $helper = self::getByAction($action);

        if (!$helper) {
            throw new RuntimeException(sprintf('Cannot find a helper for action "%s"', $action));
        }

        return $helper->invoke($action, $args);
    }

    /**
     * Invokes helper action with flat arguments
     *
     * @param string $action
     * @return mixed
     */
    public static function invoke($action /*, $arg1, $arg2, $arg3 ... $argN */)
    {
        $args = func_get_args();
        array_shift($args);
        return self::invokeArgs($action, $args);
    }

    /**
     * Sets test case to each helper instance
     *
     * @param PHPUnit_Framework_TestCase $testCase
     */
    public static function setTestCase(PHPUnit_Framework_TestCase $testCase)
    {
        foreach (self::$helpers as $helper) {
            $helper->setTestCase($testCase);
        }
    }

    /**
     * Finds a helper instance by class name
     *
     * @param string $className
     * @return array
     */
    protected static function getHelpersByClass($className)
    {
        return array_filter(self::$helpers, function ($item) use ($className) {
            return get_class($item) === $className;
        });
    }


}