Commit fbb5a69d authored by Igor Wiedler's avatar Igor Wiedler

use $app in tests, minor CS fixes

parent a085d9e2
......@@ -12,6 +12,7 @@
namespace Silex;
use Silex\Exception\ControllerFrozenException;
use Symfony\Component\Routing\Route;
/**
......
......@@ -13,6 +13,7 @@ namespace Silex\Extension;
use Silex\Application;
use Silex\ExtensionInterface;
use Symfony\Component\HttpFoundation\SessionStorage\NativeSessionStorage;
use Symfony\Component\HttpFoundation\Session;
use Symfony\Component\HttpKernel\Events as HttpKernelEvents;
......
......@@ -13,6 +13,7 @@ namespace Silex\Extension;
use Silex\Application;
use Silex\ExtensionInterface;
use Symfony\Bridge\Twig\Extension\RoutingExtension;
use Symfony\Bridge\Twig\Extension\TranslationExtension;
......
......@@ -12,6 +12,7 @@
namespace Silex\Tests;
use Silex\Application;
use Symfony\Component\HttpFoundation\Request;
/**
......@@ -23,64 +24,64 @@ class ApplicationTest extends \PHPUnit_Framework_TestCase
{
public function testMatchReturnValue()
{
$application = new Application();
$app = new Application();
$returnValue = $application->match('/foo', function() {});
$returnValue = $app->match('/foo', function() {});
$this->assertInstanceOf('Silex\Controller', $returnValue);
$returnValue = $application->get('/foo', function() {});
$returnValue = $app->get('/foo', function() {});
$this->assertInstanceOf('Silex\Controller', $returnValue);
$returnValue = $application->post('/foo', function() {});
$returnValue = $app->post('/foo', function() {});
$this->assertInstanceOf('Silex\Controller', $returnValue);
$returnValue = $application->put('/foo', function() {});
$returnValue = $app->put('/foo', function() {});
$this->assertInstanceOf('Silex\Controller', $returnValue);
$returnValue = $application->delete('/foo', function() {});
$returnValue = $app->delete('/foo', function() {});
$this->assertInstanceOf('Silex\Controller', $returnValue);
}
public function testGetRequest()
{
$application = new Application();
$app = new Application();
$application->get('/', function() {
$app->get('/', function() {
return 'root';
});
$request = Request::create('/');
$application->handle($request);
$app->handle($request);
$this->assertEquals($request, $application['request']);
$this->assertEquals($request, $app['request']);
}
public function testgetRoutesWithNoRoutes()
{
$application = new Application();
$app = new Application();
$routes = $application['routes'];
$routes = $app['routes'];
$this->assertInstanceOf('Symfony\Component\Routing\RouteCollection', $routes);
$this->assertEquals(0, count($routes->all()));
}
public function testgetRoutesWithRoutes()
{
$application = new Application();
$app = new Application();
$application->get('/foo', function() {
$app->get('/foo', function() {
return 'foo';
});
$application->get('/bar', function() {
$app->get('/bar', function() {
return 'bar';
});
$routes = $application['routes'];
$routes = $app['routes'];
$this->assertInstanceOf('Symfony\Component\Routing\RouteCollection', $routes);
$this->assertEquals(0, count($routes->all()));
$application->flush();
$app->flush();
$this->assertEquals(2, count($routes->all()));
}
}
......@@ -29,25 +29,25 @@ class BeforeAfterFilterTest extends \PHPUnit_Framework_TestCase
$i = 0;
$test = $this;
$application = new Application();
$app = new Application();
$application->before(function() use(&$i, $test) {
$app->before(function() use (&$i, $test) {
$test->assertEquals(0, $i);
$i++;
});
$application->match('/foo', function() use(&$i, $test) {
$app->match('/foo', function() use (&$i, $test) {
$test->assertEquals(1, $i);
$i++;
});
$application->after(function() use(&$i, $test) {
$app->after(function() use (&$i, $test) {
$test->assertEquals(2, $i);
$i++;
});
$request = Request::create('/foo');
$application->handle($request);
$app->handle($request);
$this->assertEquals(3, $i);
}
......@@ -56,19 +56,19 @@ class BeforeAfterFilterTest extends \PHPUnit_Framework_TestCase
{
$i = 0;
$application = new Application();
$app = new Application();
$application->match('/foo', function() use (&$i) {
$app->match('/foo', function() use (&$i) {
$i++;
return new Response('foo');
});
$application->after(function() use(&$i) {
$app->after(function() use (&$i) {
$i++;
});
$request = Request::create('/foo');
$application->handle($request);
$app->handle($request);
$this->assertEquals(2, $i);
}
......@@ -78,35 +78,35 @@ class BeforeAfterFilterTest extends \PHPUnit_Framework_TestCase
$i = 0;
$test = $this;
$application = new Application();
$app = new Application();
$application->before(function() use(&$i, $test) {
$app->before(function() use (&$i, $test) {
$test->assertEquals(0, $i);
$i++;
});
$application->before(function() use(&$i, $test) {
$app->before(function() use (&$i, $test) {
$test->assertEquals(1, $i);
$i++;
});
$application->match('/foo', function() use(&$i, $test) {
$app->match('/foo', function() use (&$i, $test) {
$test->assertEquals(2, $i);
$i++;
});
$application->after(function() use(&$i, $test) {
$app->after(function() use (&$i, $test) {
$test->assertEquals(3, $i);
$i++;
});
$application->after(function() use(&$i, $test) {
$app->after(function() use (&$i, $test) {
$test->assertEquals(4, $i);
$i++;
});
$request = Request::create('/foo');
$application->handle($request);
$app->handle($request);
$this->assertEquals(5, $i);
}
......@@ -115,26 +115,26 @@ class BeforeAfterFilterTest extends \PHPUnit_Framework_TestCase
{
$i = 0;
$application = new Application();
$app = new Application();
$application->before(function() use(&$i) {
$app->before(function() use (&$i) {
$i++;
});
$application->match('/foo', function() {
$app->match('/foo', function() {
throw new \RuntimeException();
});
$application->after(function() use(&$i) {
$app->after(function() use (&$i) {
$i++;
});
$application->error(function() {
$app->error(function() {
return 'error handled';
});
$request = Request::create('/foo');
$application->handle($request);
$app->handle($request);
$this->assertEquals(2, $i);
}
......@@ -143,22 +143,22 @@ class BeforeAfterFilterTest extends \PHPUnit_Framework_TestCase
{
$i = 0;
$application = new Application();
$app = new Application();
$application->before(function() use(&$i) {
$app->before(function() use (&$i) {
$i++;
});
$application->after(function() use(&$i) {
$app->after(function() use (&$i) {
$i++;
});
$application->error(function() {
$app->error(function() {
return 'error handled';
});
$request = Request::create('/nowhere');
$application->handle($request);
$app->handle($request);
$this->assertEquals(2, $i);
}
......
......@@ -15,6 +15,7 @@ use Silex\Application;
use Silex\Controller;
use Silex\ControllerCollection;
use Silex\Exception\ControllerFrozenException;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
......
......@@ -12,6 +12,7 @@
namespace Silex\Tests;
use Silex\Controller;
use Symfony\Component\Routing\Route;
/**
......
......@@ -25,15 +25,15 @@ class ErrorHandlerTest extends \PHPUnit_Framework_TestCase
{
public function testNoErrorHandler()
{
$application = new Application();
$app = new Application();
$application->match('/foo', function() {
$app->match('/foo', function() {
throw new \RuntimeException('foo exception');
});
try {
$request = Request::create('/foo');
$application->handle($request);
$app->handle($request);
$this->fail('->handle() should not catch exceptions where no error handler was supplied');
} catch (\RuntimeException $e) {
$this->assertEquals('foo exception', $e->getMessage());
......@@ -42,71 +42,71 @@ class ErrorHandlerTest extends \PHPUnit_Framework_TestCase
public function testOneErrorHandler()
{
$application = new Application();
$app = new Application();
$application->match('/foo', function() {
$app->match('/foo', function() {
throw new \RuntimeException('foo exception');
});
$application->error(function($e) {
$app->error(function($e) {
return new Response('foo exception handler');
});
$request = Request::create('/foo');
$this->checkRouteResponse($application, '/foo', 'foo exception handler');
$this->checkRouteResponse($app, '/foo', 'foo exception handler');
}
public function testMultipleErrorHandlers()
{
$application = new Application();
$app = new Application();
$application->match('/foo', function() {
$app->match('/foo', function() {
throw new \RuntimeException('foo exception');
});
$errors = 0;
$application->error(function($e) use (&$errors) {
$app->error(function($e) use (&$errors) {
$errors++;
});
$application->error(function($e) use (&$errors) {
$app->error(function($e) use (&$errors) {
$errors++;
});
$application->error(function($e) use (&$errors) {
$app->error(function($e) use (&$errors) {
$errors++;
return new Response('foo exception handler');
});
$application->error(function($e) use (&$errors) {
$app->error(function($e) use (&$errors) {
// should not execute
$errors++;
});
$request = Request::create('/foo');
$this->checkRouteResponse($application, '/foo', 'foo exception handler', 'should return the first response returned by an exception handler');
$this->checkRouteResponse($app, '/foo', 'foo exception handler', 'should return the first response returned by an exception handler');
$this->assertEquals(3, $errors, 'should execute error handlers until a response is returned');
}
public function testNoResponseErrorHandler()
{
$application = new Application();
$app = new Application();
$application->match('/foo', function() {
$app->match('/foo', function() {
throw new \RuntimeException('foo exception');
});
$errors = 0;
$application->error(function($e) use (&$errors) {
$app->error(function($e) use (&$errors) {
$errors++;
});
try {
$request = Request::create('/foo');
$application->handle($request);
$app->handle($request);
$this->fail('->handle() should not catch exceptions where an empty error handler was supplied');
} catch (\RuntimeException $e) {
$this->assertEquals('foo exception', $e->getMessage());
......@@ -117,45 +117,45 @@ class ErrorHandlerTest extends \PHPUnit_Framework_TestCase
public function testStringResponseErrorHandler()
{
$application = new Application();
$app = new Application();
$application->match('/foo', function() {
$app->match('/foo', function() {
throw new \RuntimeException('foo exception');
});
$application->error(function($e) {
$app->error(function($e) {
return 'foo exception handler';
});
$request = Request::create('/foo');
$this->checkRouteResponse($application, '/foo', 'foo exception handler', 'should accept a string response from the error handler');
$this->checkRouteResponse($app, '/foo', 'foo exception handler', 'should accept a string response from the error handler');
}
public function testErrorHandlerException()
{
$application = new Application();
$app = new Application();
$application->match('/foo', function() {
$app->match('/foo', function() {
throw new \RuntimeException('foo exception');
});
$application->error(function($e) {
$app->error(function($e) {
throw new \RuntimeException('foo exception handler exception');
});
try {
$request = Request::create('/foo');
$this->checkRouteResponse($application, '/foo', 'foo exception handler', 'should accept a string response from the error handler');
$this->checkRouteResponse($app, '/foo', 'foo exception handler', 'should accept a string response from the error handler');
$this->fail('->handle() should not catch exceptions thrown from an error handler');
} catch (\RuntimeException $e) {
$this->assertEquals('foo exception handler exception', $e->getMessage());
}
}
protected function checkRouteResponse($application, $path, $expectedContent, $method = 'get', $message = null)
protected function checkRouteResponse($app, $path, $expectedContent, $method = 'get', $message = null)
{
$request = Request::create($path, $method);
$response = $application->handle($request);
$response = $app->handle($request);
$this->assertEquals($expectedContent, $response->getContent(), $message);
}
}
......@@ -22,20 +22,20 @@ class FunctionalTest extends \PHPUnit_Framework_TestCase
{
public function testBind()
{
$application = new Application();
$app = new Application();
$application->get('/', function() {
$app->get('/', function() {
return 'hello';
})
->bind('homepage');
$application->get('/foo', function() {
$app->get('/foo', function() {
return 'foo';
})
->bind('foo_abc');
$application->flush();
$routes = $application['routes'];
$app->flush();
$routes = $app['routes'];
$this->assertInstanceOf('Symfony\Component\Routing\Route', $routes->get('homepage'));
$this->assertInstanceOf('Symfony\Component\Routing\Route', $routes->get('foo_abc'));
}
......
......@@ -27,72 +27,72 @@ class RouterTest extends \PHPUnit_Framework_TestCase
{
public function testMapRouting()
{
$application = new Application();
$app = new Application();
$application->match('/foo', function() {
$app->match('/foo', function() {
return 'foo';
});
$application->match('/bar', function() {
$app->match('/bar', function() {
return 'bar';
});
$application->match('/', function() {
$app->match('/', function() {
return 'root';
});
$this->checkRouteResponse($application, '/foo', 'foo');
$this->checkRouteResponse($application, '/bar', 'bar');
$this->checkRouteResponse($application, '/', 'root');
$this->checkRouteResponse($app, '/foo', 'foo');
$this->checkRouteResponse($app, '/bar', 'bar');
$this->checkRouteResponse($app, '/', 'root');
}
public function testStatusCode()
{
$application = new Application();
$app = new Application();
$application->put('/created', function() {
$app->put('/created', function() {
return new Response('', 201);
});
$application->match('/forbidden', function() {
$app->match('/forbidden', function() {
return new Response('', 403);
});
$application->match('/not_found', function() {
$app->match('/not_found', function() {
return new Response('', 404);
});
$request = Request::create('/created', 'put');
$response = $application->handle($request);
$response = $app->handle($request);
$this->assertEquals(201, $response->getStatusCode());
$request = Request::create('/forbidden');
$response = $application->handle($request);
$response = $app->handle($request);
$this->assertEquals(403, $response->getStatusCode());
$request = Request::create('/not_found');
$response = $application->handle($request);
$response = $app->handle($request);
$this->assertEquals(404, $response->getStatusCode());
}
public function testRedirect()
{
$application = new Application();
$app = new Application();
$application->match('/redirect', function() {
$app->match('/redirect', function() {
return new RedirectResponse('/target');
});
$application->match('/redirect2', function() use ($application) {
return $application->redirect('/target2');
$app->match('/redirect2', function() use ($app) {
return $app->redirect('/target2');
});
$request = Request::create('/redirect');
$response = $application->handle($request);
$response = $app->handle($request);
$this->assertTrue($response->isRedirected('/target'));
$request = Request::create('/redirect2');
$response = $application->handle($request);
$response = $app->handle($request);
$this->assertTrue($response->isRedirected('/target2'));
}
......@@ -101,68 +101,68 @@ class RouterTest extends \PHPUnit_Framework_TestCase
*/
public function testMissingRoute()
{
$application = new Application();
$app = new Application();
$request = Request::create('/baz');
$application->handle($request);
$app->handle($request);
}
public function testMethodRouting()
{
$application = new Application();
$app = new Application();
$application->match('/foo', function() {
$app->match('/foo', function() {
return 'foo';
});
$application->match('/bar', function() {
$app->match('/bar', function() {
return 'bar';
}, 'GET|POST');
$application->get('/resource', function() {
$app->get('/resource', function() {
return 'get resource';
});
$application->post('/resource', function() {
$app->post('/resource', function() {
return 'post resource';
});
$application->put('/resource', function() {
$app->put('/resource', function() {
return 'put resource';
});
$application->delete('/resource', function() {
$app->delete('/resource', function() {
return 'delete resource';
});
$this->checkRouteResponse($application, '/foo', 'foo');
$this->checkRouteResponse($application, '/bar', 'bar');
$this->checkRouteResponse($application, '/bar', 'bar', 'post');
$this->checkRouteResponse($application, '/resource', 'get resource');
$this->checkRouteResponse($application, '/resource', 'post resource', 'post');
$this->checkRouteResponse($application, '/resource', 'put resource', 'put');
$this->checkRouteResponse($application, '/resource', 'delete resource', 'delete');
$this->checkRouteResponse($app, '/foo', 'foo');
$this->checkRouteResponse($app, '/bar', 'bar');
$this->checkRouteResponse($app, '/bar', 'bar', 'post');
$this->checkRouteResponse($app, '/resource', 'get resource');
$this->checkRouteResponse($app, '/resource', 'post resource', 'post');
$this->checkRouteResponse($app, '/resource', 'put resource', 'put');
$this->checkRouteResponse($app, '/resource', 'delete resource', 'delete');
}
public function testRequestShouldBeStoredRegardlessOfRouting() {
$application = new Application();
$application->get('/foo', function() use ($application) {
return new Response($application['request']->getRequestUri());
$app = new Application();
$app->get('/foo', function() use ($app) {
return new Response($app['request']->getRequestUri());
});
$application->error(function($e) use ($application) {
return new Response($application['request']->getRequestUri());
$app->error(function($e) use ($app) {
return new Response($app['request']->getRequestUri());
});
foreach(array('/foo', '/bar') as $path) {
$request = Request::create($path);
$response = $application->handle($request);
$response = $app->handle($request);
$this->assertContains($path, $response->getContent());
}
}
protected function checkRouteResponse($application, $path, $expectedContent, $method = 'get', $message = null)
protected function checkRouteResponse($app, $path, $expectedContent, $method = 'get', $message = null)
{
$request = Request::create($path, $method);
$response = $application->handle($request);
$response = $app->handle($request);
$this->assertEquals($expectedContent, $response->getContent(), $message);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment