private function mockProfileHandler($handleCallback) { Config::set('hyperframework.db.operation_profiler.profile_handler_class', 'Hyperframework\\Db\\Test\\DbOperationProfileHandler'); $mock = $this->getMock('Hyperframework\\Db\\Test\\DbOperationProfileHandler'); Test\DbOperationProfileHandler::setDelegate($mock); return $mock->expects($this->once())->method('handle')->will($this->returnCallback($handleCallback)); }
/** * @return string */ public function getPath() { if ($this->path === null) { $this->path = Config::getString('hyperframework.logging.log_path', 'log' . DIRECTORY_SEPARATOR . 'app.log'); } return $this->path; }
public function testCustomizeErrorViewRoot() { Config::set('hyperframework.web.error_view.root_path', 'views/_custom_error_view_root'); $this->expectOutputString("custom error view root: error\n"); $view = new ErrorView(); $view->render(500, '', null); }
public function testRun() { $this->expectOutputRegex('/^<!DOCTYPE html>/'); Config::set('hyperframework.app_root_path', dirname(__DIR__)); $debugger = new Debugger(); $debugger->execute(new Error(E_ERROR, '', __FILE__, __LINE__)); }
/** * @param int $statusCode * @param string $statusReasonPhrase * @param object $error * @param string $outputFormat * @return void */ public function render($statusCode, $statusReasonPhrase, $error, $outputFormat = null) { $rootPath = Config::getString('hyperframework.web.error_view.root_path', ''); if ($rootPath === '') { $rootPath = Config::getString('hyperframework.web.view.root_path', ''); if ($rootPath === '') { $rootPath = 'views' . DIRECTORY_SEPARATOR . '_error'; } else { $rootPath = FilePathCombiner::combine($rootPath, '_error'); } } $files = [ViewPathBuilder::build($statusCode, $outputFormat), ViewPathBuilder::build('default', $outputFormat)]; $rootPath = FileFullPathBuilder::build($rootPath); $path = null; foreach ($files as $file) { $file = FilePathCombiner::combine($rootPath, $file); if (file_exists($file)) { $path = $file; break; } } if ($path === null) { Response::setHeader('Content-Type: text/plain; charset=utf-8'); echo $statusCode; if ((string) $statusReasonPhrase !== '') { echo ' ', $statusReasonPhrase; } } else { $view = ViewFactory::createView(['status_code' => $statusCode, 'status_reason_pharse' => $statusReasonPhrase, 'error' => $error]); $view->render($path); } }
protected function deleteAppLogFile() { $path = Config::getAppRootPath() . '/log/app.log'; if (file_exists($path)) { unlink($path); } }
protected function setUp() { Config::set('hyperframework.app_root_path', dirname(__DIR__)); Config::set('hyperframework.app_root_namespace', __NAMESPACE__); Config::set('hyperframework.initialize_config', false); Config::set('hyperframework.initialize_error_handler', false); Config::set('hyperframework.web.csrf_protection.enable', false); }
private function mockProfileHandler() { Config::set('hyperframework.db.operation_profiler.enable', true); Config::set('hyperframework.db.operation_profiler.enable_logger', false); $mock = $this->getMockBuilder(DbOperationProfiler::class)->setMethods(['onPreparedStatementExecuting', 'onPreparedStatementExecuted'])->getMock(); $mock->expects($this->once())->method('onPreparedStatementExecuting'); $mock->expects($this->once())->method('onPreparedStatementExecuted'); $mock->run(); }
/** * @return void */ protected function initializeToken() { $this->setToken($this->generateToken()); $cookieDomain = Config::getString('hyperframework.web.csrf_protection.cookie_domain', ''); if ($cookieDomain === '') { $cookieDomain = null; } Response::setCookie($this->getTokenName(), $this->getToken(), ['domain' => $cookieDomain]); }
public function testBatchSizeOption() { Config::set('hyperframework.db.operation_profiler.enable', true); Config::set('hyperframework.db.operation_profiler.enable_logger', false); $mock = $this->getMockBuilder(DbOperationProfiler::class)->setMethods(['onPreparedStatementExecuting', 'onPreparedStatementExecuted'])->getMock(); $mock->expects($this->exactly(2))->method('onPreparedStatementExecuting'); $mock->expects($this->exactly(2))->method('onPreparedStatementExecuted'); $mock->run(); DbImportCommand::execute('Document', [[1, 'doc 1', 12.34], [2, 'doc 2', 0]], ['column_names' => ['id', 'name', 'decimal'], 'batch_size' => 1]); }
/** * @return ResponseEngine */ public static function getEngine() { $engine = Registry::get('hyperframework.web.response_engine'); if ($engine === null) { $class = Config::getClass('hyperframework.web.response_engine_class', ResponseEngine::class); $engine = new $class(); static::setEngine($engine); } return $engine; }
/** * @return CsrfProtectionEngine */ public static function getEngine() { $engine = Registry::get('hyperframework.web.csrf_protection_engine'); if ($engine === null) { $class = Config::getClass('hyperframework.web.csrf_protection.engine_class', CsrfProtectionEngine::class); $engine = new $class(); static::setEngine($engine); } return $engine; }
/** * @param string $name * @return array */ protected function getConfig($name) { if ($this->config === null) { $this->config = ConfigFileLoader::loadPhp(Config::getString('hyperframework.db.config_path', 'db.php')); } if (isset($this->config[$name])) { return $this->config[$name]; } if ($name === null) { return $this->config; } throw new ConfigException("Database connection config '{$name}' does not exist."); }
private function writeToReadOnlyFolder() { set_error_handler(function () { }); chmod(Config::getAppRootPath() . '/log', 0555); $writer = new LogWriter(); try { $writer->write('content'); } catch (Exception $e) { restore_error_handler(); chmod(Config::getAppRootPath() . '/log', 0755); throw $e; } }
/** * @return void */ protected function renderErrorView() { $error = $this->getError(); if ($error instanceof HttpException) { $statusCode = $error->getStatusCode(); $statusReasonPhrase = $error->getStatusReasonPhrase(); } else { $statusCode = 500; $statusReasonPhrase = 'Internal Server Error'; } $class = Config::getClass('hyperframework.web.error_view.class', ErrorView::class); $view = new $class(); $view->render($statusCode, $statusReasonPhrase, $error); }
/** * @param string $name * @param string $outputFormat * @return string */ public static function build($name, $outputFormat = null) { $result = str_replace('-', '_', $name); if (Config::getBool('hyperframework.web.view.filename.include_output_format', true)) { if ($outputFormat === null) { $outputFormat = Config::getString('hyperframework.web.view.default_output_format', 'html'); } $outputFormat = (string) $outputFormat; if ($outputFormat !== '') { $result .= '.' . $outputFormat; } } $format = Config::getString('hyperframework.web.view.format', 'php'); if ($format !== '') { $result .= '.' . $format; } return $result; }
/** * @return Router */ public function getRouter() { if ($this->router === null) { $configName = 'hyperframework.web.router_class'; $class = Config::getClass($configName); if ($class === null) { $class = 'Router'; $namespace = Config::getAppRootNamespace(); if ($namespace !== '' && $namespace !== '\\') { $class = NamespaceCombiner::combine($namespace, $class); } if (class_exists($class) === false) { throw new ClassNotFoundException("Router class '{$class}' does not exist," . " can be changed using config '{$configName}'."); } } $this->router = new $class($this); } return $this->router; }
/** * @return string */ public function getName() { if ($this->name !== null) { return $this->name; } $words = []; $word = ''; $length = strlen($this->parameterName); for ($index = 0; $index < $length; ++$index) { $char = $this->parameterName[$index]; $ascii = ord($char); if ($char !== '_' && ($ascii < 65 || $ascii > 90)) { $word .= $this->parameterName[$index]; } else { if ($word !== '') { $words[] = $word; $word = ''; } if ($char !== '_') { $word = strtolower($char); } } } if ($word !== '') { if ($this->isRepeatable() && ctype_alpha($word)) { if ($word !== 'list') { $inflectorClass = Config::getClass('hyperframework.inflector_class', Inflector::class); $words[] = $inflectorClass::singularize($word); } elseif (count($words) === 0) { $words[] = 'element'; } } else { $words[] = $word; } } $this->name = implode('-', $words); return $this->name; }
public function testGetCustomRouter() { Config::set('hyperframework.web.router_class', 'Hyperframework\\Web\\Test\\FakeRouter'); $app = new App(dirname(__DIR__)); $this->assertInstanceOf('Hyperframework\\Web\\Test\\FakeRouter', $app->getRouter()); }
public function testRenderDescriptionWhichStartsWithPhpEol() { Config::set('hyperframework.cli.command_config_path', 'description_starts_with_php_eol_option_command.php'); $this->expectOutputString('Usage: ab [options] <arg>' . PHP_EOL . PHP_EOL . 'Options:' . PHP_EOL . ' --test' . PHP_EOL . 'description' . PHP_EOL); $app = $this->getMockBuilder('Hyperframework\\Cli\\App')->disableOriginalConstructor()->getMock(); $app->method('getCommandConfig')->willReturn(new CommandConfig()); $help = new Help($app); $help->render(); }
private function mockEngineMethod($method) { $engine = $this->getMock('Hyperframework\\Common\\ConfigEngine'); Config::setEngine($engine); return $engine->expects($this->once())->method($method); }
/** * @return LogWriter */ protected function getWriter() { if ($this->writer === null) { $class = Config::getClass('hyperframework.logging.log_writer_class', LogWriter::class); $this->writer = new $class(); } return $this->writer; }
/** * @expectedException Hyperframework\Common\ClassNotFoundException */ public function testInvalidEngineConfig() { Config::set('hyperframework.web.response_engine_class', 'Unknown'); Response::getEngine(); }
public function setUp() { Config::set('hyperframework.app_root_path', '/root'); }
protected function setUp() { parent::setUp(); Config::set('hyperframework.app_root_path', dirname(__DIR__)); }
/** * @expectedException Hyperframework\Common\ConfigException */ public function testInvalidConfig() { Config::set('hyperframework.exit_function', true); ExitHelper::exitScript(); }
/** * @return LoggerEngine */ public static function getEngine() { $engine = Registry::get('hyperframework.logging.logger_engine'); if ($engine === null) { $class = Config::getClass('hyperframework.logging.logger_engine_class', LoggerEngine::class); $engine = new $class(); static::setEngine($engine); } return $engine; }
/** * @expectedException Hyperframework\Common\ClassNotFoundException */ public function testCustomCommandConfigClassNotFound() { Config::set('hyperframework.cli.command_config_class', 'Unknown'); $_SERVER['argv'] = ['run', 'arg']; $app = $this->createApp(); }
/** * @param string $appRootPath */ public function __construct($appRootPath) { Config::set('hyperframework.cli.multiple_commands', true); parent::__construct($appRootPath); }
/** * @return string */ private function getSubcommandConfigRootPath() { $folder = Config::getString('hyperframework.cli.subcommand_config_root_path'); if ($folder === null) { $folder = 'subcommands'; } return ConfigFileFullPathBuilder::build($folder); }