private static function runFunctionSnippet()

in src/TestUtils/TestTrait.php [78:120]


    private static function runFunctionSnippet($sampleName, $params = [])
    {
        // Get the namespace of the sample function
        $parts = explode('\\', debug_backtrace()[1]['class']);

        // Remove the name of the testcase class
        array_pop($parts);

        // Some tests run in the "Tests" namespace, but the function isn't in
        // that namespace, so remove it
        if (end($parts) === 'Tests') {
            array_pop($parts);
        }

        $snippet = implode('\\', $parts) . '\\' . $sampleName;

        // The file has already been included. Execute the function directly
        if (function_exists($snippet)) {
            try {
                ob_start();
                $ret = call_user_func_array($snippet, $params);
                self::$lastSnippetReturnValue = $ret;
                return ob_get_clean();
            } catch (\Exception $e) {
                ob_get_clean();
                throw $e;
            }
        }

        // Include the file and run the snippet
        $output = self::runSnippet($sampleName, $params);

        if (!function_exists($snippet)) {
            // We included the file but the function still isn't defined
            throw new \LogicException(sprintf(
                'Function %s() was not found in file src/%s.php',
                $snippet,
                $sampleName
            ));
        }

        return $output;
    }