From 54bd0ca1142823959d01dc155bf8dc1c81d2c279 Mon Sep 17 00:00:00 2001 From: Meritoo Date: Fri, 29 Sep 2017 23:55:56 +0200 Subject: [PATCH] SurveyService - allow to verify if survey with given ID exists and if is active --- src/Service/SurveyService.php | 8 ++-- tests/Service/SurveyServiceTest.php | 68 ++++++++++++++++++++++------- 2 files changed, 58 insertions(+), 18 deletions(-) diff --git a/src/Service/SurveyService.php b/src/Service/SurveyService.php index d1b6cd4..4fdea6c 100644 --- a/src/Service/SurveyService.php +++ b/src/Service/SurveyService.php @@ -106,12 +106,14 @@ class SurveyService /** * Returns information if survey with given ID exists * - * @param int $surveyId ID of survey to verify + * @param int $surveyId ID of survey to verify + * @param bool $shouldBeActive (optional) If is set to true, survey should be active. If it's not, it shouldn't + * be returned, even if exists. Otherwise - it doesn't matter. * @return bool */ - public function isExistingSurvey($surveyId) + public function isExistingSurvey($surveyId, $shouldBeActive = false) { - $allSurveys = $this->getAllSurveys(); + $allSurveys = $this->getAllSurveys($shouldBeActive); /* * No surveys? diff --git a/tests/Service/SurveyServiceTest.php b/tests/Service/SurveyServiceTest.php index 6176bc8..2f62b2a 100644 --- a/tests/Service/SurveyServiceTest.php +++ b/tests/Service/SurveyServiceTest.php @@ -93,18 +93,6 @@ class SurveyServiceTest extends BaseTestCase } public function testGetAllSurveys() - { - $rpcClientManager = $this->getJsonRpcClientManager(1); - $sessionManager = $this->getSessionManager(); - - $this->createServiceWithoutSurveys($rpcClientManager, $sessionManager); - $this->createServiceWithSurveys($rpcClientManager, $sessionManager); - - static::assertCount(0, $this->serviceWithoutSurveys->getAllSurveys()); - static::assertCount(2, $this->serviceWithSurveys->getAllSurveys()); - } - - public function testIsExistingSurvey() { $rpcClientManager = $this->getJsonRpcClientManager(2); $sessionManager = $this->getSessionManager(); @@ -112,12 +100,56 @@ class SurveyServiceTest extends BaseTestCase $this->createServiceWithoutSurveys($rpcClientManager, $sessionManager); $this->createServiceWithSurveys($rpcClientManager, $sessionManager); - static::assertFalse($this->serviceWithoutSurveys->isExistingSurvey(1)); - static::assertFalse($this->serviceWithoutSurveys->isExistingSurvey(2)); + /* + * If there are no surveys, count of all or active only surveys is the same + */ + static::assertCount(0, $this->serviceWithoutSurveys->getAllSurveys()); + static::assertCount(0, $this->serviceWithoutSurveys->getAllSurveys(true)); + /* + * If there are surveys, here we've got difference between count of all or active only surveys + */ + static::assertCount(3, $this->serviceWithSurveys->getAllSurveys()); + static::assertCount(2, $this->serviceWithSurveys->getAllSurveys(true)); + } + + public function testIsExistingSurvey() + { + $rpcClientManager = $this->getJsonRpcClientManager(4); + $sessionManager = $this->getSessionManager(); + + $this->createServiceWithoutSurveys($rpcClientManager, $sessionManager); + $this->createServiceWithSurveys($rpcClientManager, $sessionManager); + + /* + * If there are no surveys, verification of existence any survey always return false + */ + static::assertFalse($this->serviceWithoutSurveys->isExistingSurvey(1)); + static::assertFalse($this->serviceWithoutSurveys->isExistingSurvey(1, true)); + + static::assertFalse($this->serviceWithoutSurveys->isExistingSurvey(2)); + static::assertFalse($this->serviceWithoutSurveys->isExistingSurvey(2, true)); + + /* + * If there are surveys, verification of existence active survey always return true + */ static::assertTrue($this->serviceWithSurveys->isExistingSurvey(1)); + static::assertTrue($this->serviceWithSurveys->isExistingSurvey(1, true)); + static::assertTrue($this->serviceWithSurveys->isExistingSurvey(2)); - static::assertFalse($this->serviceWithSurveys->isExistingSurvey(3)); + static::assertTrue($this->serviceWithSurveys->isExistingSurvey(2, true)); + + /* + * If there are surveys, verification of existence of non-active survey shows difference + */ + static::assertTrue($this->serviceWithSurveys->isExistingSurvey(3)); + static::assertFalse($this->serviceWithSurveys->isExistingSurvey(3, true)); + + /* + * If there are surveys, verification of existence non-existing survey always return false + */ + static::assertFalse($this->serviceWithSurveys->isExistingSurvey(4)); + static::assertFalse($this->serviceWithSurveys->isExistingSurvey(4, true)); } /** @@ -207,10 +239,16 @@ class SurveyServiceTest extends BaseTestCase new Survey([ 'sid' => 1, 'surveyls_title' => 'Test', + 'active' => 'Y', ]), new Survey([ 'sid' => 2, 'surveyls_title' => 'Another Test', + 'active' => 'Y', + ]), + new Survey([ + 'sid' => 3, + 'surveyls_title' => 'I am inactive', ]), ]);