Skip to content

Commit

Permalink
Merge branch 'release/1.0.2'
Browse files Browse the repository at this point in the history
  • Loading branch information
awd-studio committed Mar 10, 2018
2 parents df43a00 + 7e8828a commit 052450d
Show file tree
Hide file tree
Showing 3 changed files with 145 additions and 10 deletions.
37 changes: 27 additions & 10 deletions src/NP/Common/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down
78 changes: 78 additions & 0 deletions tests/Mock/Common/MockConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<?php

/**
* @file
* This file is part of NovaPoshta PHP library.
*
* @author Anton Karpov <[email protected]>
* @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();
}


}
40 changes: 40 additions & 0 deletions tests/NP/Common/ConfigTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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());
}
}

0 comments on commit 052450d

Please sign in to comment.