Skip to content

Commit

Permalink
test: added more tests for Search class
Browse files Browse the repository at this point in the history
  • Loading branch information
thorsten committed Sep 29, 2024
1 parent 65b95ba commit 79d8ffd
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 16 deletions.
19 changes: 6 additions & 13 deletions phpmyfaq/src/phpMyFAQ/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down Expand Up @@ -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);
}

/**
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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;
Expand Down
76 changes: 73 additions & 3 deletions tests/phpMyFAQ/SearchTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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'])
Expand All @@ -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'])
Expand All @@ -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());
}
}

0 comments on commit 79d8ffd

Please sign in to comment.