ConsoleLoggerTrait
namespace |
bhenk\logger\unit |
predicates |
Trait |
Trait capable of redirecting log output to console
Note
Caveat: only log statements logged via Log will be redirected. Test cases that want to inspect log output of classes under test use this trait. It is recommended to use this trait together with LogAttribute.
Example usage:
class SomeTest extends TestCase {
use ConsoleLoggerTrait;
...
All log statements via Log will be redirected during runtime of the TestCase,
permanently.
At least until you remove the use statement. When more flexibility is required also use
LogAttribute.
#[LogAttribute()]
class SomeTest extends TestCase {
use ConsoleLoggerTrait;
...
All log statements via Log will be redirected.
To stop redirecting log statements for the entire
TestCase just type false as the first argument for LogAttribute. To change the
Level of all log statements that pass to console do something like
#[LogAttribute(true, Level::Error)]. When more fine-grained control is needed use
LogAttribute also on individual test methods.
#[LogAttribute(false)]
public function testSomeFeature() : void {
...
}
Suppress all logging via console of code touched by SomeFeature. When revisiting the test method just change the LogAttribute parameter to true. Optionally change the level of log statements seen via console as well.
The on/off setting of LogAttribute on class level has precedence
over that on method level. A class
with #[LogAttribute(false)] will never output via console.
The setting of the level parameter of LogAttribute on individual methods has precedence over that set on class level.
If you override one of the PHPUnit fixtures make sure to call the corresponding trait-method:
#[LogAttribute(true)]
class ResourceTest extends TestCase {
use ConsoleLoggerTrait {
setUp as public traitSetUp;
}
private Resource $resource;
public function setUp(): void {
$this->traitSetUp();
$this->resource = new Resource();
}
This trait calls on Log to set the type of logger temporarily to LoggerTypes::clt. Skies look bright if the logger of this type has the handler ConsoleHandler. If so, this trait will use the ColorSchemeInterface set on this handler. Otherwise, a RuntimeException will be thrown with the message that you messed up the code.
see also
Methods
ConsoleLoggerTrait::setUpBeforeClass
predicates |
public | static |
Sets up before the TestCase starts running
If LogAttribute on class level is absent or enabled, will print a line to console with the name of the TestCase. Will call on parent::setUpBeforeClass() after this.
public static function setUpBeforeClass(): void
ConsoleLoggerTrait::tearDownAfterClass
predicates |
public | static |
Tears down after the TestCase has run
If LogAttribute on class level is absent or enabled, will print a farewell message to console as demarcation of the TestCase. Will call on parent::tearDownAfterClass() after this.
public static function tearDownAfterClass(): void
ConsoleLoggerTrait::setUp
predicates |
public |
Set up before an individual test method starts running
If LogAttribute on method level is absent or enabled, will print the name of the method to console. Sets the LoggerTypes::clt as type on Log. Will call on parent::setUp() after this.
public function setUp(): void
ConsoleLoggerTrait::tearDown
predicates |
public |
Resets the Logger type
Will reset the Log to its original LoggerType. Calls parent::tearDown() after this.
public function tearDown(): void
Sat, 29 Apr 2023 12:38:18 +0000