Skip to content

Instantly share code, notes, and snippets.

@faizanakram99
Last active March 4, 2020 18:35
Show Gist options
  • Save faizanakram99/a14e9189ccf4bf7bb87bb90fa4d5d944 to your computer and use it in GitHub Desktop.
Save faizanakram99/a14e9189ccf4bf7bb87bb90fa4d5d944 to your computer and use it in GitHub Desktop.
? Unit test ?
<?php
namespace Qbil\CommonBundle\Services;
use Qbil\CommonBundle\Exception\LoadingConfigurationFailedException;
interface ConfigurationAdapterInterface
{
/**
* @throws LoadingConfigurationFailedException
*/
public function getConfiguration(): array;
}
<?php
namespace Qbil\LegacyBridgeBundle\Services;
use Qbil\CommonBundle\Services\ConfigurationAdapterInterface;
use Symfony\Component\PropertyAccess\PropertyAccess;
class LegacyConfigurationParser implements LegacyConfigurationParserInterface
{
public static $configuration;
public function __construct(ConfigurationAdapterInterface $configurationAdapter)
{
self::$configuration = $configurationAdapter->getConfiguration();
}
public function isLoaded(): bool
{
return null !== self::$configuration;
}
public function getSetting($path)
{
$accessor = PropertyAccess::createPropertyAccessor();
try {
$setting = $accessor->getValue(self::$configuration, $path);
} catch (\Exception $e) {
$setting = null;
}
return $setting;
}
}
<?php
namespace Qbil\LegacyBridgeBundle\Services;
interface LegacyConfigurationParserInterface
{
public function isLoaded(): bool;
public function getSetting($path);
}
<?php
namespace Qbil\Tests\LegacyBridgeBundle\Services;
use PHPUnit\Framework\TestCase;
use Qbil\CommonBundle\Services\ConfigurationAdapterInterface;
use Qbil\LegacyBridgeBundle\Services\LegacyConfigurationParser;
class LegacyConfigurationParserTest extends TestCase
{
private $legacyConfigParser;
protected function setUp(): void
{
$configurationAdapter = $this->createMock(ConfigurationAdapterInterface::class);
$configurationAdapter
->expects($this->once())
->method('getConfiguration')
->willReturn(['foo' => 'bar']);
$this->legacyConfigParser = new LegacyConfigurationParser($configurationAdapter);
}
public function testIsLoaded()
{
$this->assertTrue($this->legacyConfigParser->isLoaded());
}
public function testGetSetting()
{
$this->assertSame('bar', $this->legacyConfigParser->getSetting('[foo]'));
$this->assertNull($this->legacyConfigParser->getSetting('[baz]'));
}
}
@faizanakram99
Copy link
Author

PS: https://gist.github.com/faizanakram99/a14e9189ccf4bf7bb87bb90fa4d5d944#file-legacyconfigurationparser-php-L14 is for backward compatibility, a lot of legacy code depends upon global $configuration variable

it can be still improved though .. well lets leave as it is for now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment