1. Go to this page and download the library: Download gamez/psr-testlogger library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
gamez / psr-testlogger example snippets
use Psr\Log\LoggerInterface;
class SubjectUnderTest
{
public function __construct(LoggerInterface $logger)
{
$this->logger = $logger;
}
public function execute()
{
$this->logger->info('Message with a {placeholder}', ['placeholder' => 'value']);
$this->logger->emergency('This {placeholder} will not be replaced.');
}
}
use Gamez\Psr\Log\TestLogger;
use PHPUnit\Framework\TestCase;
class MyTest extends TestCase
{
/**
* @var TestLogger
*/
private $logger;
/**
* @var SubjectUnderTest
*/
private $sut;
protected function setUp()
{
$this->logger = new TestLogger();
$this->sut = new SubjectUnderTest($this->logger);
}
public function testLogging()
{
$this->sut->execute();
$log = $this->logger->log;
$this->assertTrue($log->has('Message with a value'));
$this->assertTrue($log->hasRecordsWithContextKey('foo'));
$this->assertFalse($log->hasRecordsWithContextKeyAndValue('foo', 'unwanted'));
// This will break
$this->assertFalse($log->hasRecordsWithUnreplacedPlaceholders());
}
}
use Gamez\Psr\Log\Record;
use Gamez\Psr\Log\TestLogger;
use PHPUnit\Framework\TestCase;
class MyTest extends TestCase
{
/**
* @var TestLogger
*/
private $logger;
/**
* @var SubjectUnderTest
*/
private $sut;
protected function setUp()
{
$this->logger = new TestLogger();
$this->sut = new SubjectUnderTest($this->logger);
}
public function testSomethingSpecial()
{
$filteredLog = $this->logger->log->filter(function (Record $record) {
// Matches messages with only numbers
return ctype_digit($record->message);
});
$this->assertCount(0, $filteredLog);
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.