Skip to content

Commit

Permalink
add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ameotoko committed Nov 4, 2021
1 parent 1b39a10 commit 73b088e
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
/composer.lock
/vendor/

phpunit.xml
.phpunit.result.cache
5 changes: 5 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,10 @@
},
"extra": {
"contao-manager-plugin": "Ameotoko\\BackendRedirect\\ContaoManager\\Plugin"
},
"autoload-dev": {
"psr-4": {
"Ameotoko\\BackendRedirect\\Test\\": "tests/"
}
}
}
26 changes: 26 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/8.5/phpunit.xsd"
bootstrap="vendor/autoload.php"
executionOrder="depends,defects"
forceCoversAnnotation="true"
beStrictAboutCoversAnnotation="true"
beStrictAboutOutputDuringTests="true"
beStrictAboutTodoAnnotatedTests="true"
convertDeprecationsToExceptions="true"
verbose="true">
<php>
<const name="REQUEST_TOKEN" value="3745f4756fg" />
</php>
<testsuites>
<testsuite name="default">
<directory>tests</directory>
</testsuite>
</testsuites>

<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src</directory>
</whitelist>
</filter>
</phpunit>
62 changes: 62 additions & 0 deletions tests/Controller/RedirectControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?php
/**
* @author Andrey Vinichenko <[email protected]>
*/

namespace Ameotoko\BackendRedirect\Test\Controller;

use Ameotoko\BackendRedirect\Controller\RedirectController;
use Contao\System;
use Contao\TestCase\ContaoTestCase;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Routing\Router;

class RedirectControllerTest extends ContaoTestCase
{
public function testBackendRedirectResponse(): void
{
$request = Request::create('https://localhost/contao/redirect?do=members&act=edit&id=42');
$request->attributes->set('_contao_referer_id', '_OY4TOxP');

\define('TL_SCRIPT', substr($request->getBaseUrl().$request->getPathInfo(), \strlen($request->getBasePath().'/')));

$requestStack = $this->createMock(RequestStack::class);
$requestStack->method('getCurrentRequest')->willReturn($request);

$container = $this->getContainerWithContaoConfiguration();
$container->set('request_stack', $requestStack);

$framework = $this->mockContaoFramework();
$framework->expects($this->atLeastOnce())->method('initialize');

System::setContainer($container);

$router = $this->createMock(Router::class);
$router->method('generate')->with('contao_backend')->willReturn('/contao');

$controller = new RedirectController();

/** @var RedirectResponse $response */
$response = $controller($request, $framework, $router);

$this->assertInstanceOf(RedirectResponse::class, $response);

$parsedUrl = parse_url($response->getTargetUrl());

// Path must become /contao instead of /contao/redirect
$this->assertSame('/contao', $parsedUrl['path']);

parse_str($parsedUrl['query'], $query);

// Original query params must be preserved
$this->assertSame('members', $query['do']);
$this->assertSame('edit', $query['act']);
$this->assertSame('42', $query['id']);

// ref and request token must be added to the response url
$this->assertSame('_OY4TOxP', $query['ref']);
$this->assertArrayHasKey('rt', $query);
}
}

0 comments on commit 73b088e

Please sign in to comment.