diff --git a/src/Behat/Context/LogsContext.php b/src/Behat/Context/LogsContext.php index e43a06a..9b74ed7 100644 --- a/src/Behat/Context/LogsContext.php +++ b/src/Behat/Context/LogsContext.php @@ -33,14 +33,14 @@ class LogsContext extends RawDrupalContext { * * @var array */ - protected static $logTypes = ['php']; + protected static $types = ['php']; /** * Log levels to register. * * @var array */ - protected static $logLevels = [ + protected static $levels = [ 'ERROR' => 3, 'WARNING' => 4, 'NOTICE' => 5, @@ -51,7 +51,7 @@ class LogsContext extends RawDrupalContext { * * @var int */ - protected static $logLimit = 100; + protected static $limit = 100; /** * Drupal Helper Core class. @@ -79,7 +79,7 @@ class LogsContext extends RawDrupalContext { * * @var string */ - protected static $logPath = DRUPAL_ROOT . '/../reports/behat/dblog/report_dblog.csv'; + protected static $path = DRUPAL_ROOT . '/../reports/behat/dblog'; /** * Option to enable, disable to write the report. @@ -99,20 +99,20 @@ public function __construct(array $parameters = []) { if (isset($parameters['base_url'])) { static::$baseUrl = $parameters['base_url']; } - if (isset($parameters['log_types'])) { - static::$logTypes = $parameters['log_types']; + if (isset($parameters['types'])) { + static::$types = $parameters['types']; } - if (isset($parameters['log_levels'])) { - $this->setLevels($parameters['log_levels']); + if (isset($parameters['levels'])) { + $this->setLevels($parameters['levels']); } - if (isset($parameters['log_limit'])) { - static::$logLimit = $parameters['log_limit']; + if (isset($parameters['limit'])) { + static::$limit = $parameters['limit']; } - if (isset($parameters['log_path'])) { - static::$logPath = $parameters['log_path']; + if (isset($parameters['path'])) { + static::$path = $parameters['path']; } - if (isset($parameters['log_write_report'])) { - static::$writeReportEnabled = $parameters['log_write_report']; + if (isset($parameters['write_report'])) { + static::$writeReportEnabled = $parameters['write_report']; } } @@ -123,12 +123,12 @@ public function __construct(array $parameters = []) { * Levels list. */ protected function setLevels(array $levels_list) { - static::$logLevels = []; + static::$levels = []; foreach ($levels_list as $level) { $constant_name = '\Drupal\Core\Logger\RfcLogLevel::' . $level; if (defined($constant_name)) { - static::$logLevels[$level] = constant($constant_name); + static::$levels[$level] = constant($constant_name); } } } @@ -149,9 +149,11 @@ public static function setLogsTimeSuite(BeforeSuiteScope $before_suite_scope) { */ public static function showLogsAfterSuite(AfterSuiteScope $after_suite_scope) { $grouped_logs = static::getGroupedLogs(); - static::writeTableLogs($grouped_logs); - if (static::$writeReportEnabled) { - static::writeReport($grouped_logs); + if (!empty($grouped_logs)) { + static::writeTableLogs($grouped_logs); + if (static::$writeReportEnabled) { + static::writeReport($grouped_logs); + } } } @@ -176,7 +178,7 @@ public static function writeTableLogs(array $grouped_logs) { $levels = RfcLogLevel::getLevels(); $i = 1; - $limit = static::$logLimit; + $limit = static::$limit; $grouped_logs_limit = array_slice($grouped_logs, 0, $limit); foreach ($grouped_logs_limit as $log) { $message = static::formatMessageWatchdog($log); @@ -198,10 +200,13 @@ public static function writeTableLogs(array $grouped_logs) { * List of logs to write. */ public static function writeReport(array $grouped_logs) { - $source_file = static::$logPath; - $source_dir = dirname($source_file); + // Add date to report. + $source_dir = dirname(static::$path); + $date = date_create(); + $time = date_format($date, "Y-m-d-H-i-s"); + $source_file = $source_dir . '/dblog-report-' . $time . '.csv'; if (!file_exists($source_dir)) { - mkdir($source_dir, 0777, TRUE); + mkdir($source_dir, 0664, TRUE); } if (is_writable($source_dir)) { // Open CSV. @@ -264,7 +269,7 @@ public static function writeReport(array $grouped_logs) { public static function getGroupedLogs() { $core = static::getStaticCore(); $method = $core . "::getDbLogGroupedMessages"; - $logs = call_user_func($method, static::$suiteStartTime, static::$logLevels, static::$logTypes); + $logs = call_user_func($method, static::$suiteStartTime, static::$levels, static::$types); return $logs; } @@ -295,9 +300,9 @@ public function showDbLog(AfterScenarioScope $scope) { $module_is_enabled = in_array('dblog', $this->getCore()->getModuleList()); if ($module_is_enabled) { - $log_types = $scope->getTestResult()->getResultCode() === TestResults::PASSED ? static::$logTypes : []; + $log_types = $scope->getTestResult()->getResultCode() === TestResults::PASSED ? static::$types : []; // Filter by error, notice, and warning severity. - $logs = $this->getCore()->getDbLogMessages($this->getScenarioStartTime(), static::$logLevels, $log_types); + $logs = $this->getCore()->getDbLogMessages($this->getScenarioStartTime(), static::$levels, $log_types); if (!empty($logs)) { $this->printWatchdogLogs($logs); }