diff --git a/src/Meritoo/LimeSurvey/ApiClient/Configuration/ConnectionConfiguration.php b/src/Meritoo/LimeSurvey/ApiClient/Configuration/ConnectionConfiguration.php index f1e35f6..0faa62e 100644 --- a/src/Meritoo/LimeSurvey/ApiClient/Configuration/ConnectionConfiguration.php +++ b/src/Meritoo/LimeSurvey/ApiClient/Configuration/ConnectionConfiguration.php @@ -80,12 +80,12 @@ class ConnectionConfiguration */ public function __construct($baseUrl, $username, $password, $debugMode = false, $verifySslCertificate = true) { - $this - ->setBaseUrl($baseUrl) - ->setUsername($username) - ->setPassword($password) - ->setDebugMode($debugMode) - ->setVerifySslCertificate($verifySslCertificate); + $this->setBaseUrl($baseUrl); + + $this->username = $username; + $this->password = $password; + $this->debugMode = $debugMode; + $this->verifySslCertificate = $verifySslCertificate; } /** @@ -98,29 +98,6 @@ class ConnectionConfiguration 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 * @@ -154,19 +131,6 @@ class ConnectionConfiguration 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 * @@ -177,19 +141,6 @@ class ConnectionConfiguration 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 * @@ -200,19 +151,6 @@ class ConnectionConfiguration 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 * @@ -223,26 +161,6 @@ class ConnectionConfiguration 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. * 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); } + + /** + * 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; + } } diff --git a/tests/Meritoo/LimeSurvey/Test/ApiClient/Client/ClientTest.php b/tests/Meritoo/LimeSurvey/Test/ApiClient/Client/ClientTest.php index fa362ea..0e69174 100644 --- a/tests/Meritoo/LimeSurvey/Test/ApiClient/Client/ClientTest.php +++ b/tests/Meritoo/LimeSurvey/Test/ApiClient/Client/ClientTest.php @@ -68,9 +68,15 @@ class ClientTest extends BaseTestCase ->method('runMethod') ->willReturn([]); - $this->configuration->setDebugMode($debugMode); - $client = new Client($this->configuration, $rpcClientManager, $sessionManager); + $configuration = new ConnectionConfiguration( + $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)); } diff --git a/tests/Meritoo/LimeSurvey/Test/ApiClient/Configuration/ConnectionConfigurationTest.php b/tests/Meritoo/LimeSurvey/Test/ApiClient/Configuration/ConnectionConfigurationTest.php index 455738b..3f1883e 100644 --- a/tests/Meritoo/LimeSurvey/Test/ApiClient/Configuration/ConnectionConfigurationTest.php +++ b/tests/Meritoo/LimeSurvey/Test/ApiClient/Configuration/ConnectionConfigurationTest.php @@ -76,73 +76,13 @@ class ConnectionConfigurationTest extends BaseTestCase static::assertFalse($this->configurationAnother->isVerifySslCertificateOn()); } - public function testSetBaseUrl() + public function testGetRemoteControlUrl() { - $this->configurationWithDefaults->setBaseUrl('http://lorem.ipsum'); - static::assertEquals('http://lorem.ipsum', $this->configurationWithDefaults->getBaseUrl()); + $this->configurationWithDefaults->setRemoteControlUrl('lorem/ipsum'); + static::assertEquals('lorem/ipsum', $this->configurationWithDefaults->getRemoteControlUrl()); - $this->configurationWithDefaults->setBaseUrl('http://lorem.ipsum/'); - static::assertEquals('http://lorem.ipsum', $this->configurationWithDefaults->getBaseUrl()); - } - - 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()); + $this->configurationAnother->setRemoteControlUrl('dolor/sit'); + static::assertEquals('dolor/sit', $this->configurationAnother->getRemoteControlUrl()); } public function testGetFullUrl() @@ -195,6 +135,6 @@ class ConnectionConfigurationTest extends BaseTestCase parent::setUp(); $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); } }