From 79d8ffd8a963e7c6d751a2bf87c299d11c2f2247 Mon Sep 17 00:00:00 2001 From: Thorsten Rinne Date: Sun, 29 Sep 2024 17:05:10 +0200 Subject: [PATCH] test: added more tests for Search class --- phpmyfaq/src/phpMyFAQ/Search.php | 19 +++----- tests/phpMyFAQ/SearchTest.php | 76 ++++++++++++++++++++++++++++++-- 2 files changed, 79 insertions(+), 16 deletions(-) diff --git a/phpmyfaq/src/phpMyFAQ/Search.php b/phpmyfaq/src/phpMyFAQ/Search.php index 5029cf0cff..da23212383 100755 --- a/phpmyfaq/src/phpMyFAQ/Search.php +++ b/phpmyfaq/src/phpMyFAQ/Search.php @@ -90,7 +90,7 @@ public function search(string $searchTerm, bool $allLanguages = true): array * * @param string $searchTerm Text to auto complete * @throws Exception - * @return mixed[] + * @return array */ public function autoComplete(string $searchTerm): array { @@ -232,16 +232,12 @@ public function logSearchTerm(string $searchTerm): void public function deleteSearchTermById(int $searchTermId): bool { $query = sprintf( - " - DELETE FROM - %s - WHERE - id = '%d'", + "DELETE FROM %s WHERE id = '%d'", $this->table, $searchTermId ); - return $this->configuration->getDb()->query($query); + return (bool) $this->configuration->getDb()->query($query); } /** @@ -251,7 +247,7 @@ public function deleteAllSearchTerms(): bool { $query = sprintf('DELETE FROM %s', $this->table); - return $this->configuration->getDb()->query($query); + return (bool) $this->configuration->getDb()->query($query); } /** @@ -305,18 +301,15 @@ public function getMostPopularSearches(int $numResults = 7, bool $withLang = fal public function getSearchesCount(): int { $sql = sprintf( - 'SELECT COUNT(1) AS count FROM %s', + 'SELECT COUNT(*) AS count FROM %s', $this->table ); $result = $this->configuration->getDb()->query($sql); - return (int)$this->configuration->getDb()->fetchObject($result)->count; + return (int) $this->configuration->getDb()->fetchObject($result)->count; } - /** - * Sets the Entity object. - */ public function setCategory(Category $category): void { $this->category = $category; diff --git a/tests/phpMyFAQ/SearchTest.php b/tests/phpMyFAQ/SearchTest.php index cb67e67fae..7d43899cf4 100644 --- a/tests/phpMyFAQ/SearchTest.php +++ b/tests/phpMyFAQ/SearchTest.php @@ -3,19 +3,42 @@ namespace phpMyFAQ; use Exception; +use phpMyFAQ\Database\DatabaseDriver; +use phpMyFAQ\Database\Sqlite3; +use phpMyFAQ\Plugin\PluginException; use PHPUnit\Framework\TestCase; +use stdClass; class SearchTest extends TestCase { private Configuration $configuration; private Search $search; + private Sqlite3 $dbHandle; + /** + * @throws PluginException|Core\Exception + */ protected function setUp(): void { - $this->configuration = $this->createMock(Configuration::class); + parent::setUp(); + + Translation::create() + ->setLanguagesDir(PMF_TRANSLATION_DIR) + ->setDefaultLanguage('en') + ->setCurrentLanguage('en') + ->setMultiByteLanguage(); + + $this->dbHandle = new Sqlite3(); + $this->dbHandle->connect(PMF_TEST_DIR . '/test.db', '', ''); + $this->configuration = new Configuration($this->dbHandle); $this->search = new Search($this->configuration); } + protected function tearDown(): void + { + $this->search->deleteAllSearchTerms(); + } + public function testSetCategoryId(): void { $this->search->setCategoryId(1); @@ -33,7 +56,6 @@ public function testGetCategoryId(): void */ public function testSearchWithNumericTerm(): void { - $this->configuration->method('get')->willReturn(false); $this->search = $this->getMockBuilder(Search::class) ->setConstructorArgs([$this->configuration]) ->onlyMethods(['searchDatabase']) @@ -52,7 +74,6 @@ public function testSearchWithNumericTerm(): void */ public function testSearchWithNonNumericTerm(): void { - $this->configuration->method('get')->willReturn(false); $this->search = $this->getMockBuilder(Search::class) ->setConstructorArgs([$this->configuration]) ->onlyMethods(['searchDatabase']) @@ -65,4 +86,53 @@ public function testSearchWithNonNumericTerm(): void $this->assertEquals([], $this->search->search('test')); } + + public function testDeleteSearchTermById(): void + { + $this->dbHandle->query("INSERT INTO faqsearches VALUES (1, 'en', 'foo', ''), (2, 'en', 'bar', '')"); + + $result = $this->search->deleteSearchTermById(1); + + $this->assertTrue($result); + $this->assertEquals(1, $this->search->getSearchesCount()); + } + + public function testDeleteAllSearchTermsSuccess(): void + { + $this->dbHandle->query("INSERT INTO faqsearches VALUES (1, 'en', 'foo', ''), (2, 'en', 'bar', '')"); + + $this->assertTrue($this->search->deleteAllSearchTerms()); + } + + public function testGetMostPopularSearches(): void + { + $this->dbHandle->query( + "INSERT INTO faqsearches VALUES (1, 'en', 'foo', ''), (2, 'en', 'bar', ''), (3, 'en', 'foo', '')" + ); + + $actualSearches = $this->search->getMostPopularSearches(2); + + $this->assertEquals(2, count($actualSearches)); + $this->assertEquals('foo', $actualSearches[0]['searchterm']); + $this->assertEquals(2, $actualSearches[0]['number']); + } + + public function testGetSearchesCount(): void + { + $this->dbHandle->query("INSERT INTO faqsearches VALUES (1, 'en', 'foo', ''), (2, 'en', 'bar', '')"); + + $actualCount = $this->search->getSearchesCount(); + + $this->assertEquals(2, $actualCount); + } + + public function testSetAndGetCategory(): void + { + $categoryMock = $this->getMockBuilder(Category::class) + ->disableOriginalConstructor() + ->getMock(); + $this->search->setCategory($categoryMock); + + $this->assertEquals($categoryMock, $this->search->getCategory()); + } }