Commit acf60818 authored by Fabien Potencier's avatar Fabien Potencier

added some unit tests

parent 72ac7c5a
...@@ -30,7 +30,7 @@ trait TranslationTrait ...@@ -30,7 +30,7 @@ trait TranslationTrait
*/ */
public function trans($id, array $parameters = array(), $domain = 'messages', $locale = null) public function trans($id, array $parameters = array(), $domain = 'messages', $locale = null)
{ {
return $app['translator']->trans($id, $parameters, $domain, $locale); return $this['translator']->trans($id, $parameters, $domain, $locale);
} }
/** /**
...@@ -46,6 +46,6 @@ trait TranslationTrait ...@@ -46,6 +46,6 @@ trait TranslationTrait
*/ */
public function transChoice($id, $number, array $parameters = array(), $domain = 'messages', $locale = null) public function transChoice($id, $number, array $parameters = array(), $domain = 'messages', $locale = null)
{ {
return $app['translator']->transChoice($id, $number, $parameters, $domain, $locale); return $this['translator']->transChoice($id, $number, $parameters, $domain, $locale);
} }
} }
...@@ -42,7 +42,7 @@ trait TwigTrait ...@@ -42,7 +42,7 @@ trait TwigTrait
if ($response instanceof StreamedResponse) { if ($response instanceof StreamedResponse) {
$response->setCallback(function () use ($twig, $view, $parameters) { $response->setCallback(function () use ($twig, $view, $parameters) {
$this['twig']->display($view, $parameters); $twig->display($view, $parameters);
}); });
} else { } else {
$response->setContent($twig->render($view, $parameters)); $response->setContent($twig->render($view, $parameters));
......
<?php
/*
* This file is part of the Silex framework.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Silex\Tests\Application;
use Silex\Application;
class FormApplication extends Application
{
use Application\FormTrait;
}
<?php
/*
* This file is part of the Silex framework.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Silex\Tests\Application;
use Silex\Application;
use Silex\Provider\FormServiceProvider;
/**
* FormTrait test cases.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class FormTraitTest extends \PHPUnit_Framework_TestCase
{
public function setUp()
{
if (version_compare(phpversion(), '5.4.0', '<')) {
$this->markTestSkipped('PHP 5.4 is required for this test');
}
if (!is_dir(__DIR__.'/../../../../vendor/symfony/form')) {
$this->markTestSkipped('Form dependency was not installed.');
}
}
public function testForm()
{
$this->assertInstanceOf('Symfony\Component\Form\FormBuilder', $this->createApplication()->form());
}
public function createApplication()
{
$app = new FormApplication();
$app->register(new FormServiceProvider());
return $app;
}
}
<?php
/*
* This file is part of the Silex framework.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Silex\Tests\Application;
use Silex\Application;
class MonologApplication extends Application
{
use Application\MonologTrait;
}
<?php
/*
* This file is part of the Silex framework.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Silex\Tests\Application;
use Silex\Application;
use Silex\Provider\MonologServiceProvider;
use Monolog\Handler\TestHandler;
use Monolog\Logger;
/**
* MonologTrait test cases.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class MonologTraitTest extends \PHPUnit_Framework_TestCase
{
public function setUp()
{
if (version_compare(phpversion(), '5.4.0', '<')) {
$this->markTestSkipped('PHP 5.4 is required for this test');
}
if (!is_dir(__DIR__.'/../../../../vendor/monolog/monolog/src')) {
$this->markTestSkipped('Monolog dependency was not installed.');
}
}
public function testLog()
{
$app = $this->createApplication();
$app->log('Foo');
$app->log('Bar', array(), Logger::DEBUG);
$this->assertTrue($app['monolog.handler']->hasInfo('Foo'));
$this->assertTrue($app['monolog.handler']->hasDebug('Bar'));
}
public function createApplication()
{
$app = new MonologApplication();
$app->register(new MonologServiceProvider(), array(
'monolog.handler' => $app->share(function () use ($app) {
return new TestHandler($app['monolog.level']);
}),
));
return $app;
}
}
<?php
/*
* This file is part of the Silex framework.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Silex\Tests\Application;
use Silex\Application;
class SecurityApplication extends Application
{
use Application\SecurityTrait;
}
<?php
/*
* This file is part of the Silex framework.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Silex\Tests\Application;
use Silex\Application;
use Silex\Provider\SecurityServiceProvider;
use Symfony\Component\Security\Core\User\User;
use Symfony\Component\HttpFoundation\Request;
/**
* SecurityTrait test cases.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class SecurityTraitTest extends \PHPUnit_Framework_TestCase
{
public function setUp()
{
if (version_compare(phpversion(), '5.4.0', '<')) {
$this->markTestSkipped('PHP 5.4 is required for this test');
}
if (!is_dir(__DIR__.'/../../../../vendor/symfony/security')) {
$this->markTestSkipped('Security dependency was not installed.');
}
}
public function testUser()
{
$request = Request::create('/');
$app = $this->createApplication();
$app->get('/', function () { return 'foo'; });
$app->handle($request);
$this->assertNull($app->user());
$request->headers->set('PHP_AUTH_USER', 'fabien');
$request->headers->set('PHP_AUTH_PW', 'foo');
$app->handle($request);
$this->assertInstanceOf('Symfony\Component\Security\Core\User\UserInterface', $app->user());
$this->assertEquals('fabien', $app->user()->getUsername());
}
public function testEncodePassword()
{
$app = $this->createApplication();
$user = new User('foo', 'bar');
$this->assertEquals('5FZ2Z8QIkA7UTZ4BYkoC+GsReLf569mSKDsfods6LYQ8t+a8EW9oaircfMpmaLbPBh4FOBiiFyLfuZmTSUwzZg==', $app->encodePassword($user, 'foo'));
}
public function createApplication()
{
$app = new SecurityApplication();
$app->register(new SecurityServiceProvider(), array(
'security.firewalls' => array(
'default' => array(
'http' => true,
'users' => array(
'fabien' => array('ROLE_ADMIN', '5FZ2Z8QIkA7UTZ4BYkoC+GsReLf569mSKDsfods6LYQ8t+a8EW9oaircfMpmaLbPBh4FOBiiFyLfuZmTSUwzZg=='),
),
),
),
));
return $app;
}
}
<?php
/*
* This file is part of the Silex framework.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Silex\Tests\Application;
use Silex\Application;
class SwiftmailerApplication extends Application
{
use Application\SwiftmailerTrait;
}
<?php
/*
* This file is part of the Silex framework.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Silex\Tests\Application;
use Silex\Application;
use Silex\Provider\SwiftmailerServiceProvider;
/**
* SwiftmailerTrait test cases.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class SwiftmailerTraitTest extends \PHPUnit_Framework_TestCase
{
public function setUp()
{
if (version_compare(phpversion(), '5.4.0', '<')) {
$this->markTestSkipped('PHP 5.4 is required for this test');
}
if (!is_dir(__DIR__.'/../../../../vendor/swiftmailer/swiftmailer')) {
$this->markTestSkipped('Swiftmailer dependency was not installed.');
}
}
public function testMail()
{
$app = $this->createApplication();
$message = $this->getMockBuilder('Swift_Message')->disableOriginalConstructor()->getMock();
$app['mailer'] = $mailer = $this->getMockBuilder('Swift_Mailer')->disableOriginalConstructor()->getMock();
$mailer->expects($this->once())
->method('send')
->with($message)
;
$app->mail($message);
}
public function createApplication()
{
$app = new SwiftmailerApplication();
$app->register(new SwiftmailerServiceProvider());
return $app;
}
}
<?php
/*
* This file is part of the Silex framework.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Silex\Tests\Application;
use Silex\Application;
class TranslationApplication extends Application
{
use Application\TranslationTrait;
}
<?php
/*
* This file is part of the Silex framework.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Silex\Tests\Application;
use Silex\Application;
use Silex\Provider\TranslationServiceProvider;
/**
* TranslationTrait test cases.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class TranslationTraitTest extends \PHPUnit_Framework_TestCase
{
public function setUp()
{
if (version_compare(phpversion(), '5.4.0', '<')) {
$this->markTestSkipped('PHP 5.4 is required for this test');
}
if (!is_dir(__DIR__.'/../../../../vendor/symfony/translation')) {
$this->markTestSkipped('Translation dependency was not installed.');
}
}
public function testTrans()
{
$app = $this->createApplication();
$app['translator'] = $translator = $this->getMockBuilder('Symfony\Component\Translation\Translator')->disableOriginalConstructor()->getMock();
$translator->expects($this->once())->method('trans');
$app->trans('foo');
}
public function testTransChoice()
{
$app = $this->createApplication();
$app['translator'] = $translator = $this->getMockBuilder('Symfony\Component\Translation\Translator')->disableOriginalConstructor()->getMock();
$translator->expects($this->once())->method('transChoice');
$app->transChoice('foo', 2);
}
public function createApplication()
{
$app = new TranslationApplication();
$app->register(new TranslationServiceProvider());
return $app;
}
}
<?php
/*
* This file is part of the Silex framework.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Silex\Tests\Application;
use Silex\Application;
class TwigApplication extends Application
{
use Application\TwigTrait;
}
<?php
/*
* This file is part of the Silex framework.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Silex\Tests\Application;
use Silex\Application;
use Silex\Provider\TwigServiceProvider;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpFoundation\StreamedResponse;
/**
* TwigTrait test cases.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class TwigTraitTest extends \PHPUnit_Framework_TestCase
{
public function setUp()
{
if (version_compare(phpversion(), '5.4.0', '<')) {
$this->markTestSkipped('PHP 5.4 is required for this test');
}
if (!is_dir(__DIR__.'/../../../../vendor/twig/twig')) {
$this->markTestSkipped('Twig dependency was not installed.');
}
}
public function testRender()
{
$app = $this->createApplication();
$app['twig'] = $mailer = $this->getMockBuilder('Twig_Environment')->disableOriginalConstructor()->getMock();
$mailer->expects($this->once())->method('render')->will($this->returnValue('foo'));
$response = $app->render('view');
$this->assertEquals('Symfony\Component\HttpFoundation\Response', get_class($response));
$this->assertEquals('foo', $response->getContent());
}
public function testRenderKeepResponse()
{
$app = $this->createApplication();
$app['twig'] = $mailer = $this->getMockBuilder('Twig_Environment')->disableOriginalConstructor()->getMock();
$mailer->expects($this->once())->method('render')->will($this->returnValue('foo'));
$response = $app->render('view', array(), new Response('', 404));
$this->assertEquals(404, $response->getStatusCode());
}
public function testRenderForStream()
{
$app = $this->createApplication();
$app['twig'] = $mailer = $this->getMockBuilder('Twig_Environment')->disableOriginalConstructor()->getMock();
$mailer->expects($this->once())->method('display')->will($this->returnCallback(function () { echo 'foo'; }));
$response = $app->render('view', array(), new StreamedResponse());
$this->assertEquals('Symfony\Component\HttpFoundation\StreamedResponse', get_class($response));
ob_start();
$response->send();
$this->assertEquals('foo', ob_get_clean());
}
public function testRenderView()
{
$app = $this->createApplication();
$app['twig'] = $mailer = $this->getMockBuilder('Twig_Environment')->disableOriginalConstructor()->getMock();
$mailer->expects($this->once())->method('render');
$app->renderView('view');
}
public function createApplication()
{
$app = new TwigApplication();
$app->register(new TwigServiceProvider());
return $app;
}
}
<?php
/*
* This file is part of the Silex framework.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Silex\Tests\Application;
use Silex\Application;
class UrlGeneratorApplication extends Application
{
use Application\UrlGeneratorTrait;
}
<?php
/*
* This file is part of the Silex framework.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Silex\Tests\Application;
use Silex\Application;
use Silex\Provider\UrlGeneratorServiceProvider;
/**
* UrlGeneratorTrait test cases.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class UrlGeneratorTraitTest extends \PHPUnit_Framework_TestCase
{
public function setUp()
{
if (version_compare(phpversion(), '5.4.0', '<')) {
$this->markTestSkipped('PHP 5.4 is required for this test');
}
}
public function testUrl()
{
$app = $this->createApplication();
$app['url_generator'] = $translator = $this->getMockBuilder('Symfony\Component\Routing\Generator\UrlGeneratorInterface')->disableOriginalConstructor()->getMock();
$translator->expects($this->once())->method('generate')->with('foo', array(), true);
$app->url('foo');
}
public function testPath()
{
$app = $this->createApplication();
$app['url_generator'] = $translator = $this->getMockBuilder('Symfony\Component\Routing\Generator\UrlGeneratorInterface')->disableOriginalConstructor()->getMock();
$translator->expects($this->once())->method('generate')->with('foo', array(), false);
$app->path('foo');
}
public function createApplication()
{
$app = new UrlGeneratorApplication();
$app->register(new UrlGeneratorServiceProvider());
return $app;
}
}
<?php
/*
* This file is part of the Silex framework.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Silex\Tests\Route;
use Silex\Route;
class SecurityRoute extends Route
{
use Route\SecurityTrait;
}
<?php
/*
* This file is part of the Silex framework.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/
namespace Silex\Tests\Route;
use Silex\Application;
use Silex\Provider\SecurityServiceProvider;
use Symfony\Component\HttpFoundation\Request;
/**
* SecurityTrait test cases.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class SecurityTraitTest extends \PHPUnit_Framework_TestCase
{
public function setUp()
{
if (version_compare(phpversion(), '5.4.0', '<')) {
$this->markTestSkipped('PHP 5.4 is required for this test');
}
if (!is_dir(__DIR__.'/../../../../vendor/symfony/security')) {
$this->markTestSkipped('Security dependency was not installed.');
}
}
public function testSecure()
{
$app = new Application();
$app['route_class'] = 'Silex\Tests\Route\SecurityRoute';
$app->register(new SecurityServiceProvider(), array(
'security.firewalls' => array(
'default' => array(
'http' => true,
'users' => array(
'fabien' => array('ROLE_ADMIN', '5FZ2Z8QIkA7UTZ4BYkoC+GsReLf569mSKDsfods6LYQ8t+a8EW9oaircfMpmaLbPBh4FOBiiFyLfuZmTSUwzZg=='),
),
),
),
));
$app->get('/', function () { return 'foo'; })
->secure('ROLE_ADMIN')
;
$request = Request::create('/');
$response = $app->handle($request);
$this->assertEquals(401, $response->getStatusCode());
$request = Request::create('/');
$request->headers->set('PHP_AUTH_USER', 'fabien');
$request->headers->set('PHP_AUTH_PW', 'foo');
$response = $app->handle($request);
$this->assertEquals(200, $response->getStatusCode());
}
}
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