Updated to Symfony 2.1 BETA3

This commit is contained in:
Polonkai Gergely
2012-07-16 21:40:19 +02:00
parent 7a06301624
commit 9d0d2ce524
1551 changed files with 157774 additions and 5177 deletions

View File

@@ -31,7 +31,7 @@ class ScriptHandler
return;
}
static::executeBuildBootstrap($appDir);
static::executeBuildBootstrap($appDir, $options['process-timeout']);
}
public static function clearCache($event)
@@ -45,7 +45,7 @@ class ScriptHandler
return;
}
static::executeCommand($event, $appDir, 'cache:clear --no-warmup');
static::executeCommand($event, $appDir, 'cache:clear --no-warmup', $options['process-timeout']);
}
public static function installAssets($event)
@@ -123,7 +123,7 @@ namespace { return \$loader; }
", substr(file_get_contents($file), 5)));
}
protected static function executeCommand($event, $appDir, $cmd)
protected static function executeCommand($event, $appDir, $cmd, $timeout = 300)
{
$php = escapeshellarg(self::getPhp());
$console = escapeshellarg($appDir.'/console');
@@ -131,17 +131,17 @@ namespace { return \$loader; }
$console.= ' --ansi';
}
$process = new Process($php.' '.$console.' '.$cmd, null, null, null, 300);
$process = new Process($php.' '.$console.' '.$cmd, null, null, null, $timeout);
$process->run(function ($type, $buffer) { echo $buffer; });
}
protected static function executeBuildBootstrap($appDir)
protected static function executeBuildBootstrap($appDir, $timeout = 300)
{
$php = escapeshellarg(self::getPhp());
$cmd = escapeshellarg(__DIR__.'/../Resources/bin/build_bootstrap.php');
$appDir = escapeshellarg($appDir);
$process = new Process($php.' '.$cmd.' '.$appDir, null, null, null, 300);
$process = new Process($php.' '.$cmd.' '.$appDir, null, null, null, $timeout);
$process->run(function ($type, $buffer) { echo $buffer; });
}
@@ -155,6 +155,8 @@ namespace { return \$loader; }
$options['symfony-assets-install'] = getenv('SYMFONY_ASSETS_INSTALL') ?: $options['symfony-assets-install'];
$options['process-timeout'] = $event->getComposer()->getConfig()->get('process-timeout');
return $options;
}

View File

@@ -1,91 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sensio\Bundle\DistributionBundle\Diff;
/**
* Computes a Diff between files (line by line).
*
* Implements the Longest common subsequence problem algorithm.
*
* @see http://en.wikipedia.org/wiki/Longest_common_subsequence_problem
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class Diff
{
private $diff;
public function __construct($str1, $str2)
{
$lines1 = explode("\n", $str1);
$lines2 = explode("\n", $str2);
$this->diff = $this->computeDiff($this->computeLcs($lines1, $lines2), $lines1, $lines2, count($lines1) - 1, count($lines2) - 1);
}
public function getDiff()
{
$diff = array();
for ($i = 0, $max = count($this->diff); $i < $max; $i++) {
if ('' != $this->diff[$i][0]) {
$diff[] = array('@', sprintf(' Line %s', $this->diff[$i][2]));
do {
$diff[] = $this->diff[$i++];
} while ('' != $this->diff[$i][0]);
}
}
return $diff;
}
public function computeDiff(array $c, array $lines1, array $lines2, $i, $j)
{
$diff = array();
if ($i > -1 && $j > -1 && $lines1[$i] == $lines2[$j]) {
$diff = array_merge($diff, $this->computeDiff($c, $lines1, $lines2, $i - 1, $j - 1));
$diff[] = array('', $lines1[$i], $i, $j);
} else {
if ($j > -1 && ($i == -1 || $c[$i][$j - 1] >= $c[$i - 1][$j])) {
$diff = array_merge($diff, $this->computeDiff($c, $lines1, $lines2, $i, $j - 1));
$diff[] = array('+', $lines2[$j], $i, $j);
} elseif ($i > -1 && ($j == -1 || $c[$i][$j - 1] < $c[$i - 1][$j])) {
$diff = array_merge($diff, $this->computeDiff($c, $lines1, $lines2, $i - 1, $j));
$diff[] = array('-', $lines1[$i], $i, $j);
}
}
return $diff;
}
private function computeLcs(array $lines1, array $lines2)
{
for ($i = -1, $len1 = count($lines1); $i < $len1; $i++) {
for ($j = -1, $len2 = count($lines2); $j < $len2; $j++) {
$c[$i][$j] = 0;
}
}
for ($i = 0; $i < $len1; $i++) {
for ($j = 0; $j < $len2; $j++) {
if ($lines1[$i] == $lines2[$j]) {
$c[$i][$j] = $c[$i - 1][$j - 1] + 1;
} else {
$c[$i][$j] = max($c[$i][$j - 1], $c[$i - 1][$j]);
}
}
}
return $c;
}
}

View File

@@ -429,11 +429,11 @@ class SymfonyRequirements extends RequirementCollection
);
if (version_compare($installedPhpVersion, self::REQUIRED_PHP_VERSION, '>=')) {
$this->addRequirement(
(in_array(date_default_timezone_get(), DateTimeZone::listIdentifiers())),
sprintf('Default timezone is deprecated (%s)', date_default_timezone_get()),
'Fix your <strong>php.ini</strong> file (list of deprecated timezones http://us.php.net/manual/en/timezones.others.php).'
);
$this->addRequirement(
(in_array(date_default_timezone_get(), DateTimeZone::listIdentifiers())),
sprintf('Default timezone "%s" is not supported by your installation of PHP', date_default_timezone_get()),
'Fix your <strong>php.ini</strong> file (check for typos and have a look at the list of deprecated timezones http://php.net/manual/en/timezones.others.php).'
);
}
$this->addRequirement(
@@ -492,6 +492,12 @@ class SymfonyRequirements extends RequirementCollection
/* optional recommendations follow */
$this->addRecommendation(
version_compare($installedPhpVersion, '5.3.4', '>='),
sprintf('Your project might not work properly ("Notice: Trying to get property of non-object") due to the PHP bug #52083 before PHP 5.3.4 (%s installed)', $installedPhpVersion),
'Install PHP 5.3.4 or newer'
);
$this->addRecommendation(
version_compare($installedPhpVersion, '5.3.8', '>='),
sprintf('Annotations might not work properly due to the PHP bug #55156 before PHP 5.3.8 (%s installed)', $installedPhpVersion),

View File

@@ -1,59 +0,0 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sensio\Bundle\DistributionBundle\Upgrade;
use Sensio\Bundle\DistributionBundle\Diff\Diff;
use Symfony\Component\Console\Output\OutputInterface;
/**
* Upgrade class.
*
* @author Fabien Potencier <fabien@symfony.com>
*/
class Upgrade
{
public function outputConsoleDiff(OutputInterface $output, $file1, $file2)
{
if (is_file($file1)) {
$file1 = realpath($file1);
$str1 = file_get_contents($file1);
} else {
$str1 = '';
}
if (!is_file($file2)) {
throw new \RuntimeException(sprintf('The skeleton file "%s" does not exist.', $file2));
}
$file2 = realpath($file2);
$str2 = file_get_contents($file2);
$diff = new Diff($str1, $str2);
$output->writeln(sprintf('--- %s', $file1));
$output->writeln(sprintf('+++ %s', $file2));
foreach ($diff->getDiff() as $line) {
if ('+' == $line[0]) {
$format = '<fg=green>+%s</>';
} elseif ('-' == $line[0]) {
$format = '<fg=red>-%s</>';
} elseif ('@' == $line[0]) {
$format = '<fg=cyan>@%s</>';
} else {
$format = ' %s';
}
$output->writeln(sprintf($format, $line[1]));
}
$output->writeln('');
}
}

View File

@@ -0,0 +1,4 @@
coverage
phpunit.xml
vendor
composer.lock

View File

@@ -0,0 +1,6 @@
CHANGELOG for 2.1.x
===================
* added the possibility to configure the id name for the Doctrine converter via the id option
* [BC break] The ParamConverterInterface::apply() method now must return a
Boolean value indicating if a conversion was done.

View File

@@ -74,6 +74,19 @@ option::
{
}
This also allows you to have multiple converters in one action::
/**
* @Route("/blog/{id}/comments/{comment_id}")
* @ParamConverter("comment", class="SensioBlogBundle:Comment", options={"id" = "comment_id"})
*/
public function showAction(Post $post, Comment $comment)
{
}
In the example above, the post parameter is handled automatically, but the comment is
configured with the annotation since they can not both follow the default convention.
Creating a Converter
--------------------

View File

@@ -0,0 +1,18 @@
<?php
namespace Sensio\Bundle\FrameworkExtraBundle\Tests\Configuration;
class ConfigurationAnnotationTest extends \PHPUnit_Framework_TestCase
{
/**
* @expectedException RuntimeException
*/
public function testUndefinedSetterThrowsException()
{
$this->getMockForAbstractClass('Sensio\Bundle\FrameworkExtraBundle\Configuration\ConfigurationAnnotation', array(
array(
'doesNotExists' => true,
),
));
}
}

View File

@@ -0,0 +1,101 @@
<?php
namespace Sensio\Bundle\FrameworkExtraBundle\Tests\EventListener;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
use Sensio\Bundle\FrameworkExtraBundle\EventListener\CacheListener;
class CacheListenerTest extends \PHPUnit_Framework_TestCase
{
public function setUp()
{
$this->listener = new CacheListener();
$this->response = new Response();
$this->cache = new Cache(array());
$this->request = $this->createRequest($this->cache);
$this->event = $this->createEventMock($this->request, $this->response);
}
public function testWontReassignResponseWhenResponseIsUnsuccessful()
{
$this->event
->expects($this->never())
->method('setResponse')
;
$this->response->setStatusCode(404);
$this->assertInternalType('null', $this->listener->onKernelResponse($this->event));
}
public function testWontReassignResponseWhenNoConfigurationIsPresent()
{
$this->event
->expects($this->never())
->method('setResponse')
;
$this->request->attributes->remove('_cache');
$this->assertInternalType('null', $this->listener->onKernelResponse($this->event));
}
public function testResponseIsPublicIfConfigurationIsPublic()
{
$request = $this->createRequest(new Cache(array(
'public' => true,
)));
$this->listener->onKernelResponse($this->createEventMock($request, $this->response));
$this->assertTrue($this->response->headers->hasCacheControlDirective('public'));
$this->assertFalse($this->response->headers->hasCacheControlDirective('private'));
}
public function testConfigurationAttributesAreSetOnResponse()
{
$this->assertInternalType('null', $this->response->getMaxAge());
$this->assertInternalType('null', $this->response->getExpires());
$this->assertFalse($this->response->headers->hasCacheControlDirective('s-maxage'));
$this->request->attributes->set('_cache', new Cache(array(
'expires' => 'tomorrow',
'smaxage' => '15',
'maxage' => '15',
)));
$this->listener->onKernelResponse($this->event);
$this->assertEquals('15', $this->response->getMaxAge());
$this->assertEquals('15', $this->response->headers->getCacheControlDirective('s-maxage'));
$this->assertInstanceOf('DateTime', $this->response->getExpires());
}
protected function createRequest(Cache $cache = null)
{
return new Request(array(), array(), array(
'_cache' => $cache,
));
}
protected function createEventMock(Request $request, Response $response)
{
$event = $this->getMock('Symfony\Component\HttpKernel\Event\FilterResponseEvent', array(), array(), '', null);
$event
->expects($this->any())
->method('getRequest')
->will($this->returnValue($request))
;
$event
->expects($this->any())
->method('getResponse')
->will($this->returnValue($response))
;
return $event;
}
}

View File

@@ -0,0 +1,85 @@
<?php
namespace Sensio\Bundle\FrameworkExtraBundle\Tests\EventListener;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
use Sensio\Bundle\FrameworkExtraBundle\EventListener\ControllerListener;
use Sensio\Bundle\FrameworkExtraBundle\Tests\EventListener\Fixture\FooControllerCacheAtClass;
use Sensio\Bundle\FrameworkExtraBundle\Tests\EventListener\Fixture\FooControllerCacheAtClassAndMethod;
use Sensio\Bundle\FrameworkExtraBundle\Tests\EventListener\Fixture\FooControllerCacheAtMethod;
use Doctrine\Common\Annotations\AnnotationReader;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Event\FilterControllerEvent;
use Symfony\Component\HttpKernel\HttpKernelInterface;
class ControllerListenerTest extends \PHPUnit_Framework_TestCase
{
public function setUp()
{
$this->listener = new ControllerListener(new AnnotationReader());
$this->request = $this->createRequest();
}
public function tearDown()
{
$this->listener = null;
$this->request = null;
}
public function testCacheAnnotationAtMethod()
{
$controller = new FooControllerCacheAtMethod();
$this->event = $this->getFilterControllerEvent(array($controller, 'barAction'), $this->request);
$this->listener->onKernelController($this->event);
$this->assertNotNull($this->getReadedCache());
$this->assertEquals(FooControllerCacheAtMethod::METHOD_SMAXAGE, $this->getReadedCache()->getSMaxAge());
}
public function testCacheAnnotationAtClass()
{
$controller = new FooControllerCacheAtClass();
$this->event = $this->getFilterControllerEvent(array($controller, 'barAction'), $this->request);
$this->listener->onKernelController($this->event);
$this->assertNotNull($this->getReadedCache());
$this->assertEquals(FooControllerCacheAtClass::CLASS_SMAXAGE, $this->getReadedCache()->getSMaxAge());
}
public function testCacheAnnotationAtClassAndMethod()
{
$controller = new FooControllerCacheAtClassAndMethod();
$this->event = $this->getFilterControllerEvent(array($controller, 'barAction'), $this->request);
$this->listener->onKernelController($this->event);
$this->assertNotNull($this->getReadedCache());
$this->assertEquals(FooControllerCacheAtClassAndMethod::METHOD_SMAXAGE, $this->getReadedCache()->getSMaxAge());
$this->event = $this->getFilterControllerEvent(array($controller, 'bar2Action'), $this->request);
$this->listener->onKernelController($this->event);
$this->assertNotNull($this->getReadedCache());
$this->assertEquals(FooControllerCacheAtClassAndMethod::CLASS_SMAXAGE, $this->getReadedCache()->getSMaxAge());
}
protected function createRequest(Cache $cache = null)
{
return new Request(array(), array(), array(
'_cache' => $cache,
));
}
protected function getFilterControllerEvent($controller, Request $request)
{
$mockKernel = $this->getMockForAbstractClass('Symfony\Component\HttpKernel\Kernel', array('', ''));
return new FilterControllerEvent($mockKernel, $controller, $request, HttpKernelInterface::MASTER_REQUEST);
}
protected function getReadedCache()
{
return $this->request->attributes->get('_cache');
}
}

View File

@@ -0,0 +1,17 @@
<?php
namespace Sensio\Bundle\FrameworkExtraBundle\Tests\EventListener\Fixture;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
/**
* @Cache(smaxage="20")
*/
class FooControllerCacheAtClass
{
const CLASS_SMAXAGE = 20;
public function barAction()
{
}
}

View File

@@ -0,0 +1,25 @@
<?php
namespace Sensio\Bundle\FrameworkExtraBundle\Tests\EventListener\Fixture;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
/**
* @Cache(smaxage="20")
*/
class FooControllerCacheAtClassAndMethod
{
const CLASS_SMAXAGE = 20;
const METHOD_SMAXAGE = 15;
/**
* @Cache(smaxage="15")
*/
public function barAction()
{
}
public function bar2Action()
{
}
}

View File

@@ -0,0 +1,17 @@
<?php
namespace Sensio\Bundle\FrameworkExtraBundle\Tests\EventListener\Fixture;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Cache;
class FooControllerCacheAtMethod
{
const METHOD_SMAXAGE = 15;
/**
* @Cache(smaxage="15")
*/
public function barAction()
{
}
}

View File

@@ -0,0 +1,87 @@
<?php
namespace Sensio\Bundle\FrameworkExtraBundle\Tests\Request\ParamConverter;
use Symfony\Component\HttpFoundation\Request;
use Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\DoctrineParamConverter;
class DoctrineParamConverterTest extends \PHPUnit_Framework_TestCase
{
/**
* @var Doctrine\Common\Persistence\ManagerRegistry
*/
private $manager;
/**
* @var DoctrineParamConverter
*/
private $converter;
public function setUp()
{
if (!interface_exists('Doctrine\Common\Persistence\ManagerRegistry')) {
$this->markTestSkipped();
}
$this->manager = $this->getMock('Doctrine\Common\Persistence\ManagerRegistry');
$this->converter = new DoctrineParamConverter($this->manager);
}
public function createConfiguration($class = null, array $options = null)
{
$config = $this->getMock(
'Sensio\Bundle\FrameworkExtraBundle\Configuration\ConfigurationInterface', array(
'getClass', 'getAliasName', 'getOptions'
));
if ($options !== null) {
$config->expects($this->once())
->method('getOptions')
->will($this->returnValue($options));
}
if ($class !== null) {
$config->expects($this->any())
->method('getClass')
->will($this->returnValue($class));
}
return $config;
}
public function testApplyWithNoIdAndData()
{
$request = new Request();
$config = $this->createConfiguration(null, array());
$objectManager = $this->getMock('Doctrine\Common\Persistence\ObjectManager');
$this->manager->expects($this->never())->method('find');
$this->manager->expects($this->once())
->method('getManager')
->will($this->returnValue($objectManager));
$this->setExpectedException('LogicException');
$this->converter->apply($request, $config);
}
public function testSupports()
{
$config = $this->createConfiguration('stdClass', array());
$metadataFactory = $this->getMock('Doctrine\Common\Persistence\Mapping\ClassMetadataFactory');
$metadataFactory->expects($this->once())
->method('isTransient')
->with($this->equalTo('stdClass'))
->will($this->returnValue( false ));
$objectManager = $this->getMock('Doctrine\Common\Persistence\ObjectManager');
$objectManager->expects($this->once())
->method('getMetadataFactory')
->will($this->returnValue($metadataFactory));
$this->manager->expects($this->once())
->method('getManager')
->will($this->returnValue($objectManager));
$ret = $this->converter->supports($config);
$this->assertTrue($ret, "Should be supported");
}
}

View File

@@ -0,0 +1,99 @@
<?php
namespace Sensio\Bundle\FrameworkExtraBundle\Tests\Request\ParamConverter;
use Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\ParamConverterManager;
use Sensio\Bundle\FrameworkExtraBundle\Configuration;
use Symfony\Component\HttpFoundation\Request;
class ParamConverterManagerTest extends \PHPUnit_Framework_TestCase
{
public function setUp()
{
$this->manager = new ParamConverterManager();
}
public function testPriorities()
{
$this->assertEquals(array(), $this->manager->all());
$high = $this->createParamConverterMock();
$low = $this->createParamConverterMock();
$this->manager->add($low);
$this->manager->add($high, 10);
$this->assertEquals(array(
$high,
$low,
), $this->manager->all());
}
public function testApply()
{
$supported = $this->createParamConverterMock();
$supported
->expects($this->once())
->method('supports')
->will($this->returnValue(true))
;
$supported
->expects($this->once())
->method('apply')
->will($this->returnValue(false))
;
$invalid = $this->createParamConverterMock();
$invalid
->expects($this->once())
->method('supports')
->will($this->returnValue(false))
;
$invalid
->expects($this->never())
->method('apply')
;
$configurations = array(
new Configuration\ParamConverter(array(
'name' => 'var',
)),
);
$this->manager->add($supported);
$this->manager->add($invalid);
$this->manager->apply(new Request(), $configurations);
}
public function testApplyNotCalledOnAlreadyConvertedObjects()
{
$converter = $this->createParamConverterMock();
$converter
->expects($this->never())
->method('supports')
;
$converter
->expects($this->never())
->method('apply')
;
$this->manager->add($converter);
$request = new Request();
$request->attributes->set('converted', new \stdClass);
$configuration = new Configuration\ParamConverter(array(
'name' => 'converted',
'class' => 'stdClass',
));
$this->manager->apply($request, array($configuration));
}
protected function createParamConverterMock()
{
return $this->getMock('Sensio\Bundle\FrameworkExtraBundle\Request\ParamConverter\ParamConverterInterface');
}
}

View File

@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="./vendor/autoload.php" colors="true">
<testsuites>
<testsuite name="SensioFrameworkExtraBundle">
<directory suffix="Test.php">./Tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory>./</directory>
<exclude>
<directory>./Resources</directory>
<directory>./Tests</directory>
<directory>./vendor</directory>
</exclude>
</whitelist>
</filter>
</phpunit>

View File

@@ -0,0 +1,3 @@
phpunit.xml
vendor
composer.lock

View File

@@ -55,7 +55,7 @@ conventions):
<info>php app/console generate:bundle --namespace=Acme/BlogBundle</info>
Note that you can use <comment>/</comment> instead of <comment>\\</comment> for the namespace delimiter to avoid any
Note that you can use <comment>/</comment> instead of <comment>\\ </comment>for the namespace delimiter to avoid any
problem.
If you want to disable any user interaction, use <comment>--no-interaction</comment> but don't forget to pass all needed options:

View File

@@ -0,0 +1,101 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sensio\Bundle\GeneratorBundle\Tests\Command;
use Symfony\Component\Console\Tester\CommandTester;
use Sensio\Bundle\GeneratorBundle\Command\GenerateBundleCommand;
class GenerateBundleCommandTest extends GenerateCommandTest
{
/**
* @dataProvider getInteractiveCommandData
*/
public function testInteractiveCommand($options, $input, $expected)
{
list($namespace, $bundle, $dir, $format, $structure) = $expected;
$generator = $this->getGenerator();
$generator
->expects($this->once())
->method('generate')
->with($namespace, $bundle, $dir, $format, $structure)
;
$tester = new CommandTester($this->getCommand($generator, $input));
$tester->execute($options);
}
public function getInteractiveCommandData()
{
$tmp = sys_get_temp_dir();
return array(
array(array('--dir' => $tmp), "Foo/BarBundle\n", array('Foo\BarBundle', 'FooBarBundle', $tmp.'/', 'annotation', false)),
array(array('--dir' => $tmp), "Foo/BarBundle\nBarBundle\nfoo\nyml\nn", array('Foo\BarBundle', 'BarBundle', 'foo/', 'yml', false)),
array(array('--dir' => $tmp, '--format' => 'yml', '--bundle-name' => 'BarBundle', '--structure' => true), "Foo/BarBundle\n", array('Foo\BarBundle', 'BarBundle', $tmp.'/', 'yml', true)),
);
}
/**
* @dataProvider getNonInteractiveCommandData
*/
public function testNonInteractiveCommand($options, $expected)
{
list($namespace, $bundle, $dir, $format, $structure) = $expected;
$generator = $this->getGenerator();
$generator
->expects($this->once())
->method('generate')
->with($namespace, $bundle, $dir, $format, $structure)
;
$tester = new CommandTester($this->getCommand($generator, ''));
$tester->execute($options, array('interactive' => false));
}
public function getNonInteractiveCommandData()
{
$tmp = sys_get_temp_dir();
return array(
array(array('--dir' => $tmp, '--namespace' => 'Foo/BarBundle'), array('Foo\BarBundle', 'FooBarBundle', $tmp.'/', 'annotation', false)),
array(array('--dir' => $tmp, '--namespace' => 'Foo/BarBundle', '--format' => 'yml', '--bundle-name' => 'BarBundle', '--structure' => true), array('Foo\BarBundle', 'BarBundle', $tmp.'/', 'yml', true)),
);
}
protected function getCommand($generator, $input)
{
$command = $this
->getMockBuilder('Sensio\Bundle\GeneratorBundle\Command\GenerateBundleCommand')
->setMethods(array('checkAutoloader', 'updateKernel', 'updateRouting'))
->getMock()
;
$command->setContainer($this->getContainer());
$command->setHelperSet($this->getHelperSet($input));
$command->setGenerator($generator);
return $command;
}
protected function getGenerator()
{
// get a noop generator
return $this
->getMockBuilder('Sensio\Bundle\GeneratorBundle\Generator\BundleGenerator')
->disableOriginalConstructor()
->setMethods(array('generate'))
->getMock()
;
}
}

View File

@@ -0,0 +1,74 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sensio\Bundle\GeneratorBundle\Tests\Command;
use Symfony\Component\Console\Helper\HelperSet;
use Symfony\Component\Console\Helper\FormatterHelper;
use Sensio\Bundle\GeneratorBundle\Command\Helper\DialogHelper;
use Symfony\Component\DependencyInjection\Container;
abstract class GenerateCommandTest extends \PHPUnit_Framework_TestCase
{
protected function getHelperSet($input)
{
$dialog = new DialogHelper();
$dialog->setInputStream($this->getInputStream($input));
return new HelperSet(array(new FormatterHelper(), $dialog));
}
protected function getBundle()
{
$bundle = $this->getMock('Symfony\Component\HttpKernel\Bundle\BundleInterface');
$bundle
->expects($this->any())
->method('getPath')
->will($this->returnValue(sys_get_temp_dir()))
;
return $bundle;
}
protected function getInputStream($input)
{
$stream = fopen('php://memory', 'r+', false);
fputs($stream, $input.str_repeat("\n", 10));
rewind($stream);
return $stream;
}
protected function getContainer()
{
$kernel = $this->getMock('Symfony\Component\HttpKernel\KernelInterface');
$kernel
->expects($this->any())
->method('getBundle')
->will($this->returnValue($this->getBundle()))
;
$filesystem = $this->getMock('Symfony\Component\Filesystem\Filesystem');
$filesystem
->expects($this->any())
->method('isAbsolutePath')
->will($this->returnValue(true))
;
$container = new Container();
$container->set('kernel', $kernel);
$container->set('filesystem', $filesystem);
$container->setParameter('kernel.root_dir', sys_get_temp_dir());
return $container;
}
}

View File

@@ -0,0 +1,141 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sensio\Bundle\GeneratorBundle\Tests\Command;
use Symfony\Component\Console\Tester\CommandTester;
use Sensio\Bundle\GeneratorBundle\Command\GenerateDoctrineCrudCommand;
class GenerateDoctrineCrudCommandTest extends GenerateCommandTest
{
/**
* @dataProvider getInteractiveCommandData
*/
public function testInteractiveCommand($options, $input, $expected)
{
list($entity, $format, $prefix, $withWrite) = $expected;
$generator = $this->getGenerator();
$generator
->expects($this->once())
->method('generate')
->with($this->getBundle(), $entity, $this->getDoctrineMetadata(), $format, $prefix, $withWrite)
;
$tester = new CommandTester($this->getCommand($generator, $input));
$tester->execute($options);
}
public function getInteractiveCommandData()
{
return array(
array(array(), "AcmeBlogBundle:Blog/Post\n", array('Blog\\Post', 'annotation', 'blog_post', false)),
array(array('--entity' => 'AcmeBlogBundle:Blog/Post'), '', array('Blog\\Post', 'annotation', 'blog_post', false)),
array(array(), "AcmeBlogBundle:Blog/Post\ny\nyml\nfoobar\n", array('Blog\\Post', 'yml', 'foobar', true)),
array(array(), "AcmeBlogBundle:Blog/Post\ny\nyml\n/foobar\n", array('Blog\\Post', 'yml', 'foobar', true)),
array(array('--entity' => 'AcmeBlogBundle:Blog/Post', '--format' => 'yml', '--route-prefix' => 'foo', '--with-write' => true), '', array('Blog\\Post', 'yml', 'foo', true)),
);
}
/**
* @dataProvider getNonInteractiveCommandData
*/
public function testNonInteractiveCommand($options, $expected)
{
list($entity, $format, $prefix, $withWrite) = $expected;
$generator = $this->getGenerator();
$generator
->expects($this->once())
->method('generate')
->with($this->getBundle(), $entity, $this->getDoctrineMetadata(), $format, $prefix, $withWrite)
;
$tester = new CommandTester($this->getCommand($generator, ''));
$tester->execute($options, array('interactive' => false));
}
public function getNonInteractiveCommandData()
{
return array(
array(array('--entity' => 'AcmeBlogBundle:Blog/Post'), array('Blog\\Post', 'annotation', 'blog_post', false)),
array(array('--entity' => 'AcmeBlogBundle:Blog/Post', '--format' => 'yml', '--route-prefix' => 'foo', '--with-write' => true), array('Blog\\Post', 'yml', 'foo', true)),
);
}
protected function getCommand($generator, $input)
{
$command = $this
->getMockBuilder('Sensio\Bundle\GeneratorBundle\Command\GenerateDoctrineCrudCommand')
->setMethods(array('getEntityMetadata'))
->getMock()
;
$command
->expects($this->any())
->method('getEntityMetadata')
->will($this->returnValue(array($this->getDoctrineMetadata())))
;
$command->setContainer($this->getContainer());
$command->setHelperSet($this->getHelperSet($input));
$command->setGenerator($generator);
$command->setFormGenerator($this->getFormGenerator());
return $command;
}
protected function getDoctrineMetadata()
{
return $this
->getMockBuilder('Doctrine\ORM\Mapping\ClassMetadataInfo')
->disableOriginalConstructor()
->getMock()
;
}
protected function getGenerator()
{
// get a noop generator
return $this
->getMockBuilder('Sensio\Bundle\GeneratorBundle\Generator\DoctrineCrudGenerator')
->disableOriginalConstructor()
->setMethods(array('generate'))
->getMock()
;
}
protected function getFormGenerator()
{
return $this
->getMockBuilder('Sensio\Bundle\GeneratorBundle\Generator\DoctrineFormGenerator')
->disableOriginalConstructor()
->setMethods(array('generate'))
->getMock()
;
}
protected function getContainer()
{
$container = parent::getContainer();
$registry = $this->getMock('Symfony\Bridge\Doctrine\RegistryInterface');
$registry
->expects($this->any())
->method('getEntityNamespace')
->will($this->returnValue('Foo\\FooBundle\\Entity'))
;
$container->set('doctrine', $registry);
return $container;
}
}

View File

@@ -0,0 +1,104 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sensio\Bundle\GeneratorBundle\Tests\Command;
use Symfony\Component\Console\Tester\CommandTester;
use Sensio\Bundle\GeneratorBundle\Command\GenerateDoctrineEntityCommand;
class GenerateDoctrineEntityCommandTest extends GenerateCommandTest
{
/**
* @dataProvider getInteractiveCommandData
*/
public function testInteractiveCommand($options, $input, $expected)
{
list($entity, $format, $fields) = $expected;
$generator = $this->getGenerator();
$generator
->expects($this->once())
->method('generate')
->with($this->getBundle(), $entity, $format, $fields)
;
$tester = new CommandTester($this->getCommand($generator, $input));
$tester->execute($options);
}
public function getInteractiveCommandData()
{
return array(
array(array(), "AcmeBlogBundle:Blog/Post\n", array('Blog\\Post', 'annotation', array())),
array(array('--entity' => 'AcmeBlogBundle:Blog/Post'), '', array('Blog\\Post', 'annotation', array())),
array(array(), "AcmeBlogBundle:Blog/Post\nyml\n\n", array('Blog\\Post', 'yml', array())),
array(array(), "AcmeBlogBundle:Blog/Post\nyml\ntitle\n\n255\ndescription\ntext\n\n", array('Blog\\Post', 'yml', array(
array('fieldName' => 'title', 'type' => 'string', 'length' => 255),
array('fieldName' => 'description', 'type' => 'text'),
))),
);
}
/**
* @dataProvider getNonInteractiveCommandData
*/
public function testNonInteractiveCommand($options, $expected)
{
list($entity, $format, $fields) = $expected;
$generator = $this->getGenerator();
$generator
->expects($this->once())
->method('generate')
->with($this->getBundle(), $entity, $format, $fields)
;
$generator
->expects($this->any())
->method('isReservedKeyword')
->will($this->returnValue(false))
;
$tester = new CommandTester($this->getCommand($generator, ''));
$tester->execute($options, array('interactive' => false));
}
public function getNonInteractiveCommandData()
{
return array(
array(array('--entity' => 'AcmeBlogBundle:Blog/Post'), array('Blog\\Post', 'annotation', array())),
array(array('--entity' => 'AcmeBlogBundle:Blog/Post', '--format' => 'yml', '--fields' => 'title:string(255) description:text'), array('Blog\\Post', 'yml', array(
array('fieldName' => 'title', 'type' => 'string', 'length' => 255),
array('fieldName' => 'description', 'type' => 'text', 'length' => ''),
))),
);
}
protected function getCommand($generator, $input)
{
$command = new GenerateDoctrineEntityCommand();
$command->setContainer($this->getContainer());
$command->setHelperSet($this->getHelperSet($input));
$command->setGenerator($generator);
return $command;
}
protected function getGenerator()
{
// get a noop generator
return $this
->getMockBuilder('Sensio\Bundle\GeneratorBundle\Generator\DoctrineEntityGenerator')
->disableOriginalConstructor()
->setMethods(array('generate', 'isReservedKeyword'))
->getMock()
;
}
}

View File

@@ -0,0 +1,59 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sensio\Bundle\GeneratorBundle\Tests\Generator;
use Sensio\Bundle\GeneratorBundle\Generator\BundleGenerator;
class BundleGeneratorTest extends GeneratorTest
{
public function testGenerateYaml()
{
$generator = new BundleGenerator($this->filesystem, __DIR__.'/../../Resources/skeleton/bundle');
$generator->generate('Foo\BarBundle', 'FooBarBundle', $this->tmpDir, 'yml', false);
$files = array(
'FooBarBundle.php',
'Controller/DefaultController.php',
'Resources/views/Default/index.html.twig',
'Resources/config/routing.yml',
'Tests/Controller/DefaultControllerTest.php',
'Resources/config/services.yml',
'DependencyInjection/Configuration.php',
'DependencyInjection/FooBarExtension.php',
);
foreach ($files as $file) {
$this->assertTrue(file_exists($this->tmpDir.'/Foo/BarBundle/'.$file), sprintf('%s has been generated', $file));
}
$content = file_get_contents($this->tmpDir.'/Foo/BarBundle/FooBarBundle.php');
$this->assertContains('namespace Foo\\BarBundle', $content);
$content = file_get_contents($this->tmpDir.'/Foo/BarBundle/Controller/DefaultController.php');
$this->assertContains('public function indexAction', $content);
$this->assertNotContains('@Route("/hello/{name}"', $content);
$content = file_get_contents($this->tmpDir.'/Foo/BarBundle/Resources/views/Default/index.html.twig');
$this->assertContains('Hello {{ name }}!', $content);
}
public function testGenerateAnnotation()
{
$generator = new BundleGenerator($this->filesystem, __DIR__.'/../../Resources/skeleton/bundle');
$generator->generate('Foo\BarBundle', 'FooBarBundle', $this->tmpDir, 'annotation', false);
$this->assertFalse(file_exists($this->tmpDir.'/Foo/BarBundle/Resources/config/routing.yml'));
$this->assertFalse(file_exists($this->tmpDir.'/Foo/BarBundle/Resources/config/routing.xml'));
$content = file_get_contents($this->tmpDir.'/Foo/BarBundle/Controller/DefaultController.php');
$this->assertContains('@Route("/hello/{name}"', $content);
}
}

View File

@@ -0,0 +1,206 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sensio\Bundle\GeneratorBundle\Tests\Generator;
use Sensio\Bundle\GeneratorBundle\Generator\DoctrineCrudGenerator;
class DoctrineCrudGeneratorTest extends GeneratorTest
{
public function testGenerateYamlFull()
{
$this->getGenerator()->generate($this->getBundle(), 'Post', $this->getMetadata(), 'yml', '/post', true);
$files = array(
'Controller/PostController.php',
'Tests/Controller/PostControllerTest.php',
'Resources/config/routing/post.yml',
'Resources/views/Post/index.html.twig',
'Resources/views/Post/show.html.twig',
'Resources/views/Post/new.html.twig',
'Resources/views/Post/edit.html.twig',
);
foreach ($files as $file) {
$this->assertTrue(file_exists($this->tmpDir.'/'.$file), sprintf('%s has been generated', $file));
}
$files = array(
'Resources/config/routing/post.xml',
);
foreach ($files as $file) {
$this->assertFalse(file_exists($this->tmpDir.'/'.$file), sprintf('%s has not been generated', $file));
}
$content = file_get_contents($this->tmpDir.'/Controller/PostController.php');
$strings = array(
'namespace Foo\BarBundle\Controller;',
'public function indexAction',
'public function showAction',
'public function newAction',
'public function editAction',
);
foreach ($strings as $string) {
$this->assertContains($string, $content);
}
}
public function testGenerateXml()
{
$this->getGenerator()->generate($this->getBundle(), 'Post', $this->getMetadata(), 'xml', '/post', false);
$files = array(
'Controller/PostController.php',
'Tests/Controller/PostControllerTest.php',
'Resources/config/routing/post.xml',
'Resources/views/Post/index.html.twig',
'Resources/views/Post/show.html.twig',
);
foreach ($files as $file) {
$this->assertTrue(file_exists($this->tmpDir.'/'.$file), sprintf('%s has been generated', $file));
}
$files = array(
'Resources/config/routing/post.yml',
'Resources/views/Post/new.html.twig',
'Resources/views/Post/edit.html.twig',
);
foreach ($files as $file) {
$this->assertFalse(file_exists($this->tmpDir.'/'.$file), sprintf('%s has not been generated', $file));
}
$content = file_get_contents($this->tmpDir.'/Controller/PostController.php');
$strings = array(
'namespace Foo\BarBundle\Controller;',
'public function indexAction',
'public function showAction',
);
foreach ($strings as $string) {
$this->assertContains($string, $content);
}
$content = file_get_contents($this->tmpDir.'/Controller/PostController.php');
$strings = array(
'public function newAction',
'public function editAction',
'@Route',
);
foreach ($strings as $string) {
$this->assertNotContains($string, $content);
}
}
public function testGenerateAnnotationWrite()
{
$this->getGenerator()->generate($this->getBundle(), 'Post', $this->getMetadata(), 'annotation', '/post', true);
$files = array(
'Controller/PostController.php',
'Tests/Controller/PostControllerTest.php',
'Resources/views/Post/index.html.twig',
'Resources/views/Post/show.html.twig',
'Resources/views/Post/new.html.twig',
'Resources/views/Post/edit.html.twig',
);
foreach ($files as $file) {
$this->assertTrue(file_exists($this->tmpDir.'/'.$file), sprintf('%s has been generated', $file));
}
$files = array(
'Resources/config/routing/post.yml',
'Resources/config/routing/post.xml',
);
foreach ($files as $file) {
$this->assertFalse(file_exists($this->tmpDir.'/'.$file), sprintf('%s has not been generated', $file));
}
$content = file_get_contents($this->tmpDir.'/Controller/PostController.php');
$strings = array(
'namespace Foo\BarBundle\Controller;',
'public function indexAction',
'public function showAction',
'public function newAction',
'public function editAction',
'@Route',
);
foreach ($strings as $string) {
$this->assertContains($string, $content);
}
}
public function testGenerateAnnotation()
{
$this->getGenerator()->generate($this->getBundle(), 'Post', $this->getMetadata(), 'annotation', '/post', false);
$files = array(
'Controller/PostController.php',
'Tests/Controller/PostControllerTest.php',
'Resources/views/Post/index.html.twig',
'Resources/views/Post/show.html.twig',
);
foreach ($files as $file) {
$this->assertTrue(file_exists($this->tmpDir.'/'.$file), sprintf('%s has been generated', $file));
}
$files = array(
'Resources/config/routing/post.yml',
'Resources/config/routing/post.xml',
'Resources/views/Post/new.html.twig',
'Resources/views/Post/edit.html.twig',
);
foreach ($files as $file) {
$this->assertFalse(file_exists($this->tmpDir.'/'.$file), sprintf('%s has not been generated', $file));
}
$content = file_get_contents($this->tmpDir.'/Controller/PostController.php');
$strings = array(
'namespace Foo\BarBundle\Controller;',
'public function indexAction',
'public function showAction',
'@Route',
);
foreach ($strings as $string) {
$this->assertContains($string, $content);
}
$content = file_get_contents($this->tmpDir.'/Controller/PostController.php');
$strings = array(
'public function newAction',
'public function editAction',
);
foreach ($strings as $string) {
$this->assertNotContains($string, $content);
}
}
protected function getGenerator()
{
return new DoctrineCrudGenerator($this->filesystem, __DIR__.'/../../Resources/skeleton/crud');
}
protected function getBundle()
{
$bundle = $this->getMock('Symfony\Component\HttpKernel\Bundle\BundleInterface');
$bundle->expects($this->any())->method('getPath')->will($this->returnValue($this->tmpDir));
$bundle->expects($this->any())->method('getName')->will($this->returnValue('FooBarBundle'));
$bundle->expects($this->any())->method('getNamespace')->will($this->returnValue('Foo\BarBundle'));
return $bundle;
}
public function getMetadata()
{
$metadata = $this->getMockBuilder('Doctrine\ORM\Mapping\ClassMetadataInfo')->disableOriginalConstructor()->getMock();
$metadata->identifier = array('id');
$metadata->fieldMappings = array('title' => array('type' => 'string'));
return $metadata;
}
}

View File

@@ -0,0 +1,40 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sensio\Bundle\GeneratorBundle\Tests\Generator;
use Sensio\Bundle\GeneratorBundle\Generator\DoctrineFormGenerator;
class DoctrineFormGeneratorTest extends GeneratorTest
{
public function testGenerate()
{
$generator = new DoctrineFormGenerator($this->filesystem, __DIR__.'/../../Resources/skeleton/form');
$bundle = $this->getMock('Symfony\Component\HttpKernel\Bundle\BundleInterface');
$bundle->expects($this->any())->method('getPath')->will($this->returnValue($this->tmpDir));
$bundle->expects($this->any())->method('getNamespace')->will($this->returnValue('Foo\BarBundle'));
$metadata = $this->getMockBuilder('Doctrine\ORM\Mapping\ClassMetadataInfo')->disableOriginalConstructor()->getMock();
$metadata->identifier = array('id');
$metadata->associationMappings = array('title' => array('type' => 'string'));
$generator->generate($bundle, 'Post', $metadata);
$this->assertTrue(file_exists($this->tmpDir.'/Form/PostType.php'));
$content = file_get_contents($this->tmpDir.'/Form/PostType.php');
$this->assertContains('->add(\'title\')', $content);
$this->assertContains('class PostType extends AbstractType', $content);
$this->assertContains("'data_class' => 'Foo\BarBundle\Entity\Post'", $content);
$this->assertContains("'foo_barbundle_posttype'", $content);
}
}

View File

@@ -0,0 +1,32 @@
<?php
/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sensio\Bundle\GeneratorBundle\Tests\Generator;
use Symfony\Component\Filesystem\Filesystem;
abstract class GeneratorTest extends \PHPUnit_Framework_TestCase
{
protected $filesystem;
protected $tmpDir;
public function setUp()
{
$this->tmpDir = sys_get_temp_dir().'/sf2';
$this->filesystem = new Filesystem();
$this->filesystem->remove($this->tmpDir);
}
public function tearDown()
{
$this->filesystem->remove($this->tmpDir);
}
}

View File

@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="./vendor/autoload.php" colors="true">
<testsuites>
<testsuite name="SensioGeneratorBundle">
<directory suffix="Test.php">./Tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory>./</directory>
<exclude>
<directory>./Resources</directory>
<directory>./Tests</directory>
<directory>./vendor</directory>
</exclude>
</whitelist>
</filter>
</phpunit>