Skip to content

Commit

Permalink
Create logs folder if it does not exist
Browse files Browse the repository at this point in the history
  • Loading branch information
Guido W. Pettinari committed Jun 7, 2022
1 parent 6e516ed commit 918d86c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 10 deletions.
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@ In *examples/WordPressTest.php* you can find an example of a test which implemen

# Example phpunit.xml

In *examples/phpunit.example.xml* you can find an example of a PHPUnit configuration file with all the avilable options.
In *examples/phpunit.example.xml* you can find an example of a PHPUnit configuration file with all the available options.

# Logging

The class `WordPressTestCase` implement a simple logger trait:
The class `WordPressTestCase` implements a simple logging trait:

- To log a messag to file, call `self::log( $message )`.
- The file will be named after the test class and placed in the subfolder *tests/logs*. **Important**: if you use the logger, make sure this folder exists!
- Customize the folder with the `logsPath` environment variable (see below).
- You can also customize the filename, or even choose a different stream. For these advanced uses, see the class documentation.
- The file will be named after the test class and placed in the subfolder *tests/logs*.
- Customize the subfolder with the `logsPath` environment variable (see below).
- You can also customize the filename and, in general, the output stream: for these advanced uses, see the `Loggable` trait's documentation.

# Multisite support

Expand Down Expand Up @@ -80,4 +80,4 @@ To pass arguments to phpunit, use the notation `composer run test -- arguments`.

# To do

- Add logging to example class
- Add logging to example class
30 changes: 26 additions & 4 deletions src/Loggable.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* be named after the test class and be placed in the logs subfolder.
* - Choose a custom subfolder for the log files via the environment
* variable 'logsPath', or customize the full path overriding getLogFilePath().
* - Choose a different output stream by ovverriding getLogFileStream().
* - Choose a different output stream by ovverriding getLogStream().
*/
trait Loggable
{
Expand All @@ -18,13 +18,15 @@ trait Loggable
*/
protected static function log( $message ): void
{
$stream = static::getLogFileStream();
$stream = static::getLogStream();

if ( is_array( $message ) || is_object( $message ) ) {
fwrite( $stream, print_r( $message, true) );
}
else {
fwrite( $stream, $message );
}

fwrite( $stream, PHP_EOL );
}

Expand All @@ -33,18 +35,38 @@ protected static function log( $message ): void
*/
protected static function getLogFilePath(): string
{
return ( $_ENV['logsPath'] ?? 'tests/logs' )
$dir = $_ENV['logsPath'] ?? 'tests/logs';

$path = $dir
. DIRECTORY_SEPARATOR
. (new \ReflectionClass( static::class ))->getShortName()
. '.log';

return $path;
}

/**
* The log will be sent to the stream returned by
* this method
*/
protected static function getLogFileStream()
protected static function getLogStream()
{
static::maybeCreateDir();

return fopen( static::getLogFilePath(), 'a' );
}

/**
* Create the logs directory if it does not exist
*/
protected static function maybeCreateDir(): bool
{
$dir = dirname( self::getLogFilePath() );

if ( ! file_exists( $dir ) ) {
return mkdir( $dir, 0744, true );
}

return true;
}
}

0 comments on commit 918d86c

Please sign in to comment.