Skip to content

Commit

Permalink
Add test for QueryInformationFactory (#112)
Browse files Browse the repository at this point in the history
* Add test for QueryInformationFactory

* Update lines mentioned by php-cs-fixer
  • Loading branch information
froemken authored Aug 8, 2024
1 parent ba4af6f commit 2d9b8bc
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 5 deletions.
6 changes: 3 additions & 3 deletions Classes/Domain/Factory/QueryInformationFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@ class QueryInformationFactory
public function __construct()
{
$this->pageUid = $this->getPageUid();
$this->ip = GeneralUtility::getIndpEnv('REMOTE_ADDR');
$this->referer = GeneralUtility::getIndpEnv('HTTP_REFERER');
$this->request = GeneralUtility::getIndpEnv('TYPO3_REQUEST_URL');
$this->ip = (string)GeneralUtility::getIndpEnv('REMOTE_ADDR');
$this->referer = (string)GeneralUtility::getIndpEnv('HTTP_REFERER');
$this->request = (string)GeneralUtility::getIndpEnv('TYPO3_REQUEST_URL');
$this->mode = $this->getTypo3Mode();
$this->uniqueCallIdentifier = uniqid('', true);
$this->crdate = (int)$GLOBALS['EXEC_TIME'];
Expand Down
103 changes: 101 additions & 2 deletions Tests/Unit/Domain/Factory/QueryInformationFactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use PHPUnit\Framework\Attributes\Test;
use StefanFroemken\Mysqlreport\Domain\Factory\QueryInformationFactory;
use TYPO3\CMS\Core\Core\Environment;
use TYPO3\CMS\Core\Core\SystemEnvironmentBuilder;
use TYPO3\CMS\Core\Http\ServerRequest;
use TYPO3\CMS\Core\Routing\PageArguments;
Expand All @@ -25,28 +26,126 @@ class QueryInformationFactoryTest extends UnitTestCase
{
private QueryInformationFactory $subject;

private int $time;

protected function setUp(): void
{
$GLOBALS['TYPO3_REQUEST'] = (new ServerRequest('https://www.example.com/'))
$this->time = time();

$GLOBALS['EXEC_TIME'] = $this->time;

$_SERVER = array_merge($_SERVER, [
'REMOTE_ADDR' => '123.124.125.126',
'HTTP_HOST' => 'example.com',
'HTTP_REFERER' => 'https://www.typo3lexikon.de',
'REQUEST_URI' => '/index.php',
'SSL_SESSION_ID' => '1234567890',
]);

$GLOBALS['TYPO3_REQUEST'] = (new ServerRequest('https://www.example.com/', 'GET', 'php://input', [], $_SERVER))
->withAttribute('applicationType', SystemEnvironmentBuilder::REQUESTTYPE_FE)
->withAttribute('routing', new PageArguments(1, '0', []));

Environment::initialize(
Environment::getContext(),
false,
Environment::isComposerMode(),
Environment::getProjectPath(),
Environment::getPublicPath(),
Environment::getVarPath(),
Environment::getConfigPath(),
Environment::getCurrentScript(),
Environment::isWindows() ? 'WINDOWS' : 'UNIX',
);

$this->subject = new QueryInformationFactory();
}

protected function tearDown(): void
{
unset(
$this->subject,
$GLOBALS['TYPO3_REQUEST'],
$GLOBALS['EXEC_TIME'],
);
}

#[Test]
public function createNewQueryInformationWillCreateQueryInformationWithPid1(): void
public function createNewQueryInformationWillCreateQueryInformationWithFrontendPid(): void
{
self::assertSame(
1,
$this->subject->createNewQueryInformation()->getPid(),
);
}

#[Test]
public function createNewQueryInformationWillCreateQueryInformationWithBackendPid(): void
{
$GLOBALS['TYPO3_REQUEST'] = (new ServerRequest('https://www.example.com/typo3/?id=30', 'GET', 'php://input', [], $_SERVER))
->withAttribute('applicationType', SystemEnvironmentBuilder::REQUESTTYPE_BE)
->withQueryParams(['id' => 30]);

$this->subject = new QueryInformationFactory();

self::assertSame(
30,
$this->subject->createNewQueryInformation()->getPid(),
);
}

#[Test]
public function createNewQueryInformationWillCreateQueryInformationWithIp(): void
{
self::assertSame(
'123.124.125.126',
$this->subject->createNewQueryInformation()->getIp(),
);
}

#[Test]
public function createNewQueryInformationWillCreateQueryInformationWithReferer(): void
{
self::assertSame(
'https://www.typo3lexikon.de',
$this->subject->createNewQueryInformation()->getReferer(),
);
}

#[Test]
public function createNewQueryInformationWillCreateQueryInformationWithRequest(): void
{
self::assertSame(
'https://example.com/index.php',
$this->subject->createNewQueryInformation()->getRequest(),
);
}

#[Test]
public function createNewQueryInformationWillCreateQueryInformationWithTypo3Mode(): void
{
self::assertSame(
'FE',
$this->subject->createNewQueryInformation()->getMode(),
);
}

#[Test]
public function createNewQueryInformationWillCreateQueryInformationWithCallIdentifier(): void
{
// We are working with more entropy
self::assertStringContainsString(
'.',
$this->subject->createNewQueryInformation()->getUniqueCallIdentifier(),
);
}

#[Test]
public function createNewQueryInformationWillCreateQueryInformationWithCrdate(): void
{
self::assertSame(
$this->time,
$this->subject->createNewQueryInformation()->getCrdate(),
);
}
}

0 comments on commit 2d9b8bc

Please sign in to comment.