Skip to content

Commit

Permalink
test: added tests for Services class
Browse files Browse the repository at this point in the history
  • Loading branch information
thorsten committed Mar 29, 2024
1 parent bf9d1b5 commit aea3da7
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 5 deletions.
10 changes: 5 additions & 5 deletions phpmyfaq/src/phpMyFAQ/Search.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class Search
{
private ?int $categoryId = null;

private ?\phpMyFAQ\Category $category = null;
private ?Category $category = null;

private readonly string $table;

Expand All @@ -59,7 +59,7 @@ public function setCategoryId(int $categoryId): void
/**
* Getter for category.
*
* @return int
* @return int|null
*/
public function getCategoryId(): ?int
{
Expand Down Expand Up @@ -111,10 +111,10 @@ public function autoComplete(string $searchTerm): array
*
* @param string $searchTerm Text/Number (solution id)
* @param bool $allLanguages true to search over all languages
* @throws Exception
* @return mixed[]
* @return array
*@throws Exception
*/
public function searchDatabase(string $searchTerm, $allLanguages = true): array
public function searchDatabase(string $searchTerm, bool $allLanguages = true): array
{
$fdTable = Database::getTablePrefix() . 'faqdata AS fd';
$fcrTable = Database::getTablePrefix() . 'faqcategoryrelations';
Expand Down
59 changes: 59 additions & 0 deletions tests/phpMyFAQ/ServicesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

namespace phpMyFAQ;

use PHPUnit\Framework\TestCase;

class ServicesTest extends TestCase
{
public function testGetSuggestLink(): void
{
// Create a mock Configuration object
$configuration = $this->createMock(Configuration::class);
$configuration->method('getDefaultUrl')->willReturn('http://example.com/');

// Create a Services object
$services = new Services($configuration);
$services->setCategoryId(1);
$services->setFaqId(123);
$services->setLanguage('en');

// Test getSuggestLink method
$expected = 'http://example.com/index.php?action=send2friend&cat=1&id=123&artlang=en';
$this->assertEquals($expected, $services->getSuggestLink());
}

public function testGetPdfLink(): void
{
// Create a mock Configuration object
$configuration = $this->createMock(Configuration::class);
$configuration->method('getDefaultUrl')->willReturn('http://example.com/');

// Create a Services object
$services = new Services($configuration);
$services->setCategoryId(1);
$services->setFaqId(123);
$services->setLanguage('en');

// Test getPdfLink method
$expected = 'http://example.com/pdf.php?cat=1&id=123&artlang=en';
$this->assertEquals($expected, $services->getPdfLink());
}

public function testGetPdfApiLink(): void
{
// Create a mock Configuration object
$configuration = $this->createMock(Configuration::class);
$configuration->method('getDefaultUrl')->willReturn('http://example.com/');

// Create a Services object
$services = new Services($configuration);
$services->setCategoryId(1);
$services->setFaqId(123);
$services->setLanguage('en');

// Test getPdfApiLink method
$expected = 'http://example.com/pdf.php?cat=1&id=123&artlang=en';
$this->assertEquals($expected, $services->getPdfApiLink());
}
}

0 comments on commit aea3da7

Please sign in to comment.