Skip to content

Instantly share code, notes, and snippets.

@JLNNN
Last active August 5, 2019 11:01
Show Gist options
  • Save JLNNN/806cbf9e42975f33f82d96ba363f7fe8 to your computer and use it in GitHub Desktop.
Save JLNNN/806cbf9e42975f33f82d96ba363f7fe8 to your computer and use it in GitHub Desktop.
<phpunit bootstrap="vendor/autoload.php">
<testsuites>
<testsuite name="Tests">
<directory>Tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory>Classes</directory>
</whitelist>
</filter>
</phpunit>
<?php
namespace My\NamespaceRedirect\Tests\Unit\Hooks;
use My\NamespaceRedirect\Hooks\RequestHandlerHook;
use My\NamespaceRedirect\Service\DomainDataService;
use My\NamespaceRedirect\Service\HeaderService;
use My\NamespaceRedirect\Service\RedirectDataService;
use TYPO3\CMS\Core\Core\Environment;
use TYPO3\CMS\Core\Core\SystemEnvironmentBuilder;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Core\Utility\HttpUtility;
use TYPO3\TestingFramework\Core\Unit\UnitTestCase;
class RequestHandlerHookTest extends UnitTestCase {
/**
* @var \PHPUnit_Framework_MockObject_MockObject|DomainDataService
*/
protected $domainDataServiceMock;
/**
* @var \PHPUnit_Framework_MockObject_MockObject|RedirectDataService
*/
protected $redirectDataServiceMock;
/**
* @var \PHPUnit_Framework_MockObject_MockObject|HeaderService
*/
protected $headerServiceMock;
/**
* @var RequestHandlerHook
*/
protected $requestHandlerHookSUT;
protected $backupEnvironment = TRUE;
protected function setUp() {
define("TYPO3_PATH_WEB", "");
define("PATH_thisScript", "./Tests/Unit/Hooks/RequestHandlerHookTest.php");
SystemEnvironmentBuilder::run();
$this->resetSingletonInstances = TRUE;
parent::setUp();
$this->domainDataServiceMock = $this->createMock(DomainDataService::class);
GeneralUtility::setSingletonInstance(DomainDataService::class, $this->domainDataServiceMock);
$this->redirectDataServiceMock = $this->createMock(RedirectDataService::class);
GeneralUtility::setSingletonInstance(RedirectDataService::class, $this->redirectDataServiceMock);
$this->headerServiceMock = $this->createMock(HeaderService::class);
GeneralUtility::addInstance(HeaderService::class, $this->headerServiceMock);
$this->requestHandlerHookSUT = new RequestHandlerHook();
}
public function testHandleRequest() {
Environment::initialize(
Environment::getContext(),
TRUE,
FALSE,
Environment::getProjectPath(),
Environment::getPublicPath(),
Environment::getVarPath(),
Environment::getConfigPath(),
Environment::getBackendPath().'/index.php',
Environment::isWindows() ? 'WINDOWS' : 'UNIX'
);
$path = "seite.html";
$domain = "www.mydomain.de";
$expectedStartPageId = 3;
$redirectUid = 45;
$redirectDestination = "https://www.foreignDomain.de";
$GLOBALS["TYPO3_CONF_VARS"]["SYS"] = [
"reverseProxySSL" => "",
"reverseProxyIP" => "",
"requestURIvar" => FALSE
];
$_SERVER["REMOTE_ADDR"] = "127.0.0.1";
$_SERVER["SSL_SESSION_ID"] = "";
$_SERVER["ORIG_SCRIPT_NAME"] = "";
$_SERVER["SCRIPT_NAME"] = "/index.php";
$_SERVER["HTTPS"] = "on";
$_SERVER["HTTP_HOST"] = $domain;
$_SERVER["REQUEST_URI"] = "/$path";
$this->domainDataServiceMock
->expects($this->once())
->method("getStartPageId")
->with($domain)
->willReturn($expectedStartPageId);
$this->redirectDataServiceMock
->expects($this->once())
->method("findBySource")
->with($path, $expectedStartPageId)
->willReturn([
"uid" => $redirectUid,
"destination" => $redirectDestination
]);
$this->headerServiceMock
->expects($this->once())
->method("redirect")
->with($redirectDestination, HttpUtility::HTTP_STATUS_301);
$this->requestHandlerHookSUT->handleRequest();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment