Skip to content

Commit

Permalink
Simplify test cases.
Browse files Browse the repository at this point in the history
  • Loading branch information
hswong3i committed Aug 10, 2015
1 parent 2de0a13 commit 25dc803
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 126 deletions.
19 changes: 12 additions & 7 deletions app/AppKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,27 @@

use Symfony\Component\HttpFoundation\Request;

# Register MUST have Silex providers for AuthBucketOAuth2ServiceProvider.
$app->register(new AuthBucket\OAuth2\Provider\AuthBucketOAuth2ServiceProvider());
$app->register(new AuthBucket\Push\Tests\TestBundle\TestBundleServiceProvider());
$app->register(new Silex\Provider\MonologServiceProvider());
$app->register(new Silex\Provider\SecurityServiceProvider());
$app->register(new Silex\Provider\ValidatorServiceProvider());

# Register AuthBucketPushServiceProvider.
$app->register(new AuthBucket\Push\Provider\AuthBucketPushServiceProvider());

# Register additional Silex providers for TestBundleServiceProvider.
$app->register(new Silex\Provider\DoctrineServiceProvider());
$app->register(new Silex\Provider\FormServiceProvider());
$app->register(new Silex\Provider\SecurityServiceProvider());
$app->register(new Silex\Provider\SerializerServiceProvider());
$app->register(new Silex\Provider\RememberMeServiceProvider());
$app->register(new Silex\Provider\ServiceControllerServiceProvider());
$app->register(new Silex\Provider\SessionServiceProvider());
$app->register(new Silex\Provider\TranslationServiceProvider());
$app->register(new Silex\Provider\TwigServiceProvider());
$app->register(new Silex\Provider\UrlGeneratorServiceProvider());
$app->register(new Silex\Provider\ValidatorServiceProvider());

$provider = new AuthBucket\Push\Provider\AuthBucketPushServiceProvider();
$app->register($provider);
$app->mount('/', $provider);
# Register TestBundleServiceProvider.
$app->register(new AuthBucket\Push\Tests\TestBundle\TestBundleServiceProvider());

require __DIR__.'/config/config_'.$app['env'].'.php';

Expand Down
19 changes: 2 additions & 17 deletions app/config/security.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,28 +14,13 @@
'http' => true,
'users' => $app['security.user_provider.admin'],
),
'dummy_oauth2_debug' => array(
'pattern' => '^/dummy/v1.0/oauth2/debug',
'anonymous' => true,
),
'dummy' => array(
'pattern' => '^/dummy/v1.0',
'oauth2_resource' => array(
'resource_type' => 'debug_endpoint',
'scope' => array(),
'options' => array(
'debug_endpoint' => '/dummy/v1.0/oauth2/debug',
'cache' => false,
),
),
),
'api' => array(
'pattern' => '^/api/v1.0',
'pattern' => '^/api',
'oauth2_resource' => array(
'resource_type' => 'debug_endpoint',
'scope' => array(),
'options' => array(
'debug_endpoint' => 'http://oauth2-php.authbucket.com/api/v1.0/oauth2/debug',
'debug_endpoint' => 'http://oauth2-php.authbucket.com/api/oauth2/debug',
'cache' => false,
),
),
Expand Down
41 changes: 40 additions & 1 deletion src/EventListener/ExceptionListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,28 @@
namespace AuthBucket\Push\EventListener;

use AuthBucket\Push\Exception\ExceptionInterface;
use Psr\Log\LoggerInterface;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\GetResponseForExceptionEvent;
use Symfony\Component\HttpKernel\KernelEvents;

/**
* ExceptionListener.
*
* @author Wong Hoi Sing Edison <hswong3i@pantarei-design.com>
*/
class ExceptionListener
class ExceptionListener implements EventSubscriberInterface
{
private $logger;

public function __construct(LoggerInterface $logger)
{
$this->logger = $logger;
}

public function onKernelException(GetResponseForExceptionEvent $event)
{
$exception = $event->getException();
Expand All @@ -38,6 +48,23 @@ private function handleException(
GetResponseForExceptionEvent $event,
ExceptionInterface $exception
) {
if (null !== $this->logger) {
$message = sprintf(
'%s: %s (code %s) at %s line %s',
get_class($exception),
$exception->getMessage(),
$exception->getCode(),
$exception->getFile(),
$exception->getLine()
);

if ($exception->getCode() < 500) {
$this->logger->error($message, array('exception' => $exception));
} else {
$this->logger->critical($message, array('exception' => $exception));
}
}

$message = unserialize($exception->getMessage());

if (isset($message['redirect_uri'])) {
Expand All @@ -57,4 +84,16 @@ private function handleException(

$event->setResponse($response);
}

public static function getSubscribedEvents()
{
return array(
/*
* Priority -2 is used to come after those from SecurityServiceProvider (0)
* but before the error handlers added with Silex\EventListener\LogListener (-4)
* and Silex\Application::error (defaults to -8)
*/
KernelEvents::EXCEPTION => array('onKernelException', -2),
);
}
}
27 changes: 6 additions & 21 deletions src/Provider/AuthBucketPushServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
use AuthBucket\Push\EventListener\ExceptionListener;
use AuthBucket\Push\ServiceType\ServiceTypeHandlerFactory;
use Silex\Application;
use Silex\ControllerProviderInterface;
use Silex\ServiceProviderInterface;
use Symfony\Component\HttpKernel\KernelEvents;

Expand All @@ -24,7 +23,7 @@
*
* @author Wong Hoi Sing Edison <hswong3i@pantarei-design.com>
*/
class AuthBucketPushServiceProvider implements ServiceProviderInterface, ControllerProviderInterface
class AuthBucketPushServiceProvider implements ServiceProviderInterface
{
public function register(Application $app)
{
Expand All @@ -38,8 +37,10 @@ public function register(Application $app)
'gcm' => 'AuthBucket\\Push\\ServiceType\\GcmServiceTypeHandler',
);

$app['authbucket_push.exception_listener'] = $app->share(function () {
return new ExceptionListener();
$app['authbucket_push.exception_listener'] = $app->share(function ($app) {
return new ExceptionListener(
$app['logger']
);
});

$app['authbucket_push.service_handler.factory'] = $app->share(function ($app) {
Expand All @@ -61,24 +62,8 @@ public function register(Application $app)
});
}

public function connect(Application $app)
{
$controllers = $app['controllers_factory'];

$app->post('/api/v1.0/push/register', 'authbucket_push.push_controller:registerAction')
->bind('api_push_register');

$app->post('/api/v1.0/push/unregister', 'authbucket_push.push_controller:unregisterAction')
->bind('api_push_unregister');

$app->post('/api/v1.0/push/send', 'authbucket_push.push_controller:sendAction')
->bind('api_push_send');

return $controllers;
}

public function boot(Application $app)
{
$app['dispatcher']->addListener(KernelEvents::EXCEPTION, array($app['authbucket_push.exception_listener'], 'onKernelException'), -8);
$app['dispatcher']->addSubscriber($app['authbucket_push.exception_listener']);
}
}
6 changes: 3 additions & 3 deletions tests/Controller/PushControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public function testGoodRegisterAction()
'HTTP_Authorization' => 'Bearer 18cdaa6481c0d5f323351ea1029fc065',
);
$client = $this->createClient();
$crawler = $client->request('POST', '/dummy/v1.0/push/register', $parameters, array(), $server);
$crawler = $client->request('POST', '/api/push/register', $parameters, array(), $server);
$this->assertEquals(200, $client->getResponse()->getStatusCode());
$response = json_decode($client->getResponse()->getContent(), true);
$this->assertEquals('0027956241e3ca5090de548fe468334d', $response['device_token']);
Expand All @@ -42,7 +42,7 @@ public function testGoodUnrgisterAction()
'HTTP_Authorization' => 'Bearer 18cdaa6481c0d5f323351ea1029fc065',
);
$client = $this->createClient();
$crawler = $client->request('POST', '/dummy/v1.0/push/unregister', $parameters, array(), $server);
$crawler = $client->request('POST', '/api/push/unregister', $parameters, array(), $server);
$this->assertEquals(200, $client->getResponse()->getStatusCode());
$response = json_decode($client->getResponse()->getContent(), true);
$this->assertEquals('0027956241e3ca5090de548fe468334d', $response['device_token']);
Expand All @@ -57,7 +57,7 @@ public function testGoodSendAction()
'HTTP_Authorization' => 'Bearer 18cdaa6481c0d5f323351ea1029fc065',
);
$client = $this->createClient();
$crawler = $client->request('POST', '/dummy/v1.0/push/send', $parameters, array(), $server);
$crawler = $client->request('POST', '/api/push/send', $parameters, array(), $server);
$this->assertEquals(200, $client->getResponse()->getStatusCode());
$response = json_decode($client->getResponse()->getContent(), true);
$this->assertEquals('6fb577c6fce866c7c06a95c7f9010d85', $response['payload']['alert']);
Expand Down
64 changes: 0 additions & 64 deletions tests/TestBundle/Controller/OAuth2Controller.php

This file was deleted.

16 changes: 8 additions & 8 deletions tests/TestBundle/Resources/config/routing.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@
$app->get('/demo', 'authbucket_push.tests.demo_controller:indexAction')
->bind('demo');

$app->match('/dummy/v1.0/oauth2/debug', 'authbucket_push.tests.oauth2_controller:debugAction')
->bind('dummy_oauth2_debug')
$app->match('/api/oauth2/debug', 'authbucket_push.tests.oauth2_controller:debugAction')
->bind('api_oauth2_debug')
->method('GET|POST');

$app->post('/dummy/v1.0/push/register', 'authbucket_push.push_controller:registerAction')
->bind('dummy_push_register');
$app->post('/api/push/register', 'authbucket_push.push_controller:registerAction')
->bind('api_push_register');

$app->post('/dummy/v1.0/push/unregister', 'authbucket_push.push_controller:unregisterAction')
->bind('dummy_push_unregister');
$app->post('/api/push/unregister', 'authbucket_push.push_controller:unregisterAction')
->bind('api_push_unregister');

$app->post('/dummy/v1.0/push/send', 'authbucket_push.push_controller:sendAction')
->bind('dummy_push_send');
$app->post('/api/push/send', 'authbucket_push.push_controller:sendAction')
->bind('api_push_send');
5 changes: 0 additions & 5 deletions tests/TestBundle/TestBundleServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@

use AuthBucket\Push\Tests\TestBundle\Controller\DefaultController;
use AuthBucket\Push\Tests\TestBundle\Controller\DemoController;
use AuthBucket\Push\Tests\TestBundle\Controller\OAuth2Controller;
use Silex\Application;
use Silex\ServiceProviderInterface;

Expand All @@ -33,10 +32,6 @@ public function register(Application $app)
$app['authbucket_push.tests.demo_controller'] = $app->share(function () {
return new DemoController();
});

$app['authbucket_push.tests.oauth2_controller'] = $app->share(function () {
return new OAuth2Controller();
});
}

public function boot(Application $app)
Expand Down

0 comments on commit 25dc803

Please sign in to comment.