PHPUnit extension for testing implementations of phptailors/singleton-interface.
composer require --dev "phptailors/singleton-testing:^1.0"
composer require --dev "phpunit/phpunit"<?php
use PHPUnit\Framework\TestCase;
use Tailors\Testing\Lib\Singleton\AssertIsSingletonTrait;
final class MySingletonTest extends TestCase
{
use AssertIsSingletonTrait;
public function testMySingletonIsSingleton(): void
{
$this->assertIsSingleton(MySingleton::class);
}
}The following tests are performed by assertIsSingleton($class):
- Assert that the the provided string
$classis a class. - Assert that
$classhas private constructor. - Assert that
$classhas public static methodgetInstance(). - Assert that
$class::getInstance()is callable. - Assert that
$class::getInstance()returns an instance of$class. - Assert that
$class::getInstance()is idempotent. - Assert that
$classis not cloneable. - Assert that it throws Tailors\Lib\Singleton\SingletonException on unserialize().
The name of the getInstance() method may be customized, for example:
$this->assertIsSingleton(MySingleton::class, getInstance: "getSingleInstance")will use getSingleInstance instead of getInstance.