init without trunk

This commit is contained in:
Kevin Adametz 2020-07-09 12:49:32 +02:00
parent ed24ac4994
commit bb809e7233
14652 changed files with 177862 additions and 94817 deletions

View file

@ -0,0 +1,98 @@
<?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\AutoComplete;
use Doctrine\ORM\EntityManagerInterface;
use Sensio\Bundle\GeneratorBundle\Command\AutoComplete\EntitiesAutoCompleter;
class EntitiesAutoCompleterTest extends \PHPUnit_Framework_TestCase
{
/**
* @dataProvider getNamespaces
*/
public function testSuggestions($expected, $alias, $classes)
{
$autoCompleter = new EntitiesAutoCompleter($this->getEntityManagerMock($alias, $classes));
$this->assertSame($expected, $autoCompleter->getSuggestions());
}
public function getNamespaces()
{
return array(
array(
array('AcmeBlogBundle:Post'),
array('AcmeBlogBundle' => 'Acme\Bundle\BlogBundle\Entity'),
array('Acme\Bundle\BlogBundle\Entity\Post'),
),
array(
array('AcmeBlogBundle:Blog\Post'),
array('AcmeBlogBundle' => 'Acme\Bundle\BlogBundle\Entity'),
array('Acme\Bundle\BlogBundle\Entity\Blog\Post'),
),
array(
array(
'AcmeBlogBundle:Post',
'AcmeCommentBundle:Comment',
'AcmeBlogBundle:Blog\Post',
),
array(
'AcmeBlogBundle' => 'Acme\Bundle\BlogBundle\Entity',
'AcmeCommentBundle' => 'Acme\Bundle\CommentBundle\Entity',
),
array(
'Acme\Bundle\BlogBundle\Entity\Post',
'Acme\Bundle\CommentBundle\Entity\Comment',
'Acme\Bundle\BlogBundle\Entity\Blog\Post',
),
),
);
}
/**
* @param $aliases
* @param $classes
*
* @return EntityManagerInterface
*/
protected function getEntityManagerMock($aliases, $classes)
{
$cache = $this->getMockBuilder('Doctrine\Common\Persistence\Mapping\Driver\MappingDriver')->getMock();
$cache
->expects($this->any())
->method('getAllClassNames')
->will($this->returnValue($classes))
;
$configuration = $this->getMockBuilder('Doctrine\ORM\Configuration')->getMock();
$configuration
->expects($this->any())
->method('getMetadataDriverImpl')
->will($this->returnValue($cache))
;
$configuration
->expects($this->any())
->method('getEntityNamespaces')
->will($this->returnValue($aliases))
;
$manager = $this->getMockBuilder('Doctrine\ORM\EntityManagerInterface')->getMock();
$manager
->expects($this->any())
->method('getConfiguration')
->will($this->returnValue($configuration))
;
return $manager;
}
}

View file

@ -0,0 +1,149 @@
<?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 Sensio\Bundle\GeneratorBundle\Model\Bundle;
use Symfony\Component\Console\Tester\CommandTester;
class GenerateBundleCommandTest extends GenerateCommandTest
{
/**
* @dataProvider getInteractiveCommandData
*/
public function testInteractiveCommand($options, $input, $expected)
{
list($namespace, $bundleName, $dir, $format, $shared) = $expected;
$bundle = new Bundle($namespace, $bundleName, $dir, $format, $shared);
$container = $this->getContainer();
// not shared? the tests should be at the root of the project
if (!$shared) {
$bundle->setTestsDirectory($container->getParameter('kernel.root_dir').'/../tests/'.$bundleName);
}
$generator = $this->getGenerator();
$generator
->expects($this->once())
->method('generateBundle')
->with($bundle)
;
$tester = new CommandTester($command = $this->getCommand($generator, $container));
$this->setInputs($tester, $command, $input);
$tester->execute($options);
}
public function getInteractiveCommandData()
{
$tmp = sys_get_temp_dir();
return array(
array(
array('--shared' => true, '--dir' => $tmp, '--format' => 'annotation'),
// shared, namespace, bundle name, directory, format
"\nFoo/BarBundle\n\n\n\n",
array('Foo\BarBundle', 'FooBarBundle', $tmp.'/', 'annotation', true),
),
array(
array(),
// shared, namespace, bundle name, directory, format
"y\nFoo/BarBundle\nBarBundle\nfoo\nyml",
array('Foo\BarBundle', 'BarBundle', 'foo/', 'yml', true),
),
array(
array('--shared' => true, '--dir' => $tmp, '--format' => 'yml', '--bundle-name' => 'BarBundle'),
// shared, namespace, bundle name, directory, format
"\nFoo/BarBundle\n\n\n\n",
array('Foo\BarBundle', 'BarBundle', $tmp.'/', 'yml', true),
),
array(
array(),
// shared, namespace, bundle name, directory, format
"n\nBazBundle\n\nsrc\nannotation",
array('BazBundle', 'BazBundle', 'src/', 'annotation', false),
),
);
}
/**
* @dataProvider getNonInteractiveCommandData
*/
public function testNonInteractiveCommand($options, $expected)
{
list($namespace, $bundleName, $dir, $format, $shared) = $expected;
$bundle = new Bundle($namespace, $bundleName, $dir, $format, $shared);
$container = $this->getContainer();
// not shared? the tests should be at the root of the project
if (!$shared) {
$bundle->setTestsDirectory($container->getParameter('kernel.root_dir').'/../tests/'.$bundleName);
}
$generator = $this->getGenerator();
$generator
->expects($this->once())
->method('generateBundle')
->with($bundle)
;
$tester = new CommandTester($this->getCommand($generator, $container));
$tester->execute($options, array('interactive' => false));
}
public function getNonInteractiveCommandData()
{
$tmp = sys_get_temp_dir();
return array(
array(
array('--shared' => true, '--dir' => $tmp, '--namespace' => 'Foo/BarBundle'),
array('Foo\BarBundle', 'FooBarBundle', $tmp.'/', 'annotation', true),
),
array(
array('--shared' => true, '--dir' => $tmp, '--namespace' => 'Foo/BarBundle', '--format' => 'yml', '--bundle-name' => 'BarBundle'),
array('Foo\BarBundle', 'BarBundle', $tmp.'/', 'yml', true),
),
array(
array('--dir' => $tmp, '--namespace' => 'BazBundle', '--format' => 'yml', '--bundle-name' => 'BazBundle'),
array('BazBundle', 'BazBundle', $tmp.'/', 'yml', false),
),
);
}
protected function getCommand($generator, $container)
{
$command = $this
->getMockBuilder('Sensio\Bundle\GeneratorBundle\Command\GenerateBundleCommand')
->setMethods(array('checkAutoloader', 'updateKernel', 'updateRouting'))
->getMock()
;
$command->setContainer($container);
$command->setHelperSet($this->getHelperSet());
$command->setGenerator($generator);
return $command;
}
protected function getGenerator()
{
// get a noop generator
return $this
->getMockBuilder('Sensio\Bundle\GeneratorBundle\Generator\BundleGenerator')
->disableOriginalConstructor()
->setMethods(array('generateBundle'))
->getMock()
;
}
}

View file

@ -0,0 +1,161 @@
<?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\Application;
use Symfony\Component\Console\Tester\CommandTester;
use Sensio\Bundle\GeneratorBundle\Command\GenerateCommandCommand;
class GenerateCommandCommandTest extends GenerateCommandTest
{
protected $generator;
/**
* @dataProvider getInteractiveCommandData
*/
public function testInteractiveCommand($options, $input, $expected)
{
list($bundle, $name) = $expected;
$generator = $this->getGenerator();
$generator
->expects($this->once())
->method('generate')
->with($this->getBundle(), $name)
;
$tester = new CommandTester($command = $this->getCommand($generator));
$this->setInputs($tester, $command, $input);
$tester->execute($options);
}
public function getInteractiveCommandData()
{
return array(
array(
array(),
"FooBarBundle\napp:foo-bar\n",
array('FooBarBundle', 'app:foo-bar'),
),
array(
array('bundle' => 'FooBarBundle'),
"app:foo-bar\n",
array('FooBarBundle', 'app:foo-bar'),
),
array(
array('name' => 'app:foo-bar'),
"FooBarBundle\n",
array('FooBarBundle', 'app:foo-bar'),
),
array(
array('bundle' => 'FooBarBundle', 'name' => 'app:foo-bar'),
'',
array('FooBarBundle', 'app:foo-bar'),
),
);
}
/**
* @dataProvider getNonInteractiveCommandData
*/
public function testNonInteractiveCommand($options, $expected)
{
list($bundle, $name) = $expected;
$generator = $this->getGenerator();
$generator
->expects($this->once())
->method('generate')
->with($this->getBundle(), $name)
;
$tester = new CommandTester($command = $this->getCommand($generator));
$tester->execute($options, array('interactive' => false));
}
public function getNonInteractiveCommandData()
{
return array(
array(
array('bundle' => 'FooBarBundle', 'name' => 'app:my-command'),
array('FooBarBundle', 'app:my-command'),
),
);
}
protected function getCommand($generator)
{
$command = new GenerateCommandCommand();
$command->setContainer($this->getContainer());
$command->setHelperSet($this->getHelperSet());
$command->setGenerator($generator);
return $command;
}
protected function getApplication($input = '')
{
$application = new Application();
$command = new GenerateCommandCommand();
$command->setContainer($this->getContainer());
$command->setHelperSet($this->getHelperSet($input));
$command->setGenerator($this->getGenerator());
$application->add($command);
return $application;
}
protected function getGenerator()
{
if (null === $this->generator) {
$this->setGenerator();
}
return $this->generator;
}
protected function setGenerator()
{
// get a noop generator
$this->generator = $this
->getMockBuilder('Sensio\Bundle\GeneratorBundle\Generator\CommandGenerator')
->disableOriginalConstructor()
->setMethods(array('generate'))
->getMock()
;
}
protected function getBundle()
{
if (null == $this->bundle) {
$this->setBundle();
}
return $this->bundle;
}
protected function setBundle()
{
$bundle = $this->getMockBuilder('Symfony\Component\HttpKernel\Bundle\BundleInterface')->getMock();
$bundle->expects($this->any())->method('getPath')->will($this->returnValue(''));
$bundle->expects($this->any())->method('getName')->will($this->returnValue('FooBarBundle'));
$bundle->expects($this->any())->method('getNamespace')->will($this->returnValue('Foo\BarBundle'));
$this->bundle = $bundle;
}
}

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\Helper\HelperSet;
use Symfony\Component\Console\Helper\FormatterHelper;
use Symfony\Component\Filesystem\Filesystem;
use Sensio\Bundle\GeneratorBundle\Command\Helper\QuestionHelper;
use Symfony\Component\DependencyInjection\Container;
abstract class GenerateCommandTest extends \PHPUnit_Framework_TestCase
{
protected $bundle;
protected function tearDown()
{
if (null !== $this->bundle) {
$fs = new Filesystem();
$fs->remove($this->bundle->getPath());
}
}
protected function getHelperSet()
{
return new HelperSet(array(new FormatterHelper(), new QuestionHelper()));
}
protected function setInputs($tester, $command, $input)
{
$input .= str_repeat("\n", 10);
if (method_exists($tester, 'setInputs')) {
$tester->setInputs(explode("\n", $input));
} else {
$stream = fopen('php://memory', 'r+', false);
fwrite($stream, $input);
rewind($stream);
$command->getHelperSet()->get('question')->setInputStream($stream);
}
}
protected function getBundle()
{
if (null !== $this->bundle) {
return $this->bundle;
}
$tmpDir = sys_get_temp_dir().'/sf'.mt_rand(111111, 999999);
@mkdir($tmpDir, 0777, true);
$this->bundle = $this->getMockBuilder('Symfony\Component\HttpKernel\Bundle\BundleInterface')->getMock();
$this->bundle
->expects($this->any())
->method('getPath')
->will($this->returnValue($tmpDir))
;
return $this->bundle;
}
protected function getContainer()
{
$bundle = $this->getBundle();
$kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\KernelInterface')->getMock();
$kernel
->expects($this->any())
->method('getBundle')
->will($this->returnValue($bundle))
;
$kernel
->expects($this->any())
->method('getBundles')
->will($this->returnValue(array($bundle)))
;
$filesystem = $this->getMockBuilder('Symfony\Component\Filesystem\Filesystem')->getMock();
$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', $bundle->getPath());
return $container;
}
}

View file

@ -0,0 +1,196 @@
<?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\Application;
use Symfony\Component\Console\Tester\CommandTester;
use Sensio\Bundle\GeneratorBundle\Command\GenerateControllerCommand;
class GenerateControllerCommandTest extends GenerateCommandTest
{
protected $generator;
/**
* @dataProvider getInteractiveCommandData
*/
public function testInteractiveCommand($options, $input, $expected)
{
list($controller, $routeFormat, $templateFormat, $actions) = $expected;
$generator = $this->getGenerator();
$generator
->expects($this->once())
->method('generate')
->with($this->getBundle(), $controller, $routeFormat, $templateFormat, $actions)
;
$tester = new CommandTester($command = $this->getCommand($generator));
$this->setInputs($tester, $command, $input);
$tester->execute($options);
}
public function getInteractiveCommandData()
{
return array(
array(array(), "AcmeBlogBundle:Post\n", array('Post', 'annotation', 'twig', array())),
array(array('--controller' => 'AcmeBlogBundle:Post'), '', array('Post', 'annotation', 'twig', array())),
array(array(), "AcmeBlogBundle:Post\nyml\nphp\n", array('Post', 'yml', 'php', array())),
array(array(), "AcmeBlogBundle:Post\nyml\nphp\nshowAction\n\n\ngetListAction\n/_getlist/{max}\nAcmeBlogBundle:Lists:post.html.php\n", array('Post', 'yml', 'php', array(
'showAction' => array(
'name' => 'showAction',
'route' => '/show',
'placeholders' => array(),
'template' => 'AcmeBlogBundle:Post:show.html.php',
),
'getListAction' => array(
'name' => 'getListAction',
'route' => '/_getlist/{max}',
'placeholders' => array('max'),
'template' => 'AcmeBlogBundle:Lists:post.html.php',
),
))),
array(array('--route-format' => 'xml', '--template-format' => 'php', '--actions' => array('showAction:/{slug}:AcmeBlogBundle:article.html.php')), 'AcmeBlogBundle:Post', array('Post', 'xml', 'php', array(
'showAction' => array(
'name' => 'showAction',
'route' => '/{slug}',
'placeholders' => array('slug'),
'template' => 'AcmeBlogBundle:article.html.php',
),
))),
);
}
/**
* @dataProvider getNonInteractiveCommandData
*/
public function testNonInteractiveCommand($options, $expected)
{
list($controller, $routeFormat, $templateFormat, $actions) = $expected;
$generator = $this->getGenerator();
$generator
->expects($this->once())
->method('generate')
->with($this->getBundle(), $controller, $routeFormat, $templateFormat, $actions)
;
$tester = new CommandTester($command = $this->getCommand($generator));
$tester->execute($options, array('interactive' => false));
}
public function getNonInteractiveCommandData()
{
return array(
array(array('--controller' => 'AcmeBlogBundle:Post'), array('Post', 'annotation', 'twig', array())),
array(array('--controller' => 'AcmeBlogBundle:Post', '--route-format' => 'yml', '--template-format' => 'php'), array('Post', 'yml', 'php', array())),
array(array('--controller' => 'AcmeBlogBundle:Post', '--actions' => array('showAction getListAction:/_getlist/{max}:AcmeBlogBundle:List:post.html.twig createAction:/admin/create')), array('Post', 'annotation', 'twig', array(
'showAction' => array(
'name' => 'showAction',
'route' => '/show',
'placeholders' => array(),
'template' => 'default',
),
'getListAction' => array(
'name' => 'getListAction',
'route' => '/_getlist/{max}',
'placeholders' => array('max'),
'template' => 'AcmeBlogBundle:List:post.html.twig',
),
'createAction' => array(
'name' => 'createAction',
'route' => '/admin/create',
'placeholders' => array(),
'template' => 'default',
),
))),
array(array('--controller' => 'AcmeBlogBundle:Post', '--route-format' => 'xml', '--template-format' => 'php', '--actions' => array('showAction::')), array('Post', 'xml', 'php', array(
'showAction' => array(
'name' => 'showAction',
'route' => '/show',
'placeholders' => array(),
'template' => 'default',
),
))),
);
}
protected function getCommand($generator)
{
$command = $this
->getMockBuilder('Sensio\Bundle\GeneratorBundle\Command\GenerateControllerCommand')
->setMethods(array('generateRouting'))
->getMock()
;
$command->setContainer($this->getContainer());
$command->setHelperSet($this->getHelperSet());
$command->setGenerator($generator);
return $command;
}
protected function getApplication($input = '')
{
$application = new Application();
$command = new GenerateControllerCommand();
$command->setContainer($this->getContainer());
$command->setHelperSet($this->getHelperSet($input));
$command->setGenerator($this->getGenerator());
$application->add($command);
return $application;
}
protected function getGenerator()
{
if (null == $this->generator) {
$this->setGenerator();
}
return $this->generator;
}
protected function setGenerator()
{
// get a noop generator
$this->generator = $this
->getMockBuilder('Sensio\Bundle\GeneratorBundle\Generator\ControllerGenerator')
->disableOriginalConstructor()
->setMethods(array('generate'))
->getMock()
;
}
protected function getBundle()
{
if (null == $this->bundle) {
$this->setBundle();
}
return $this->bundle;
}
protected function setBundle()
{
$bundle = $this->getMockBuilder('Symfony\Component\HttpKernel\Bundle\BundleInterface')->getMock();
$bundle->expects($this->any())->method('getPath')->will($this->returnValue(''));
$bundle->expects($this->any())->method('getName')->will($this->returnValue('FooBarBundle'));
$bundle->expects($this->any())->method('getNamespace')->will($this->returnValue('Foo\BarBundle'));
$this->bundle = $bundle;
}
}

View file

@ -0,0 +1,300 @@
<?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;
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($command = $this->getCommand($generator));
$this->setInputs($tester, $command, $input);
$tester->execute($options);
}
public function getInteractiveCommandData()
{
return array(
array(array(), "AcmeBlogBundle:Blog/Post\n", 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'), "\ny\nyml\nfoobar\n", array('Blog\\Post', 'yml', 'foobar', true)),
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)),
// Deprecated, to be removed in 4.0
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)),
);
}
/**
* @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)),
// Deprecated, to be removed in 4.0
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)),
);
}
public function testCreateCrudWithAnnotationInNonAnnotationBundle()
{
$rootDir = $this->getContainer()->getParameter('kernel.root_dir');
$routing = <<<DATA
acme_blog:
resource: "@AcmeBlogBundle/Resources/config/routing.xml"
prefix: /
DATA;
@mkdir($rootDir.'/config', 0777, true);
file_put_contents($rootDir.'/config/routing.yml', $routing);
$options = array();
$input = "AcmeBlogBundle:Blog/Post\ny\nannotation\n/foobar\n";
$expected = array('Blog\\Post', 'annotation', 'foobar', true);
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($command = $this->getCommand($generator));
$this->setInputs($tester, $command, $input);
$tester->execute($options);
$this->assertContains('acme_blog_post:', file_get_contents($rootDir.'/config/routing.yml'));
}
public function testCreateCrudWithAnnotationInAnnotationBundle()
{
$rootDir = $this->getContainer()->getParameter('kernel.root_dir');
$routing = <<<DATA
acme_blog:
resource: "@AcmeBlogBundle/Controller/"
type: annotation
DATA;
@mkdir($rootDir.'/config', 0777, true);
file_put_contents($rootDir.'/config/routing.yml', $routing);
$options = array();
$input = "AcmeBlogBundle:Blog/Post\ny\nyml\n/foobar\n";
$expected = array('Blog\\Post', 'yml', 'foobar', true);
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($command = $this->getCommand($generator));
$this->setInputs($tester, $command, $input);
$tester->execute($options);
$this->assertEquals($routing, file_get_contents($rootDir.'/config/routing.yml'));
}
public function testAddACrudWithOneAlreadyDefined()
{
$rootDir = $this->getContainer()->getParameter('kernel.root_dir');
$routing = <<<DATA
acme_blog:
resource: "@AcmeBlogBundle/Controller/OtherController.php"
type: annotation
DATA;
@mkdir($rootDir.'/config', 0777, true);
file_put_contents($rootDir.'/config/routing.yml', $routing);
$options = array();
$input = "AcmeBlogBundle:Blog/Post\ny\nannotation\n/foobar\n";
$expected = array('Blog\\Post', 'annotation', 'foobar', true);
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($command = $this->getCommand($generator));
$this->setInputs($tester, $command, $input);
$tester->execute($options);
$expected = '@AcmeBlogBundle/Controller/PostController.php';
$this->assertContains($expected, file_get_contents($rootDir.'/config/routing.yml'));
}
protected function getCommand($generator)
{
$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());
$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 getBundle()
{
$bundle = parent::getBundle();
$bundle
->expects($this->any())
->method('getName')
->will($this->returnValue('AcmeBlogBundle'))
;
return $bundle;
}
protected function getContainer()
{
$container = parent::getContainer();
$container->set('doctrine', $this->getDoctrine());
return $container;
}
protected function getDoctrine()
{
$cache = $this->getMockBuilder('Doctrine\Common\Persistence\Mapping\Driver\MappingDriver')->getMock();
$cache
->expects($this->any())
->method('getAllClassNames')
->will($this->returnValue(array('Acme\Bundle\BlogBundle\Entity\Post')))
;
$configuration = $this->getMockBuilder('Doctrine\ORM\Configuration')->getMock();
$configuration
->expects($this->any())
->method('getMetadataDriverImpl')
->will($this->returnValue($cache))
;
$configuration
->expects($this->any())
->method('getEntityNamespaces')
->will($this->returnValue(array('AcmeBlogBundle' => 'Acme\Bundle\BlogBundle\Entity')))
;
$manager = $this->getMockBuilder('Doctrine\ORM\EntityManagerInterface')->getMock();
$manager
->expects($this->any())
->method('getConfiguration')
->will($this->returnValue($configuration))
;
$registry = $this->getMockBuilder('Symfony\Bridge\Doctrine\RegistryInterface')->getMock();
$registry
->expects($this->any())
->method('getAliasNamespace')
->will($this->returnValue('Acme\Bundle\BlogBundle\Entity\Blog\Post'))
;
$registry
->expects($this->any())
->method('getManager')
->will($this->returnValue($manager))
;
return $registry;
}
}

View file

@ -0,0 +1,122 @@
<?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 Sensio\Bundle\GeneratorBundle\Model\EntityGeneratorResult;
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)
->willReturn(new EntityGeneratorResult('', '', ''))
;
$tester = new CommandTester($command = $this->getCommand($generator));
$this->setInputs($tester, $command, $input);
$tester->execute($options);
}
public function getInteractiveCommandData()
{
return array(
array(array(), "Acme2BlogBundle:Blog/Post\n", array('Blog\\Post', 'annotation', array())),
array(array('entity' => 'Acme2BlogBundle:Blog/Post'), '', array('Blog\\Post', 'annotation', array())),
array(array(), "Acme2BlogBundle:Blog/Post\nyml\n\n", array('Blog\\Post', 'yml', array())),
array(array(), "Acme2BlogBundle:Blog/Post\nyml\ncreated_by\n\n255\nfalse\nfalse\ndescription\ntext\nfalse\ntrue\nupdated_at\ndatetime\ntrue\nfalse\nrating\ndecimal\n5\n3\nfalse\nfalse\n\n", array('Blog\\Post', 'yml', array(
array('fieldName' => 'createdBy', 'type' => 'string', 'length' => 255, 'columnName' => 'created_by'),
array('fieldName' => 'description', 'type' => 'text', 'unique' => true, 'columnName' => 'description'),
array('fieldName' => 'updatedAt', 'type' => 'datetimetz', 'nullable' => true, 'columnName' => 'updated_at'),
array('fieldName' => 'rating', 'type' => 'decimal', 'precision' => 5, 'scale' => 3, 'columnName' => 'rating'),
))),
// Deprecated, to be removed in 4.0
array(array('--entity' => 'Acme2BlogBundle:Blog/Post'), '', array('Blog\\Post', 'annotation', array())),
);
}
/**
* @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)
->willReturn(new EntityGeneratorResult('', '', ''))
;
$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' => 'Acme2BlogBundle:Blog/Post'), array('Blog\\Post', 'annotation', array())),
array(array('entity' => 'Acme2BlogBundle:Blog/Post', '--format' => 'yml', '--fields' => 'created_by:string(255) updated_by:string(length=128 nullable=true) description:text rating:decimal(precision=7 scale=2)'), array('Blog\\Post', 'yml', array(
array('fieldName' => 'created_by', 'type' => 'string', 'length' => 255),
array('fieldName' => 'updated_by', 'type' => 'string', 'length' => 128, 'nullable' => true),
array('fieldName' => 'description', 'type' => 'text'),
array('fieldName' => 'rating', 'type' => 'decimal', 'precision' => 7, 'scale' => 2),
))),
// Deprecated, to be removed in 4.0
array(array('--entity' => 'Acme2BlogBundle:Blog/Post'), array('Blog\\Post', 'annotation', array())),
array(array('--entity' => 'Acme2BlogBundle:Blog/Post', '--format' => 'yml', '--fields' => 'created_by:string(255) updated_by:string(length=128 nullable=true) description:text rating:decimal(precision=7 scale=2)'), array('Blog\\Post', 'yml', array(
array('fieldName' => 'created_by', 'type' => 'string', 'length' => 255),
array('fieldName' => 'updated_by', 'type' => 'string', 'length' => 128, 'nullable' => true),
array('fieldName' => 'description', 'type' => 'text'),
array('fieldName' => 'rating', 'type' => 'decimal', 'precision' => 7, 'scale' => 2),
))),
);
}
protected function getCommand($generator)
{
$command = new GenerateDoctrineEntityCommand();
$command->setContainer($this->getContainer());
$command->setHelperSet($this->getHelperSet());
$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()
;
}
}