Skip to content

Commit

Permalink
Merge pull request #278 from lmc-eu/feature/singleton-php8
Browse files Browse the repository at this point in the history
Replace singleton implementation to support PHP 8
  • Loading branch information
OndraM committed Mar 10, 2021
2 parents 1a7c42e + 3b30125 commit 4b71f5c
Show file tree
Hide file tree
Showing 5 changed files with 53 additions and 9 deletions.
1 change: 0 additions & 1 deletion composer.json
Expand Up @@ -30,7 +30,6 @@
"beberlei/assert": "^3.0",
"clue/graph": "^0.9.2",
"doctrine/inflector": "^2.0.3",
"florianwolters/component-util-singleton": "^0.3.2",
"graphp/algorithms": "^0.8.2",
"hanneskod/classtools": "^1.2",
"ondram/ci-detector": "^4.0",
Expand Down
7 changes: 5 additions & 2 deletions src-tests/ConfigProviderHelper.php
Expand Up @@ -10,9 +10,12 @@ class ConfigProviderHelper extends ConfigProvider
{
protected $config = [];

public function __construct(array $config)
public static function createWithConfig(array $config): self
{
$this->config = $config;
$configHelper = new static();
$configHelper->config = $config;

return $configHelper;
}

public function __get(string $name)
Expand Down
15 changes: 15 additions & 0 deletions src-tests/ConfigProviderTest.php
Expand Up @@ -138,4 +138,19 @@ public function testShouldFailIfSettingCustomConfigurationOptionsAfterFirstInsta
// This should fail, as the Config instance was already created
$config->setCustomConfigurationOptions(['CUSTOM_OPTION']);
}

public function testShouldAlwaysGetTheSameSingletonInstance(): void
{
$firstInstance = ConfigProvider::getInstance();
$secondInstance = ConfigProvider::getInstance();

$this->assertSame($firstInstance, $secondInstance);
}

public function testShouldNotAllowUnserializing(): void
{
$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('ConfigProvider is a singleton and cannot be unserialized.');
unserialize(serialize(ConfigProvider::getInstance()));
}
}
6 changes: 3 additions & 3 deletions src-tests/Selenium/CapabilitiesResolverTest.php
Expand Up @@ -27,7 +27,7 @@ public function testShouldResolveBasicDesiredCapabilities(string $browser, calla
/** @var AbstractTestCase $test */
$test = $this->getMockForAbstractClass(AbstractTestCase::class, ['name'], 'FooBarTest');

$configMock = new ConfigProviderHelper(['browserName' => $browser]);
$configMock = ConfigProviderHelper::createWithConfig(['browserName' => $browser]);

$resolver = new CapabilitiesResolver($configMock);
$resolver->setCiDetector($this->createConfiguredMock(CiDetector::class, ['isCiDetected' => false]));
Expand Down Expand Up @@ -71,7 +71,7 @@ public function testShouldResolveExtraDesiredCapabilitiesOnCiServer(): void
/** @var AbstractTestCase $test */
$test = $this->getMockForAbstractClass(AbstractTestCase::class, ['name'], 'FooBarTest');

$configMock = new ConfigProviderHelper(
$configMock = ConfigProviderHelper::createWithConfig(
[
'browserName' => WebDriverBrowserType::FIREFOX,
'env' => 'staging',
Expand Down Expand Up @@ -113,7 +113,7 @@ public function testShouldCallCustomCapabilitiesResolverIfDefined(): void
/** @var AbstractTestCase $test */
$test = $this->getMockForAbstractClass(AbstractTestCase::class, ['name'], 'FooBarTest');

$configMock = new ConfigProviderHelper(
$configMock = ConfigProviderHelper::createWithConfig(
[
'browserName' => 'firefox',
'capabilitiesResolver' => CapabilitiesResolverFixture::class,
Expand Down
33 changes: 30 additions & 3 deletions src/ConfigProvider.php
Expand Up @@ -4,13 +4,11 @@

use Doctrine\Inflector\InflectorFactory;
use Doctrine\Inflector\Language;
use FlorianWolters\Component\Util\Singleton\SingletonTrait;

/**
* Provide access to global configuration (from within the tests).
* The configuration is immutable - ie. cannot be altered after it is used for the first time.
*
* @method static ConfigProvider getInstance()
* @property-read string $browserName
* @property-read string $env
* @property-read string $serverUrl
Expand All @@ -21,13 +19,35 @@
*/
class ConfigProvider
{
use SingletonTrait;
/** @var ConfigProvider */
private static $instance;

/** @var array Configuration options and theirs values */
private $config;
/** @var array Array of custom configuration options that should be added to the default ones */
private $customConfigurationOptions = [];

final protected function __construct()
{
}

public static function getInstance(): self
{
if (self::$instance === null) {
self::$instance = new static();
}

return self::$instance;
}

/**
* Prevent unserializing the singleton
*/
public function __wakeup(): void
{
throw new \RuntimeException('ConfigProvider is a singleton and cannot be unserialized.');
}

public function __get(string $name)
{
$this->initialize();
Expand Down Expand Up @@ -133,4 +153,11 @@ private function retrieveConfigurationValues(array $options): array

return $outputValues;
}

/**
* Prevent cloning of the singleton
*/
private function __clone()
{
}
}

0 comments on commit 4b71f5c

Please sign in to comment.