<commit_before><?php

class CMTest_TH {

    private static $_timeStart;
    private static $timeDelta = 0;
    private static $_configBackup;

    public static function init() {
        $output = new CM_OutputStream_Null();
        $loader = CM_App::getInstance()->getProvisionLoader();
        $loader->unload($output);
        $loader->load($output);

        self::$_configBackup = serialize(CM_Config::get());

        // Reset environment
        self::clearEnv();
        self::randomizeAutoincrement();
        self::timeInit();
    }

    public static function clearEnv() {
        self::clearDb();
        self::clearCache();
        self::timeReset();
        self::clearFilesystem();
        CM_Service_Manager::getInstance()->resetServiceInstances();
        CM_Config::set(unserialize(self::$_configBackup));
    }

    public static function clearFilesystem() {
        $script = new CM_File_Filesystem_SetupScript(CM_Service_Manager::getInstance());
        $script->unload(new CM_OutputStream_Null());
    }

    public static function clearCache() {
        CM_Cache_Shared::getInstance()->flush();
        CM_Cache_Local::getInstance()->flush();
    }

    public static function timeInit() {
        if (!isset(self::$_timeStart)) {
            runkit_function_copy('time', 'time_original');
            runkit_function_redefine('time', '', 'return CMTest_TH::time();');
        }
        self::$_timeStart = time_original();
        self::$timeDelta = 0;
    }

    public static function time() {
        return self::$_timeStart + self::$timeDelta;
    }

    public static function timeForward($sec) {
        self::$timeDelta += $sec;
        self::clearCache();
    }

    public static function timeDaysForward($days) {
        self::timeForward($days * 24 * 60 * 60);
    }

    public static function timeReset() {
        self::$timeDelta = 0;
        self::clearCache();
    }

    public static function timeDelta() {
        return self::$timeDelta;
    }

    public static function timeDiffInDays($stamp1, $stamp2) {
        return round(($stamp2 - $stamp1) / (60 * 60 * 24));
    }

    /**
     * @return CM_Service_Manager
     */
    public static function getServiceManager() {
        return CM_Service_Manager::getInstance();
    }

    /**
     * @return CM_Model_User
     */
    public static function createUser() {
        return CM_Model_User::createStatic();
    }

    /**
     * @param string|null $abbreviation
     * @return CM_Model_Language
     */
    public static function createLanguage($abbreviation = null) {
        if (!$abbreviation) {
            do {
                $abbreviation = self::_randStr(5);
            } while (CM_Model_Language::findByAbbreviation($abbreviation));
        }
        return CM_Model_Language::create('English', $abbreviation, true);
    }

    /**
     * @param string             $pageClass
     * @param CM_Model_User|null $viewer OPTIONAL
     * @param array              $params OPTIONAL
     * @return CM_Page_Abstract
     */
    public static function createPage($pageClass, CM_Model_User $viewer = null, $params = array()) {
        $request = new CM_Http_Request_Get('?' . http_build_query($params), array(), $viewer);
        return new $pageClass(CM_Params::factory($request->getQuery(), true), $request->getViewer());
    }

    /**
     * @param CM_Model_User|null $user
     * @return CM_Session
     */
    public static function createSession(CM_Model_User $user = null) {
        if (is_null($user)) {
            $user = self::createUser();
        }
        $session = new CM_Session();
        $session->setUser($user);
        $session->write();
        return $session;
    }

    /**
     * @param int|null $type
     * @param int|null $adapterType
     * @return CM_Model_StreamChannel_Abstract
     */
    public static function createStreamChannel($type = null, $adapterType = null) {
        if (null === $type) {
            $type = CM_Model_StreamChannel_Media::getTypeStatic();
        }

        if (null === $adapterType) {
            $adapterType = CM_Wowza_Service::getTypeStatic();
        }

        $data = array('key' => rand(1, 10000) . '_' . rand(1, 100));
        if (CM_Model_StreamChannel_Media::getTypeStatic() == $type) {
            $data['width'] = 480;
            $data['height'] = 720;
            $data['serverId'] = 1;
            $data['thumbnailCount'] = 0;
            $data['adapterType'] = $adapterType;
        }
        return CM_Model_StreamChannel_Abstract::createType($type, $data);
    }

    /**
     * @param CM_Model_StreamChannel_Media|null $streamChannel
     * @param CM_Model_User|null                $user
     * @param string|null                       $filename
     * @return CM_Model_StreamChannelArchive_Media
     */
    public static function createStreamChannelVideoArchive(CM_Model_StreamChannel_Media $streamChannel = null, CM_Model_User $user = null, $filename = null) {
        if (is_null($streamChannel)) {
            $streamChannel = self::createStreamChannel();
            self::createStreamPublish($user, $streamChannel);
        }
        if (!$streamChannel->hasStreamPublish()) {
            self::createStreamPublish($user, $streamChannel);
        }
        if (null !== $filename) {
            $filename = (string) $filename;
        }
        return CM_Model_StreamChannelArchive_Media::createStatic(array('streamChannel' => $streamChannel, 'file' => $filename));
    }

    /**
     * @param CM_Model_User|null                   $user
     * @param CM_Model_StreamChannel_Abstract|null $streamChannel
     * @return CM_Model_Stream_Publish
     */
    public static function createStreamPublish(CM_Model_User $user = null, CM_Model_StreamChannel_Abstract $streamChannel = null) {
        if (!$user) {
            $user = self::createUser();
        }
        if (is_null($streamChannel)) {
            $streamChannel = self::createStreamChannel();
        }
        return CM_Model_Stream_Publish::createStatic(array(
            'streamChannel' => $streamChannel,
            'user'          => $user, 'start' => time(),<commit_msg>use SKTest methods if called from there<commit_after><?php

class CMTest_TH {

    private static $_timeStart;
    private static $timeDelta = 0;
    private static $_configBackup;

    public static function init() {
        $output = new CM_OutputStream_Null();
        $loader = CM_App::getInstance()->getProvisionLoader();
        $loader->unload($output);
        $loader->load($output);

        self::$_configBackup = serialize(CM_Config::get());

        // Reset environment
        self::clearEnv();
        self::randomizeAutoincrement();
        self::timeInit();
    }

    public static function clearEnv() {
        self::clearDb();
        self::clearCache();
        self::timeReset();
        self::clearFilesystem();
        CM_Service_Manager::getInstance()->resetServiceInstances();
        CM_Config::set(unserialize(self::$_configBackup));
    }

    public static function clearFilesystem() {
        $script = new CM_File_Filesystem_SetupScript(CM_Service_Manager::getInstance());
        $script->unload(new CM_OutputStream_Null());
    }

    public static function clearCache() {
        CM_Cache_Shared::getInstance()->flush();
        CM_Cache_Local::getInstance()->flush();
    }

    public static function timeInit() {
        if (!isset(self::$_timeStart)) {
            runkit_function_copy('time', 'time_original');
            runkit_function_redefine('time', '', 'return CMTest_TH::time();');
        }
        self::$_timeStart = time_original();
        self::$timeDelta = 0;
    }

    public static function time() {
        return self::$_timeStart + self::$timeDelta;
    }

    public static function timeForward($sec) {
        self::$timeDelta += $sec;
        self::clearCache();
    }

    public static function timeDaysForward($days) {
        self::timeForward($days * 24 * 60 * 60);
    }

    public static function timeReset() {
        self::$timeDelta = 0;
        self::clearCache();
    }

    public static function timeDelta() {
        return self::$timeDelta;
    }

    public static function timeDiffInDays($stamp1, $stamp2) {
        return round(($stamp2 - $stamp1) / (60 * 60 * 24));
    }

    /**
     * @return CM_Service_Manager
     */
    public static function getServiceManager() {
        return CM_Service_Manager::getInstance();
    }

    /**
     * @return CM_Model_User
     */
    public static function createUser() {
        return CM_Model_User::createStatic();
    }

    /**
     * @param string|null $abbreviation
     * @return CM_Model_Language
     */
    public static function createLanguage($abbreviation = null) {
        if (!$abbreviation) {
            do {
                $abbreviation = self::_randStr(5);
            } while (CM_Model_Language::findByAbbreviation($abbreviation));
        }
        return CM_Model_Language::create('English', $abbreviation, true);
    }

    /**
     * @param string             $pageClass
     * @param CM_Model_User|null $viewer OPTIONAL
     * @param array              $params OPTIONAL
     * @return CM_Page_Abstract
     */
    public static function createPage($pageClass, CM_Model_User $viewer = null, $params = array()) {
        $request = new CM_Http_Request_Get('?' . http_build_query($params), array(), $viewer);
        return new $pageClass(CM_Params::factory($request->getQuery(), true), $request->getViewer());
    }

    /**
     * @param CM_Model_User|null $user
     * @return CM_Session
     */
    public static function createSession(CM_Model_User $user = null) {
        if (is_null($user)) {
            $user = self::createUser();
        }
        $session = new CM_Session();
        $session->setUser($user);
        $session->write();
        return $session;
    }

    /**
     * @param int|null $type
     * @param int|null $adapterType
     * @return CM_Model_StreamChannel_Abstract
     */
    public static function createStreamChannel($type = null, $adapterType = null) {
        if (null === $type) {
            $type = CM_Model_StreamChannel_Media::getTypeStatic();
        }

        if (null === $adapterType) {
            $adapterType = CM_Wowza_Service::getTypeStatic();
        }

        $data = array('key' => rand(1, 10000) . '_' . rand(1, 100));
        if (CM_Model_StreamChannel_Media::getTypeStatic() == $type) {
            $data['width'] = 480;
            $data['height'] = 720;
            $data['serverId'] = 1;
            $data['thumbnailCount'] = 0;
            $data['adapterType'] = $adapterType;
        }
        return CM_Model_StreamChannel_Abstract::createType($type, $data);
    }

    /**
     * @param CM_Model_StreamChannel_Media|null $streamChannel
     * @param CM_Model_User|null                $user
     * @param string|null                       $filename
     * @return CM_Model_StreamChannelArchive_Media
     */
    public static function createStreamChannelVideoArchive(CM_Model_StreamChannel_Media $streamChannel = null, CM_Model_User $user = null, $filename = null) {
        if (is_null($streamChannel)) {
            $streamChannel = static::createStreamChannel();
            static::createStreamPublish($user, $streamChannel);
        }
        if (!$streamChannel->hasStreamPublish()) {
            static::createStreamPublish($user, $streamChannel);
        }
        if (null !== $filename) {
            $filename = (string) $filename;
        }
        return CM_Model_StreamChannelArchive_Media::createStatic(array('streamChannel' => $streamChannel, 'file' => $filename));
    }

    /**
     * @param CM_Model_User|null                   $user
     * @param CM_Model_StreamChannel_Abstract|null $streamChannel
     * @return CM_Model_Stream_Publish
     */
    public static function createStreamPublish(CM_Model_User $user = null, CM_Model_StreamChannel_Abstract $streamChannel = null) {
        if (!$user) {
            $user = self::createUser();
        }
        if (is_null($streamChannel)) {
            $streamChannel = self::createStreamChannel();
        }
        return CM_Model_Stream_Publish::createStatic(array(
            'streamChannel' => $streamChannel,
            'user'          => $user, 'start' => time(),
{'fraction_non_alphanumeric': 0.13014478607450788, 'fraction_numerical': 0.006181877338539124, 'mean_word_length': 2.5300028710881426, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 39, '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': '6428228', 'n_tokens_mistral': 3532, 'n_tokens_neox': 3427, 'n_words': 669}