<commit_before><?php
require_once dirname(__FILE__).'/../../../../config/ProjectConfiguration.class.php';

/**
 * sfBasePhpunitFunctionalTestCase is the super class for all functional
 * tests using PHPUnit.
 * The "getBrowser" method provides the current functional test/browser
 * instance of symfony and you can do anything with it you are used from
 * the normal lime based tests.
 *
 * @package    sfPhpunitPlugin
 * @subpackage lib
 * @author     <NAME> <<EMAIL>>
 */
abstract class sfBasePhpunitFunctionalTestCase extends PHPUnit_Framework_TestCase
{
    /**
     * The sfContext instance
     *
     * @var sfContext
     */
    private $context = null;

    /**
     * The sfTestFunctional instance
     *
     * @var sfTestFunctional
     */
    protected $browser;

    /**
     * Returns application name
     *
     * @return string
     */
    abstract protected function getApplication();


    /**
     * Returns environment name
     *
     * @return string
     */
    abstract protected function getEnvironment();

    /**
     * Returns if the test should be run in debug mode
     *
     * @return bool
     */
    protected function isDebug()
    {
        return true;
    }

    /**
     * Dev hook for custom "setUp" stuff
     *
     */
    protected function _start()
    {
    }

    /**
     * Dev hook for custom "tearDown" stuff
     *
     */
    protected function _end()
    {
    }

    /**
     * setUp method for PHPUnit
     *
     */
    protected function setUp()
    {
        // first we need the context and autoloading
        $this->initializeContext();

        // autoloading ready, continue
        $this->browser = new sfTestFunctional(new sfPhpunitTestBrowser, new sfPhpunitTest($this));

        // Initialize SCRIPT_NAME for correct work $this->generateUrl()
        // when $_SERVER is empty before first request
        $_SERVER['SCRIPT_NAME'] = '/index.php';

        $this->_start();
    }

    /**
     * Returns the sfTestFunctional instance
     *
     * @return sfTestFunctional
     */
    public function getBrowser()
    {
        return $this->browser;
    }


    /**
     * tearDown method for PHPUnit
     *
     */
    protected function tearDown()
    {
        $this->_end();
    }

    /**
     * Intializes the context for this test
     *
     */
    private function initializeContext()
    {
        // only initialize the context one time
        if(!$this->context)
        {
            $configuration = ProjectConfiguration::getApplicationConfiguration($this->getApplication(), $this->getEnvironment(), $this->isDebug());
            sfContext::createInstance($configuration);

            // remove all cache
            sfToolkit::clearDirectory(sfConfig::get('sf_app_cache_dir'));
        }
    }

    /*
     * Returns sfContext instance
     *
     * @return sfContext
     */
    protected function getContext()
    {
        if(!$this->context)
        {
            $this->context = sfContext::getInstance();
        }

        return $this->context;
    }


    /**
     * Generate URL from route name
     *
     * Example:
     *   $this->generateUrl('homepage');
     *      -> "/"
     *   $this->generateUrl('article_edit', $articleObject);
     *      -> "/article/1/edit"
     *   $this->generateUrl('custom_route', $arrRouteParams);
     *
     * @see sfPatternRouting::generate()
     *
     * @param  string      $name   - Route name from routing.yml
     * @param  array|Model $params - Routing params
     * @return string
     */
    protected function generateUrl($name, $params = array())
    {
        return $this->browser->getContext()->getRouting()->generate($name, $params);
    }


    /**
     * Run test
     *
     * Catch exception and decorate it with last request data
     */
    protected function runTest()
    {
        try {
            parent::runTest();
        } catch (Exception $e) {
            throw $this->_decorateExeption($e);
        }
    }


    /**
     * Decorate exception with last request data
     *
     * @param  Exception $e
     * @return Exception
     */
    private function _decorateExeption(Exception $e)
    {
        if (!$this->browser->getLastRequestUri()) {
            return $e;
        }

        $className = get_class($e);

        if ($e instanceof PHPUnit_Framework_ExpectationFailedException) {
            if (!$e->getCustomMessage()) {
                return new $className(
                    $this->_makeRequestErrorMessage($e->getDescription(), $e) . PHP_EOL,
                    $e->getComparisonFailure()
                );
            } else {
                return new $className(
                    $e->getDescription(),
                    $e->getComparisonFailure(),
                    $this->_makeRequestErrorMessage($e->getCustomMessage(), $e) . PHP_EOL
                );
            }

        } else if ($e instanceof PHPUnit_Framework_Error) {
            return new $className(
                $this->_makeRequestErrorMessage($e->getMessage(), $e),
                $e->getCode(),
                $e->getFile(),
                $e->getLine(),
                $e->getTrace()
            );
        }

        return $e;
    }


    /**
     * Make request error message with last request uri and params
     *
     * @param  string - User defined message
     * @return strung
     */
    private function _makeRequestErrorMessage($mess, Exception $e)
    {
        $result = $mess  . PHP_EOL . PHP_EOL
                . 'Request: ' . $this->browser->getLastRequestUri() . PHP_EOL
                . 'Request params: ' . PHP_EOL . $this->browser->getLastRequestParams();

        if ($_FILES) {
            $result .= PHP_EOL . PHP_EOL
                    .  'Submited FILES: '  . PHP_EOL
                    .  var_export($_FILES, true);
        }

        $result .= PHP_EOL . PHP_EOL
                .  'Trace: ' . PHP_EOL
                .  PHPUnit_Util_Filter::getFilteredStacktrace($e, false);

        return $result;
    }

}
<commit_msg>FunctionalTestCase: support for custom testers<commit_after><?php
require_once dirname(__FILE__).'/../../../../config/ProjectConfiguration.class.php';

/**
 * sfBasePhpunitFunctionalTestCase is the super class for all functional
 * tests using PHPUnit.
 * The "getBrowser" method provides the current functional test/browser
 * instance of symfony and you can do anything with it you are used from
 * the normal lime based tests.
 *
 * @package    sfPhpunitPlugin
 * @subpackage lib
 * @author     <NAME> <<EMAIL>>
 */
abstract class sfBasePhpunitFunctionalTestCase extends PHPUnit_Framework_TestCase
{
    /**
     * The sfContext instance
     *
     * @var sfContext
     */
    private $context = null;

    /**
     * The sfTestFunctional instance
     *
     * @var sfTestFunctional
     */
    protected $browser;

    /**
     * Returns application name
     *
     * @return string
     */
    abstract protected function getApplication();


    /**
     * Returns environment name
     *
     * @return string
     */
    abstract protected function getEnvironment();

    /**
     * Returns if the test should be run in debug mode
     *
     * @return bool
     */
    protected function isDebug()
    {
        return true;
    }

    /**
     * Dev hook for custom "setUp" stuff
     *
     */
    protected function _start()
    {
    }

    /**
     * Dev hook for custom "tearDown" stuff
     *
     */
    protected function _end()
    {
    }


    /**
     * setUp method for PHPUnit
     *
     */
    protected function setUp()
    {
        // first we need the context and autoloading
        $this->initializeContext();

        // autoloading ready, continue
        $this->browser = new sfTestFunctional(new sfPhpunitTestBrowser, new sfPhpunitTest($this), $this->getFunctionalTesters());

        // Initialize SCRIPT_NAME for correct work $this->generateUrl()
        // when $_SERVER is empty before first request
        $_SERVER['SCRIPT_NAME'] = '/index.php';

        $this->_start();
    }


    /**
     * tearDown method for PHPUnit
     *
     */
    protected function tearDown()
    {
        $this->_end();
    }


    /**
     * Inject your own functional testers
     *
     * @see sfTestFunctionalBase::setTesters()
     *
     * @return array
     *          'request'  => 'sfTesterRequest',
     *          'response' => 'sfTesterResponse',
     *          'user'     => 'sfTesterUser',
     */
    protected function getFunctionalTesters()
    {
        return array();
    }


    /**
     * Returns the sfTestFunctional instance
     *
     * @return sfTestFunctional
     */
    public function getBrowser()
    {
        return $this->browser;
    }


    /**
     * Intializes the context for this test
     *
     */
    private function initializeContext()
    {
        // only initialize the context one time
        if(!$this->context)
        {
            $configuration = ProjectConfiguration::getApplicationConfiguration($this->getApplication(), $this->getEnvironment(), $this->isDebug());
            sfContext::createInstance($configuration);

            // remove all cache
            sfToolkit::clearDirectory(sfConfig::get('sf_app_cache_dir'));
        }
    }

    /*
     * Returns sfContext instance
     *
     * @return sfContext
     */
    protected function getContext()
    {
        if(!$this->context)
        {
            $this->context = sfContext::getInstance();
        }

        return $this->context;
    }


    /**
     * Generate URL from route name
     *
     * Example:
     *   $this->generateUrl('homepage');
     *      -> "/"
     *   $this->generateUrl('article_edit', $articleObject);
     *      -> "/article/1/edit"
     *   $this->generateUrl('custom_route', $arrRouteParams);
     *
     * @see sfPatternRouting::generate()
     *
     * @param  string      $name   - Route name from routing.yml
     * @param  array|Model $params - Routing params
     * @return string
     */
    protected function generateUrl($name, $params = array())
    {
        return $this->browser->getContext()->getRouting()->generate($name, $params);
    }


    /**
     * Run test
     *
     * Catch exception and decorate it with last request data
     */
    protected function runTest()
    {
        try {
            parent::runTest();
        } catch (Exception $e) {
            throw $this->_decorateExeption($e);
        }
    }


    /**
     * Decorate exception with last request data
     *
     * @param  Exception $e
     * @return Exception
     */
    private function _decorateExeption(Exception $e)
    {
        if (!$this->browser->getLastRequestUri()) {
            return $e;
        }

        $className = get_class($e);

        if ($e instanceof PHPUnit_Framework_ExpectationFailedException) {
            if (!$e->getCustomMessage()) {
                return new $className(
                    $this->_makeRequestErrorMessage($e->getDescription(), $e) . PHP_EOL,
                    $e->getComparisonFailure()
                );
            } else {
                return new $className(
                    $e->getDescription(),
                    $e->getComparisonFailure(),
                    $this->_makeRequestErrorMessage($e->getCustomMessage(), $e) . PHP_EOL
                );
            }

        } else if ($e instanceof PHPUnit_Framework_Error) {
            return new $className(
                $this->_makeRequestErrorMessage($e->getMessage(), $e),
                $e->getCode(),
                $e->getFile(),
                $e->getLine(),
                $e->getTrace()
            );
        }

        return $e;
    }


    /**
     * Make request error message with last request uri and params
     *
     * @param  string - User defined message
     * @return strung
     */
    private function _makeRequestErrorMessage($mess, Exception $e)
    {
        $result = $mess  . PHP_EOL . PHP_EOL
                . 'Request: ' . $this->browser->getLastRequestUri() . PHP_EOL
                . 'Request params: ' . PHP_EOL . $this->browser->getLastRequestParams();

        if ($_FILES) {
            $result .= PHP_EOL . PHP_EOL
                    .  'Submited FILES: '  . PHP_EOL
                    .  var_export($_FILES, true);
        }

        $result .= PHP_EOL . PHP_EOL
                .  'Trace: ' . PHP_EOL
                .  PHPUnit_Util_Filter::getFilteredStacktrace($e, false);

        return $result;
    }

}

{'fraction_non_alphanumeric': 0.11833466773418735, 'fraction_numerical': 0.0001601281024819856, 'mean_word_length': 1.7671688081524146, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 99, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5894446', 'n_tokens_mistral': 3508, 'n_tokens_neox': 3382, 'n_words': 797}