1. Go to this page and download the library: Download geekcell/container-facade 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/ */
geekcell / container-facade example snippets
namespace App\Service;
// ...
class Logger
{
public function __construct(
private readonly FileWriter $writer,
) {
}
public function log(string $message, LogLevel $level = LogLevel::INFO): void
{
$line = sprintf(
'%s (%s): %s',
(new \DateTime)->format('c'),
$level->value,
$message,
);
$this->writer->writeLine($line);
}
}
namespace App\Support\Facade;
use App\Service\Logger as LoggerRoot;
use GeekCell\Facade\Facade;
class Logger extends Facade
{
protected static function getFacadeAccessor(): string
{
return 'app.logger';
}
}
namespace App;
use GeekCell\Facade\Facade;
use Symfony\Bundle\FrameworkBundle\Kernel\MicroKernelTrait;
use Symfony\Component\HttpKernel\Kernel as BaseKernel;
class Kernel extends BaseKernel
{
use MicroKernelTrait;
public function boot()
{
parent::boot();
// This is where the magic happens!
Facade::setContainer($this->container);
}
}
// ...
use App\Support\Facade\Logger;
class SomeClass
{
public function doStuff()
{
Logger::log('Calling ' __CLASS__ . '::doStuff()', LogLevel::DEBUG);
// The acutal method logic ...
}
}
// ...
use App\Support\Facade\Logger;
use PHPUnit\Framework\TestCase;
class SomeClassTest extends TestCase
{
public function tearDown(): void
{
Logger::clear();
}
// ...
public function testDoStuff(): void
{
// Swap real service with mock
$loggerMock = Logger::swapMock();
// Set expectations for mock
$loggerMock->shouldReceive('log')->once();
$out = new SomeClass();
$result = $out->doStuff(); // This will now call the mock!
// Test assertions ...
}
}
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.