This commit is contained in:
Xes
2025-08-14 22:41:49 +02:00
parent 2de81ccc46
commit 8ce45119b6
39774 changed files with 4309466 additions and 0 deletions

View File

@@ -0,0 +1,170 @@
<?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 Symfony\Bundle\FrameworkBundle\Tests\Console;
use Symfony\Bundle\FrameworkBundle\Console\Application;
use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\NullOutput;
use Symfony\Component\Console\Tester\ApplicationTester;
class ApplicationTest extends TestCase
{
public function testBundleInterfaceImplementation()
{
$bundle = $this->getMock('Symfony\Component\HttpKernel\Bundle\BundleInterface');
$kernel = $this->getKernel(array($bundle), true);
$application = new Application($kernel);
$application->doRun(new ArrayInput(array('list')), new NullOutput());
}
public function testBundleCommandsAreRegistered()
{
$bundle = $this->getMock('Symfony\Component\HttpKernel\Bundle\Bundle');
$bundle->expects($this->once())->method('registerCommands');
$kernel = $this->getKernel(array($bundle), true);
$application = new Application($kernel);
$application->doRun(new ArrayInput(array('list')), new NullOutput());
// Calling twice: registration should only be done once.
$application->doRun(new ArrayInput(array('list')), new NullOutput());
}
public function testBundleCommandsAreRetrievable()
{
$bundle = $this->getMock('Symfony\Component\HttpKernel\Bundle\Bundle');
$bundle->expects($this->once())->method('registerCommands');
$kernel = $this->getKernel(array($bundle));
$application = new Application($kernel);
$application->all();
// Calling twice: registration should only be done once.
$application->all();
}
public function testBundleSingleCommandIsRetrievable()
{
$bundle = $this->getMock('Symfony\Component\HttpKernel\Bundle\Bundle');
$bundle->expects($this->once())->method('registerCommands');
$kernel = $this->getKernel(array($bundle));
$application = new Application($kernel);
$command = new Command('example');
$application->add($command);
$this->assertSame($command, $application->get('example'));
}
public function testBundleCommandCanBeFound()
{
$bundle = $this->getMock('Symfony\Component\HttpKernel\Bundle\Bundle');
$bundle->expects($this->once())->method('registerCommands');
$kernel = $this->getKernel(array($bundle));
$application = new Application($kernel);
$command = new Command('example');
$application->add($command);
$this->assertSame($command, $application->find('example'));
}
public function testBundleCommandCanBeFoundByAlias()
{
$bundle = $this->getMock('Symfony\Component\HttpKernel\Bundle\Bundle');
$bundle->expects($this->once())->method('registerCommands');
$kernel = $this->getKernel(array($bundle));
$application = new Application($kernel);
$command = new Command('example');
$command->setAliases(array('alias'));
$application->add($command);
$this->assertSame($command, $application->find('alias'));
}
public function testBundleCommandsHaveRightContainer()
{
$command = $this->getMockForAbstractClass('Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand', array('foo'), '', true, true, true, array('setContainer'));
$command->setCode(function () {});
$command->expects($this->exactly(2))->method('setContainer');
$application = new Application($this->getKernel(array(), true));
$application->setAutoExit(false);
$application->setCatchExceptions(false);
$application->add($command);
$tester = new ApplicationTester($application);
// set container is called here
$tester->run(array('command' => 'foo'));
// as the container might have change between two runs, setContainer must called again
$tester->run(array('command' => 'foo'));
}
private function getKernel(array $bundles, $useDispatcher = false)
{
$container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
if ($useDispatcher) {
$dispatcher = $this->getMock('Symfony\Component\EventDispatcher\EventDispatcherInterface');
$dispatcher
->expects($this->atLeastOnce())
->method('dispatch')
;
$container
->expects($this->atLeastOnce())
->method('get')
->with($this->equalTo('event_dispatcher'))
->will($this->returnValue($dispatcher));
}
$container
->expects($this->once())
->method('hasParameter')
->with($this->equalTo('console.command.ids'))
->will($this->returnValue(true))
;
$container
->expects($this->once())
->method('getParameter')
->with($this->equalTo('console.command.ids'))
->will($this->returnValue(array()))
;
$kernel = $this->getMock('Symfony\Component\HttpKernel\KernelInterface');
$kernel
->expects($this->any())
->method('getBundles')
->will($this->returnValue($bundles))
;
$kernel
->expects($this->any())
->method('getContainer')
->will($this->returnValue($container))
;
return $kernel;
}
}

View File

@@ -0,0 +1,200 @@
<?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 Symfony\Bundle\FrameworkBundle\Tests\Console\Descriptor;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Output\BufferedOutput;
use Symfony\Component\Console\Style\SymfonyStyle;
use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
abstract class AbstractDescriptorTest extends \PHPUnit_Framework_TestCase
{
/** @dataProvider getDescribeRouteCollectionTestData */
public function testDescribeRouteCollection(RouteCollection $routes, $expectedDescription)
{
$this->assertDescription($expectedDescription, $routes);
}
public function getDescribeRouteCollectionTestData()
{
return $this->getDescriptionTestData(ObjectsProvider::getRouteCollections());
}
/** @dataProvider getDescribeRouteTestData */
public function testDescribeRoute(Route $route, $expectedDescription)
{
$this->assertDescription($expectedDescription, $route);
}
public function getDescribeRouteTestData()
{
return $this->getDescriptionTestData(ObjectsProvider::getRoutes());
}
/** @dataProvider getDescribeContainerParametersTestData */
public function testDescribeContainerParameters(ParameterBag $parameters, $expectedDescription)
{
$this->assertDescription($expectedDescription, $parameters);
}
public function getDescribeContainerParametersTestData()
{
return $this->getDescriptionTestData(ObjectsProvider::getContainerParameters());
}
/** @dataProvider getDescribeContainerBuilderTestData */
public function testDescribeContainerBuilder(ContainerBuilder $builder, $expectedDescription, array $options)
{
$this->assertDescription($expectedDescription, $builder, $options);
}
public function getDescribeContainerBuilderTestData()
{
return $this->getContainerBuilderDescriptionTestData(ObjectsProvider::getContainerBuilders());
}
/** @dataProvider getDescribeContainerDefinitionTestData */
public function testDescribeContainerDefinition(Definition $definition, $expectedDescription)
{
$this->assertDescription($expectedDescription, $definition);
}
public function getDescribeContainerDefinitionTestData()
{
return $this->getDescriptionTestData(ObjectsProvider::getContainerDefinitions());
}
/** @dataProvider getDescribeContainerAliasTestData */
public function testDescribeContainerAlias(Alias $alias, $expectedDescription)
{
$this->assertDescription($expectedDescription, $alias);
}
public function getDescribeContainerAliasTestData()
{
return $this->getDescriptionTestData(ObjectsProvider::getContainerAliases());
}
/** @dataProvider getDescribeContainerParameterTestData */
public function testDescribeContainerParameter($parameter, $expectedDescription, array $options)
{
$this->assertDescription($expectedDescription, $parameter, $options);
}
public function getDescribeContainerParameterTestData()
{
$data = $this->getDescriptionTestData(ObjectsProvider::getContainerParameter());
$data[0][] = array('parameter' => 'database_name');
$data[1][] = array('parameter' => 'twig.form.resources');
return $data;
}
/** @dataProvider getDescribeEventDispatcherTestData */
public function testDescribeEventDispatcher(EventDispatcher $eventDispatcher, $expectedDescription, array $options)
{
$this->assertDescription($expectedDescription, $eventDispatcher, $options);
}
public function getDescribeEventDispatcherTestData()
{
return $this->getEventDispatcherDescriptionTestData(ObjectsProvider::getEventDispatchers());
}
/** @dataProvider getDescribeCallableTestData */
public function testDescribeCallable($callable, $expectedDescription)
{
$this->assertDescription($expectedDescription, $callable);
}
public function getDescribeCallableTestData()
{
return $this->getDescriptionTestData(ObjectsProvider::getCallables());
}
abstract protected function getDescriptor();
abstract protected function getFormat();
private function assertDescription($expectedDescription, $describedObject, array $options = array())
{
$options['raw_output'] = true;
$output = new BufferedOutput(BufferedOutput::VERBOSITY_NORMAL, true);
if ('txt' === $this->getFormat()) {
$options['output'] = new SymfonyStyle(new ArrayInput(array()), $output);
}
$this->getDescriptor()->describe($output, $describedObject, $options);
if ('json' === $this->getFormat()) {
$this->assertEquals(json_decode($expectedDescription), json_decode($output->fetch()));
} else {
$this->assertEquals(trim($expectedDescription), trim(str_replace(PHP_EOL, "\n", $output->fetch())));
}
}
private function getDescriptionTestData(array $objects)
{
$data = array();
foreach ($objects as $name => $object) {
$description = file_get_contents(sprintf('%s/../../Fixtures/Descriptor/%s.%s', __DIR__, $name, $this->getFormat()));
$data[] = array($object, $description);
}
return $data;
}
private function getContainerBuilderDescriptionTestData(array $objects)
{
$variations = array(
'services' => array('show_private' => true),
'public' => array('show_private' => false),
'tag1' => array('show_private' => true, 'tag' => 'tag1'),
'tags' => array('group_by' => 'tags', 'show_private' => true),
);
$data = array();
foreach ($objects as $name => $object) {
foreach ($variations as $suffix => $options) {
$description = file_get_contents(sprintf('%s/../../Fixtures/Descriptor/%s_%s.%s', __DIR__, $name, $suffix, $this->getFormat()));
$data[] = array($object, $description, $options);
}
}
return $data;
}
private function getEventDispatcherDescriptionTestData(array $objects)
{
$variations = array(
'events' => array(),
'event1' => array('event' => 'event1'),
);
$data = array();
foreach ($objects as $name => $object) {
foreach ($variations as $suffix => $options) {
$description = file_get_contents(sprintf('%s/../../Fixtures/Descriptor/%s_%s.%s', __DIR__, $name, $suffix, $this->getFormat()));
$data[] = array($object, $description, $options);
}
}
return $data;
}
}

View File

@@ -0,0 +1,27 @@
<?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 Symfony\Bundle\FrameworkBundle\Tests\Console\Descriptor;
use Symfony\Bundle\FrameworkBundle\Console\Descriptor\JsonDescriptor;
class JsonDescriptorTest extends AbstractDescriptorTest
{
protected function getDescriptor()
{
return new JsonDescriptor();
}
protected function getFormat()
{
return 'json';
}
}

View File

@@ -0,0 +1,27 @@
<?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 Symfony\Bundle\FrameworkBundle\Tests\Console\Descriptor;
use Symfony\Bundle\FrameworkBundle\Console\Descriptor\MarkdownDescriptor;
class MarkdownDescriptorTest extends AbstractDescriptorTest
{
protected function getDescriptor()
{
return new MarkdownDescriptor();
}
protected function getFormat()
{
return 'md';
}
}

View File

@@ -0,0 +1,173 @@
<?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 Symfony\Bundle\FrameworkBundle\Tests\Console\Descriptor;
use Symfony\Component\DependencyInjection\Alias;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBag;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
class ObjectsProvider
{
public static function getRouteCollections()
{
$collection1 = new RouteCollection();
foreach (self::getRoutes() as $name => $route) {
$collection1->add($name, $route);
}
return array('route_collection_1' => $collection1);
}
public static function getRoutes()
{
return array(
'route_1' => new Route(
'/hello/{name}',
array('name' => 'Joseph'),
array('name' => '[a-z]+'),
array('opt1' => 'val1', 'opt2' => 'val2'),
'localhost',
array('http', 'https'),
array('get', 'head')
),
'route_2' => new Route(
'/name/add',
array(),
array(),
array('opt1' => 'val1', 'opt2' => 'val2'),
'localhost',
array('http', 'https'),
array('put', 'post')
),
);
}
public static function getContainerParameters()
{
return array(
'parameters_1' => new ParameterBag(array(
'integer' => 12,
'string' => 'Hello world!',
'boolean' => true,
'array' => array(12, 'Hello world!', true),
)),
);
}
public static function getContainerParameter()
{
$builder = new ContainerBuilder();
$builder->setParameter('database_name', 'symfony');
$builder->setParameter('twig.form.resources', array(
'bootstrap_3_horizontal_layout.html.twig',
'bootstrap_3_layout.html.twig',
'form_div_layout.html.twig',
'form_table_layout.html.twig',
));
return array(
'parameter' => $builder,
'array_parameter' => $builder,
);
}
public static function getContainerBuilders()
{
$builder1 = new ContainerBuilder();
$builder1->setDefinitions(self::getContainerDefinitions());
$builder1->setAliases(self::getContainerAliases());
return array('builder_1' => $builder1);
}
public static function getContainerDefinitions()
{
$definition1 = new Definition('Full\\Qualified\\Class1');
$definition2 = new Definition('Full\\Qualified\\Class2');
return array(
'definition_1' => $definition1
->setPublic(true)
->setSynthetic(false)
->setLazy(true)
->setAbstract(true)
->setFactory(array('Full\\Qualified\\FactoryClass', 'get')),
'definition_2' => $definition2
->setPublic(false)
->setSynthetic(true)
->setFile('/path/to/file')
->setLazy(false)
->setAbstract(false)
->addTag('tag1', array('attr1' => 'val1', 'attr2' => 'val2'))
->addTag('tag1', array('attr3' => 'val3'))
->addTag('tag2')
->setFactory(array(new Reference('factory.service'), 'get')),
);
}
public static function getContainerAliases()
{
return array(
'alias_1' => new Alias('service_1', true),
'alias_2' => new Alias('service_2', false),
);
}
public static function getEventDispatchers()
{
$eventDispatcher = new EventDispatcher();
$eventDispatcher->addListener('event1', 'global_function', 255);
$eventDispatcher->addListener('event1', function () { return 'Closure'; }, -1);
$eventDispatcher->addListener('event2', new CallableClass());
return array('event_dispatcher_1' => $eventDispatcher);
}
public static function getCallables()
{
return array(
'callable_1' => 'array_key_exists',
'callable_2' => array('Symfony\\Bundle\\FrameworkBundle\\Tests\\Console\\Descriptor\\CallableClass', 'staticMethod'),
'callable_3' => array(new CallableClass(), 'method'),
'callable_4' => 'Symfony\\Bundle\\FrameworkBundle\\Tests\\Console\\Descriptor\\CallableClass::staticMethod',
'callable_5' => array('Symfony\\Bundle\\FrameworkBundle\\Tests\\Console\\Descriptor\\ExtendedCallableClass', 'parent::staticMethod'),
'callable_6' => function () { return 'Closure'; },
'callable_7' => new CallableClass(),
);
}
}
class CallableClass
{
public function __invoke()
{
}
public static function staticMethod()
{
}
public function method()
{
}
}
class ExtendedCallableClass extends CallableClass
{
public static function staticMethod()
{
}
}

View File

@@ -0,0 +1,27 @@
<?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 Symfony\Bundle\FrameworkBundle\Tests\Console\Descriptor;
use Symfony\Bundle\FrameworkBundle\Console\Descriptor\TextDescriptor;
class TextDescriptorTest extends AbstractDescriptorTest
{
protected function getDescriptor()
{
return new TextDescriptor();
}
protected function getFormat()
{
return 'txt';
}
}

View File

@@ -0,0 +1,27 @@
<?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 Symfony\Bundle\FrameworkBundle\Tests\Console\Descriptor;
use Symfony\Bundle\FrameworkBundle\Console\Descriptor\XmlDescriptor;
class XmlDescriptorTest extends AbstractDescriptorTest
{
protected function getDescriptor()
{
return new XmlDescriptor();
}
protected function getFormat()
{
return 'xml';
}
}