Skip to content

Commit

Permalink
test: added more tests
Browse files Browse the repository at this point in the history
  • Loading branch information
thorsten committed Mar 22, 2024
1 parent 28ab932 commit 839a29d
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 2 deletions.
2 changes: 1 addition & 1 deletion phpmyfaq/src/phpMyFAQ/Captcha/BuiltinCaptcha.php
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ public function setUserIsLoggedIn(bool $userIsLoggedIn): BuiltinCaptcha
/**
* Gives the HTML output code for the Captcha.
*
* @param string $action The action parameter
* @return string
*/
public function renderCaptchaImage(): string
{
Expand Down
15 changes: 15 additions & 0 deletions tests/phpMyFAQ/Attachment/AbstractMimeTypeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<?php

namespace phpMyFAQ\Attachment;

use PHPUnit\Framework\TestCase;

class AbstractMimeTypeTest extends TestCase
{
public function testGuessByExt(): void
{
$this->assertEquals('application/andrew-inset', AbstractMimeType::guessByExt('ez'));
$this->assertEquals('application/mac-binhex40', AbstractMimeType::guessByExt('hqx'));
$this->assertEquals('application/octet-stream', AbstractMimeType::guessByExt('extension'));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
/**
* Class VanillaTest
*/
class VanillaTest extends TestCase
class VanillaFileTest extends TestCase
{
/** @var VanillaFile*/
private VanillaFile $instance;
Expand Down
43 changes: 43 additions & 0 deletions tests/phpMyFAQ/Captcha/CaptchaTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace phpMyFAQ\Captcha;

use phpMyFAQ\Configuration;
use phpMyFAQ\Database\Sqlite3;
use phpMyFAQ\Strings;
use PHPUnit\Framework\TestCase;

class CaptchaTest extends TestCase
{
private static ?CaptchaInterface $captcha = null;

protected Configuration $configuration;

protected function setUp(): void
{
parent::setUp();

Strings::init();

$dbHandle = new Sqlite3();
$dbHandle->connect(PMF_TEST_DIR . '/test.db', '', '');
$this->configuration = new Configuration($dbHandle);
}
public function testGetInstanceWithGoogleRecaptchaEnabled(): void
{
$this->configuration->set('security.enableGoogleReCaptchaV2', true);

$captcha = Captcha::getInstance($this->configuration);

$this->assertInstanceOf(GoogleRecaptcha::class, $captcha);
}

public function testGetInstanceWithGoogleRecaptchaDisabled(): void
{
$this->configuration->set('security.enableGoogleReCaptchaV2', false);

$captcha = Captcha::getInstance($this->configuration);

$this->assertInstanceOf(BuiltinCaptcha::class, $captcha);
}
}
43 changes: 43 additions & 0 deletions tests/phpMyFAQ/Captcha/Helper/CaptchaHelperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace phpMyFAQ\Captcha\Helper;

use phpMyFAQ\Captcha\BuiltinCaptcha;
use phpMyFAQ\Configuration;
use phpMyFAQ\Database\Sqlite3;
use phpMyFAQ\Strings;
use PHPUnit\Framework\TestCase;

class CaptchaHelperTest extends TestCase
{
protected Configuration $configuration;

protected function setUp(): void
{
parent::setUp();

Strings::init();

$dbHandle = new Sqlite3();
$dbHandle->connect(PMF_TEST_DIR . '/test.db', '', '');
$this->configuration = new Configuration($dbHandle);
}

public function testGetInstanceWithGoogleRecaptchaEnabled(): void
{
$this->configuration->set('security.enableGoogleReCaptchaV2', true);

$captchaHelper = CaptchaHelper::getInstance($this->configuration);

$this->assertInstanceOf(GoogleRecaptchaHelper::class, $captchaHelper);
}

public function testGetInstanceWithGoogleRecaptchaDisabled(): void
{
$this->configuration->set('security.enableGoogleReCaptchaV2', false);

$captchaHelper = CaptchaHelper::getInstance($this->configuration);

$this->assertInstanceOf(BuiltinCaptchaHelper::class, $captchaHelper);
}
}
30 changes: 30 additions & 0 deletions tests/phpMyFAQ/Core/ErrorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace phpMyFAQ\Core;

use ErrorException;
use PHPUnit\Framework\TestCase;

class ErrorTest extends TestCase
{
public function testErrorHandler(): void
{
// Arrange
$level = E_NOTICE;
$message = "Undefined variable: x";
$filename = "test.php";
$line = 10;

// Act
try {
Error::errorHandler($level, $message, $filename, $line);
} catch (ErrorException $exception) {
// Assert
$this->assertSame($message, $exception->getMessage());
$this->assertSame(0, $exception->getCode());
$this->assertSame($level, $exception->getSeverity());
$this->assertSame(DEBUG ? $filename : basename($filename), $exception->getFile());
$this->assertSame($line, $exception->getLine());
}
}
}
29 changes: 29 additions & 0 deletions tests/phpMyFAQ/Core/ExceptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace phpMyFAQ\Core;

use PHPUnit\Framework\TestCase;

class ExceptionTest extends TestCase
{
public function testToString(): void
{
$message = 'Test exception message';
$file = __FILE__;
$line = 14;
$exception = new Exception($message, 0, null);

// Generate the expected string representation
$expected = sprintf(
"Exception %s with message %s in %s: %d\nStack trace:\n%s",
Exception::class,
$message,
$file,
$line,
$exception->getTraceAsString()
);

// Ensure the __toString method returns the expected string
$this->assertSame($expected, (string)$exception);
}
}

0 comments on commit 839a29d

Please sign in to comment.