Skip to content

Commit

Permalink
Update dependency (#14)
Browse files Browse the repository at this point in the history
* Symfony 5 compatibility
  • Loading branch information
IndraGunawan authored Feb 26, 2020
1 parent d82b3fd commit b5124b4
Show file tree
Hide file tree
Showing 16 changed files with 112 additions and 180 deletions.
3 changes: 3 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/Tests export-ignore
/phpunit.xml.dist export-ignore
/.gitignore export-ignore
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ composer.lock
composer.phar
.php_cs.cache
/build
.phpunit.result.cache
7 changes: 7 additions & 0 deletions .scrutinizer.yml
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
build:
nodes:
analysis:
tests:
override:
- php-scrutinizer-run

filter:
excluded_paths: [vendor/*, Tests/*]
tools:
Expand Down
6 changes: 3 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
language: php

sudo: false
os: linux

cache:
directories:
- $HOME/.composer/cache

matrix:
jobs:
include:
- php: '7.0'
- php: '7.1'
- php: '7.2'
- php: '7.3'
- php: '7.3'
env: deps=low
- php: '7.4'

before_install:
- composer self-update
Expand Down
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
CHANGELOG
=========

v0.4.0
------

* Symfony 5 compatibility (#14)

v0.3.0
------

* Feature. Configuration per using annotations (#9)
* Feature. Rate limit to specific methods (#12)
* Fix null ApiRateLimit instance in RateLimitHandler (#11)

v0.2.1
------

* Adding Symfony4 support (#5)
* Fix dependencies for Symfony Flex

Expand Down
31 changes: 7 additions & 24 deletions DependencyInjection/Configuration.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,17 @@ final class Configuration implements ConfigurationInterface
*/
public function getConfigTreeBuilder()
{
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('indragunawan_api_rate_limit');
if (method_exists(TreeBuilder::class, 'getRootNode')) {
$treeBuilder = new TreeBuilder('indragunawan_api_rate_limit');
$rootNode = $treeBuilder->getRootNode();
} else {
$treeBuilder = new TreeBuilder();
$rootNode = $treeBuilder->root('indragunawan_api_rate_limit');
}

$rootNode
->children()
->booleanNode('enabled')->defaultTrue()->end()
->scalarNode('storage')->defaultNull()->cannotBeEmpty()->end()
->scalarNode('cache')->defaultNull()->cannotBeEmpty()->end()
->arrayNode('header')
->addDefaultsIfNotSet()
Expand All @@ -51,29 +55,8 @@ public function getConfigTreeBuilder()
->end()
->end()
->arrayNode('throttle')
->beforeNormalization()
->ifTrue(function ($v) { return is_array($v) && (isset($v['limit']) || isset($v['period'])); })
->then(function ($v) {
$v['default'] = [];
if (isset($v['limit'])) {
@trigger_error('The indragunawan_api_rate_limit.throttle.limit configuration key is deprecated since version v0.2.0 and will be removed in v0.3.0. Use the indragunawan_api_rate_limit.throttle.default.limit configuration key instead.', E_USER_DEPRECATED);

$v['default']['limit'] = $v['limit'];
}

if (isset($v['period'])) {
@trigger_error('The indragunawan_api_rate_limit.throttle.period configuration key is deprecated since version v0.2.0 and will be removed in v0.3.0. Use the indragunawan_api_rate_limit.throttle.default.period configuration key instead.', E_USER_DEPRECATED);

$v['default']['period'] = $v['period'];
}

return $v;
})
->end()
->addDefaultsIfNotSet()
->children()
->integerNode('limit')->min(1)->defaultValue(60)->end()
->integerNode('period')->min(1)->defaultValue(60)->end()
->arrayNode('default')
->addDefaultsIfNotSet()
->children()
Expand Down
4 changes: 0 additions & 4 deletions DependencyInjection/IndragunawanApiRateLimitExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,6 @@ private function registerServiceConfig(ContainerBuilder $container, array $confi
{
if (null !== $config['cache']) {
$cache = new Reference($config['cache']);
} elseif (null !== $config['storage']) {
@trigger_error('The indragunawan_api_rate_limit.storage configuration key is deprecated since version v0.2.0 and will be removed in v0.3.0. Use the indragunawan_api_rate_limit.cache configuration key instead.', E_USER_DEPRECATED);

$cache = new Definition(DoctrineAdapter::class, [new Reference($config['storage']), 'api_rate_limit']);
} else {
$cache = new Definition(FilesystemAdapter::class, ['api_rate_limit', 0, $container->getParameter('kernel.cache_dir')]);
}
Expand Down
6 changes: 3 additions & 3 deletions EventListener/HeaderModificationListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace Indragunawan\ApiRateLimitBundle\EventListener;

use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpKernel\Event\ResponseEvent;

/**
* @author Indra Gunawan <[email protected]>
Expand All @@ -29,9 +29,9 @@ public function __construct(array $header)
}

/**
* @param FilterResponseEvent $event
* @param ResponseEvent $event
*/
public function onKernelResponse(FilterResponseEvent $event)
public function onKernelResponse(ResponseEvent $event)
{
if (false === $this->header['display']) {
return;
Expand Down
4 changes: 2 additions & 2 deletions EventListener/RateLimitListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
use Indragunawan\ApiRateLimitBundle\Exception\RateLimitExceededException;
use Indragunawan\ApiRateLimitBundle\Service\RateLimitHandler;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;

/**
Expand Down Expand Up @@ -50,7 +50,7 @@ public function __construct(bool $enabled, RateLimitHandler $rateLimitHandler, a
$this->tokenStorage = $tokenStorage;
}

public function onKernelRequest(GetResponseEvent $event)
public function onKernelRequest(RequestEvent $event)
{
if (!$this->enabled) {
return;
Expand Down
19 changes: 1 addition & 18 deletions Tests/DependencyInjection/ConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class ConfigurationTest extends TestCase
/**
* {@inheritdoc}
*/
protected function setUp()
protected function setUp(): void
{
$this->configuration = new Configuration(false);
$this->processor = new Processor();
Expand Down Expand Up @@ -115,21 +115,4 @@ public function testValidExceptionClass()

$this->assertSame(ValidRateLimitExceededException::class, $config['exception']['custom_exception']);
}

public function testDeprecatedConfiguration()
{
$config = $this->processor->processConfiguration(
$this->configuration,
[
[
'throttle' => [
'limit' => 10,
'period' => 10,
],
],
]
);

$this->assertSame(['limit' => 10, 'period' => 10], $config['throttle']['default']);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class IndragunawanApiRateLimitExtensionTest extends TestCase
/**
* {@inheritdoc}
*/
protected function setUp()
protected function setUp(): void
{
$this->container = new ContainerBuilder();
$this->container->setParameter('kernel.cache_dir', '../../var/cache');
Expand All @@ -41,7 +41,7 @@ protected function setUp()
/**
* {@inheritdoc}
*/
protected function tearDown()
protected function tearDown(): void
{
unset($this->container, $this->extension);
}
Expand Down Expand Up @@ -79,22 +79,6 @@ public function testServiceConfig()
$this->assertSame('custom_cache', (string) $storageDefinition->getArgument(0));
}

public function testDeprecatedServiceConfig()
{
$config = [
[
'storage' => 'custom_storage',
],
];

$this->extension->load($config, $this->container);

$this->assertTrue($this->container->hasDefinition('indragunawan_api_rate_limit.service.rate_limit_handler'));

$storageDefinition = $this->container->getDefinition('indragunawan_api_rate_limit.service.rate_limit_handler');
$this->assertSame('custom_storage', (string) $storageDefinition->getArgument(0)->getArgument(0));
}

public function testSortFirstMatch()
{
$roles = [
Expand Down
66 changes: 34 additions & 32 deletions Tests/EventListener/HeaderModificationListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,36 @@

use Indragunawan\ApiRateLimitBundle\EventListener\HeaderModificationListener;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\ParameterBag;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
use Symfony\Component\HttpFoundation\ResponseHeaderBag;
use Symfony\Component\HttpKernel\Event\ResponseEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;

class HeaderModificationListenerTest extends TestCase
{
private $kernel;

public function setUp(): void
{
$this->kernel = $this->createMock(HttpKernelInterface::class);
}

public function tearDown(): void
{
$this->kernel = null;
}

public function testNotDisplayHeader()
{
$event = $this->getMockBuilder(FilterResponseEvent::class)
->disableOriginalConstructor()
->getMock();
$responseHeaderBag = $this->prophesize(ResponseHeaderBag::class);
$responseHeaderBag->set()->shouldNotHaveBeenCalled();

$event->expects($this->never())
->method('getRequest');
$response = $this->prophesize(Response::class);
$response->headers = $responseHeaderBag->reveal();

$event = new ResponseEvent($this->kernel, new Request(), HttpKernelInterface::MASTER_REQUEST, $response->reveal());

$headerConfig = [
'display' => false,
Expand All @@ -38,21 +54,13 @@ public function testNotDisplayHeader()

public function testNoRateLimitInfoAttributes()
{
$event = $this->getMockBuilder(FilterResponseEvent::class)
->disableOriginalConstructor()
->getMock();

$resetTime = gmdate('U');
$attributes = $this->prophesize(ParameterBag::class);
$attributes->get('_api_rate_limit_info', null)->willReturn(null)->shouldBeCalledTimes(1);

$request = Request::create('/api/me');
$request->attributes->set('_api_rate_limit_info', null);
$request = $this->prophesize(Request::class);
$request->attributes = $attributes->reveal();

$event->expects($this->once())
->method('getRequest')
->will($this->returnValue($request));

$event->expects($this->never())
->method('getResponse');
$event = new ResponseEvent($this->kernel, $request->reveal(), HttpKernelInterface::MASTER_REQUEST, new Response());

$headerConfig = [
'display' => true,
Expand All @@ -64,27 +72,21 @@ public function testNoRateLimitInfoAttributes()

public function testSetResponseHeaders()
{
$event = $this->getMockBuilder(FilterResponseEvent::class)
->disableOriginalConstructor()
->getMock();

$resetTime = gmdate('U');

$request = Request::create('/api/me');
$request->attributes->set('_api_rate_limit_info', [
$attributes = $this->prophesize(ParameterBag::class);
$attributes->get('_api_rate_limit_info', null)->willReturn([
'limit' => 60,
'remaining' => 59,
'reset' => $resetTime,
]);
])->shouldBeCalledTimes(1);

$event->expects($this->once())
->method('getRequest')
->will($this->returnValue($request));
$request = $this->prophesize(Request::class);
$request->attributes = $attributes->reveal();

$response = new Response();
$event->expects($this->once())
->method('getResponse')
->will($this->returnValue($response));

$event = new ResponseEvent($this->kernel, $request->reveal(), HttpKernelInterface::MASTER_REQUEST, $response);

$headerConfig = [
'display' => true,
Expand Down
Loading

0 comments on commit b5124b4

Please sign in to comment.