Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Konstantin Wagner committed Mar 14, 2018
1 parent e69f2c4 commit c885218
Show file tree
Hide file tree
Showing 9 changed files with 292 additions and 12 deletions.
16 changes: 6 additions & 10 deletions src/Manager/AjaxActionManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
use Contao\Controller;
use Contao\PageModel;
use Contao\System;
use HeimrichHannot\AjaxBundle\Response\ResponseError;
use HeimrichHannot\AjaxBundle\Exception\AjaxExitException;
use Symfony\Component\DependencyInjection\Exception\BadMethodCallException;

class AjaxActionManager
Expand Down Expand Up @@ -117,22 +117,19 @@ public function call($objContext)
$objItem = null;

if (null === $objContext) {
$objResponse = new ResponseError('Bad Request, context not set.');
$objResponse->send();
System::getContainer()->get('huh.ajax')->sendResponseError('Bad Request, context not set.');
throw new \Exception('Bad Request, context not set.');
}

if (!method_exists($objContext, $this->strAction)) {
$objResponse = new ResponseError('Bad Request, ajax method does not exist within context.');
$objResponse->send();
System::getContainer()->get('huh.ajax')->sendResponseError('Bad Request, ajax method does not exist within context.');
throw new BadMethodCallException('Bad Request, ajax method does not exist within context.');
}

$reflection = new \ReflectionMethod($objContext, $this->strAction);

if (!$reflection->isPublic()) {
$objResponse = new ResponseError('Bad Request, the called method is not public.');
$objResponse->send();
System::getContainer()->get('huh.ajax')->sendResponseError('Bad Request, the called method is not public.');
throw new BadMethodCallException('Bad Request, the called method is not public.');
}

Expand All @@ -157,9 +154,8 @@ protected function getArguments()
}

if (count(preg_grep('/'.$argument.'/i', $arrOptional)) < 1 && count(preg_grep('/'.$argument.'/i', array_keys($arrCurrentArguments))) < 1) {
$objResponse = new ResponseError('Bad Request, missing argument '.$argument);
$objResponse->send();
exit;
System::getContainer()->get('huh.ajax')->sendResponseError('Bad Request, missing argument '.$argument);
throw new AjaxExitException('Bad Request, missing argument '.$argument);
}

$varValue = System::getContainer()->get('huh.request')->isMethod('POST') ? System::getContainer()->get('huh.request')->getPost($argument) : System::getContainer()->get('huh.request')->getGet($argument);
Expand Down
5 changes: 4 additions & 1 deletion src/Response/ResponseData.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,10 @@ public function setHtml($html)
$this->html = $html;
}

public function jsonSerialize()
/**
* @return array
*/
public function jsonSerialize(): array
{
return get_object_vars($this);
}
Expand Down
150 changes: 150 additions & 0 deletions tests/Manager/AjaxActionManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,21 @@

namespace HeimrichHannot\AjaxBundle\Tests\Manager;

use Contao\CoreBundle\Routing\ScopeMatcher;
use Contao\PageModel;
use Contao\System;
use Contao\TestCase\ContaoTestCase;
use HeimrichHannot\AjaxBundle\Exception\AjaxExitException;
use HeimrichHannot\AjaxBundle\Manager\AjaxActionManager;
use HeimrichHannot\AjaxBundle\Manager\AjaxManager;
use HeimrichHannot\AjaxBundle\Response\ResponseSuccess;
use HeimrichHannot\RequestBundle\Component\HttpFoundation\Request;
use HeimrichHannot\UtilsBundle\Arrays\ArrayUtil;
use HeimrichHannot\UtilsBundle\Classes\ClassUtil;
use HeimrichHannot\UtilsBundle\String\StringUtil;
use HeimrichHannot\UtilsBundle\Url\UrlUtil;
use Symfony\Component\HttpFoundation\RequestMatcher;
use Symfony\Component\HttpFoundation\RequestStack;

class AjaxActionManagerTest extends ContaoTestCase
{
Expand All @@ -36,6 +44,7 @@ public function setUp()
$container->set('huh.utils.string', new StringUtil($this->mockContaoFramework()));
$container->set('huh.request', $request);
$container->set('huh.ajax.token', $token);
$container->set('huh.ajax', new AjaxManager());
System::setContainer($container);

if (!\function_exists('ampersand')) {
Expand All @@ -58,4 +67,145 @@ public function testGetParams()
$this->assertSame(['as' => 'ajax', 'ag' => 'ag', 'aa' => 'testAction', 'ato' => 'token'], $manager->getParams('ag', 'testAction'));
$this->assertSame(['as' => 'ajax', 'ag' => 'ag'], $manager->getParams('ag'));
}

public function testCall()
{
$requestStack = new RequestStack();
$requestStack->push(new \Symfony\Component\HttpFoundation\Request());

$backendMatcher = new RequestMatcher('/contao', 'test.com', null, ['192.168.1.0']);
$frontendMatcher = new RequestMatcher('/index', 'test.com', null, ['192.168.1.0']);

$scopeMatcher = new ScopeMatcher($backendMatcher, $frontendMatcher);

$tokenAdapter = $this->mockAdapter(['getToken', 'getValue']);
$tokenAdapter->method('getToken')->willReturnSelf();
$tokenAdapter->method('getValue')->willReturn('token');

$request = new Request($this->mockContaoFramework(), $requestStack, $scopeMatcher);
$request->setGet(AjaxManager::AJAX_ATTR_ACT, 'getResponse');
$request->setGet(AjaxManager::AJAX_ATTR_TOKEN, 'ag');
$request->setGet(AjaxManager::AJAX_ATTR_SCOPE, 'ajax');
$request->setGet(AjaxManager::AJAX_ATTR_GROUP, 'ag');

$token = $this->mockAdapter(['getActiveToken', 'remove', 'create']);
$token->method('getActiveToken')->willReturn('token');
$token->method('create')->willReturn('token');
$container = System::getContainer();
$container->set('huh.request', $request);
$container->set('huh.ajax.token', $token);
System::setContainer($container);

try {
$manager = new AjaxActionManager('ag', 'getResponse');
$manager->call(null);
} catch (\Exception $exception) {
$this->assertSame('Bad Request, context not set.', $exception->getMessage());
}

try {
$manager = new AjaxActionManager('ag', 'test');
$manager->call($this);
} catch (\Exception $exception) {
$this->assertSame('Bad Request, ajax method does not exist within context.', $exception->getMessage());
}

try {
$manager = new AjaxActionManager('ag', 'getProtectedResponse');
$manager->call($this);
} catch (\Exception $exception) {
$this->assertSame('Bad Request, the called method is not public.', $exception->getMessage());
}

$manager = new AjaxActionManager('ag', 'getResponse');
$this->assertInstanceOf(ResponseSuccess::class, $manager->call($this));
}

public function testGetArguments()
{
$requestStack = new RequestStack();
$requestStack->push(new \Symfony\Component\HttpFoundation\Request());

$backendMatcher = new RequestMatcher('/contao', 'test.com', null, ['192.168.1.0']);
$frontendMatcher = new RequestMatcher('/index', 'test.com', null, ['192.168.1.0']);

$scopeMatcher = new ScopeMatcher($backendMatcher, $frontendMatcher);

$tokenAdapter = $this->mockAdapter(['getToken', 'getValue']);
$tokenAdapter->method('getToken')->willReturnSelf();
$tokenAdapter->method('getValue')->willReturn('token');

$request = new Request($this->mockContaoFramework(), $requestStack, $scopeMatcher);
$request->setGet(AjaxManager::AJAX_ATTR_ACT, 'false');
$request->setGet(AjaxManager::AJAX_ATTR_TOKEN, 'ag');
$request->setGet(AjaxManager::AJAX_ATTR_SCOPE, 'ajax');
$request->setGet(AjaxManager::AJAX_ATTR_GROUP, 'ag');

$token = $this->mockAdapter(['getActiveToken', 'remove', 'create']);
$token->method('getActiveToken')->willReturn('token');
$token->method('create')->willReturn('token');
$container = System::getContainer();
$container->set('huh.request', $request);
$container->set('huh.ajax.token', $token);
System::setContainer($container);

$manager = new AjaxActionManager('ag', 'getResponse', ['arguments' => [true, 'test']]);

try {
$function = $this->getMethod(AjaxActionManager::class, 'getArguments');
$function->invoke($manager);
} catch (AjaxExitException $exception) {
$this->assertStringStartsWith('Bad Request, missing argument ', $exception->getMessage());
}

$manager = new AjaxActionManager('ag', 'getResponse', ['arguments' => [true, 'aa']]);
$function = $this->getMethod(AjaxActionManager::class, 'getArguments');
$this->assertSame([true, false], $function->invoke($manager));
}

public function testGenerateUrl()
{
global $objPage;

$objPage = $this->mockClassWithProperties(PageModel::class, []);
$objPage->method('row')->willReturn(['row']);

$controller = $this->mockAdapter(['generateFrontendUrl']);
$controller->method('generateFrontendUrl')->willReturn('url');

$url = $this->mockAdapter(['addQueryString']);
$url->method('addQueryString')->willReturn('url');

$container = System::getContainer();
$container->set('huh.utils.url', $url);
System::setContainer($container);

$manager = new AjaxActionManager('ag', 'test');
$this->assertSame('url', $manager->generateUrl('ag', 'test'));
}

/**
* @return ResponseSuccess
*/
public function getResponse()
{
return new ResponseSuccess();
}

protected function getMethod($class, $name)
{
$class = new \ReflectionClass($class);
$method = $class->getMethod($name);
$method->setAccessible(true);

return $method;
}

/**
* @return ResponseSuccess
*/
protected function getProtectedResponse()
{
return new ResponseSuccess();
}
}
6 changes: 5 additions & 1 deletion tests/Manager/AjaxManagerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,6 @@ public function testRunActiveAction()
$tokenAdapter->method('getValue')->willReturn('token');

$request = new Request($this->mockContaoFramework(), $requestStack, $scopeMatcher);
$request->headers->set('X-Requested-With', 'XMLHttpRequest');
$request->setGet(AjaxManager::AJAX_ATTR_ACT, 'getResponse');
$request->setGet(AjaxManager::AJAX_ATTR_TOKEN, 'ag');
$request->setGet(AjaxManager::AJAX_ATTR_SCOPE, 'ajax');
Expand All @@ -268,6 +267,11 @@ public function testRunActiveAction()
$container->set('huh.ajax.token', $token);
System::setContainer($container);

// is no xml http request
$manager->runActiveAction('ag', 'getResponse', $this);

// is xml http request
$request->headers->set('X-Requested-With', 'XMLHttpRequest');
$GLOBALS['AJAX'] = ['ag' => ['actions' => ['getResponse' => ['csrf_protection' => true]]]];
try {
ob_start();
Expand Down
23 changes: 23 additions & 0 deletions tests/Response/Response404Test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/*
* Copyright (c) 2018 Heimrich & Hannot GmbH
*
* @license LGPL-3.0-or-later
*/

namespace HeimrichHannot\AjaxBundle\Tests\Response;

use Contao\TestCase\ContaoTestCase;
use HeimrichHannot\AjaxBundle\Response\Response;
use HeimrichHannot\AjaxBundle\Response\Response404;

class Response404Test extends ContaoTestCase
{
public function testResponse()
{
$response = new Response404('test');
$this->assertSame(Response::HTTP_NOT_FOUND, $response->getStatusCode());
$this->assertSame('test', $response->getMessage());
}
}
35 changes: 35 additions & 0 deletions tests/Response/ResponseDataTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php

/*
* Copyright (c) 2018 Heimrich & Hannot GmbH
*
* @license LGPL-3.0-or-later
*/

namespace HeimrichHannot\AjaxBundle\Tests\Response;

use Contao\TestCase\ContaoTestCase;
use HeimrichHannot\AjaxBundle\Response\ResponseData;

class ResponseDataTest extends ContaoTestCase
{
public function testSetGetData()
{
$response = new ResponseData();
$response->setData(['data']);
$this->assertSame(['data'], $response->getData());
}

public function testSetGetHtml()
{
$response = new ResponseData();
$response->setHtml('html');
$this->assertSame('html', $response->getHtml());
}

public function testJsonSerialize()
{
$response = new ResponseData('html', ['data']);
$this->assertSame(['data' => ['data'], 'html' => 'html'], $response->jsonSerialize());
}
}
23 changes: 23 additions & 0 deletions tests/Response/ResponseErrorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/*
* Copyright (c) 2018 Heimrich & Hannot GmbH
*
* @license LGPL-3.0-or-later
*/

namespace HeimrichHannot\AjaxBundle\Tests\Response;

use Contao\TestCase\ContaoTestCase;
use HeimrichHannot\AjaxBundle\Response\Response;
use HeimrichHannot\AjaxBundle\Response\ResponseError;

class ResponseErrorTest extends ContaoTestCase
{
public function testResponse()
{
$response = new ResponseError('test');
$this->assertSame(Response::HTTP_BAD_REQUEST, $response->getStatusCode());
$this->assertSame('test', $response->getMessage());
}
}
23 changes: 23 additions & 0 deletions tests/Response/ResponseRedirectTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/*
* Copyright (c) 2018 Heimrich & Hannot GmbH
*
* @license LGPL-3.0-or-later
*/

namespace HeimrichHannot\AjaxBundle\Tests\Response;

use Contao\TestCase\ContaoTestCase;
use HeimrichHannot\AjaxBundle\Response\Response;
use HeimrichHannot\AjaxBundle\Response\ResponseRedirect;

class ResponseRedirectTest extends ContaoTestCase
{
public function testResponse()
{
$response = new ResponseRedirect('test');
$this->assertSame(Response::HTTP_MULTIPLE_CHOICES, $response->getStatusCode());
$this->assertSame('test', $response->getMessage());
}
}
23 changes: 23 additions & 0 deletions tests/Response/ResponseSuccessTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

/*
* Copyright (c) 2018 Heimrich & Hannot GmbH
*
* @license LGPL-3.0-or-later
*/

namespace HeimrichHannot\AjaxBundle\Tests\Response;

use Contao\TestCase\ContaoTestCase;
use HeimrichHannot\AjaxBundle\Response\Response;
use HeimrichHannot\AjaxBundle\Response\ResponseSuccess;

class ResponseSuccessTest extends ContaoTestCase
{
public function testResponse()
{
$response = new ResponseSuccess('test');
$this->assertSame(Response::HTTP_OK, $response->getStatusCode());
$this->assertSame('test', $response->getMessage());
}
}

0 comments on commit c885218

Please sign in to comment.