Files
yii2/tests/framework/log/TargetTest.php
Robert Korulczyk ba0ab403b5 Added php-cs-fixer coding standards validation to Travis CI (#14100)
* php-cs-fixer: PSR2 rule.

* php-cs-fixer: PSR2 rule - fix views.

* Travis setup refactoring.

* Add php-cs-fixer to travis cs tests.

* Fix tests on hhvm-3.12

* improve travis config

* composer update

* revert composer update

* improve travis config

* Fix CS.

* Extract config to separate classes.

* Extract config to separate classes.

* Add file header.

* Force short array syntax.

* binary_operator_spaces fixer

* Fix broken tests

* cast_spaces fixer

* concat_space fixer

* dir_constant fixer

* ereg_to_preg fixer

* function_typehint_space fixer

* hash_to_slash_comment fixer

* is_null fixer

* linebreak_after_opening_tag fixer

* lowercase_cast fixer

* magic_constant_casing fixer

* modernize_types_casting fixer

* native_function_casing fixer

* new_with_braces fixer

* no_alias_functions fixer

* no_blank_lines_after_class_opening fixer

* no_blank_lines_after_phpdoc fixer

* no_empty_comment fixer

* no_empty_phpdoc fixer

* no_empty_statement fixer

* no_extra_consecutive_blank_lines fixer

* no_leading_import_slash fixer

* no_leading_namespace_whitespace fixer

* no_mixed_echo_print fixer

* no_multiline_whitespace_around_double_arrow fixer

* no_multiline_whitespace_before_semicolons fixer

* no_php4_constructor fixer

* no_short_bool_cast fixer

* no_singleline_whitespace_before_semicolons fixer

* no_spaces_around_offset fixer

* no_trailing_comma_in_list_call fixer

* no_trailing_comma_in_singleline_array fixer

* no_unneeded_control_parentheses fixer

* no_unused_imports fixer

* no_useless_return fixer

* no_whitespace_before_comma_in_array fixer

* no_whitespace_in_blank_line fixer

* not_operator_with_successor_space fixer

* object_operator_without_whitespace fixer

* ordered_imports fixer

* php_unit_construct fixer

* php_unit_dedicate_assert fixer

* php_unit_fqcn_annotation fixer

* phpdoc_indent fixer

* phpdoc_no_access fixer

* phpdoc_no_empty_return fixer

* phpdoc_no_package fixer

* phpdoc_no_useless_inheritdoc fixer

* Fix broken tests

* phpdoc_return_self_reference fixer

* phpdoc_single_line_var_spacing fixer

* phpdoc_single_line_var_spacing fixer

* phpdoc_to_comment fixer

* phpdoc_trim fixer

* phpdoc_var_without_name fixer

* psr4 fixer

* self_accessor fixer

* short_scalar_cast fixer

* single_blank_line_before_namespace fixer

* single_quote fixer

* standardize_not_equals fixer

* ternary_operator_spaces fixer

* trailing_comma_in_multiline_array fixer

* trim_array_spaces fixer

* protected_to_private fixer

* unary_operator_spaces fixer

* whitespace_after_comma_in_array fixer

* `parent::setRules()` -> `$this->setRules()`

* blank_line_after_opening_tag fixer

* Update finder config.

* Revert changes for YiiRequirementChecker.

* Fix array formatting.

* Add missing import.

* Fix CS for new code merged from master.

* Fix some indentation issues.
2017-06-12 12:25:45 +03:00

190 lines
6.3 KiB
PHP

<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace yiiunit\framework\log;
use yii\log\Dispatcher;
use yii\log\Logger;
use yii\log\Target;
use yiiunit\TestCase;
/**
* @group log
*/
class TargetTest extends TestCase
{
public static $messages;
public function filters()
{
return [
[[], ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']],
[['levels' => 0], ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H']],
[
['levels' => Logger::LEVEL_INFO | Logger::LEVEL_WARNING | Logger::LEVEL_ERROR | Logger::LEVEL_TRACE],
['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H'],
],
[['levels' => ['error']], ['B', 'G', 'H']],
[['levels' => Logger::LEVEL_ERROR], ['B', 'G', 'H']],
[['levels' => ['error', 'warning']], ['B', 'C', 'G', 'H']],
[['levels' => Logger::LEVEL_ERROR | Logger::LEVEL_WARNING], ['B', 'C', 'G', 'H']],
[['categories' => ['application']], ['A', 'B', 'C', 'D', 'E']],
[['categories' => ['application*']], ['A', 'B', 'C', 'D', 'E', 'F']],
[['categories' => ['application.*']], ['F']],
[['categories' => ['application.components']], []],
[['categories' => ['application.components.Test']], ['F']],
[['categories' => ['application.components.*']], ['F']],
[['categories' => ['application.*', 'yii.db.*']], ['F', 'G', 'H']],
[['categories' => ['application.*', 'yii.db.*'], 'except' => ['yii.db.Command.*']], ['F', 'G']],
[['categories' => ['application', 'yii.db.*'], 'levels' => Logger::LEVEL_ERROR], ['B', 'G', 'H']],
[['categories' => ['application'], 'levels' => Logger::LEVEL_ERROR], ['B']],
[['categories' => ['application'], 'levels' => Logger::LEVEL_ERROR | Logger::LEVEL_WARNING], ['B', 'C']],
];
}
/**
* @dataProvider filters
*/
public function testFilter($filter, $expected)
{
static::$messages = [];
$logger = new Logger();
$dispatcher = new Dispatcher([
'logger' => $logger,
'targets' => [new TestTarget(array_merge($filter, ['logVars' => []]))],
'flushInterval' => 1,
]);
$logger->log('testA', Logger::LEVEL_INFO);
$logger->log('testB', Logger::LEVEL_ERROR);
$logger->log('testC', Logger::LEVEL_WARNING);
$logger->log('testD', Logger::LEVEL_TRACE);
$logger->log('testE', Logger::LEVEL_INFO, 'application');
$logger->log('testF', Logger::LEVEL_INFO, 'application.components.Test');
$logger->log('testG', Logger::LEVEL_ERROR, 'yii.db.Command');
$logger->log('testH', Logger::LEVEL_ERROR, 'yii.db.Command.whatever');
$this->assertEquals(count($expected), count(static::$messages));
$i = 0;
foreach ($expected as $e) {
$this->assertEquals('test' . $e, static::$messages[$i++][0]);
}
}
public function testGetContextMessage()
{
$target = new TestTarget([
'logVars' => [
'A', '!A.A_b', 'A.A_d',
'B.B_a',
'C', 'C.C_a',
'D',
],
]);
$GLOBALS['A'] = [
'A_a' => 1,
'A_b' => 1,
'A_c' => 1,
];
$GLOBALS['B'] = [
'B_a' => 1,
'B_b' => 1,
'B_c' => 1,
];
$GLOBALS['C'] = [
'C_a' => 1,
'C_b' => 1,
'C_c' => 1,
];
$GLOBALS['E'] = [
'C_a' => 1,
'C_b' => 1,
'C_c' => 1,
];
$context = $target->getContextMessage();
$this->assertContains('A_a', $context);
$this->assertNotContains('A_b', $context);
$this->assertContains('A_c', $context);
$this->assertContains('B_a', $context);
$this->assertNotContains('B_b', $context);
$this->assertNotContains('B_c', $context);
$this->assertContains('C_a', $context);
$this->assertContains('C_b', $context);
$this->assertContains('C_c', $context);
$this->assertNotContains('D_a', $context);
$this->assertNotContains('D_b', $context);
$this->assertNotContains('D_c', $context);
$this->assertNotContains('E_a', $context);
$this->assertNotContains('E_b', $context);
$this->assertNotContains('E_c', $context);
}
/**
* @covers \yii\log\Target::setLevels()
* @covers \yii\log\Target::getLevels()
*/
public function testSetupLevelsThroughArray()
{
$target = $this->getMockForAbstractClass('yii\\log\\Target');
$target->setLevels(['info', 'error']);
$this->assertEquals(Logger::LEVEL_INFO | Logger::LEVEL_ERROR, $target->getLevels());
$target->setLevels(['trace']);
$this->assertEquals(Logger::LEVEL_TRACE, $target->getLevels());
$this->expectException('yii\\base\\InvalidConfigException');
$this->expectExceptionMessage('Unrecognized level: unknown level');
$target->setLevels(['info', 'unknown level']);
}
/**
* @covers \yii\log\Target::setLevels()
* @covers \yii\log\Target::getLevels()
*/
public function testSetupLevelsThroughBitmap()
{
$target = $this->getMockForAbstractClass('yii\\log\\Target');
$target->setLevels(Logger::LEVEL_INFO | Logger::LEVEL_WARNING);
$this->assertEquals(Logger::LEVEL_INFO | Logger::LEVEL_WARNING, $target->getLevels());
$target->setLevels(Logger::LEVEL_TRACE);
$this->assertEquals(Logger::LEVEL_TRACE, $target->getLevels());
$this->expectException('yii\\base\\InvalidConfigException');
$this->expectExceptionMessage('Incorrect 128 value');
$target->setLevels(128);
}
}
class TestTarget extends Target
{
public $exportInterval = 1;
/**
* Exports log [[messages]] to a specific destination.
* Child classes must implement this method.
*/
public function export()
{
TargetTest::$messages = array_merge(TargetTest::$messages, $this->messages);
$this->messages = [];
}
/**
* @inheritdoc
*/
public function getContextMessage()
{
return parent::getContextMessage();
}
}