Skip to content

Commit

Permalink
test: added new tests for Api and Report classes
Browse files Browse the repository at this point in the history
  • Loading branch information
thorsten committed Mar 17, 2024
1 parent 5239c64 commit e46decd
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 11 deletions.
20 changes: 11 additions & 9 deletions phpmyfaq/src/phpMyFAQ/Administration/Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
namespace phpMyFAQ\Administration;

use Exception;
use JsonException;
use phpMyFAQ\Configuration;
use phpMyFAQ\Core;
use phpMyFAQ\System;
Expand All @@ -43,24 +42,22 @@ class Api

private ?string $remoteHashes = null;

private readonly HttpClientInterface $httpClient;
private HttpClientInterface $httpClient;

/**
* Api constructor.
*/
public function __construct(private readonly Configuration $configuration)
{
$this->httpClient = HttpClient::create([
'max_redirects' => 2,
]);
$this->setHttpClient(HttpClient::create(['max_redirects' => 2]));
}

/**
* Returns the installed, the current available and the next version
* as an array.
*
* @return string[]
* @throws Core\Exception|DecodingExceptionInterface|TransportExceptionInterface
* @throws Exception|DecodingExceptionInterface|TransportExceptionInterface
*/
public function getVersions(): array
{
Expand Down Expand Up @@ -99,7 +96,7 @@ public function getVersions(): array
/**
* Returns true, if an installed version can be verified. Otherwise, false.
*
* @throws Core\Exception|TransportExceptionInterface|\JsonException
* @throws Exception|TransportExceptionInterface|\JsonException
*/
public function isVerified(): bool
{
Expand All @@ -119,15 +116,15 @@ public function isVerified(): bool
ServerExceptionInterface |
TransportExceptionInterface $e
) {
throw new Core\Exception('phpMyFAQ Verification API is not available: ' . $e->getMessage());
throw new Exception('phpMyFAQ Verification API is not available: ' . $e->getMessage());
}

return false;
}

/**
* @return string[]
* @throws JsonException
* @throws \JsonException
* @throws Exception
*/
public function getVerificationIssues(): array
Expand All @@ -138,4 +135,9 @@ public function getVerificationIssues(): array
json_decode((string) $this->remoteHashes, true, 512, JSON_THROW_ON_ERROR)
);
}

public function setHttpClient(HttpClientInterface $httpClient): void
{
$this->httpClient = $httpClient;
}
}
4 changes: 2 additions & 2 deletions phpmyfaq/src/phpMyFAQ/Administration/Report.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,12 @@
*
* @package phpMyFAQ
*/
class Report
readonly class Report
{
/**
* Constructor.
*/
public function __construct(private readonly Configuration $configuration)
public function __construct(private Configuration $configuration)
{
}

Expand Down
100 changes: 100 additions & 0 deletions tests/phpMyFAQ/Administration/ApiTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
<?php

namespace phpMyFAQ\Administration;

use phpMyFAQ\Configuration;
use PHPUnit\Framework\MockObject\Exception;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpClient\Exception\TransportException;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Contracts\HttpClient\Exception\DecodingExceptionInterface;
use Symfony\Contracts\HttpClient\Exception\TransportExceptionInterface;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;

class ApiTest extends TestCase
{
private Api $api;
private Configuration $configuration;
private HttpClientInterface $httpClient;

protected function setUp(): void
{
$this->configuration = $this->createMock(Configuration::class);
$this->httpClient = $this->createMock(HttpClientInterface::class);

$this->api = new Api($this->configuration);
$this->api->setHttpClient($this->httpClient);
}

/**
* @throws TransportExceptionInterface
* @throws Exception
* @throws DecodingExceptionInterface
* @throws \phpMyFAQ\Core\Exception
*/
public function testGetVersionsSuccess(): void
{
$versions = [
'stable' => '5.0.0',
'development' => '5.1.0',
'nightly' => '5.2.0',
];

$response = $this->createMock(ResponseInterface::class);
$response->method('getStatusCode')->willReturn(Response::HTTP_OK);
$response->method('toArray')->willReturn($versions);

$this->httpClient->expects($this->once())
->method('request')
->with('GET', 'https://api.phpmyfaq.de/versions')
->willReturn($response);

$this->configuration->method('getVersion')->willReturn('5.0.0');

$expected = [
'installed' => '5.0.0',
'stable' => '5.0.0',
'development' => '5.1.0',
'nightly' => '5.2.0'
];

$this->assertEquals($expected, $this->api->getVersions());
}

public function testIsVerifiedSuccess(): void
{
$response = $this->createMock(ResponseInterface::class);
$response->method('getContent')->willReturn('{"hash1": "abc", "hash2": "def"}');
$response->method('getStatusCode')->willReturn(Response::HTTP_OK);

$this->httpClient->expects($this->once())
->method('request')
->willReturn($response);

$this->assertTrue($this->api->isVerified());
}

public function testIsVerifiedFailure(): void
{
$response = $this->createMock(ResponseInterface::class);
$response->method('getStatusCode')->willReturn(Response::HTTP_INTERNAL_SERVER_ERROR);

$this->httpClient->expects($this->once())
->method('request')
->willReturn($response);

$this->expectException('JsonException');
$this->api->isVerified();
}

public function testIsVerifiedTransportException(): void
{
$this->httpClient->expects($this->once())
->method('request')
->willThrowException(new TransportException());

$this->expectException('Symfony\Component\HttpClient\Exception\TransportException');
$this->api->isVerified();
}
}
11 changes: 11 additions & 0 deletions tests/phpMyFAQ/Administration/ReportTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace phpMyFAQ\Administration;

use phpMyFAQ\Configuration;
use PHPUnit\Framework\TestCase;

class ReportTest extends TestCase
Expand All @@ -28,4 +29,14 @@ public function testSanitize(): void

$this->assertEquals($expected, $actual);
}

public function testConvertEncodingWithHtmlEntitiesAndCommas(): void
{
$inputString = '&lt;p&gt;This is a test, &amp;sample string.&lt;/p&gt;';
$expectedOutput = '<p>This is a test &sample string.</p>';
$report = new Report(Configuration::getConfigurationInstance());
$actualOutput = $report->convertEncoding($inputString);

$this->assertEquals($expectedOutput, $actualOutput);
}
}

0 comments on commit e46decd

Please sign in to comment.