Configuration of connecting - do not allow to change (remove setters)

This commit is contained in:
Meritoo
2017-09-28 20:13:54 +02:00
parent 5061f5a295
commit a6866d994c
3 changed files with 43 additions and 156 deletions

View File

@@ -80,12 +80,12 @@ class ConnectionConfiguration
*/ */
public function __construct($baseUrl, $username, $password, $debugMode = false, $verifySslCertificate = true) public function __construct($baseUrl, $username, $password, $debugMode = false, $verifySslCertificate = true)
{ {
$this $this->setBaseUrl($baseUrl);
->setBaseUrl($baseUrl)
->setUsername($username) $this->username = $username;
->setPassword($password) $this->password = $password;
->setDebugMode($debugMode) $this->debugMode = $debugMode;
->setVerifySslCertificate($verifySslCertificate); $this->verifySslCertificate = $verifySslCertificate;
} }
/** /**
@@ -98,29 +98,6 @@ class ConnectionConfiguration
return $this->baseUrl; return $this->baseUrl;
} }
/**
* Sets the base url, protocol & domain
*
* @param string $baseUrl The base url, protocol & domain
* @return $this
*
* @throws InvalidUrlException
*/
public function setBaseUrl($baseUrl)
{
if (!Regex::isValidUrl($baseUrl)) {
throw new InvalidUrlException($baseUrl);
}
if (Regex::endsWith($baseUrl, '/')) {
$baseUrl = substr($baseUrl, 0, strlen($baseUrl) - 1);
}
$this->baseUrl = $baseUrl;
return $this;
}
/** /**
* Returns the url of the LimeSurvey's remote control * Returns the url of the LimeSurvey's remote control
* *
@@ -154,19 +131,6 @@ class ConnectionConfiguration
return $this->username; return $this->username;
} }
/**
* Sets the name of user used to authenticate to LimeSurvey
*
* @param string $username The name of user used to authenticate to LimeSurvey
* @return $this
*/
public function setUsername($username)
{
$this->username = $username;
return $this;
}
/** /**
* Returns the password used to authenticate to LimeSurvey * Returns the password used to authenticate to LimeSurvey
* *
@@ -177,19 +141,6 @@ class ConnectionConfiguration
return $this->password; return $this->password;
} }
/**
* Sets the password used to authenticate to LimeSurvey
*
* @param string $password The password used to authenticate to LimeSurvey
* @return $this
*/
public function setPassword($password)
{
$this->password = $password;
return $this;
}
/** /**
* Returns information if the "debug" mode is turned on * Returns information if the "debug" mode is turned on
* *
@@ -200,19 +151,6 @@ class ConnectionConfiguration
return $this->debugMode; return $this->debugMode;
} }
/**
* Sets information if the "debug" mode is turned on
*
* @param bool $debugMode (optional) If is set to true, the "debug" mode is turned on. Otherwise - turned off.
* @return $this
*/
public function setDebugMode($debugMode = false)
{
$this->debugMode = $debugMode;
return $this;
}
/** /**
* Returns information if the SSL certificate verification is turned on * Returns information if the SSL certificate verification is turned on
* *
@@ -223,26 +161,6 @@ class ConnectionConfiguration
return $this->verifySslCertificate; return $this->verifySslCertificate;
} }
/**
* Sets information if the SSL certificate verification is turned on
*
* @param bool $verifySslCertificate (optional) If is set to true, the SSL certificate verification is turned on.
* Otherwise - turned off.
* @return $this
*/
public function setVerifySslCertificate($verifySslCertificate = true)
{
$this->verifySslCertificate = $verifySslCertificate;
return $this;
}
/*
* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
* Additional / extra methods (neither getters, nor setters)
* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
*/
/** /**
* Returns full url of the LimeSurvey's API. * Returns full url of the LimeSurvey's API.
* It's a base url with part related to remote control. * It's a base url with part related to remote control.
@@ -253,4 +171,27 @@ class ConnectionConfiguration
{ {
return sprintf('%s/%s', $this->baseUrl, $this->remoteControlUrl); return sprintf('%s/%s', $this->baseUrl, $this->remoteControlUrl);
} }
/**
* Sets the base url, protocol & domain
*
* @param string $baseUrl The base url, protocol & domain
* @return $this
*
* @throws InvalidUrlException
*/
private function setBaseUrl($baseUrl)
{
if (!Regex::isValidUrl($baseUrl)) {
throw new InvalidUrlException($baseUrl);
}
if (Regex::endsWith($baseUrl, '/')) {
$baseUrl = substr($baseUrl, 0, strlen($baseUrl) - 1);
}
$this->baseUrl = $baseUrl;
return $this;
}
} }

View File

@@ -68,9 +68,15 @@ class ClientTest extends BaseTestCase
->method('runMethod') ->method('runMethod')
->willReturn([]); ->willReturn([]);
$this->configuration->setDebugMode($debugMode); $configuration = new ConnectionConfiguration(
$client = new Client($this->configuration, $rpcClientManager, $sessionManager); $this->configuration->getBaseUrl(),
$this->configuration->getUsername(),
$this->configuration->getPassword(),
$debugMode,
$this->configuration->isVerifySslCertificateOn()
);
$client = new Client($configuration, $rpcClientManager, $sessionManager);
static::assertInstanceOf(Result::class, $client->run($method, $arguments)); static::assertInstanceOf(Result::class, $client->run($method, $arguments));
} }

View File

@@ -76,73 +76,13 @@ class ConnectionConfigurationTest extends BaseTestCase
static::assertFalse($this->configurationAnother->isVerifySslCertificateOn()); static::assertFalse($this->configurationAnother->isVerifySslCertificateOn());
} }
public function testSetBaseUrl() public function testGetRemoteControlUrl()
{ {
$this->configurationWithDefaults->setBaseUrl('http://lorem.ipsum'); $this->configurationWithDefaults->setRemoteControlUrl('lorem/ipsum');
static::assertEquals('http://lorem.ipsum', $this->configurationWithDefaults->getBaseUrl()); static::assertEquals('lorem/ipsum', $this->configurationWithDefaults->getRemoteControlUrl());
$this->configurationWithDefaults->setBaseUrl('http://lorem.ipsum/'); $this->configurationAnother->setRemoteControlUrl('dolor/sit');
static::assertEquals('http://lorem.ipsum', $this->configurationWithDefaults->getBaseUrl()); static::assertEquals('dolor/sit', $this->configurationAnother->getRemoteControlUrl());
}
public function testSetRemoteControlUrl()
{
$this->configurationWithDefaults->setRemoteControlUrl('/lorem/ipsum');
static::assertEquals('/lorem/ipsum', $this->configurationWithDefaults->getRemoteControlUrl());
}
public function testSetUsername()
{
$this->configurationWithDefaults->setUsername('lorem');
static::assertEquals('lorem', $this->configurationWithDefaults->getUsername());
}
public function testSetPassword()
{
$this->configurationWithDefaults->setPassword('ipsum');
static::assertEquals('ipsum', $this->configurationWithDefaults->getPassword());
}
public function testSetDebugMode()
{
$this->configurationWithDefaults->setDebugMode();
$this->configurationAnother->setDebugMode();
static::assertFalse($this->configurationWithDefaults->isDebugModeOn());
static::assertFalse($this->configurationAnother->isDebugModeOn());
$this->configurationWithDefaults->setDebugMode(false);
$this->configurationAnother->setDebugMode(false);
static::assertFalse($this->configurationWithDefaults->isDebugModeOn());
static::assertFalse($this->configurationAnother->isDebugModeOn());
$this->configurationWithDefaults->setDebugMode(true);
$this->configurationAnother->setDebugMode(true);
static::assertTrue($this->configurationWithDefaults->isDebugModeOn());
static::assertTrue($this->configurationAnother->isDebugModeOn());
}
public function testSetVerifySslCertificate()
{
$this->configurationWithDefaults->setVerifySslCertificate();
$this->configurationAnother->setVerifySslCertificate();
static::assertTrue($this->configurationWithDefaults->isVerifySslCertificateOn());
static::assertTrue($this->configurationAnother->isVerifySslCertificateOn());
$this->configurationWithDefaults->setVerifySslCertificate(false);
$this->configurationAnother->setVerifySslCertificate(false);
static::assertFalse($this->configurationWithDefaults->isVerifySslCertificateOn());
static::assertFalse($this->configurationAnother->isVerifySslCertificateOn());
$this->configurationWithDefaults->setVerifySslCertificate(true);
$this->configurationAnother->setVerifySslCertificate(true);
static::assertTrue($this->configurationWithDefaults->isVerifySslCertificateOn());
static::assertTrue($this->configurationAnother->isVerifySslCertificateOn());
} }
public function testGetFullUrl() public function testGetFullUrl()
@@ -195,6 +135,6 @@ class ConnectionConfigurationTest extends BaseTestCase
parent::setUp(); parent::setUp();
$this->configurationWithDefaults = new ConnectionConfiguration('http://test.com', 'test1', 'test2'); $this->configurationWithDefaults = new ConnectionConfiguration('http://test.com', 'test1', 'test2');
$this->configurationAnother = new ConnectionConfiguration('http://lets-test.com', 'test11', 'test22', true, false); $this->configurationAnother = new ConnectionConfiguration('http://lets-test.com/', 'test11', 'test22', true, false);
} }
} }