From 7e8828a8c6f26eb31c3e9083524c567f14a4a522 Mon Sep 17 00:00:00 2001 From: Anton Karpov Date: Sat, 10 Mar 2018 06:10:31 +0200 Subject: [PATCH] Config: Refactor and Covered by tests --- src/NP/Common/Config.php | 37 +++++++++++---- tests/Mock/Common/MockConfig.php | 78 ++++++++++++++++++++++++++++++++ tests/NP/Common/ConfigTest.php | 40 ++++++++++++++++ 3 files changed, 145 insertions(+), 10 deletions(-) create mode 100644 tests/Mock/Common/MockConfig.php diff --git a/src/NP/Common/Config.php b/src/NP/Common/Config.php index e04b5be..19c8e09 100644 --- a/src/NP/Common/Config.php +++ b/src/NP/Common/Config.php @@ -125,24 +125,41 @@ public function setProperty($name, $value) /** * Get default HTTP driver. - * - * ToDo: Refactor; - * ToDo: Make test-friendly & testable at all; */ private function setDefaultDriver() { - try { + if ($this->isGuzzle()) { $this->driver = new GuzzleDriver(); - } catch (\Exception $exception) { - try { - $this->driver = new CurlDriver(); - } catch (\Exception $exception) { - $this->errors[] = new Error('There are no installed "Guzzle" library or "php_curl" extension!'); - } + } elseif ($this->isCurl()) { + $this->driver = new CurlDriver(); + } else { + $this->errors[] = new Error('There are no installed "Guzzle" library or "php_curl" extension!'); } } + /** + * Check Guzzle availability. + * + * @return bool + */ + public function isGuzzle(): bool + { + return class_exists(\GuzzleHttp\Client::class); + } + + + /** + * Check CURL availability. + * + * @return bool + */ + public function isCurl(): bool + { + return function_exists('curl_init'); + } + + /** * Get errors. * diff --git a/tests/Mock/Common/MockConfig.php b/tests/Mock/Common/MockConfig.php new file mode 100644 index 0000000..ae62e5b --- /dev/null +++ b/tests/Mock/Common/MockConfig.php @@ -0,0 +1,78 @@ + + * @license http://www.opensource.org/licenses/mit-license.php MIT + * @link https://github.com/awd-studio/novaposhta + */ + +declare(strict_types=1); // strict mode + + +namespace NP\Mock\Common; + +use NP\Common\Config; + + +/** + * Class MockConfig + * @package Mock\Common + */ +class MockConfig extends Config +{ + + /** + * @var array + */ + protected $ignoredDriver = []; + + + /** + * MockConfig constructor. + * @param $config + * @param array $ignoredDriver + */ + public function __construct($config, array $ignoredDriver) + { + $this->ignoredDriver = $ignoredDriver; + + parent::__construct($config); + } + + + /** + * @param string $needle + * @return bool + */ + protected function inArray(string $needle) + { + return in_array($needle, $this->ignoredDriver); + } + + + /** + * Check Guzzle availability. + * + * @return bool + */ + public function isGuzzle(): bool + { + return $this->inArray('GuzzleDriver') ? false : parent::isGuzzle(); + } + + + /** + * Check CURL availability. + * + * @return bool + */ + public function isCurl(): bool + { + return $this->inArray('CurlDriver') ? false : parent::isCurl(); + } + + +} diff --git a/tests/NP/Common/ConfigTest.php b/tests/NP/Common/ConfigTest.php index 930e29d..a08e037 100644 --- a/tests/NP/Common/ConfigTest.php +++ b/tests/NP/Common/ConfigTest.php @@ -11,6 +11,7 @@ namespace NP\Test\Common; +use NP\Mock\Common\MockConfig; use NP\Common\Config; use NP\Exception\Error; use NP\Http\DriverInterface; @@ -109,4 +110,43 @@ public function testGetLanguage() { $this->assertRegExp('/Uk|Ru/', $this->instance->getLanguage()); } + + + /** + * @covers \NP\Common\Config::isGuzzle + */ + public function testIsGuzzle() + { + $this->assertEquals(class_exists(\GuzzleHttp\Client::class), $this->instance->isGuzzle()); + } + + + /** + * @covers \NP\Common\Config::isCurl + */ + public function testIsCurl() + { + $this->assertEquals(function_exists('curl_init'), $this->instance->isCurl()); + } + + + /** + * @covers \NP\Common\Config::setUp + * @covers \NP\Common\Config::setDefaultDriver + * @covers \NP\Common\Config::getErrors + */ + public function testSetDefaultDriver() + { + $ignoredDriverCurl = ['CurlDriver']; + $config = new MockConfig($this->key, $ignoredDriverCurl); + $this->assertArrayNotHasKey(0, $config->getErrors()); + + $ignoredDriverGuzzle = ['GuzzleDriver']; + $config = new MockConfig($this->key, $ignoredDriverGuzzle); + $this->assertArrayNotHasKey(1, $config->getErrors()); + + $ignoredDriverBoth = ['GuzzleDriver', 'CurlDriver']; + $config = new MockConfig($this->key, $ignoredDriverBoth); + $this->assertArrayNotHasKey(1, $config->getErrors()); + } }