Skip to content

Commit

Permalink
formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
timacdonald committed Aug 19, 2019
1 parent 657987e commit 2ab651b
Show file tree
Hide file tree
Showing 5 changed files with 93 additions and 90 deletions.
65 changes: 42 additions & 23 deletions src/ChannelFake.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,62 +4,81 @@

use Psr\Log\LoggerInterface;

class ChannelFake extends FakeLogger implements LoggerInterface
class ChannelFake implements LoggerInterface
{
use LogHelpers;

/**
* @var $log LogFake
* The logger to proxy calls to.
*
* @var \TiMacDonald\Log\LogFake
*/
protected $log;

/**
* @var $name string
* The name of the current channel.
*
* @var string
*/
protected $name;

/**
* ChannelFake constructor.
* Create a new instance.
*
* @param $log
* @param $name
* @param \TiMacDonald\Log\LogFake $log
* @param string $name
* @return void
*/
public function __construct($log, $name)
{
$this->log = $log;

$this->name = $name;
}

/**
* @param $method
* @param $arguments
* Proxy a 'log' call to the logger.
*
* @param string $level
* @param string $message
* @param array $context
* @return void
*/
public function log($level, $message, array $context = [])
{
$this->proxy(function () use ($level, $message, $context) {
$this->log->log($level, $message, $context);
});
}

/**
* Handle dynamic calls to the instance.
*
* @param string $method
* @param array $arguments
* @return mixed
*/
public function __call($method, $arguments)
{
$this->log->setCurrentChannel($this->name);

$result = $this->log->{$method}(...$arguments);

$this->log->setCurrentChannel(null);

return $result;
return $this->proxy(function () use ($method, $arguments) {
return $this->log->{$method}(...$arguments);
});
}

/**
* Log a message to the logs.
*
* @param string $level
* @param string $message
* @param array $context
* Proxy calls to the logger.
*
* @return void
* @param \Closure $closure
* @return mixed
*/
public function log($level, $message, array $context = [])
private function proxy($closure)
{
$this->log->setCurrentChannel($this->name);

$this->log->log($level, $message, $context);
$result = $closure();

$this->log->setCurrentChannel(null);

return $result;
}
}
69 changes: 37 additions & 32 deletions src/LogFake.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@
use Illuminate\Support\Collection;
use PHPUnit\Framework\Assert as PHPUnit;

class LogFake extends FakeLogger implements LoggerInterface
class LogFake implements LoggerInterface
{
use LogHelpers;

/**
* All of the created logs.
*
Expand All @@ -17,14 +19,16 @@ class LogFake extends FakeLogger implements LoggerInterface

/**
* The channel being logged to.
*
* @var string|null
*/
protected $currentChannel;

/**
* Assert if a log was created based on a truth-test callback.
*
* @param string $level
* @param callable|int|null $callback
* @param string $level
* @param callable|int|null $callback
* @return void
*/
public function assertLogged($level, $callback = null)
Expand All @@ -42,9 +46,9 @@ public function assertLogged($level, $callback = null)
/**
* Assert if a log was created a number of times.
*
* @param string $level
* @param int $times
* @param callable|null $callback
* @param string $level
* @param int $times
* @param callable|null $callback
* @return void
*/
public function assertLoggedTimes($level, $times = 1, $callback = null)
Expand All @@ -58,8 +62,8 @@ public function assertLoggedTimes($level, $times = 1, $callback = null)
/**
* Determine if a log was not created based on a truth-test callback.
*
* @param string $level
* @param callable|null $callback
* @param string $level
* @param callable|null $callback
* @return void
*/
public function assertNotLogged($level, $callback = null)
Expand All @@ -83,8 +87,8 @@ public function assertNothingLogged()
/**
* Assert logged a specfic message.
*
* @param string $level
* @param string $message
* @param string $level
* @param string $message
* @return void
*/
public function assertLoggedMessage($level, $message)
Expand All @@ -97,8 +101,8 @@ public function assertLoggedMessage($level, $message)
/**
* Get all of the logs matching a truth-test callback.
*
* @param string $level
* @param callable|null $callback
* @param string $level
* @param callable|null $callback
* @return \Illuminate\Support\Collection
*/
public function logged($level, $callback = null)
Expand All @@ -115,7 +119,7 @@ public function logged($level, $callback = null)
/**
* Determine if the given log level has been created.
*
* @param string $level
* @param string $level
* @return bool
*/
public function hasLogged($level)
Expand All @@ -126,7 +130,7 @@ public function hasLogged($level)
/**
* Determine if the given log level has not been created.
*
* @param string $level
* @param string $level
* @return bool
*/
public function hasNotLogged($level)
Expand All @@ -137,7 +141,7 @@ public function hasNotLogged($level)
/**
* Get all of the created logs for a given level.
*
* @param string $type
* @param string $type
* @return \Illuminate\Support\Collection
*/
protected function logsOfLevel($level)
Expand All @@ -162,9 +166,9 @@ protected function logsInCurrentChannel()
/**
* Log a message to the logs.
*
* @param string $level
* @param string $message
* @param array $context
* @param string $level
* @param string $message
* @param array $context
* @return void
*/
public function log($level, $message, array $context = [])
Expand All @@ -180,9 +184,9 @@ public function log($level, $message, array $context = [])
/**
* Dynamically pass log calls into the writer.
*
* @param string $level
* @param string $message
* @param array $context
* @param string $level
* @param string $message
* @param array $context
* @return void
*/
public function write($level, $message, array $context = [])
Expand All @@ -193,8 +197,8 @@ public function write($level, $message, array $context = [])
/**
* Get a log channel instance.
*
* @param string|null $channel
* @return ChannelFake
* @param string|null $channel
* @return \TiMacDonald\Log\ChannelFake
*/
public function channel($channel = null)
{
Expand All @@ -204,8 +208,8 @@ public function channel($channel = null)
/**
* Get a log driver instance.
*
* @param string|null $driver
* @return ChannelFake
* @param string|null $driver
* @return \TiMacDonald\Log\ChannelFake
*/
public function driver($driver = null)
{
Expand All @@ -215,9 +219,9 @@ public function driver($driver = null)
/**
* Create a new, on-demand aggregate logger instance.
*
* @param array $channels
* @param string|null $channel
* @return ChannelFake
* @param array $channels
* @param string|null $channel
* @return \TiMacDonald\Log\ChannelFake
*/
public function stack(array $channels, $channel = null)
{
Expand All @@ -227,8 +231,8 @@ public function stack(array $channels, $channel = null)
/**
* Create a stack based channel name.
*
* @param array $channels
* @param string|null $channel
* @param array $channels
* @param string|null $channel
* @return string
*/
protected function createStackChannelName($channels, $channel)
Expand All @@ -239,7 +243,7 @@ protected function createStackChannelName($channels, $channel)
/**
* Set the current channel being logged to.
*
* @param string $name
* @param string|null $name
* @return void
*/
public function setCurrentChannel($name)
Expand All @@ -260,6 +264,7 @@ public function currentChannel()
/**
* Determine if in provided channel.
*
* @param string|null $channel
* @return string
*/
protected function currentChannelIs($channel)
Expand All @@ -280,7 +285,7 @@ public function getDefaultDriver()
/**
* Set the default log driver name.
*
* @param string $name
* @param string $name
* @return void
*/
public function setDefaultDriver($name)
Expand Down
Loading

0 comments on commit 2ab651b

Please sign in to comment.