public static function getInstance() { if (self::$instance == null) { self::$instance = new self(); } return self::$instance; }
public function execute($command, $doit = true) { if ($doit) { $this->output = system($command, $this->returnCode); return $this->returnCode === 0; } else { nbLogger::getInstance()->logLine($command); return true; } }
public function __construct() { $this->argumentSet = new nbArgumentSet(); $this->optionSet = new nbOptionSet(); $this->logger = nbLogger::getInstance(); $this->configure(); if (!$this->getName()) { throw new InvalidArgumentException('Command name must be set'); } // Config file option must be added after setName if (!$this->optionSet->hasOption('config-file')) { $this->optionSet->addOption(new nbOption('config-file', '', nbOption::PARAMETER_OPTIONAL, 'Reads configuration from file', $this->generateDefaultConfigFile())); } }
protected static function format($text, $level) { $logger = nbLogger::getInstance(); return $logger->format($text, $level); }
<?php require_once dirname(__FILE__) . '/../../../bootstrap/unit.php'; $t = new lime_test(9); $t->comment('nbLoggerTest - Test log'); $output = new nbStreamOutput(); $logger = nbLogger::getInstance(); $logger->setOutput($output); $logger->log("test"); $t->is($output->getStream(), "test", '->log() has written "test"'); $t->comment('nbLoggerTest - Test format levels'); $t->is(nbLogger::formatLevel(nbLogger::ERROR), 'error', '->formatLevel() has ERROR level'); $t->is(nbLogger::formatLevel(nbLogger::INFO), 'info', '->formatLevel() has INFO level'); $t->is(nbLogger::formatLevel(nbLogger::COMMENT), 'comment', '->formatLevel() has COMMENT level'); $t->is(nbLogger::formatLevel(nbLogger::QUESTION), 'question', '->formatLevel() has QUESTION level'); $t->is($logger->format('text', nbLogger::INFO), '<info>text</info>', '->format() formats "text" as "<info>text</info"'); //$t->is($logger->format('text', 'info'), '<info>text</info>', '->format() formats "text" as "<info>text</info"'); $logger->logLine("test"); $t->is($output->getStream(), "test\n", '->logLine() has written "test" with line feed'); $t->comment('nbLoggerTest - Test log array'); $logger->log(array('item1', 'item2')); $t->is($output->getStream(), "0 => item1\n1 => item2\n", '->log() has printed an array'); $logger->log(array('1' => 'item1', 'two' => 'item2')); $t->is($output->getStream(), "1 => item1\ntwo => item2\n", '->log() has printed an array');
<?php require_once dirname(__FILE__) . '/../../bootstrap/unit.php'; $t = new lime_test(3); $output = new nbStreamOutput(); nbLogger::getInstance()->setOutput($output); $application = new DummyApplication($serviceContainer); $command = new nbHelpCommand(); $command->setApplication($application); $serviceContainer->commandLoader->getCommands()->addCommand(new DummyCommand('dummy1')); $serviceContainer->commandLoader->getCommands()->addCommand($command); $t->comment('nbHelpCommandTest - Test get name'); $t->is($command->getName(), 'help', '->getName() is "help"'); //$t->comment('nbHelpCommandTest - Test print command help'); //$command->run(new nbCommandLineParser(), array('help')); //$t->ok($application->executedFormatHelpString, '->run() called nbApplication::formatHelpString()'); //$application->executedFormatHelpString = false; $t->comment('nbHelpCommandTest - Test unknown command'); try { $command->run(new nbCommandLineParser(), array('cmd')); $t->fail('->run() throws an Exception if command is not found'); } catch (Exception $e) { $t->pass('->run() throws an Exception if command is not found'); } $t->ok(!$application->executedFormatHelpString, '->run() didn\'t call nbApplication::formatHelpString()'); //$t->comment('nbHelpCommandTest - Test existing command'); //$command->run(new nbCommandLineParser(), array('dummy1')); //$t->ok($application->executedFormatHelpString, '->run() called nbApplication::formatHelpString()');
<?php require_once dirname(__FILE__) . '/../../../bootstrap/unit.php'; if (nbConfig::get('nb_ignore_long_tests', true)) { nbLogger::getInstance()->logLine('Ignoring tests: ' . dirname(__FILE__), nbLogger::INFO); $t = new lime_test(0); return; } $t = new lime_test(9); // Setup // This folder must be outside the bee folder to avoid recursion $installDir = nbConfig::get('nb_bee_dir') . '/../bee-sandbox/'; $t->comment('Bee Install Command'); $cmd = new nbBeeInstallCommand(); $t->comment(' 1. bee:install requires the destination folder'); try { $cmd->run(new nbCommandLineParser(), ''); $t->fail('Command requires 1 argument'); } catch (Exception $e) { $t->pass('Command requires 1 argument'); } $t->comment(' 2. bee:install installs correctly on ' . $installDir); $cmd->run(new nbCommandLineParser(), $installDir . ' -s ' . nbConfig::get('nb_bee_dir') . '/'); $t->ok(file_exists($installDir . '/config'), 'Command created config directory in installation folder'); $t->ok(file_exists($installDir . '/data'), 'Command created data directory in installation folder'); $t->ok(file_exists($installDir . '/docs'), 'Command created docs directory in installation folder'); $t->ok(file_exists($installDir . '/lib'), 'Command created lib directory in installation folder'); $t->ok(file_exists($installDir . '/plugins'), 'Command created plugin directory in installation folder'); $t->ok(file_exists($installDir . '/test'), 'Command created test directory in installation folder'); $t->ok(file_exists($installDir . '/bee'), 'Command created bee file in installation folder'); /*
<?php require_once dirname(__FILE__) . '/../../lib/core/autoload/nbAutoload.php'; $autoload = nbAutoload::getInstance(); $autoload->register(); $autoload->addDirectory('vendor/', '*.php', true); $autoload->addDirectory('lib/', '*.php', true); $autoload->addDirectory('test/lib/', '*.php', true); // Configures bee variables $configParser = new nbYamlConfigParser(); nbConfig::set('nb_bee_dir', dirname(__FILE__) . '/../..'); nbConfig::set('nb_config_dir', nbConfig::get('nb_bee_dir') . '/config'); nbConfig::set('nb_test_config_dir', dirname(__FILE__) . '/../config/'); $configParser->parseFile(nbConfig::get('nb_bee_dir') . '/.bee/config.yml', '', true); $configParser->parseFile(nbConfig::get('nb_config_dir') . '/config.yml', '', true); $configParser->parseFile(nbConfig::get('nb_test_config_dir') . '/config.yml', '', true); $serviceContainer = new sfServiceContainerBuilder(); $serviceContainer->register('pluginLoader', 'nbPluginLoader')->addArgument(nbConfig::get('nb_plugins_dir'))->addArgument(new sfServiceReference('commandLoader'))->setShared(true); $serviceContainer->register('commandLoader', 'nbCommandLoaderWithReset')->setShared(true); $output = new nbConsoleOutput(); $logger = nbLogger::getInstance(); $logger->setOutput($output);
require_once dirname(__FILE__) . '/../../../../test/bootstrap/unit.php'; nbConfig::set('nb_command_dir', nbConfig::get('nb_sandbox_dir')); $dataDir = nbConfig::get('nb_data_dir') . '/config'; $sandboxDir = nbConfig::get('nb_sandbox_dir'); $configFileOk = $dataDir . '/config.ok.yml'; $configFileNoField = $dataDir . '/config.nofield.yml'; $configFileNoChild = $dataDir . '/config.nochild.yml'; $configFileNotExists = $dataDir . '/config.notexists.yml'; $dirNotExists = $dataDir . '/config.dirnotexists.yml'; $fileNotExists = $dataDir . '/config.filenotexists.yml'; $templateFile = $dataDir . '/template.config.yml'; $templateFileNotExists = $dataDir . '/template.notexists.sample.yml'; $t = new lime_test(16); $t->comment('Check Configuration'); $t->comment(' 1. Config file checks correctly'); $checker = new nbConfigurationChecker(array('verbose' => true, 'logger' => nbLogger::getInstance())); $t->ok($checker->checkConfigFile($templateFile, $configFileOk), 'Project configuration checked successfully'); $t->comment(' 2. Config file has errors (no child and no required field)'); try { $checker->checkConfigFile($templateFile, $configFileNoField); $t->fail('Config file without required field not checked successfully'); } catch (Exception $e) { $t->pass('Config file without required field not checked successfully'); } $t->ok($checker->hasErrors(), 'Config file has errors'); $t->is(count($checker->getErrors()), 2, 'Config file has 2 errors'); $errors = array('app_required_field' => 'required', 'app_required_child_field' => 'required'); $t->is($checker->getErrors(), $errors, 'Config file has errors formatted correctly'); $t->comment(' 3. Config file has errors (no child)'); try { $checker->checkConfigFile($templateFile, $configFileNoChild);
/** * Renders an exception. * * @param Exception $e An exception object */ public function renderException(Exception $e) { $title = sprintf(' [%s] ', get_class($e)); $len = strlen($title); $lines = array(); foreach (explode("\n", $e->getMessage()) as $line) { $lines[] = sprintf(' %s ', $line); $len = max(strlen($line) + 4, $len); } $messages = array(str_repeat(' ', $len)); if ($this->trace) { $messages[] = $title . str_repeat(' ', $len - strlen($title)); } foreach ($lines as $line) { $messages[] = $line . str_repeat(' ', $len - strlen($line)); } $messages[] = str_repeat(' ', $len); $output = new nbFileOutput(STDERR); $logger = nbLogger::getInstance(); $logger->setOutput($output); $logger->log("\n"); foreach ($messages as $message) { $logger->log($message, nbLogger::ERROR); $logger->log("\n"); } if ($this->trace) { $logger->log("\n"); $logger->log("Exception trace:\n", nbLogger::COMMENT); // exception related properties $trace = $e->getTrace(); $file = $e->getFile() != null ? $e->getFile() : 'n/a'; $line = $e->getLine() != null ? $e->getLine() : 'n/a'; $logger->log(sprintf(" %s:%s\n", $file, $line), nbLogger::INFO); for ($i = 0, $count = count($trace); $i < $count; ++$i) { $class = isset($trace[$i]['class']) ? $trace[$i]['class'] : ''; $type = isset($trace[$i]['type']) ? $trace[$i]['type'] : ''; $function = $trace[$i]['function'] . '()'; $file = isset($trace[$i]['file']) ? $trace[$i]['file'] : 'n/a'; $line = isset($trace[$i]['line']) ? $trace[$i]['line'] : 'n/a'; $message = sprintf(" %s%s%s at %s:%s\n", $class, $type, $function, $file, $line); $logger->log($message, nbLogger::INFO); } } }
/** * Logs a message in a section. * * @param string $section The section name * @param string $message The message * @param int $size The maximum size of a line */ protected function logLine($message, $level = nbLogger::INFO) { if ($this->isVerbose) { nbLogger::getInstance()->logLine($message, $level); } }