Last active
March 4, 2020 18:35
-
-
Save faizanakram99/a14e9189ccf4bf7bb87bb90fa4d5d944 to your computer and use it in GitHub Desktop.
? Unit test ?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Qbil\CommonBundle\Services; | |
use Qbil\CommonBundle\Exception\LoadingConfigurationFailedException; | |
interface ConfigurationAdapterInterface | |
{ | |
/** | |
* @throws LoadingConfigurationFailedException | |
*/ | |
public function getConfiguration(): array; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Qbil\LegacyBridgeBundle\Services; | |
interface LegacyConfigurationParserInterface | |
{ | |
public function isLoaded(): bool; | |
public function getSetting($path); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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]')); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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