From 13edcf64e2872a2f4e12d059310c0bf8173c9e01 Mon Sep 17 00:00:00 2001 From: Fabien Potencier Date: Thu, 18 Mar 2010 08:48:03 +0100 Subject: [PATCH] converted unit tests from lime to PHPUnit for the following components: Console, DependencyInjection, EventDispatcher, OutputEscaper, and Yaml --- .../Components/Console/ApplicationTest.php | 330 ++++++++++++++++ .../Console/Command/CommandTest.php | 242 ++++++++++++ .../Console/Command/HelpCommandTest.php | 42 ++ .../Console/Command/ListCommandTest.php | 31 ++ .../Console/Helper/FormatterHelperTest.php | 35 ++ .../Console/Input/ArgvInputTest.php | 175 +++++++++ .../Console/Input/ArrayInputTest.php | 95 +++++ .../Console/Input/InputArgumentTest.php | 102 +++++ .../Console/Input/InputDefinitionTest.php | 368 ++++++++++++++++++ .../Console/Input/InputOptionTest.php | 142 +++++++ .../Components/Console/Input/InputTest.php | 126 ++++++ .../Console/Input/StringInputTest.php | 98 +++++ .../Console/Output/ConsoleOutputTest.php | 25 ++ .../Console/Output/NullOutputTest.php | 25 ++ .../Components/Console/Output/OutputTest.php | 107 +++++ .../Console/Output/StreamOutputTest.php | 56 +++ .../Console/Tester/ApplicationTesterTest.php | 60 +++ .../Console/Tester/CommandTesterTest.php | 57 +++ .../BuilderConfigurationTest.php | 205 ++++++++++ .../DependencyInjection/BuilderTest.php | 328 ++++++++++++++++ .../DependencyInjection/ContainerTest.php | 208 ++++++++++ .../DependencyInjection/CrossCheckTest.php | 88 +++++ .../DependencyInjection/DefinitionTest.php | 95 +++++ .../DependencyInjection/Dumper/DumperTest.php | 37 ++ .../Dumper/GraphvizDumperTest.php | 55 +++ .../Dumper/PhpDumperTest.php | 62 +++ .../Dumper/XmlDumperTest.php | 61 +++ .../Dumper/YamlDumperTest.php | 61 +++ .../DependencyInjection/FileResourceTest.php | 47 +++ .../Loader/FileLoaderTest.php | 57 +++ .../Loader/IniFileLoaderTest.php | 51 +++ .../Loader/LoaderExtensionTest.php | 35 ++ .../DependencyInjection/Loader/LoaderTest.php | 22 +- .../Loader/XmlFileLoaderTest.php | 203 ++++++++++ .../Loader/YamlFileLoaderTest.php | 145 +++++++ .../DependencyInjection/ParameterTest.php | 24 ++ .../DependencyInjection/ReferenceTest.php | 24 ++ .../EventDispatcher/EventDispatcherTest.php | 143 +++++++ .../Components/EventDispatcher/EventTest.php | 100 +++++ .../OutputEscaper/ArrayDecoratorTest.php | 90 +++++ .../Components/OutputEscaper/EscaperTest.php | 182 +++++++++ .../OutputEscaper/ObjectDecoratorTest.php | 59 +++ .../OutputEscaper/SafeDecoratorTest.php | 98 +++++ .../Tests/Components/Yaml/DumperTest.php | 173 ++++++++ .../Tests/Components/Yaml/InlineTest.php | 163 ++++++++ .../Tests/Components/Yaml/ParserTest.php | 103 +++++ .../Tests/bootstrap.php} | 14 +- .../Components/Console/ApplicationTest.php | 312 --------------- .../Console/Command/CommandTest.php | 206 ---------- .../Console/Command/HelpCommandTest.php | 39 -- .../Console/Command/ListCommandTest.php | 28 -- .../Console/Helper/FormatterHelperTest.php | 28 -- .../Console/Input/ArgvInputTest.php | 174 --------- .../Console/Input/ArrayInputTest.php | 93 ----- .../Console/Input/InputArgumentTest.php | 97 ----- .../Console/Input/InputDefinitionTest.php | 305 --------------- .../Console/Input/InputOptionTest.php | 138 ------- .../Components/Console/Input/InputTest.php | 124 ------ .../Console/Input/StringInputTest.php | 94 ----- .../Console/Output/ConsoleOutputTest.php | 21 - .../Components/Console/Output/OutputTest.php | 100 ----- .../Console/Output/StreamOutputTest.php | 47 --- .../Console/Tester/ApplicationTesterTest.php | 47 --- .../Console/Tester/CommandTesterTest.php | 44 --- .../BuilderConfigurationTest.php | 192 --------- .../DependencyInjection/BuilderTest.php | 309 --------------- .../DependencyInjection/ContainerTest.php | 207 ---------- .../DependencyInjection/CrossCheckTest.php | 78 ---- .../DependencyInjection/DefinitionTest.php | 84 ---- .../DependencyInjection/Dumper/DumperTest.php | 32 -- .../Dumper/GraphvizDumperTest.php | 46 --- .../Dumper/PhpDumperTest.php | 52 --- .../Dumper/XmlDumperTest.php | 51 --- .../Dumper/YamlDumperTest.php | 51 --- .../DependencyInjection/FileResourceTest.php | 31 -- .../Loader/FileLoaderTest.php | 52 --- .../Loader/IniLoaderTest.php | 42 -- .../Loader/LoaderExtensionTest.php | 32 -- .../Loader/XmlFileLoaderTest.php | 182 --------- .../Loader/YamlFileLoaderTest.php | 134 ------- .../DependencyInjection/ParameterTest.php | 21 - .../DependencyInjection/ReferenceTest.php | 21 - .../EventDispatcher/EventDispatcherTest.php | 134 ------- .../Components/EventDispatcher/EventTest.php | 80 ---- .../OutputEscaper/ArrayDecoratorTest.php | 78 ---- .../Components/OutputEscaper/EscaperTest.php | 154 -------- .../OutputEscaper/ObjectDecoratorTest.php | 47 --- .../OutputEscaper/SafeDecoratorTest.php | 93 ----- .../Symfony/Components/Yaml/DumperTest.php | 153 -------- .../Symfony/Components/Yaml/InlineTest.php | 148 ------- .../Symfony/Components/Yaml/ParserTest.php | 85 ---- 91 files changed, 5032 insertions(+), 4503 deletions(-) create mode 100644 tests/Symfony/Tests/Components/Console/ApplicationTest.php create mode 100644 tests/Symfony/Tests/Components/Console/Command/CommandTest.php create mode 100644 tests/Symfony/Tests/Components/Console/Command/HelpCommandTest.php create mode 100644 tests/Symfony/Tests/Components/Console/Command/ListCommandTest.php create mode 100644 tests/Symfony/Tests/Components/Console/Helper/FormatterHelperTest.php create mode 100644 tests/Symfony/Tests/Components/Console/Input/ArgvInputTest.php create mode 100644 tests/Symfony/Tests/Components/Console/Input/ArrayInputTest.php create mode 100644 tests/Symfony/Tests/Components/Console/Input/InputArgumentTest.php create mode 100644 tests/Symfony/Tests/Components/Console/Input/InputDefinitionTest.php create mode 100644 tests/Symfony/Tests/Components/Console/Input/InputOptionTest.php create mode 100644 tests/Symfony/Tests/Components/Console/Input/InputTest.php create mode 100644 tests/Symfony/Tests/Components/Console/Input/StringInputTest.php create mode 100644 tests/Symfony/Tests/Components/Console/Output/ConsoleOutputTest.php create mode 100644 tests/Symfony/Tests/Components/Console/Output/NullOutputTest.php create mode 100644 tests/Symfony/Tests/Components/Console/Output/OutputTest.php create mode 100644 tests/Symfony/Tests/Components/Console/Output/StreamOutputTest.php create mode 100644 tests/Symfony/Tests/Components/Console/Tester/ApplicationTesterTest.php create mode 100644 tests/Symfony/Tests/Components/Console/Tester/CommandTesterTest.php create mode 100644 tests/Symfony/Tests/Components/DependencyInjection/BuilderConfigurationTest.php create mode 100644 tests/Symfony/Tests/Components/DependencyInjection/BuilderTest.php create mode 100644 tests/Symfony/Tests/Components/DependencyInjection/ContainerTest.php create mode 100644 tests/Symfony/Tests/Components/DependencyInjection/CrossCheckTest.php create mode 100644 tests/Symfony/Tests/Components/DependencyInjection/DefinitionTest.php create mode 100644 tests/Symfony/Tests/Components/DependencyInjection/Dumper/DumperTest.php create mode 100644 tests/Symfony/Tests/Components/DependencyInjection/Dumper/GraphvizDumperTest.php create mode 100644 tests/Symfony/Tests/Components/DependencyInjection/Dumper/PhpDumperTest.php create mode 100644 tests/Symfony/Tests/Components/DependencyInjection/Dumper/XmlDumperTest.php create mode 100644 tests/Symfony/Tests/Components/DependencyInjection/Dumper/YamlDumperTest.php create mode 100644 tests/Symfony/Tests/Components/DependencyInjection/FileResourceTest.php create mode 100644 tests/Symfony/Tests/Components/DependencyInjection/Loader/FileLoaderTest.php create mode 100644 tests/Symfony/Tests/Components/DependencyInjection/Loader/IniFileLoaderTest.php create mode 100644 tests/Symfony/Tests/Components/DependencyInjection/Loader/LoaderExtensionTest.php rename tests/{unit/Symfony => Symfony/Tests}/Components/DependencyInjection/Loader/LoaderTest.php (51%) create mode 100644 tests/Symfony/Tests/Components/DependencyInjection/Loader/XmlFileLoaderTest.php create mode 100644 tests/Symfony/Tests/Components/DependencyInjection/Loader/YamlFileLoaderTest.php create mode 100644 tests/Symfony/Tests/Components/DependencyInjection/ParameterTest.php create mode 100644 tests/Symfony/Tests/Components/DependencyInjection/ReferenceTest.php create mode 100644 tests/Symfony/Tests/Components/EventDispatcher/EventDispatcherTest.php create mode 100644 tests/Symfony/Tests/Components/EventDispatcher/EventTest.php create mode 100644 tests/Symfony/Tests/Components/OutputEscaper/ArrayDecoratorTest.php create mode 100644 tests/Symfony/Tests/Components/OutputEscaper/EscaperTest.php create mode 100644 tests/Symfony/Tests/Components/OutputEscaper/ObjectDecoratorTest.php create mode 100644 tests/Symfony/Tests/Components/OutputEscaper/SafeDecoratorTest.php create mode 100644 tests/Symfony/Tests/Components/Yaml/DumperTest.php create mode 100644 tests/Symfony/Tests/Components/Yaml/InlineTest.php create mode 100644 tests/Symfony/Tests/Components/Yaml/ParserTest.php rename tests/{unit/Symfony/Components/Console/Output/NullOutputTest.php => Symfony/Tests/bootstrap.php} (50%) delete mode 100644 tests/unit/Symfony/Components/Console/ApplicationTest.php delete mode 100644 tests/unit/Symfony/Components/Console/Command/CommandTest.php delete mode 100644 tests/unit/Symfony/Components/Console/Command/HelpCommandTest.php delete mode 100644 tests/unit/Symfony/Components/Console/Command/ListCommandTest.php delete mode 100644 tests/unit/Symfony/Components/Console/Helper/FormatterHelperTest.php delete mode 100644 tests/unit/Symfony/Components/Console/Input/ArgvInputTest.php delete mode 100644 tests/unit/Symfony/Components/Console/Input/ArrayInputTest.php delete mode 100644 tests/unit/Symfony/Components/Console/Input/InputArgumentTest.php delete mode 100644 tests/unit/Symfony/Components/Console/Input/InputDefinitionTest.php delete mode 100644 tests/unit/Symfony/Components/Console/Input/InputOptionTest.php delete mode 100644 tests/unit/Symfony/Components/Console/Input/InputTest.php delete mode 100644 tests/unit/Symfony/Components/Console/Input/StringInputTest.php delete mode 100644 tests/unit/Symfony/Components/Console/Output/ConsoleOutputTest.php delete mode 100644 tests/unit/Symfony/Components/Console/Output/OutputTest.php delete mode 100644 tests/unit/Symfony/Components/Console/Output/StreamOutputTest.php delete mode 100644 tests/unit/Symfony/Components/Console/Tester/ApplicationTesterTest.php delete mode 100644 tests/unit/Symfony/Components/Console/Tester/CommandTesterTest.php delete mode 100644 tests/unit/Symfony/Components/DependencyInjection/BuilderConfigurationTest.php delete mode 100644 tests/unit/Symfony/Components/DependencyInjection/BuilderTest.php delete mode 100644 tests/unit/Symfony/Components/DependencyInjection/ContainerTest.php delete mode 100644 tests/unit/Symfony/Components/DependencyInjection/CrossCheckTest.php delete mode 100644 tests/unit/Symfony/Components/DependencyInjection/DefinitionTest.php delete mode 100644 tests/unit/Symfony/Components/DependencyInjection/Dumper/DumperTest.php delete mode 100644 tests/unit/Symfony/Components/DependencyInjection/Dumper/GraphvizDumperTest.php delete mode 100644 tests/unit/Symfony/Components/DependencyInjection/Dumper/PhpDumperTest.php delete mode 100644 tests/unit/Symfony/Components/DependencyInjection/Dumper/XmlDumperTest.php delete mode 100644 tests/unit/Symfony/Components/DependencyInjection/Dumper/YamlDumperTest.php delete mode 100644 tests/unit/Symfony/Components/DependencyInjection/FileResourceTest.php delete mode 100644 tests/unit/Symfony/Components/DependencyInjection/Loader/FileLoaderTest.php delete mode 100644 tests/unit/Symfony/Components/DependencyInjection/Loader/IniLoaderTest.php delete mode 100644 tests/unit/Symfony/Components/DependencyInjection/Loader/LoaderExtensionTest.php delete mode 100644 tests/unit/Symfony/Components/DependencyInjection/Loader/XmlFileLoaderTest.php delete mode 100644 tests/unit/Symfony/Components/DependencyInjection/Loader/YamlFileLoaderTest.php delete mode 100644 tests/unit/Symfony/Components/DependencyInjection/ParameterTest.php delete mode 100644 tests/unit/Symfony/Components/DependencyInjection/ReferenceTest.php delete mode 100644 tests/unit/Symfony/Components/EventDispatcher/EventDispatcherTest.php delete mode 100644 tests/unit/Symfony/Components/EventDispatcher/EventTest.php delete mode 100644 tests/unit/Symfony/Components/OutputEscaper/ArrayDecoratorTest.php delete mode 100644 tests/unit/Symfony/Components/OutputEscaper/EscaperTest.php delete mode 100644 tests/unit/Symfony/Components/OutputEscaper/ObjectDecoratorTest.php delete mode 100644 tests/unit/Symfony/Components/OutputEscaper/SafeDecoratorTest.php delete mode 100644 tests/unit/Symfony/Components/Yaml/DumperTest.php delete mode 100644 tests/unit/Symfony/Components/Yaml/InlineTest.php delete mode 100644 tests/unit/Symfony/Components/Yaml/ParserTest.php diff --git a/tests/Symfony/Tests/Components/Console/ApplicationTest.php b/tests/Symfony/Tests/Components/Console/ApplicationTest.php new file mode 100644 index 0000000000..9084040943 --- /dev/null +++ b/tests/Symfony/Tests/Components/Console/ApplicationTest.php @@ -0,0 +1,330 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Tests\Components\Console; + +require_once __DIR__.'/../../bootstrap.php'; + +use Symfony\Components\Console\Application; +use Symfony\Components\Console\Input\ArrayInput; +use Symfony\Components\Console\Output\Output; +use Symfony\Components\Console\Output\StreamOutput; +use Symfony\Components\Console\Tester\ApplicationTester; + +class ApplicationTest extends \PHPUnit_Framework_TestCase +{ + static protected $fixturesPath; + + static public function setUpBeforeClass() + { + self::$fixturesPath = realpath(__DIR__.'/../../../../fixtures/Symfony/Components/Console/'); + require_once self::$fixturesPath.'/FooCommand.php'; + require_once self::$fixturesPath.'/Foo1Command.php'; + require_once self::$fixturesPath.'/Foo2Command.php'; + } + + public function testConstructor() + { + $application = new Application('foo', 'bar'); + $this->assertEquals($application->getName(), 'foo', '__construct() takes the application name as its first argument'); + $this->assertEquals($application->getVersion(), 'bar', '__construct() takes the application version as its first argument'); + $this->assertEquals(array_keys($application->getCommands()), array('help', 'list'), '__construct() registered the help and list commands by default'); + } + + public function testSetGetName() + { + $application = new Application(); + $application->setName('foo'); + $this->assertEquals($application->getName(), 'foo', '->setName() sets the name of the application'); + } + + public function testSetGetVersion() + { + $application = new Application(); + $application->setVersion('bar'); + $this->assertEquals($application->getVersion(), 'bar', '->setVersion() sets the version of the application'); + } + + public function testGetLongVersion() + { + $application = new Application('foo', 'bar'); + $this->assertEquals($application->getLongVersion(), 'foo version bar', '->getLongVersion() returns the long version of the application'); + } + + public function testHelp() + { + $application = new Application(); + $this->assertEquals($application->getHelp(), file_get_contents(self::$fixturesPath.'/application_gethelp.txt'), '->setHelp() returns a help message'); + } + + public function testGetCommands() + { + $application = new Application(); + $commands = $application->getCommands(); + $this->assertEquals(get_class($commands['help']), 'Symfony\\Components\\Console\\Command\\HelpCommand', '->getCommands() returns the registered commands'); + + $application->addCommand(new \FooCommand()); + $commands = $application->getCommands('foo'); + $this->assertEquals(count($commands), 1, '->getCommands() takes a namespace as its first argument'); + } + + public function testRegister() + { + $application = new Application(); + $command = $application->register('foo'); + $this->assertEquals($command->getName(), 'foo', '->register() regiters a new command'); + } + + public function testAddCommand() + { + $application = new Application(); + $application->addCommand($foo = new \FooCommand()); + $commands = $application->getCommands(); + $this->assertEquals($commands['foo:bar'], $foo, '->addCommand() registers a command'); + + $application = new Application(); + $application->addCommands(array($foo = new \FooCommand(), $foo1 = new \Foo1Command())); + $commands = $application->getCommands(); + $this->assertEquals(array($commands['foo:bar'], $commands['foo:bar1']), array($foo, $foo1), '->addCommands() registers an array of commands'); + } + + public function testHasGetCommand() + { + $application = new Application(); + $this->assertTrue($application->hasCommand('list'), '->hasCommand() returns true if a named command is registered'); + $this->assertTrue(!$application->hasCommand('afoobar'), '->hasCommand() returns false if a named command is not registered'); + + $application->addCommand($foo = new \FooCommand()); + $this->assertTrue($application->hasCommand('afoobar'), '->hasCommand() returns true if an alias is registered'); + $this->assertEquals($application->getCommand('foo:bar'), $foo, '->getCommand() returns a command by name'); + $this->assertEquals($application->getCommand('afoobar'), $foo, '->getCommand() returns a command by alias'); + + try + { + $application->getCommand('foofoo'); + $this->fail('->getCommand() throws an \InvalidArgumentException if the command does not exist'); + } + catch (\InvalidArgumentException $e) + { + } + + $application = new TestApplication(); + $application->addCommand($foo = new \FooCommand()); + $application->setWantHelps(); + $command = $application->getCommand('foo:bar'); + $this->assertEquals(get_class($command), 'Symfony\Components\Console\Command\HelpCommand', '->getCommand() returns the help command if --help is provided as the input'); + } + + public function testGetNamespaces() + { + $application = new TestApplication(); + $application->addCommand(new \FooCommand()); + $application->addCommand(new \Foo1Command()); + $this->assertEquals($application->getNamespaces(), array('foo'), '->getNamespaces() returns an array of unique used namespaces'); + } + + public function testFindNamespace() + { + $application = new TestApplication(); + $application->addCommand(new \FooCommand()); + $this->assertEquals($application->findNamespace('foo'), 'foo', '->findNamespace() returns the given namespace if it exists'); + $this->assertEquals($application->findNamespace('f'), 'foo', '->findNamespace() finds a namespace given an abbreviation'); + $application->addCommand(new \Foo2Command()); + $this->assertEquals($application->findNamespace('foo'), 'foo', '->findNamespace() returns the given namespace if it exists'); + try + { + $application->findNamespace('f'); + $this->fail('->findNamespace() throws an \InvalidArgumentException if the abbreviation is ambiguous'); + } + catch (\InvalidArgumentException $e) + { + } + + try + { + $application->findNamespace('bar'); + $this->fail('->findNamespace() throws an \InvalidArgumentException if no command is in the given namespace'); + } + catch (\InvalidArgumentException $e) + { + } + } + + public function testFindCommand() + { + $application = new TestApplication(); + $application->addCommand(new \FooCommand()); + $this->assertEquals(get_class($application->findCommand('foo:bar')), 'FooCommand', '->findCommand() returns a command if its name exists'); + $this->assertEquals(get_class($application->findCommand('h')), 'Symfony\Components\Console\Command\HelpCommand', '->findCommand() returns a command if its name exists'); + $this->assertEquals(get_class($application->findCommand('f:bar')), 'FooCommand', '->findCommand() returns a command if the abbreviation for the namespace exists'); + $this->assertEquals(get_class($application->findCommand('f:b')), 'FooCommand', '->findCommand() returns a command if the abbreviation for the namespace and the command name exist'); + $this->assertEquals(get_class($application->findCommand('a')), 'FooCommand', '->findCommand() returns a command if the abbreviation exists for an alias'); + + $application->addCommand(new \Foo1Command()); + $application->addCommand(new \Foo2Command()); + + try + { + $application->findCommand('f'); + $this->fail('->findCommand() throws an \InvalidArgumentException if the abbreviation is ambiguous for a namespace'); + } + catch (\InvalidArgumentException $e) + { + } + + try + { + $application->findCommand('a'); + $this->fail('->findCommand() throws an \InvalidArgumentException if the abbreviation is ambiguous for an alias'); + } + catch (\InvalidArgumentException $e) + { + } + + try + { + $application->findCommand('foo:b'); + $this->fail('->findCommand() throws an \InvalidArgumentException if the abbreviation is ambiguous for a command'); + } + catch (\InvalidArgumentException $e) + { + } + } + + public function testSetCatchExceptions() + { + $application = new Application(); + $application->setAutoExit(false); + $tester = new ApplicationTester($application); + + $application->setCatchExceptions(true); + $tester->run(array('command' => 'foo')); + $this->assertEquals($tester->getDisplay(), file_get_contents(self::$fixturesPath.'/application_renderexception1.txt'), '->setCatchExceptions() sets the catch exception flag'); + + $application->setCatchExceptions(false); + try + { + $tester->run(array('command' => 'foo')); + $this->fail('->setCatchExceptions() sets the catch exception flag'); + } + catch (\Exception $e) + { + } + } + + public function testAsText() + { + $application = new Application(); + $application->addCommand(new \FooCommand); + $this->assertEquals($application->asText(), file_get_contents(self::$fixturesPath.'/application_astext1.txt'), '->asText() returns a text representation of the application'); + $this->assertEquals($application->asText('foo'), file_get_contents(self::$fixturesPath.'/application_astext2.txt'), '->asText() returns a text representation of the application'); + } + + public function testAsXml() + { + $application = new Application(); + $application->addCommand(new \FooCommand); + $this->assertEquals($application->asXml(), file_get_contents(self::$fixturesPath.'/application_asxml1.txt'), '->asXml() returns an XML representation of the application'); + $this->assertEquals($application->asXml('foo'), file_get_contents(self::$fixturesPath.'/application_asxml2.txt'), '->asXml() returns an XML representation of the application'); + } + + public function testRenderException() + { + $application = new Application(); + $application->setAutoExit(false); + $tester = new ApplicationTester($application); + + $tester->run(array('command' => 'foo')); + $this->assertEquals($tester->getDisplay(), file_get_contents(self::$fixturesPath.'/application_renderexception1.txt'), '->renderException() renders a pretty exception'); + + $tester->run(array('command' => 'foo'), array('verbosity' => Output::VERBOSITY_VERBOSE)); + $this->assertRegExp('/Exception trace/', $tester->getDisplay(), '->renderException() renders a pretty exception with a stack trace when verbosity is verbose'); + + $tester->run(array('command' => 'list', '--foo' => true)); + $this->assertEquals($tester->getDisplay(), file_get_contents(self::$fixturesPath.'/application_renderexception2.txt'), '->renderException() renders the command synopsis when an exception occurs in the context of a command'); + } + + public function testRun() + { + $application = new Application(); + $application->setAutoExit(false); + $application->setCatchExceptions(false); + $application->addCommand($command = new \Foo1Command()); + $_SERVER['argv'] = array('cli.php', 'foo:bar1'); + + ob_start(); + $application->run(); + ob_end_clean(); + + $this->assertEquals(get_class($command->input), 'Symfony\Components\Console\Input\ArgvInput', '->run() creates an ArgvInput by default if none is given'); + $this->assertEquals(get_class($command->output), 'Symfony\Components\Console\Output\ConsoleOutput', '->run() creates a ConsoleOutput by default if none is given'); + + $application = new Application(); + $application->setAutoExit(false); + $application->setCatchExceptions(false); + $tester = new ApplicationTester($application); + $tester->run(array()); + $this->assertEquals($tester->getDisplay(), file_get_contents(self::$fixturesPath.'/application_run1.txt'), '->run() runs the list command if no argument is passed'); + + $tester->run(array('--help' => true)); + $this->assertEquals($tester->getDisplay(), file_get_contents(self::$fixturesPath.'/application_run2.txt'), '->run() runs the help command if --help is passed'); + + $application = new Application(); + $application->setAutoExit(false); + $application->setCatchExceptions(false); + $tester = new ApplicationTester($application); + $tester->run(array('command' => 'list', '--help' => true)); + $this->assertEquals($tester->getDisplay(), file_get_contents(self::$fixturesPath.'/application_run3.txt'), '->run() displays the help if --help is passed'); + + $application = new Application(); + $application->setAutoExit(false); + $application->setCatchExceptions(false); + $tester = new ApplicationTester($application); + $tester->run(array('--color' => true)); + $this->assertTrue($tester->getOutput()->isDecorated(), '->run() forces color output if --color is passed'); + + $application = new Application(); + $application->setAutoExit(false); + $application->setCatchExceptions(false); + $tester = new ApplicationTester($application); + $tester->run(array('--version' => true)); + $this->assertEquals($tester->getDisplay(), file_get_contents(self::$fixturesPath.'/application_run4.txt'), '->run() displays the program version if --version is passed'); + + $application = new Application(); + $application->setAutoExit(false); + $application->setCatchExceptions(false); + $tester = new ApplicationTester($application); + $tester->run(array('command' => 'list', '--quiet' => true)); + $this->assertEquals($tester->getDisplay(), '', '->run() removes all output if --quiet is passed'); + + $application = new Application(); + $application->setAutoExit(false); + $application->setCatchExceptions(false); + $tester = new ApplicationTester($application); + $tester->run(array('command' => 'list', '--verbose' => true)); + $this->assertEquals($tester->getOutput()->getVerbosity(), Output::VERBOSITY_VERBOSE, '->run() sets the output to verbose is --verbose is passed'); + + $application = new Application(); + $application->setAutoExit(false); + $application->setCatchExceptions(false); + $application->addCommand(new \FooCommand()); + $tester = new ApplicationTester($application); + $tester->run(array('command' => 'foo:bar', '--no-interaction' => true)); + $this->assertEquals($tester->getDisplay(), "called\n", '->run() does not called interact() if --no-interaction is passed'); + } +} + +class TestApplication extends Application +{ + public function setWantHelps() + { + $this->wantHelps = true; + } +} diff --git a/tests/Symfony/Tests/Components/Console/Command/CommandTest.php b/tests/Symfony/Tests/Components/Console/Command/CommandTest.php new file mode 100644 index 0000000000..dbb7e32141 --- /dev/null +++ b/tests/Symfony/Tests/Components/Console/Command/CommandTest.php @@ -0,0 +1,242 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Tests\Components\Console\Command; + +require_once __DIR__.'/../../../bootstrap.php'; + +use Symfony\Components\Console\Command\Command; +use Symfony\Components\Console\Application; +use Symfony\Components\Console\Input\InputDefinition; +use Symfony\Components\Console\Input\InputArgument; +use Symfony\Components\Console\Input\InputOption; +use Symfony\Components\Console\Input\InputInterface; +use Symfony\Components\Console\Input\StringInput; +use Symfony\Components\Console\Output\OutputInterface; +use Symfony\Components\Console\Output\NullOutput; +use Symfony\Components\Console\Output\StreamOutput; +use Symfony\Components\Console\Tester\CommandTester; + +class CommandTest extends \PHPUnit_Framework_TestCase +{ + static protected $fixturesPath; + + static public function setUpBeforeClass() + { + self::$fixturesPath = __DIR__.'/../../../../../fixtures/Symfony/Components/Console/'; + require_once self::$fixturesPath.'/TestCommand.php'; + } + + public function testConstructor() + { + $application = new Application(); + try + { + $command = new Command(); + $this->fail('__construct() throws a \LogicException if the name is null'); + } + catch (\LogicException $e) + { + } + $command = new Command('foo:bar'); + $this->assertEquals($command->getFullName(), 'foo:bar', '__construct() takes the command name as its first argument'); + } + + public function testSetApplication() + { + $application = new Application(); + $command = new \TestCommand(); + $command->setApplication($application); + $this->assertEquals($command->getApplication(), $application, '->setApplication() sets the current application'); + } + + public function testSetGetDefinition() + { + $command = new \TestCommand(); + $ret = $command->setDefinition($definition = new InputDefinition()); + $this->assertEquals($ret, $command, '->setDefinition() implements a fluent interface'); + $this->assertEquals($command->getDefinition(), $definition, '->setDefinition() sets the current InputDefinition instance'); + $command->setDefinition(array(new InputArgument('foo'), new InputOption('bar'))); + $this->assertTrue($command->getDefinition()->hasArgument('foo'), '->setDefinition() also takes an array of InputArguments and InputOptions as an argument'); + $this->assertTrue($command->getDefinition()->hasOption('bar'), '->setDefinition() also takes an array of InputArguments and InputOptions as an argument'); + $command->setDefinition(new InputDefinition()); + } + + public function testAddArgument() + { + $command = new \TestCommand(); + $ret = $command->addArgument('foo'); + $this->assertEquals($ret, $command, '->addArgument() implements a fluent interface'); + $this->assertTrue($command->getDefinition()->hasArgument('foo'), '->addArgument() adds an argument to the command'); + } + + public function testAddOption() + { + $command = new \TestCommand(); + $ret = $command->addOption('foo'); + $this->assertEquals($ret, $command, '->addOption() implements a fluent interface'); + $this->assertTrue($command->getDefinition()->hasOption('foo'), '->addOption() adds an option to the command'); + } + + public function testgetNamespaceGetNameGetFullNameSetName() + { + $command = new \TestCommand(); + $this->assertEquals($command->getNamespace(), 'namespace', '->getNamespace() returns the command namespace'); + $this->assertEquals($command->getName(), 'name', '->getName() returns the command name'); + $this->assertEquals($command->getFullName(), 'namespace:name', '->getNamespace() returns the full command name'); + $command->setName('foo'); + $this->assertEquals($command->getName(), 'foo', '->setName() sets the command name'); + + $command->setName(':bar'); + $this->assertEquals($command->getName(), 'bar', '->setName() sets the command name'); + $this->assertEquals($command->getNamespace(), '', '->setName() can set the command namespace'); + + $ret = $command->setName('foobar:bar'); + $this->assertEquals($ret, $command, '->setName() implements a fluent interface'); + $this->assertEquals($command->getName(), 'bar', '->setName() sets the command name'); + $this->assertEquals($command->getNamespace(), 'foobar', '->setName() can set the command namespace'); + + try + { + $command->setName(''); + $this->fail('->setName() throws an \InvalidArgumentException if the name is empty'); + } + catch (\InvalidArgumentException $e) + { + } + + try + { + $command->setName('foo:'); + $this->fail('->setName() throws an \InvalidArgumentException if the name is empty'); + } + catch (\InvalidArgumentException $e) + { + } + } + + public function testGetSetDescription() + { + $command = new \TestCommand(); + $this->assertEquals($command->getDescription(), 'description', '->getDescription() returns the description'); + $ret = $command->setDescription('description1'); + $this->assertEquals($ret, $command, '->setDescription() implements a fluent interface'); + $this->assertEquals($command->getDescription(), 'description1', '->setDescription() sets the description'); + } + + public function testGetSetHelp() + { + $command = new \TestCommand(); + $this->assertEquals($command->getHelp(), 'help', '->getHelp() returns the help'); + $ret = $command->setHelp('help1'); + $this->assertEquals($ret, $command, '->setHelp() implements a fluent interface'); + $this->assertEquals($command->getHelp(), 'help1', '->setHelp() sets the help'); + } + + public function testGetSetAliases() + { + $command = new \TestCommand(); + $this->assertEquals($command->getAliases(), array('name'), '->getAliases() returns the aliases'); + $ret = $command->setAliases(array('name1')); + $this->assertEquals($ret, $command, '->setAliases() implements a fluent interface'); + $this->assertEquals($command->getAliases(), array('name1'), '->setAliases() sets the aliases'); + } + + public function testGetSynopsis() + { + $command = new \TestCommand(); + $command->addOption('foo'); + $command->addArgument('foo'); + $this->assertEquals($command->getSynopsis(), 'namespace:name [--foo] [foo]', '->getSynopsis() returns the synopsis'); + } + + public function testMergeApplicationDefinition() + { + $application1 = new Application(); + $application1->getDefinition()->addArguments(array(new InputArgument('foo'))); + $application1->getDefinition()->addOptions(array(new InputOption('bar'))); + $command = new \TestCommand(); + $command->setApplication($application1); + $command->setDefinition($definition = new InputDefinition(array(new InputArgument('bar'), new InputOption('foo')))); + $command->mergeApplicationDefinition(); + $this->assertTrue($command->getDefinition()->hasArgument('foo'), '->mergeApplicationDefinition() merges the application arguments and the command arguments'); + $this->assertTrue($command->getDefinition()->hasArgument('bar'), '->mergeApplicationDefinition() merges the application arguments and the command arguments'); + $this->assertTrue($command->getDefinition()->hasOption('foo'), '->mergeApplicationDefinition() merges the application options and the command options'); + $this->assertTrue($command->getDefinition()->hasOption('bar'), '->mergeApplicationDefinition() merges the application options and the command options'); + + $command->mergeApplicationDefinition(); + $this->assertEquals($command->getDefinition()->getArgumentCount(), 3, '->mergeApplicationDefinition() does not try to merge twice the application arguments and options'); + + $command = new \TestCommand(); + $command->mergeApplicationDefinition(); + } + + public function testRun() + { + $command = new \TestCommand(); + $application = new Application(); + $command->setApplication($application); + $tester = new CommandTester($command); + try + { + $tester->execute(array('--bar' => true)); + $this->fail('->run() throws a \RuntimeException when the input does not validate the current InputDefinition'); + } + catch (\RuntimeException $e) + { + } + + $this->assertEquals($tester->execute(array(), array('interactive' => true)), "interact called\nexecute called\n", '->run() calls the interact() method if the input is interactive'); + $this->assertEquals($tester->execute(array(), array('interactive' => false)), "execute called\n", '->run() does not call the interact() method if the input is not interactive'); + + $command = new Command('foo'); + try + { + $command->run(new StringInput(''), new NullOutput()); + $this->fail('->run() throws a \LogicException if the execute() method has not been overriden and no code has been provided'); + } + catch (\LogicException $e) + { + } + } + + public function testSetCode() + { + $application = new Application(); + $command = new \TestCommand(); + $command->setApplication($application); + $ret = $command->setCode(function (InputInterface $input, OutputInterface $output) + { + $output->writeln('from the code...'); + }); + $this->assertEquals($ret, $command, '->setCode() implements a fluent interface'); + $tester = new CommandTester($command); + $tester->execute(array()); + $this->assertEquals($tester->getDisplay(), "interact called\nfrom the code...\n"); + } + + public function testAsText() + { + $command = new \TestCommand(); + $command->setApplication(new Application()); + $tester = new CommandTester($command); + $tester->execute(array()); + $this->assertEquals($command->asText(), file_get_contents(self::$fixturesPath.'/command_astext.txt'), '->asText() returns a text representation of the command'); + } + + public function testAsXml() + { + $command = new \TestCommand(); + $command->setApplication(new Application()); + $tester = new CommandTester($command); + $tester->execute(array()); + $this->assertEquals($command->asXml(), file_get_contents(self::$fixturesPath.'/command_asxml.txt'), '->asXml() returns an XML representation of the command'); + } +} diff --git a/tests/Symfony/Tests/Components/Console/Command/HelpCommandTest.php b/tests/Symfony/Tests/Components/Console/Command/HelpCommandTest.php new file mode 100644 index 0000000000..2c61f44727 --- /dev/null +++ b/tests/Symfony/Tests/Components/Console/Command/HelpCommandTest.php @@ -0,0 +1,42 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Tests\Components\Console\Command; + +require_once __DIR__.'/../../../bootstrap.php'; + +use Symfony\Components\Console\Tester\CommandTester; +use Symfony\Components\Console\Command\HelpCommand; +use Symfony\Components\Console\Command\ListCommand; +use Symfony\Components\Console\Application; + +class HelpCommandTest extends \PHPUnit_Framework_TestCase +{ + public function testExecute() + { + $command = new HelpCommand(); + $command->setCommand(new ListCommand()); + + $commandTester = new CommandTester($command); + $commandTester->execute(array()); + $this->assertRegExp('/list \[--xml\] \[namespace\]/', $commandTester->getDisplay(), '->execute() returns a text help for the given command'); + + $commandTester->execute(array('--xml' => true)); + $this->assertRegExp('/getDisplay(), '->execute() returns an XML help text if --xml is passed'); + + $application = new Application(); + $commandTester = new CommandTester($application->getCommand('help')); + $commandTester->execute(array('command_name' => 'list')); + $this->assertRegExp('/list \[--xml\] \[namespace\]/', $commandTester->getDisplay(), '->execute() returns a text help for the given command'); + + $commandTester->execute(array('command_name' => 'list', '--xml' => true)); + $this->assertRegExp('/getDisplay(), '->execute() returns an XML help text if --xml is passed'); + } +} diff --git a/tests/Symfony/Tests/Components/Console/Command/ListCommandTest.php b/tests/Symfony/Tests/Components/Console/Command/ListCommandTest.php new file mode 100644 index 0000000000..e7c81b41f3 --- /dev/null +++ b/tests/Symfony/Tests/Components/Console/Command/ListCommandTest.php @@ -0,0 +1,31 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Tests\Components\Console\Command; + +require_once __DIR__.'/../../../bootstrap.php'; + +use Symfony\Components\Console\Tester\CommandTester; +use Symfony\Components\Console\Application; + +class ListCommandTest extends \PHPUnit_Framework_TestCase +{ + public function testExecute() + { + $application = new Application(); + + $commandTester = new CommandTester($application->getCommand('list')); + $commandTester->execute(array()); + $this->assertRegExp('/help Displays help for a command/', $commandTester->getDisplay(), '->execute() returns a list of available commands'); + + $commandTester->execute(array('--xml' => true)); + $this->assertRegExp('//', $commandTester->getDisplay(), '->execute() returns a list of available commands in XML if --xml is passed'); + } +} diff --git a/tests/Symfony/Tests/Components/Console/Helper/FormatterHelperTest.php b/tests/Symfony/Tests/Components/Console/Helper/FormatterHelperTest.php new file mode 100644 index 0000000000..7d237c13ed --- /dev/null +++ b/tests/Symfony/Tests/Components/Console/Helper/FormatterHelperTest.php @@ -0,0 +1,35 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Tests\Components\Console\Formatter; + +require_once __DIR__.'/../../../bootstrap.php'; + +use Symfony\Components\Console\Helper\FormatterHelper; + +class FormatterHelperTest extends \PHPUnit_Framework_TestCase +{ + public function testFormatSection() + { + $formatter = new FormatterHelper(); + + $this->assertEquals($formatter->formatSection('cli', 'Some text to display'), '[cli] Some text to display', '::formatSection() formats a message in a section'); + } + + public function testFormatBlock() + { + $formatter = new FormatterHelper(); + + $this->assertEquals($formatter->formatBlock('Some text to display', 'error'), ' Some text to display ', '::formatBlock() formats a message in a block'); + $this->assertEquals($formatter->formatBlock(array('Some text to display', 'foo bar'), 'error'), " Some text to display \n foo bar ", '::formatBlock() formats a message in a block'); + + $this->assertEquals($formatter->formatBlock('Some text to display', 'error', true), " \n Some text to display \n ", '::formatBlock() formats a message in a block'); + } +} diff --git a/tests/Symfony/Tests/Components/Console/Input/ArgvInputTest.php b/tests/Symfony/Tests/Components/Console/Input/ArgvInputTest.php new file mode 100644 index 0000000000..40864fba33 --- /dev/null +++ b/tests/Symfony/Tests/Components/Console/Input/ArgvInputTest.php @@ -0,0 +1,175 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Tests\Components\Console\Input; + +require_once __DIR__.'/../../../bootstrap.php'; + +use Symfony\Components\Console\Input\ArgvInput; +use Symfony\Components\Console\Input\InputDefinition; +use Symfony\Components\Console\Input\InputArgument; +use Symfony\Components\Console\Input\InputOption; + +class ArgvInputTest extends \PHPUnit_Framework_TestCase +{ + public function testConstructor() + { + $_SERVER['argv'] = array('cli.php', 'foo'); + $input = new TestInput(); + $this->assertEquals($input->getTokens(), array('foo'), '__construct() automatically get its input from the argv server variable'); + } + + public function testParser() + { + $input = new TestInput(array('cli.php', 'foo')); + $input->bind(new InputDefinition(array(new InputArgument('name')))); + $this->assertEquals($input->getArguments(), array('name' => 'foo'), '->parse() parses required arguments'); + + $input->bind(new InputDefinition(array(new InputArgument('name')))); + $this->assertEquals($input->getArguments(), array('name' => 'foo'), '->parse() is stateless'); + + $input = new TestInput(array('cli.php', '--foo')); + $input->bind(new InputDefinition(array(new InputOption('foo')))); + $this->assertEquals($input->getOptions(), array('foo' => true), '->parse() parses long options without parameter'); + + $input = new TestInput(array('cli.php', '--foo=bar')); + $input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::PARAMETER_REQUIRED)))); + $this->assertEquals($input->getOptions(), array('foo' => 'bar'), '->parse() parses long options with a required parameter (with a = separator)'); + + $input = new TestInput(array('cli.php', '--foo', 'bar')); + $input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::PARAMETER_REQUIRED)))); + $this->assertEquals($input->getOptions(), array('foo' => 'bar'), '->parse() parses long options with a required parameter (with a space separator)'); + + try + { + $input = new TestInput(array('cli.php', '--foo')); + $input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::PARAMETER_REQUIRED)))); + $this->fail('->parse() throws a \RuntimeException if no parameter is passed to an option when it is required'); + } + catch (\RuntimeException $e) + { + } + + $input = new TestInput(array('cli.php', '-f')); + $input->bind(new InputDefinition(array(new InputOption('foo', 'f')))); + $this->assertEquals($input->getOptions(), array('foo' => true), '->parse() parses short options without parameter'); + + $input = new TestInput(array('cli.php', '-fbar')); + $input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::PARAMETER_REQUIRED)))); + $this->assertEquals($input->getOptions(), array('foo' => 'bar'), '->parse() parses short options with a required parameter (with no separator)'); + + $input = new TestInput(array('cli.php', '-f', 'bar')); + $input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::PARAMETER_REQUIRED)))); + $this->assertEquals($input->getOptions(), array('foo' => 'bar'), '->parse() parses short options with a required parameter (with a space separator)'); + + $input = new TestInput(array('cli.php', '-f', '-b', 'foo')); + $input->bind(new InputDefinition(array(new InputArgument('name'), new InputOption('foo', 'f', InputOption::PARAMETER_OPTIONAL), new InputOption('bar', 'b')))); + $this->assertEquals($input->getOptions(), array('foo' => null, 'bar' => true), '->parse() parses short options with an optional parameter which is not present'); + + try + { + $input = new TestInput(array('cli.php', '-f')); + $input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::PARAMETER_REQUIRED)))); + $this->fail('->parse() throws a \RuntimeException if no parameter is passed to an option when it is required'); + } + catch (\RuntimeException $e) + { + } + + try + { + $input = new TestInput(array('cli.php', '-ffoo')); + $input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::PARAMETER_NONE)))); + $this->fail('->parse() throws a \RuntimeException if a value is passed to an option which does not take one'); + } + catch (\RuntimeException $e) + { + } + + try + { + $input = new TestInput(array('cli.php', 'foo', 'bar')); + $input->bind(new InputDefinition()); + $this->fail('->parse() throws a \RuntimeException if too many arguments are passed'); + } + catch (\RuntimeException $e) + { + } + + try + { + $input = new TestInput(array('cli.php', '--foo')); + $input->bind(new InputDefinition()); + $this->fail('->parse() throws a \RuntimeException if an unknown long option is passed'); + } + catch (\RuntimeException $e) + { + } + + try + { + $input = new TestInput(array('cli.php', '-f')); + $input->bind(new InputDefinition()); + $this->fail('->parse() throws a \RuntimeException if an unknown short option is passed'); + } + catch (\RuntimeException $e) + { + } + + $input = new TestInput(array('cli.php', '-fb')); + $input->bind(new InputDefinition(array(new InputOption('foo', 'f'), new InputOption('bar', 'b')))); + $this->assertEquals($input->getOptions(), array('foo' => true, 'bar' => true), '->parse() parses short options when they are aggregated as a single one'); + + $input = new TestInput(array('cli.php', '-fb', 'bar')); + $input->bind(new InputDefinition(array(new InputOption('foo', 'f'), new InputOption('bar', 'b', InputOption::PARAMETER_REQUIRED)))); + $this->assertEquals($input->getOptions(), array('foo' => true, 'bar' => 'bar'), '->parse() parses short options when they are aggregated as a single one and the last one has a required parameter'); + + $input = new TestInput(array('cli.php', '-fb', 'bar')); + $input->bind(new InputDefinition(array(new InputOption('foo', 'f'), new InputOption('bar', 'b', InputOption::PARAMETER_OPTIONAL)))); + $this->assertEquals($input->getOptions(), array('foo' => true, 'bar' => 'bar'), '->parse() parses short options when they are aggregated as a single one and the last one has an optional parameter'); + + $input = new TestInput(array('cli.php', '-fbbar')); + $input->bind(new InputDefinition(array(new InputOption('foo', 'f'), new InputOption('bar', 'b', InputOption::PARAMETER_OPTIONAL)))); + $this->assertEquals($input->getOptions(), array('foo' => true, 'bar' => 'bar'), '->parse() parses short options when they are aggregated as a single one and the last one has an optional parameter with no separator'); + + $input = new TestInput(array('cli.php', '-fbbar')); + $input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::PARAMETER_OPTIONAL), new InputOption('bar', 'b', InputOption::PARAMETER_OPTIONAL)))); + $this->assertEquals($input->getOptions(), array('foo' => 'bbar', 'bar' => null), '->parse() parses short options when they are aggregated as a single one and one of them takes a parameter'); + } + + public function testGetFirstArgument() + { + $input = new TestInput(array('cli.php', '-fbbar')); + $this->assertEquals($input->getFirstArgument(), '', '->getFirstArgument() returns the first argument from the raw input'); + + $input = new TestInput(array('cli.php', '-fbbar', 'foo')); + $this->assertEquals($input->getFirstArgument(), 'foo', '->getFirstArgument() returns the first argument from the raw input'); + } + + public function testHasParameterOption() + { + $input = new TestInput(array('cli.php', '-f', 'foo')); + $this->assertTrue($input->hasParameterOption('-f'), '->hasParameterOption() returns true if the given short option is in the raw input'); + + $input = new TestInput(array('cli.php', '--foo', 'foo')); + $this->assertTrue($input->hasParameterOption('--foo'), '->hasParameterOption() returns true if the given short option is in the raw input'); + + $input = new TestInput(array('cli.php', 'foo')); + $this->assertTrue(!$input->hasParameterOption('--foo'), '->hasParameterOption() returns false if the given short option is not in the raw input'); + } +} + +class TestInput extends ArgvInput +{ + public function getTokens() + { + return $this->tokens; + } +} diff --git a/tests/Symfony/Tests/Components/Console/Input/ArrayInputTest.php b/tests/Symfony/Tests/Components/Console/Input/ArrayInputTest.php new file mode 100644 index 0000000000..f75f66219d --- /dev/null +++ b/tests/Symfony/Tests/Components/Console/Input/ArrayInputTest.php @@ -0,0 +1,95 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Tests\Components\Console\Input; + +require_once __DIR__.'/../../../bootstrap.php'; + +use Symfony\Components\Console\Input\ArrayInput; +use Symfony\Components\Console\Input\InputDefinition; +use Symfony\Components\Console\Input\InputArgument; +use Symfony\Components\Console\Input\InputOption; + +class ArrayInputTest extends \PHPUnit_Framework_TestCase +{ + public function testGetFirstArgument() + { + $input = new ArrayInput(array()); + $this->assertEquals($input->getFirstArgument(), null, '->getFirstArgument() returns null if no argument were passed'); + $input = new ArrayInput(array('name' => 'Fabien')); + $this->assertEquals($input->getFirstArgument(), 'Fabien', '->getFirstArgument() returns the first passed argument'); + $input = new ArrayInput(array('--foo' => 'bar', 'name' => 'Fabien')); + $this->assertEquals($input->getFirstArgument(), 'Fabien', '->getFirstArgument() returns the first passed argument'); + } + + public function testHasParameterOption() + { + $input = new ArrayInput(array('name' => 'Fabien', '--foo' => 'bar')); + $this->assertTrue($input->hasParameterOption('--foo'), '->hasParameterOption() returns true if an option is present in the passed parameters'); + $this->assertTrue(!$input->hasParameterOption('--bar'), '->hasParameterOption() returns false if an option is not present in the passed parameters'); + + $input = new ArrayInput(array('--foo')); + $this->assertTrue($input->hasParameterOption('--foo'), '->hasParameterOption() returns true if an option is present in the passed parameters'); + } + + public function testParse() + { + $input = new ArrayInput(array('name' => 'foo'), new InputDefinition(array(new InputArgument('name')))); + $this->assertEquals($input->getArguments(), array('name' => 'foo'), '->parse() parses required arguments'); + + try + { + $input = new ArrayInput(array('foo' => 'foo'), new InputDefinition(array(new InputArgument('name')))); + $this->fail('->parse() throws an \InvalidArgumentException exception if an invalid argument is passed'); + } + catch (\RuntimeException $e) + { + } + + $input = new ArrayInput(array('--foo' => 'bar'), new InputDefinition(array(new InputOption('foo')))); + $this->assertEquals($input->getOptions(), array('foo' => 'bar'), '->parse() parses long options'); + + $input = new ArrayInput(array('--foo' => 'bar'), new InputDefinition(array(new InputOption('foo', 'f', InputOption::PARAMETER_OPTIONAL, '', 'default')))); + $this->assertEquals($input->getOptions(), array('foo' => 'bar'), '->parse() parses long options with a default value'); + + $input = new ArrayInput(array('--foo' => null), new InputDefinition(array(new InputOption('foo', 'f', InputOption::PARAMETER_OPTIONAL, '', 'default')))); + $this->assertEquals($input->getOptions(), array('foo' => 'default'), '->parse() parses long options with a default value'); + + try + { + $input = new ArrayInput(array('--foo' => null), new InputDefinition(array(new InputOption('foo', 'f', InputOption::PARAMETER_REQUIRED)))); + $this->fail('->parse() throws an \InvalidArgumentException exception if a required option is passed without a value'); + } + catch (\RuntimeException $e) + { + } + + try + { + $input = new ArrayInput(array('--foo' => 'foo'), new InputDefinition()); + $this->fail('->parse() throws an \InvalidArgumentException exception if an invalid option is passed'); + } + catch (\RuntimeException $e) + { + } + + $input = new ArrayInput(array('-f' => 'bar'), new InputDefinition(array(new InputOption('foo', 'f')))); + $this->assertEquals($input->getOptions(), array('foo' => 'bar'), '->parse() parses short options'); + + try + { + $input = new ArrayInput(array('-o' => 'foo'), new InputDefinition()); + $this->fail('->parse() throws an \InvalidArgumentException exception if an invalid option is passed'); + } + catch (\RuntimeException $e) + { + } + } +} diff --git a/tests/Symfony/Tests/Components/Console/Input/InputArgumentTest.php b/tests/Symfony/Tests/Components/Console/Input/InputArgumentTest.php new file mode 100644 index 0000000000..5e0964551d --- /dev/null +++ b/tests/Symfony/Tests/Components/Console/Input/InputArgumentTest.php @@ -0,0 +1,102 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Tests\Components\Console\Input; + +require_once __DIR__.'/../../../bootstrap.php'; + +use Symfony\Components\Console\Input\InputArgument; +use Symfony\Components\Console\Exception; + +class InputArgumentTest extends \PHPUnit_Framework_TestCase +{ + public function testConstructor() + { + $argument = new InputArgument('foo'); + $this->assertEquals($argument->getName(), 'foo', '__construct() takes a name as its first argument'); + + // mode argument + $argument = new InputArgument('foo'); + $this->assertEquals($argument->isRequired(), false, '__construct() gives a "Argument::OPTIONAL" mode by default'); + + $argument = new InputArgument('foo', null); + $this->assertEquals($argument->isRequired(), false, '__construct() can take "Argument::OPTIONAL" as its mode'); + + $argument = new InputArgument('foo', InputArgument::OPTIONAL); + $this->assertEquals($argument->isRequired(), false, '__construct() can take "Argument::PARAMETER_OPTIONAL" as its mode'); + + $argument = new InputArgument('foo', InputArgument::REQUIRED); + $this->assertEquals($argument->isRequired(), true, '__construct() can take "Argument::PARAMETER_REQUIRED" as its mode'); + + try + { + $argument = new InputArgument('foo', 'ANOTHER_ONE'); + $this->fail('__construct() throws an Exception if the mode is not valid'); + } + catch (\Exception $e) + { + } + } + + public function testIsArray() + { + $argument = new InputArgument('foo', InputArgument::IS_ARRAY); + $this->assertTrue($argument->isArray(), '->isArray() returns true if the argument can be an array'); + $argument = new InputArgument('foo', InputArgument::OPTIONAL | InputArgument::IS_ARRAY); + $this->assertTrue($argument->isArray(), '->isArray() returns true if the argument can be an array'); + $argument = new InputArgument('foo', InputArgument::OPTIONAL); + $this->assertTrue(!$argument->isArray(), '->isArray() returns false if the argument can not be an array'); + } + + public function testGetDescription() + { + $argument = new InputArgument('foo', null, 'Some description'); + $this->assertEquals($argument->getDescription(), 'Some description', '->getDescription() return the message description'); + } + + public function testGetDefault() + { + $argument = new InputArgument('foo', InputArgument::OPTIONAL, '', 'default'); + $this->assertEquals($argument->getDefault(), 'default', '->getDefault() return the default value'); + } + + public function testSetDefault() + { + $argument = new InputArgument('foo', InputArgument::OPTIONAL, '', 'default'); + $argument->setDefault(null); + $this->assertTrue(is_null($argument->getDefault()), '->setDefault() can reset the default value by passing null'); + $argument->setDefault('another'); + $this->assertEquals($argument->getDefault(), 'another', '->setDefault() changes the default value'); + + $argument = new InputArgument('foo', InputArgument::OPTIONAL | InputArgument::IS_ARRAY); + $argument->setDefault(array(1, 2)); + $this->assertEquals($argument->getDefault(), array(1, 2), '->setDefault() changes the default value'); + + try + { + $argument = new InputArgument('foo', InputArgument::REQUIRED); + $argument->setDefault('default'); + $this->fail('->setDefault() throws an Exception if you give a default value for a required argument'); + } + catch (\Exception $e) + { + } + + try + { + $argument = new InputArgument('foo', InputArgument::IS_ARRAY); + $argument->setDefault('default'); + $this->fail('->setDefault() throws an Exception if you give a default value which is not an array for a IS_ARRAY option'); + } + catch (\Exception $e) + { + } + } +} diff --git a/tests/Symfony/Tests/Components/Console/Input/InputDefinitionTest.php b/tests/Symfony/Tests/Components/Console/Input/InputDefinitionTest.php new file mode 100644 index 0000000000..6f19f2b05b --- /dev/null +++ b/tests/Symfony/Tests/Components/Console/Input/InputDefinitionTest.php @@ -0,0 +1,368 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Tests\Components\Console\Input; + +require_once __DIR__.'/../../../bootstrap.php'; + +use Symfony\Components\Console\Input\InputDefinition; +use Symfony\Components\Console\Input\InputArgument; +use Symfony\Components\Console\Input\InputOption; +use Symfony\Components\Console\Exception; + +class InputDefinitionTest extends \PHPUnit_Framework_TestCase +{ + static protected $fixtures; + + protected $foo, $bar, $foo1, $foo2; + + static public function setUpBeforeClass() + { + self::$fixtures = __DIR__.'/../../../../../fixtures/Symfony/Components/Console'; + } + + public function testConstructor() + { + $this->initializeArguments(); + + $definition = new InputDefinition(); + $this->assertEquals($definition->getArguments(), array(), '__construct() creates a new InputDefinition object'); + + $definition = new InputDefinition(array($this->foo, $this->bar)); + $this->assertEquals($definition->getArguments(), array('foo' => $this->foo, 'bar' => $this->bar), '__construct() takes an array of InputArgument objects as its first argument'); + + $this->initializeOptions(); + + $definition = new InputDefinition(); + $this->assertEquals($definition->getOptions(), array(), '__construct() creates a new InputDefinition object'); + + $definition = new InputDefinition(array($this->foo, $this->bar)); + $this->assertEquals($definition->getOptions(), array('foo' => $this->foo, 'bar' => $this->bar), '__construct() takes an array of InputOption objects as its first argument'); + } + + public function testSetArguments() + { + $this->initializeArguments(); + + $definition = new InputDefinition(); + $definition->setArguments(array($this->foo)); + $this->assertEquals($definition->getArguments(), array('foo' => $this->foo), '->setArguments() sets the array of InputArgument objects'); + $definition->setArguments(array($this->bar)); + + $this->assertEquals($definition->getArguments(), array('bar' => $this->bar), '->setArguments() clears all InputArgument objects'); + } + + public function testAddArguments() + { + $this->initializeArguments(); + + $definition = new InputDefinition(); + $definition->addArguments(array($this->foo)); + $this->assertEquals($definition->getArguments(), array('foo' => $this->foo), '->addArguments() adds an array of InputArgument objects'); + $definition->addArguments(array($this->bar)); + $this->assertEquals($definition->getArguments(), array('foo' => $this->foo, 'bar' => $this->bar), '->addArguments() does not clear existing InputArgument objects'); + } + + public function testAddArgument() + { + $this->initializeArguments(); + + $definition = new InputDefinition(); + $definition->addArgument($this->foo); + $this->assertEquals($definition->getArguments(), array('foo' => $this->foo), '->addArgument() adds a InputArgument object'); + $definition->addArgument($this->bar); + $this->assertEquals($definition->getArguments(), array('foo' => $this->foo, 'bar' => $this->bar), '->addArgument() adds a InputArgument object'); + + // arguments must have different names + try + { + $definition->addArgument($this->foo1); + $this->fail('->addArgument() throws a Exception if another argument is already registered with the same name'); + } + catch (\Exception $e) + { + } + + // cannot add a parameter after an array parameter + $definition->addArgument(new InputArgument('fooarray', InputArgument::IS_ARRAY)); + try + { + $definition->addArgument(new InputArgument('anotherbar')); + $this->fail('->addArgument() throws a Exception if there is an array parameter already registered'); + } + catch (\Exception $e) + { + } + + // cannot add a required argument after an optional one + $definition = new InputDefinition(); + $definition->addArgument($this->foo); + try + { + $definition->addArgument($this->foo2); + $this->fail('->addArgument() throws an exception if you try to add a required argument after an optional one'); + } + catch (\Exception $e) + { + } + } + + public function testGetArgument() + { + $this->initializeArguments(); + + $definition = new InputDefinition(); + $definition->addArguments(array($this->foo)); + $this->assertEquals($definition->getArgument('foo'), $this->foo, '->getArgument() returns a InputArgument by its name'); + try + { + $definition->getArgument('bar'); + $this->fail('->getArgument() throws an exception if the InputArgument name does not exist'); + } + catch (\Exception $e) + { + } + } + + public function testHasArgument() + { + $this->initializeArguments(); + + $definition = new InputDefinition(); + $definition->addArguments(array($this->foo)); + $this->assertEquals($definition->hasArgument('foo'), true, '->hasArgument() returns true if a InputArgument exists for the given name'); + $this->assertEquals($definition->hasArgument('bar'), false, '->hasArgument() returns false if a InputArgument exists for the given name'); + } + + public function testGetArgumentRequiredCount() + { + $this->initializeArguments(); + + $definition = new InputDefinition(); + $definition->addArgument($this->foo2); + $this->assertEquals($definition->getArgumentRequiredCount(), 1, '->getArgumentRequiredCount() returns the number of required arguments'); + $definition->addArgument($this->foo); + $this->assertEquals($definition->getArgumentRequiredCount(), 1, '->getArgumentRequiredCount() returns the number of required arguments'); + } + + public function testGetArgumentCount() + { + $this->initializeArguments(); + + $definition = new InputDefinition(); + $definition->addArgument($this->foo2); + $this->assertEquals($definition->getArgumentCount(), 1, '->getArgumentCount() returns the number of arguments'); + $definition->addArgument($this->foo); + $this->assertEquals($definition->getArgumentCount(), 2, '->getArgumentCount() returns the number of arguments'); + } + + public function testGetArgumentDefaults() + { + $definition = new InputDefinition(array( + new InputArgument('foo1', InputArgument::OPTIONAL), + new InputArgument('foo2', InputArgument::OPTIONAL, '', 'default'), + new InputArgument('foo3', InputArgument::OPTIONAL | InputArgument::IS_ARRAY), + // new InputArgument('foo4', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, '', array(1, 2)), + )); + $this->assertEquals($definition->getArgumentDefaults(), array('foo1' => null, 'foo2' => 'default', 'foo3' => array()), '->getArgumentDefaults() return the default values for each argument'); + + $definition = new InputDefinition(array( + new InputArgument('foo4', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, '', array(1, 2)), + )); + $this->assertEquals($definition->getArgumentDefaults(), array('foo4' => array(1, 2)), '->getArgumentDefaults() return the default values for each argument'); + } + + public function testSetOptions() + { + $this->initializeOptions(); + + $definition = new InputDefinition(array($this->foo)); + $this->assertEquals($definition->getOptions(), array('foo' => $this->foo), '->setOptions() sets the array of InputOption objects'); + $definition->setOptions(array($this->bar)); + $this->assertEquals($definition->getOptions(), array('bar' => $this->bar), '->setOptions() clears all InputOption objects'); + try + { + $definition->getOptionForShortcut('f'); + $this->fail('->setOptions() clears all InputOption objects'); + } + catch (\Exception $e) + { + } + } + + public function testAddOptions() + { + $this->initializeOptions(); + + $definition = new InputDefinition(array($this->foo)); + $this->assertEquals($definition->getOptions(), array('foo' => $this->foo), '->addOptions() adds an array of InputOption objects'); + $definition->addOptions(array($this->bar)); + $this->assertEquals($definition->getOptions(), array('foo' => $this->foo, 'bar' => $this->bar), '->addOptions() does not clear existing InputOption objects'); + } + + public function testAddOption() + { + $this->initializeOptions(); + + $definition = new InputDefinition(); + $definition->addOption($this->foo); + $this->assertEquals($definition->getOptions(), array('foo' => $this->foo), '->addOption() adds a InputOption object'); + $definition->addOption($this->bar); + $this->assertEquals($definition->getOptions(), array('foo' => $this->foo, 'bar' => $this->bar), '->addOption() adds a InputOption object'); + try + { + $definition->addOption($this->foo2); + $this->fail('->addOption() throws a Exception if the another option is already registered with the same name'); + } + catch (\Exception $e) + { + } + try + { + $definition->addOption($this->foo1); + $this->fail('->addOption() throws a Exception if the another option is already registered with the same shortcut'); + } + catch (\Exception $e) + { + } + } + + public function testGetOption() + { + $this->initializeOptions(); + + $definition = new InputDefinition(array($this->foo)); + $this->assertEquals($definition->getOption('foo'), $this->foo, '->getOption() returns a InputOption by its name'); + try + { + $definition->getOption('bar'); + $this->fail('->getOption() throws an exception if the option name does not exist'); + } + catch (\Exception $e) + { + } + } + + public function testHasOption() + { + $this->initializeOptions(); + + $definition = new InputDefinition(array($this->foo)); + $this->assertEquals($definition->hasOption('foo'), true, '->hasOption() returns true if a InputOption exists for the given name'); + $this->assertEquals($definition->hasOption('bar'), false, '->hasOption() returns false if a InputOption exists for the given name'); + } + + public function testHasShortcut() + { + $this->initializeOptions(); + + $definition = new InputDefinition(array($this->foo)); + $this->assertEquals($definition->hasShortcut('f'), true, '->hasShortcut() returns true if a InputOption exists for the given shortcut'); + $this->assertEquals($definition->hasShortcut('b'), false, '->hasShortcut() returns false if a InputOption exists for the given shortcut'); + } + + public function testGetOptionForShortcut() + { + $this->initializeOptions(); + + $definition = new InputDefinition(array($this->foo)); + $this->assertEquals($definition->getOptionForShortcut('f'), $this->foo, '->getOptionForShortcut() returns a InputOption by its shortcut'); + try + { + $definition->getOptionForShortcut('l'); + $this->fail('->getOption() throws an exception if the shortcut does not exist'); + } + catch (\Exception $e) + { + } + } + + public function testGetOptionDefaults() + { + $definition = new InputDefinition(array( + new InputOption('foo1', null, InputOption::PARAMETER_NONE), + new InputOption('foo2', null, InputOption::PARAMETER_REQUIRED), + new InputOption('foo3', null, InputOption::PARAMETER_REQUIRED, '', 'default'), + new InputOption('foo4', null, InputOption::PARAMETER_OPTIONAL), + new InputOption('foo5', null, InputOption::PARAMETER_OPTIONAL, '', 'default'), + new InputOption('foo6', null, InputOption::PARAMETER_OPTIONAL | InputOption::PARAMETER_IS_ARRAY), + new InputOption('foo7', null, InputOption::PARAMETER_OPTIONAL | InputOption::PARAMETER_IS_ARRAY, '', array(1, 2)), + )); + $defaults = array( + 'foo1' => null, + 'foo2' => null, + 'foo3' => 'default', + 'foo4' => null, + 'foo5' => 'default', + 'foo6' => array(), + 'foo7' => array(1, 2), + ); + $this->assertEquals($definition->getOptionDefaults(), $defaults, '->getOptionDefaults() returns the default values for all options'); + } + + public function testGetSynopsis() + { + $definition = new InputDefinition(array(new InputOption('foo'))); + $this->assertEquals($definition->getSynopsis(), '[--foo]', '->getSynopsis() returns a synopsis of arguments and options'); + $definition = new InputDefinition(array(new InputOption('foo', 'f'))); + $this->assertEquals($definition->getSynopsis(), '[-f|--foo]', '->getSynopsis() returns a synopsis of arguments and options'); + $definition = new InputDefinition(array(new InputOption('foo', 'f', InputOption::PARAMETER_REQUIRED))); + $this->assertEquals($definition->getSynopsis(), '[-f|--foo="..."]', '->getSynopsis() returns a synopsis of arguments and options'); + $definition = new InputDefinition(array(new InputOption('foo', 'f', InputOption::PARAMETER_OPTIONAL))); + $this->assertEquals($definition->getSynopsis(), '[-f|--foo[="..."]]', '->getSynopsis() returns a synopsis of arguments and options'); + + $definition = new InputDefinition(array(new InputArgument('foo'))); + $this->assertEquals($definition->getSynopsis(), '[foo]', '->getSynopsis() returns a synopsis of arguments and options'); + $definition = new InputDefinition(array(new InputArgument('foo', InputArgument::REQUIRED))); + $this->assertEquals($definition->getSynopsis(), 'foo', '->getSynopsis() returns a synopsis of arguments and options'); + $definition = new InputDefinition(array(new InputArgument('foo', InputArgument::IS_ARRAY))); + $this->assertEquals($definition->getSynopsis(), '[foo1] ... [fooN]', '->getSynopsis() returns a synopsis of arguments and options'); + $definition = new InputDefinition(array(new InputArgument('foo', InputArgument::REQUIRED | InputArgument::IS_ARRAY))); + $this->assertEquals($definition->getSynopsis(), 'foo1 ... [fooN]', '->getSynopsis() returns a synopsis of arguments and options'); + } + + public function testAsText() + { + $definition = new InputDefinition(array( + new InputArgument('foo', InputArgument::OPTIONAL, 'The bar argument'), + new InputArgument('bar', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, 'The foo argument', array('bar')), + new InputOption('foo', 'f', InputOption::PARAMETER_REQUIRED, 'The foo option'), + new InputOption('bar', 'b', InputOption::PARAMETER_OPTIONAL, 'The foo option', 'bar'), + )); + $this->assertEquals($definition->asText(), file_get_contents(self::$fixtures.'/definition_astext.txt'), '->asText() returns a textual representation of the InputDefinition'); + } + + public function testAsXml() + { + $definition = new InputDefinition(array( + new InputArgument('foo', InputArgument::OPTIONAL, 'The bar argument'), + new InputArgument('bar', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, 'The foo argument', array('bar')), + new InputOption('foo', 'f', InputOption::PARAMETER_REQUIRED, 'The foo option'), + new InputOption('bar', 'b', InputOption::PARAMETER_OPTIONAL, 'The foo option', 'bar'), + )); + $this->assertEquals($definition->asXml(), file_get_contents(self::$fixtures.'/definition_asxml.txt'), '->asText() returns a textual representation of the InputDefinition'); + } + + protected function initializeArguments() + { + $this->foo = new InputArgument('foo'); + $this->bar = new InputArgument('bar'); + $this->foo1 = new InputArgument('foo'); + $this->foo2 = new InputArgument('foo2', InputArgument::REQUIRED); + } + + protected function initializeOptions() + { + $this->foo = new InputOption('foo', 'f'); + $this->bar = new InputOption('bar', 'b'); + $this->foo1 = new InputOption('fooBis', 'f'); + $this->foo2 = new InputOption('foo', 'p'); + } +} diff --git a/tests/Symfony/Tests/Components/Console/Input/InputOptionTest.php b/tests/Symfony/Tests/Components/Console/Input/InputOptionTest.php new file mode 100644 index 0000000000..ac650854cc --- /dev/null +++ b/tests/Symfony/Tests/Components/Console/Input/InputOptionTest.php @@ -0,0 +1,142 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Tests\Components\Console\Input; + +require_once __DIR__.'/../../../bootstrap.php'; + +use Symfony\Components\Console\Input\InputOption; +use Symfony\Components\Console\Exception; + +class InputOptionTest extends \PHPUnit_Framework_TestCase +{ + public function testConstructor() + { + $option = new InputOption('foo'); + $this->assertEquals($option->getName(), 'foo', '__construct() takes a name as its first argument'); + $option = new InputOption('--foo'); + $this->assertEquals($option->getName(), 'foo', '__construct() removes the leading -- of the option name'); + + try + { + $option = new InputOption('foo', 'f', InputOption::PARAMETER_IS_ARRAY); + $this->fail('->setDefault() throws an Exception if PARAMETER_IS_ARRAY option is used when an option does not accept a value'); + } + catch (\Exception $e) + { + } + + // shortcut argument + $option = new InputOption('foo', 'f'); + $this->assertEquals($option->getShortcut(), 'f', '__construct() can take a shortcut as its second argument'); + $option = new InputOption('foo', '-f'); + $this->assertEquals($option->getShortcut(), 'f', '__construct() removes the leading - of the shortcut'); + + // mode argument + $option = new InputOption('foo', 'f'); + $this->assertEquals($option->acceptParameter(), false, '__construct() gives a "Option::PARAMETER_NONE" mode by default'); + $this->assertEquals($option->isParameterRequired(), false, '__construct() gives a "Option::PARAMETER_NONE" mode by default'); + $this->assertEquals($option->isParameterOptional(), false, '__construct() gives a "Option::PARAMETER_NONE" mode by default'); + + $option = new InputOption('foo', 'f', null); + $this->assertEquals($option->acceptParameter(), false, '__construct() can take "Option::PARAMETER_NONE" as its mode'); + $this->assertEquals($option->isParameterRequired(), false, '__construct() can take "Option::PARAMETER_NONE" as its mode'); + $this->assertEquals($option->isParameterOptional(), false, '__construct() can take "Option::PARAMETER_NONE" as its mode'); + + $option = new InputOption('foo', 'f', InputOption::PARAMETER_NONE); + $this->assertEquals($option->acceptParameter(), false, '__construct() can take "Option::PARAMETER_NONE" as its mode'); + $this->assertEquals($option->isParameterRequired(), false, '__construct() can take "Option::PARAMETER_NONE" as its mode'); + $this->assertEquals($option->isParameterOptional(), false, '__construct() can take "Option::PARAMETER_NONE" as its mode'); + + $option = new InputOption('foo', 'f', InputOption::PARAMETER_REQUIRED); + $this->assertEquals($option->acceptParameter(), true, '__construct() can take "Option::PARAMETER_REQUIRED" as its mode'); + $this->assertEquals($option->isParameterRequired(), true, '__construct() can take "Option::PARAMETER_REQUIRED" as its mode'); + $this->assertEquals($option->isParameterOptional(), false, '__construct() can take "Option::PARAMETER_REQUIRED" as its mode'); + + $option = new InputOption('foo', 'f', InputOption::PARAMETER_OPTIONAL); + $this->assertEquals($option->acceptParameter(), true, '__construct() can take "Option::PARAMETER_OPTIONAL" as its mode'); + $this->assertEquals($option->isParameterRequired(), false, '__construct() can take "Option::PARAMETER_OPTIONAL" as its mode'); + $this->assertEquals($option->isParameterOptional(), true, '__construct() can take "Option::PARAMETER_OPTIONAL" as its mode'); + + try + { + $option = new InputOption('foo', 'f', 'ANOTHER_ONE'); + $this->fail('__construct() throws an Exception if the mode is not valid'); + } + catch (\Exception $e) + { + } + } + + public function testIsArray() + { + $option = new InputOption('foo', null, InputOption::PARAMETER_OPTIONAL | InputOption::PARAMETER_IS_ARRAY); + $this->assertTrue($option->isArray(), '->isArray() returns true if the option can be an array'); + $option = new InputOption('foo', null, InputOption::PARAMETER_NONE); + $this->assertTrue(!$option->isArray(), '->isArray() returns false if the option can not be an array'); + } + + public function testGetDescription() + { + $option = new InputOption('foo', 'f', null, 'Some description'); + $this->assertEquals($option->getDescription(), 'Some description', '->getDescription() returns the description message'); + } + + public function testGetDefault() + { + $option = new InputOption('foo', null, InputOption::PARAMETER_OPTIONAL, '', 'default'); + $this->assertEquals($option->getDefault(), 'default', '->getDefault() returns the default value'); + + $option = new InputOption('foo', null, InputOption::PARAMETER_REQUIRED, '', 'default'); + $this->assertEquals($option->getDefault(), 'default', '->getDefault() returns the default value'); + + $option = new InputOption('foo', null, InputOption::PARAMETER_REQUIRED); + $this->assertTrue(is_null($option->getDefault()), '->getDefault() returns null if no default value is configured'); + + $option = new InputOption('foo', null, InputOption::PARAMETER_OPTIONAL | InputOption::PARAMETER_IS_ARRAY); + $this->assertEquals($option->getDefault(), array(), '->getDefault() returns an empty array if option is an array'); + + $option = new InputOption('foo', null, InputOption::PARAMETER_NONE); + $this->assertTrue($option->getDefault() === false, '->getDefault() returns false if the option does not take a parameter'); + } + + public function testSetDefault() + { + $option = new InputOption('foo', null, InputOption::PARAMETER_REQUIRED, '', 'default'); + $option->setDefault(null); + $this->assertTrue(is_null($option->getDefault()), '->setDefault() can reset the default value by passing null'); + $option->setDefault('another'); + $this->assertEquals($option->getDefault(), 'another', '->setDefault() changes the default value'); + + $option = new InputOption('foo', null, InputOption::PARAMETER_REQUIRED | InputOption::PARAMETER_IS_ARRAY); + $option->setDefault(array(1, 2)); + $this->assertEquals($option->getDefault(), array(1, 2), '->setDefault() changes the default value'); + + $option = new InputOption('foo', 'f', InputOption::PARAMETER_NONE); + try + { + $option->setDefault('default'); + $this->fail('->setDefault() throws an Exception if you give a default value for a PARAMETER_NONE option'); + } + catch (\Exception $e) + { + } + + $option = new InputOption('foo', 'f', InputOption::PARAMETER_OPTIONAL | InputOption::PARAMETER_IS_ARRAY); + try + { + $option->setDefault('default'); + $this->fail('->setDefault() throws an Exception if you give a default value which is not an array for a PARAMETER_IS_ARRAY option'); + } + catch (\Exception $e) + { + } + } +} diff --git a/tests/Symfony/Tests/Components/Console/Input/InputTest.php b/tests/Symfony/Tests/Components/Console/Input/InputTest.php new file mode 100644 index 0000000000..467ce0427c --- /dev/null +++ b/tests/Symfony/Tests/Components/Console/Input/InputTest.php @@ -0,0 +1,126 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Tests\Components\Console\Input; + +require_once __DIR__.'/../../../bootstrap.php'; + +use Symfony\Components\Console\Input\ArrayInput; +use Symfony\Components\Console\Input\InputDefinition; +use Symfony\Components\Console\Input\InputArgument; +use Symfony\Components\Console\Input\InputOption; + +class InputTest extends \PHPUnit_Framework_TestCase +{ + public function testConstructor() + { + $input = new ArrayInput(array('name' => 'foo'), new InputDefinition(array(new InputArgument('name')))); + $this->assertEquals($input->getArgument('name'), 'foo', '->__construct() takes a InputDefinition as an argument'); + } + + public function testOptions() + { + $input = new ArrayInput(array('--name' => 'foo'), new InputDefinition(array(new InputOption('name')))); + $this->assertEquals($input->getOption('name'), 'foo', '->getOption() returns the value for the given option'); + + $input->setOption('name', 'bar'); + $this->assertEquals($input->getOption('name'), 'bar', '->setOption() sets the value for a given option'); + $this->assertEquals($input->getOptions(), array('name' => 'bar'), '->getOptions() returns all option values'); + + $input = new ArrayInput(array('--name' => 'foo'), new InputDefinition(array(new InputOption('name'), new InputOption('bar', '', InputOption::PARAMETER_OPTIONAL, '', 'default')))); + $this->assertEquals($input->getOption('bar'), 'default', '->getOption() returns the default value for optional options'); + $this->assertEquals($input->getOptions(), array('name' => 'foo', 'bar' => 'default'), '->getOptions() returns all option values, even optional ones'); + + try + { + $input->setOption('foo', 'bar'); + $this->fail('->setOption() throws a \InvalidArgumentException if the option does not exist'); + } + catch (\InvalidArgumentException $e) + { + } + + try + { + $input->getOption('foo'); + $this->fail('->getOption() throws a \InvalidArgumentException if the option does not exist'); + } + catch (\InvalidArgumentException $e) + { + } + } + + public function testArguments() + { + $input = new ArrayInput(array('name' => 'foo'), new InputDefinition(array(new InputArgument('name')))); + $this->assertEquals($input->getArgument('name'), 'foo', '->getArgument() returns the value for the given argument'); + + $input->setArgument('name', 'bar'); + $this->assertEquals($input->getArgument('name'), 'bar', '->setArgument() sets the value for a given argument'); + $this->assertEquals($input->getArguments(), array('name' => 'bar'), '->getArguments() returns all argument values'); + + $input = new ArrayInput(array('name' => 'foo'), new InputDefinition(array(new InputArgument('name'), new InputArgument('bar', InputArgument::OPTIONAL, '', 'default')))); + $this->assertEquals($input->getArgument('bar'), 'default', '->getArgument() returns the default value for optional arguments'); + $this->assertEquals($input->getArguments(), array('name' => 'foo', 'bar' => 'default'), '->getArguments() returns all argument values, even optional ones'); + + try + { + $input->setArgument('foo', 'bar'); + $this->fail('->setArgument() throws a \InvalidArgumentException if the argument does not exist'); + } + catch (\InvalidArgumentException $e) + { + } + + try + { + $input->getArgument('foo'); + $this->fail('->getArgument() throws a \InvalidArgumentException if the argument does not exist'); + } + catch (\InvalidArgumentException $e) + { + } + } + + public function testValidate() + { + $input = new ArrayInput(array()); + $input->bind(new InputDefinition(array(new InputArgument('name', InputArgument::REQUIRED)))); + + try + { + $input->validate(); + $this->fail('->validate() throws a \RuntimeException if not enough arguments are given'); + } + catch (\RuntimeException $e) + { + } + + $input = new ArrayInput(array('name' => 'foo')); + $input->bind(new InputDefinition(array(new InputArgument('name', InputArgument::REQUIRED)))); + + try + { + $input->validate(); + } + catch (\RuntimeException $e) + { + $this->fail('->validate() does not throw a \RuntimeException if enough arguments are given'); + } + } + + public function testSetFetInteractive() + { + $input = new ArrayInput(array()); + $this->assertTrue($input->isInteractive(), '->isInteractive() returns whether the input should be interactive or not'); + $input->setInteractive(false); + $this->assertTrue(!$input->isInteractive(), '->setInteractive() changes the interactive flag'); + } +} diff --git a/tests/Symfony/Tests/Components/Console/Input/StringInputTest.php b/tests/Symfony/Tests/Components/Console/Input/StringInputTest.php new file mode 100644 index 0000000000..e2dbc73b02 --- /dev/null +++ b/tests/Symfony/Tests/Components/Console/Input/StringInputTest.php @@ -0,0 +1,98 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Tests\Components\Console\Input; + +require_once __DIR__.'/../../../bootstrap.php'; + +use Symfony\Components\Console\Input\StringInput; + +class StringInputTest extends \PHPUnit_Framework_TestCase +{ + public function testTokenize() + { + $input = new TestInput1(''); + $this->assertEquals($input->getTokens(), array(), '->tokenize() parses an empty string'); + + $input = new TestInput1('foo'); + $this->assertEquals($input->getTokens(), array('foo'), '->tokenize() parses arguments'); + + $input = new TestInput1(' foo bar '); + $this->assertEquals($input->getTokens(), array('foo', 'bar'), '->tokenize() ignores whitespaces between arguments'); + + $input = new TestInput1('"quoted"'); + $this->assertEquals($input->getTokens(), array('quoted'), '->tokenize() parses quoted arguments'); + + $input = new TestInput1("'quoted'"); + $this->assertEquals($input->getTokens(), array('quoted'), '->tokenize() parses quoted arguments'); + + $input = new TestInput1('\"quoted\"'); + $this->assertEquals($input->getTokens(), array('"quoted"'), '->tokenize() parses escaped-quoted arguments'); + + $input = new TestInput1("\'quoted\'"); + $this->assertEquals($input->getTokens(), array('\'quoted\''), '->tokenize() parses escaped-quoted arguments'); + + $input = new TestInput1('-a'); + $this->assertEquals($input->getTokens(), array('-a'), '->tokenize() parses short options'); + + $input = new TestInput1('-azc'); + $this->assertEquals($input->getTokens(), array('-azc'), '->tokenize() parses aggregated short options'); + + $input = new TestInput1('-awithavalue'); + $this->assertEquals($input->getTokens(), array('-awithavalue'), '->tokenize() parses short options with a value'); + + $input = new TestInput1('-a"foo bar"'); + $this->assertEquals($input->getTokens(), array('-afoo bar'), '->tokenize() parses short options with a value'); + + $input = new TestInput1('-a"foo bar""foo bar"'); + $this->assertEquals($input->getTokens(), array('-afoo barfoo bar'), '->tokenize() parses short options with a value'); + + $input = new TestInput1('-a\'foo bar\''); + $this->assertEquals($input->getTokens(), array('-afoo bar'), '->tokenize() parses short options with a value'); + + $input = new TestInput1('-a\'foo bar\'\'foo bar\''); + $this->assertEquals($input->getTokens(), array('-afoo barfoo bar'), '->tokenize() parses short options with a value'); + + $input = new TestInput1('-a\'foo bar\'"foo bar"'); + $this->assertEquals($input->getTokens(), array('-afoo barfoo bar'), '->tokenize() parses short options with a value'); + + $input = new TestInput1('--long-option'); + $this->assertEquals($input->getTokens(), array('--long-option'), '->tokenize() parses long options'); + + $input = new TestInput1('--long-option=foo'); + $this->assertEquals($input->getTokens(), array('--long-option=foo'), '->tokenize() parses long options with a value'); + + $input = new TestInput1('--long-option="foo bar"'); + $this->assertEquals($input->getTokens(), array('--long-option=foo bar'), '->tokenize() parses long options with a value'); + + $input = new TestInput1('--long-option="foo bar""another"'); + $this->assertEquals($input->getTokens(), array('--long-option=foo baranother'), '->tokenize() parses long options with a value'); + + $input = new TestInput1('--long-option=\'foo bar\''); + $this->assertEquals($input->getTokens(), array('--long-option=foo bar'), '->tokenize() parses long options with a value'); + + $input = new TestInput1("--long-option='foo bar''another'"); + $this->assertEquals($input->getTokens(), array("--long-option=foo baranother"), '->tokenize() parses long options with a value'); + + $input = new TestInput1("--long-option='foo bar'\"another\""); + $this->assertEquals($input->getTokens(), array("--long-option=foo baranother"), '->tokenize() parses long options with a value'); + + $input = new TestInput1('foo -a -ffoo --long bar'); + $this->assertEquals($input->getTokens(), array('foo', '-a', '-ffoo', '--long', 'bar'), '->tokenize() parses when several arguments and options'); + } +} + +class TestInput1 extends StringInput +{ + public function getTokens() + { + return $this->tokens; + } +} diff --git a/tests/Symfony/Tests/Components/Console/Output/ConsoleOutputTest.php b/tests/Symfony/Tests/Components/Console/Output/ConsoleOutputTest.php new file mode 100644 index 0000000000..1d34aaa0c1 --- /dev/null +++ b/tests/Symfony/Tests/Components/Console/Output/ConsoleOutputTest.php @@ -0,0 +1,25 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Tests\Components\Console\Output; + +require_once __DIR__.'/../../../bootstrap.php'; + +use Symfony\Components\Console\Output\ConsoleOutput; +use Symfony\Components\Console\Output\Output; + +class ConsoleOutputTest extends \PHPUnit_Framework_TestCase +{ + public function testConstructor() + { + $output = new ConsoleOutput(Output::VERBOSITY_QUIET, true); + $this->assertEquals($output->getVerbosity(), Output::VERBOSITY_QUIET, '__construct() takes the verbosity as its first argument'); + } +} diff --git a/tests/Symfony/Tests/Components/Console/Output/NullOutputTest.php b/tests/Symfony/Tests/Components/Console/Output/NullOutputTest.php new file mode 100644 index 0000000000..62ce474f82 --- /dev/null +++ b/tests/Symfony/Tests/Components/Console/Output/NullOutputTest.php @@ -0,0 +1,25 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Tests\Components\Console\Output; + +require_once __DIR__.'/../../../bootstrap.php'; + +use Symfony\Components\Console\Output\NullOutput; + +class NullOutputTest extends \PHPUnit_Framework_TestCase +{ + public function testConstructor() + { + $output = new NullOutput(); + $output->write('foo'); + $this->assertTrue(true, '->write() does nothing'); + } +} diff --git a/tests/Symfony/Tests/Components/Console/Output/OutputTest.php b/tests/Symfony/Tests/Components/Console/Output/OutputTest.php new file mode 100644 index 0000000000..16749a2479 --- /dev/null +++ b/tests/Symfony/Tests/Components/Console/Output/OutputTest.php @@ -0,0 +1,107 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Tests\Components\Console\Output; + +require_once __DIR__.'/../../../bootstrap.php'; + +use Symfony\Components\Console\Output\Output; + +class OutputTest extends \PHPUnit_Framework_TestCase +{ + public function testConstructor() + { + $output = new TestOutput(Output::VERBOSITY_QUIET, true); + $this->assertEquals($output->getVerbosity(), Output::VERBOSITY_QUIET, '__construct() takes the verbosity as its first argument'); + $this->assertEquals($output->isDecorated(), true, '__construct() takes the decorated flag as its second argument'); + } + + public function testSetIsDecorated() + { + $output = new TestOutput(); + $output->setDecorated(true); + $this->assertEquals($output->isDecorated(), true, 'setDecorated() sets the decorated flag'); + } + + public function testSetGetVerbosity() + { + $output = new TestOutput(); + $output->setVerbosity(Output::VERBOSITY_QUIET); + $this->assertEquals($output->getVerbosity(), Output::VERBOSITY_QUIET, '->setVerbosity() sets the verbosity'); + } + + public function testSetStyle() + { + Output::setStyle('FOO', array('bg' => 'red', 'fg' => 'yellow', 'blink' => true)); + $this->assertEquals(TestOutput::getStyle('foo'), array('bg' => 'red', 'fg' => 'yellow', 'blink' => true), '::setStyle() sets a new style'); + } + + public function testWrite() + { + $output = new TestOutput(Output::VERBOSITY_QUIET); + $output->writeln('foo'); + $this->assertEquals($output->output, '', '->writeln() outputs nothing if verbosity is set to VERBOSITY_QUIET'); + + $output = new TestOutput(); + $output->writeln(array('foo', 'bar')); + $this->assertEquals($output->output, "foo\nbar\n", '->writeln() can take an array of messages to output'); + + $output = new TestOutput(); + $output->writeln('foo', Output::OUTPUT_RAW); + $this->assertEquals($output->output, "foo\n", '->writeln() outputs the raw message if OUTPUT_RAW is specified'); + + $output = new TestOutput(); + $output->writeln('foo', Output::OUTPUT_PLAIN); + $this->assertEquals($output->output, "foo\n", '->writeln() strips decoration tags if OUTPUT_PLAIN is specified'); + + $output = new TestOutput(); + $output->setDecorated(false); + $output->writeln('foo'); + $this->assertEquals($output->output, "foo\n", '->writeln() strips decoration tags if decoration is set to false'); + + $output = new TestOutput(); + $output->setDecorated(true); + $output->writeln('foo'); + $this->assertEquals($output->output, "\033[33;41;5mfoo\033[0m\n", '->writeln() decorates the output'); + + try + { + $output->writeln('foo', 24); + $this->fail('->writeln() throws an \InvalidArgumentException when the type does not exist'); + } + catch (\InvalidArgumentException $e) + { + } + + try + { + $output->writeln('foo'); + $this->fail('->writeln() throws an \InvalidArgumentException when a style does not exist'); + } + catch (\InvalidArgumentException $e) + { + } + } +} + +class TestOutput extends Output +{ + public $output = ''; + + static public function getStyle($name) + { + return static::$styles[$name]; + } + + public function doWrite($message, $newline) + { + $this->output .= $message.($newline ? "\n" : ''); + } +} diff --git a/tests/Symfony/Tests/Components/Console/Output/StreamOutputTest.php b/tests/Symfony/Tests/Components/Console/Output/StreamOutputTest.php new file mode 100644 index 0000000000..20a81b1731 --- /dev/null +++ b/tests/Symfony/Tests/Components/Console/Output/StreamOutputTest.php @@ -0,0 +1,56 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Tests\Components\Console\Output; + +require_once __DIR__.'/../../../bootstrap.php'; + +use Symfony\Components\Console\Output\Output; +use Symfony\Components\Console\Output\StreamOutput; + +class StreamOutputTest extends \PHPUnit_Framework_TestCase +{ + protected $stream; + + public function setUp() + { + $this->stream = fopen('php://memory', 'a', false); + } + + public function testConstructor() + { + try + { + $output = new StreamOutput('foo'); + $this->fail('__construct() throws an \InvalidArgumentException if the first argument is not a stream'); + } + catch (\InvalidArgumentException $e) + { + } + + $output = new StreamOutput($this->stream, Output::VERBOSITY_QUIET, true); + $this->assertEquals($output->getVerbosity(), Output::VERBOSITY_QUIET, '__construct() takes the verbosity as its first argument'); + $this->assertEquals($output->isDecorated(), true, '__construct() takes the decorated flag as its second argument'); + } + + public function testGetStream() + { + $output = new StreamOutput($this->stream); + $this->assertEquals($output->getStream(), $this->stream, '->getStream() returns the current stream'); + } + + public function testDoWrite() + { + $output = new StreamOutput($this->stream); + $output->writeln('foo'); + rewind($output->getStream()); + $this->assertEquals(stream_get_contents($output->getStream()), "foo\n", '->doWrite() writes to the stream'); + } +} diff --git a/tests/Symfony/Tests/Components/Console/Tester/ApplicationTesterTest.php b/tests/Symfony/Tests/Components/Console/Tester/ApplicationTesterTest.php new file mode 100644 index 0000000000..efe8aa229e --- /dev/null +++ b/tests/Symfony/Tests/Components/Console/Tester/ApplicationTesterTest.php @@ -0,0 +1,60 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Tests\Components\Console\Tester; + +require_once __DIR__.'/../../../bootstrap.php'; + +use Symfony\Components\Console\Application; +use Symfony\Components\Console\Output\Output; +use Symfony\Components\Console\Tester\ApplicationTester; + +class ApplicationTesterTest extends \PHPUnit_Framework_TestCase +{ + protected $application; + protected $tester; + + public function setUp() + { + $this->application = new Application(); + $this->application->setAutoExit(false); + $this->application->register('foo') + ->addArgument('command') + ->addArgument('foo') + ->setCode(function ($input, $output) { $output->writeln('foo'); }) + ; + + $this->tester = new ApplicationTester($this->application); + $this->tester->run(array('command' => 'foo', 'foo' => 'bar'), array('interactive' => false, 'decorated' => false, 'verbosity' => Output::VERBOSITY_VERBOSE)); + } + + public function testRun() + { + $this->assertEquals($this->tester->getInput()->isInteractive(), false, '->execute() takes an interactive option'); + $this->assertEquals($this->tester->getOutput()->isDecorated(), false, '->execute() takes a decorated option'); + $this->assertEquals($this->tester->getOutput()->getVerbosity(), Output::VERBOSITY_VERBOSE, '->execute() takes a verbosity option'); + } + + public function testGetInput() + { + $this->assertEquals($this->tester->getInput()->getArgument('foo'), 'bar', '->getInput() returns the current input instance'); + } + + public function testGetOutput() + { + rewind($this->tester->getOutput()->getStream()); + $this->assertEquals(stream_get_contents($this->tester->getOutput()->getStream()), "foo\n", '->getOutput() returns the current output instance'); + } + + public function testGetDisplay() + { + $this->assertEquals($this->tester->getDisplay(), "foo\n", '->getDisplay() returns the display of the last execution'); + } +} diff --git a/tests/Symfony/Tests/Components/Console/Tester/CommandTesterTest.php b/tests/Symfony/Tests/Components/Console/Tester/CommandTesterTest.php new file mode 100644 index 0000000000..ab52cec481 --- /dev/null +++ b/tests/Symfony/Tests/Components/Console/Tester/CommandTesterTest.php @@ -0,0 +1,57 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Tests\Components\Console\Tester; + +require_once __DIR__.'/../../../bootstrap.php'; + +use Symfony\Components\Console\Command\Command; +use Symfony\Components\Console\Output\Output; +use Symfony\Components\Console\Tester\CommandTester; + +class CommandTesterTest extends \PHPUnit_Framework_TestCase +{ + protected $application; + protected $tester; + + public function setUp() + { + $this->command = new Command('foo'); + $this->command->addArgument('command'); + $this->command->addArgument('foo'); + $this->command->setCode(function ($input, $output) { $output->writeln('foo'); }); + + $this->tester = new CommandTester($this->command); + $this->tester->execute(array('foo' => 'bar'), array('interactive' => false, 'decorated' => false, 'verbosity' => Output::VERBOSITY_VERBOSE)); + } + + public function testExecute() + { + $this->assertEquals($this->tester->getInput()->isInteractive(), false, '->execute() takes an interactive option'); + $this->assertEquals($this->tester->getOutput()->isDecorated(), false, '->execute() takes a decorated option'); + $this->assertEquals($this->tester->getOutput()->getVerbosity(), Output::VERBOSITY_VERBOSE, '->execute() takes a verbosity option'); + } + + public function testGetInput() + { + $this->assertEquals($this->tester->getInput()->getArgument('foo'), 'bar', '->getInput() returns the current input instance'); + } + + public function testGetOutput() + { + rewind($this->tester->getOutput()->getStream()); + $this->assertEquals(stream_get_contents($this->tester->getOutput()->getStream()), "foo\n", '->getOutput() returns the current output instance'); + } + + public function testGetDisplay() + { + $this->assertEquals($this->tester->getDisplay(), "foo\n", '->getDisplay() returns the display of the last execution'); + } +} diff --git a/tests/Symfony/Tests/Components/DependencyInjection/BuilderConfigurationTest.php b/tests/Symfony/Tests/Components/DependencyInjection/BuilderConfigurationTest.php new file mode 100644 index 0000000000..ee5d227b7a --- /dev/null +++ b/tests/Symfony/Tests/Components/DependencyInjection/BuilderConfigurationTest.php @@ -0,0 +1,205 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Tests\Components\DependencyInjection; + +require_once __DIR__.'/../../bootstrap.php'; + +use Symfony\Components\DependencyInjection\Builder; +use Symfony\Components\DependencyInjection\BuilderConfiguration; +use Symfony\Components\DependencyInjection\Definition; +use Symfony\Components\DependencyInjection\Reference; +use Symfony\Components\DependencyInjection\FileResource; + +class BuilderConfigurationTest extends \PHPUnit_Framework_TestCase +{ + static protected $fixturesPath; + + static public function setUpBeforeClass() + { + self::$fixturesPath = __DIR__.'/../../../../fixtures/Symfony/Components/DependencyInjection/'; + } + + public function testConstructor() + { + $definitions = array( + 'foo' => new Definition('FooClass'), + 'bar' => new Definition('BarClass'), + ); + $parameters = array( + 'foo' => 'foo', + 'bar' => 'bar', + ); + $configuration = new BuilderConfiguration($definitions, $parameters); + $this->assertEquals($configuration->getDefinitions(), $definitions, '__construct() takes an array of definitions as its first argument'); + $this->assertEquals($configuration->getParameters(), $parameters, '__construct() takes an array of parameters as its second argument'); + } + + public function testMerge() + { + $configuration = new BuilderConfiguration(); + $configuration->merge(null); + $this->assertEquals($configuration->getParameters(), array(), '->merge() accepts null as an argument'); + $this->assertEquals($configuration->getDefinitions(), array(), '->merge() accepts null as an argument'); + + $configuration = new BuilderConfiguration(array(), array('bar' => 'foo')); + $configuration1 = new BuilderConfiguration(array(), array('foo' => 'bar')); + $configuration->merge($configuration1); + $this->assertEquals($configuration->getParameters(), array('bar' => 'foo', 'foo' => 'bar'), '->merge() merges current parameters with the loaded ones'); + + $configuration = new BuilderConfiguration(array(), array('bar' => 'foo', 'foo' => 'baz')); + $config = new BuilderConfiguration(array(), array('foo' => 'bar')); + $configuration->merge($config); + $this->assertEquals($configuration->getParameters(), array('bar' => 'foo', 'foo' => 'bar'), '->merge() overrides existing parameters'); + + $configuration = new BuilderConfiguration(array('foo' => new Definition('FooClass'), 'bar' => new Definition('BarClass'))); + $config = new BuilderConfiguration(array('baz' => new Definition('BazClass'))); + $config->setAlias('alias_for_foo', 'foo'); + $configuration->merge($config); + $this->assertEquals(array_keys($configuration->getDefinitions()), array('foo', 'bar', 'baz'), '->merge() merges definitions already defined ones'); + $this->assertEquals($configuration->getAliases(), array('alias_for_foo' => 'foo'), '->merge() registers defined aliases'); + + $configuration = new BuilderConfiguration(array('foo' => new Definition('FooClass'))); + $config->setDefinition('foo', new Definition('BazClass')); + $configuration->merge($config); + $this->assertEquals($configuration->getDefinition('foo')->getClass(), 'BazClass', '->merge() overrides already defined services'); + + $configuration = new BuilderConfiguration(); + $configuration->addResource($a = new FileResource('foo.xml')); + $config = new BuilderConfiguration(); + $config->addResource($b = new FileResource('foo.yml')); + $configuration->merge($config); + $this->assertEquals($configuration->getResources(), array($a, $b), '->merge() merges resources'); + } + + public function testSetGetParameters() + { + $configuration = new BuilderConfiguration(); + $this->assertEquals($configuration->getParameters(), array(), '->getParameters() returns an empty array if no parameter has been defined'); + + $configuration->setParameters(array('foo' => 'bar')); + $this->assertEquals($configuration->getParameters(), array('foo' => 'bar'), '->setParameters() sets the parameters'); + + $configuration->setParameters(array('bar' => 'foo')); + $this->assertEquals($configuration->getParameters(), array('bar' => 'foo'), '->setParameters() overrides the previous defined parameters'); + + $configuration->setParameters(array('Bar' => 'foo')); + $this->assertEquals($configuration->getParameters(), array('bar' => 'foo'), '->setParameters() converts the key to lowercase'); + } + + public function testSetGetParameter() + { + $configuration = new BuilderConfiguration(array(), array('foo' => 'bar')); + $configuration->setParameter('bar', 'foo'); + $this->assertEquals($configuration->getParameter('bar'), 'foo', '->setParameter() sets the value of a new parameter'); + + $configuration->setParameter('foo', 'baz'); + $this->assertEquals($configuration->getParameter('foo'), 'baz', '->setParameter() overrides previously set parameter'); + + $configuration->setParameter('Foo', 'baz1'); + $this->assertEquals($configuration->getParameter('foo'), 'baz1', '->setParameter() converts the key to lowercase'); + $this->assertEquals($configuration->getParameter('FOO'), 'baz1', '->getParameter() converts the key to lowercase'); + + try + { + $configuration->getParameter('baba'); + $this->fail('->getParameter() throws an \InvalidArgumentException if the key does not exist'); + } + catch (\InvalidArgumentException $e) + { + } + } + + public function testHasParameter() + { + $configuration = new BuilderConfiguration(array(), array('foo' => 'bar')); + $this->assertTrue($configuration->hasParameter('foo'), '->hasParameter() returns true if a parameter is defined'); + $this->assertTrue($configuration->hasParameter('Foo'), '->hasParameter() converts the key to lowercase'); + $this->assertTrue(!$configuration->hasParameter('bar'), '->hasParameter() returns false if a parameter is not defined'); + } + + public function testAddParameters() + { + $configuration = new BuilderConfiguration(array(), array('foo' => 'bar')); + $configuration->addParameters(array('bar' => 'foo')); + $this->assertEquals($configuration->getParameters(), array('foo' => 'bar', 'bar' => 'foo'), '->addParameters() adds parameters to the existing ones'); + $configuration->addParameters(array('Bar' => 'fooz')); + $this->assertEquals($configuration->getParameters(), array('foo' => 'bar', 'bar' => 'fooz'), '->addParameters() converts keys to lowercase'); + } + + public function testAliases() + { + $configuration = new BuilderConfiguration(); + $configuration->setAlias('bar', 'foo'); + $this->assertEquals($configuration->getAlias('bar'), 'foo', '->setAlias() defines a new alias'); + $this->assertTrue($configuration->hasAlias('bar'), '->hasAlias() returns true if the alias is defined'); + $this->assertTrue(!$configuration->hasAlias('baba'), '->hasAlias() returns false if the alias is not defined'); + + try + { + $configuration->getAlias('baba'); + $this->fail('->getAlias() throws an \InvalidArgumentException if the alias does not exist'); + } + catch (\InvalidArgumentException $e) + { + } + + $configuration->setAlias('barbar', 'foofoo'); + $this->assertEquals($configuration->getAliases(), array('bar' => 'foo', 'barbar' => 'foofoo'), '->getAliases() returns an array of all defined aliases'); + + $configuration->addAliases(array('foo' => 'bar')); + $this->assertEquals($configuration->getAliases(), array('bar' => 'foo', 'barbar' => 'foofoo', 'foo' => 'bar'), '->addAliases() adds some aliases'); + } + + public function testDefinitions() + { + $configuration = new BuilderConfiguration(); + $definitions = array( + 'foo' => new Definition('FooClass'), + 'bar' => new Definition('BarClass'), + ); + $configuration->setDefinitions($definitions); + $this->assertEquals($configuration->getDefinitions(), $definitions, '->setDefinitions() sets the service definitions'); + $this->assertTrue($configuration->hasDefinition('foo'), '->hasDefinition() returns true if a service definition exists'); + $this->assertTrue(!$configuration->hasDefinition('foobar'), '->hasDefinition() returns false if a service definition does not exist'); + + $configuration->setDefinition('foobar', $foo = new Definition('FooBarClass')); + $this->assertEquals($configuration->getDefinition('foobar'), $foo, '->getDefinition() returns a service definition if defined'); + $this->assertTrue($configuration->setDefinition('foobar', $foo = new Definition('FooBarClass')) === $foo, '->setDefinition() implements a fuild interface by returning the service reference'); + + $configuration->addDefinitions($defs = array('foobar' => new Definition('FooBarClass'))); + $this->assertEquals($configuration->getDefinitions(), array_merge($definitions, $defs), '->addDefinitions() adds the service definitions'); + + try + { + $configuration->getDefinition('baz'); + $this->fail('->getDefinition() throws an InvalidArgumentException if the service definition does not exist'); + } + catch (\InvalidArgumentException $e) + { + } + } + + public function testFindDefinition() + { + $configuration = new BuilderConfiguration(array('foo' => $definition = new Definition('FooClass'))); + $configuration->setAlias('bar', 'foo'); + $configuration->setAlias('foobar', 'bar'); + $this->assertEquals($configuration->findDefinition('foobar'), $definition, '->findDefinition() returns a Definition'); + } + + public function testResources() + { + $configuration = new BuilderConfiguration(); + $configuration->addResource($a = new FileResource('foo.xml')); + $configuration->addResource($b = new FileResource('foo.yml')); + $this->assertEquals($configuration->getResources(), array($a, $b), '->getResources() returns an array of resources read for the current configuration'); + } +} diff --git a/tests/Symfony/Tests/Components/DependencyInjection/BuilderTest.php b/tests/Symfony/Tests/Components/DependencyInjection/BuilderTest.php new file mode 100644 index 0000000000..acdb17ad56 --- /dev/null +++ b/tests/Symfony/Tests/Components/DependencyInjection/BuilderTest.php @@ -0,0 +1,328 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Tests\Components\DependencyInjection; + +require_once __DIR__.'/../../bootstrap.php'; + +use Symfony\Components\DependencyInjection\Builder; +use Symfony\Components\DependencyInjection\BuilderConfiguration; +use Symfony\Components\DependencyInjection\Definition; +use Symfony\Components\DependencyInjection\Reference; + +class BuilderTest extends \PHPUnit_Framework_TestCase +{ + static protected $fixturesPath; + + static public function setUpBeforeClass() + { + self::$fixturesPath = __DIR__.'/../../../../fixtures/Symfony/Components/DependencyInjection/'; + } + + public function testDefinitions() + { + $builder = new Builder(); + $definitions = array( + 'foo' => new Definition('FooClass'), + 'bar' => new Definition('BarClass'), + ); + $builder->setDefinitions($definitions); + $this->assertEquals($builder->getDefinitions(), $definitions, '->setDefinitions() sets the service definitions'); + $this->assertTrue($builder->hasDefinition('foo'), '->hasDefinition() returns true if a service definition exists'); + $this->assertTrue(!$builder->hasDefinition('foobar'), '->hasDefinition() returns false if a service definition does not exist'); + + $builder->setDefinition('foobar', $foo = new Definition('FooBarClass')); + $this->assertEquals($builder->getDefinition('foobar'), $foo, '->getDefinition() returns a service definition if defined'); + $this->assertTrue($builder->setDefinition('foobar', $foo = new Definition('FooBarClass')) === $foo, '->setDefinition() implements a fuild interface by returning the service reference'); + + $builder->addDefinitions($defs = array('foobar' => new Definition('FooBarClass'))); + $this->assertEquals($builder->getDefinitions(), array_merge($definitions, $defs), '->addDefinitions() adds the service definitions'); + + try + { + $builder->getDefinition('baz'); + $this->fail('->getDefinition() throws an InvalidArgumentException if the service definition does not exist'); + } + catch (\InvalidArgumentException $e) + { + } + } + + public function testRegister() + { + $builder = new Builder(); + $builder->register('foo', 'FooClass'); + $this->assertTrue($builder->hasDefinition('foo'), '->register() registers a new service definition'); + $this->assertTrue($builder->getDefinition('foo') instanceof Definition, '->register() returns the newly created Definition instance'); + } + + public function testHasService() + { + $builder = new Builder(); + $this->assertTrue(!$builder->hasService('foo'), '->hasService() returns false if the service does not exist'); + $builder->register('foo', 'FooClass'); + $this->assertTrue($builder->hasService('foo'), '->hasService() returns true if a service definition exists'); + $builder->bar = new \stdClass(); + $this->assertTrue($builder->hasService('bar'), '->hasService() returns true if a service exists'); + } + + public function testGetService() + { + $builder = new Builder(); + try + { + $builder->getService('foo'); + $this->fail('->getService() throws an InvalidArgumentException if the service does not exist'); + } + catch (\InvalidArgumentException $e) + { + } + $builder->register('foo', 'stdClass'); + $this->assertTrue(is_object($builder->getService('foo')), '->getService() returns the service definition associated with the id'); + $builder->bar = $bar = new \stdClass(); + $this->assertEquals($builder->getService('bar'), $bar, '->getService() returns the service associated with the id'); + $builder->register('bar', 'stdClass'); + $this->assertEquals($builder->getService('bar'), $bar, '->getService() returns the service associated with the id even if a definition has been defined'); + + $builder->register('baz', 'stdClass')->setArguments(array(new Reference('baz'))); + try + { + @$builder->getService('baz'); + $this->fail('->getService() throws a LogicException if the service has a circular reference to itself'); + } + catch (\LogicException $e) + { + } + + $builder->register('foobar', 'stdClass')->setShared(true); + $this->assertTrue($builder->getService('bar') === $builder->getService('bar'), '->getService() always returns the same instance if the service is shared'); + } + + public function testGetServiceIds() + { + $builder = new Builder(); + $builder->register('foo', 'stdClass'); + $builder->bar = $bar = new \stdClass(); + $builder->register('bar', 'stdClass'); + $this->assertEquals($builder->getServiceIds(), array('foo', 'bar', 'service_container'), '->getServiceIds() returns all defined service ids'); + } + + public function testAliases() + { + $builder = new Builder(); + $builder->register('foo', 'stdClass'); + $builder->setAlias('bar', 'foo'); + $this->assertTrue($builder->hasAlias('bar'), '->hasAlias() returns true if the alias exists'); + $this->assertTrue(!$builder->hasAlias('foobar'), '->hasAlias() returns false if the alias does not exist'); + $this->assertEquals($builder->getAlias('bar'), 'foo', '->getAlias() returns the aliased service'); + $this->assertTrue($builder->hasService('bar'), '->setAlias() defines a new service'); + $this->assertTrue($builder->getService('bar') === $builder->getService('foo'), '->setAlias() creates a service that is an alias to another one'); + + try + { + $builder->getAlias('foobar'); + $this->fail('->getAlias() throws an InvalidArgumentException if the alias does not exist'); + } + catch (\InvalidArgumentException $e) + { + } + } + + public function testGetAliases() + { + $builder = new Builder(); + $builder->setAlias('bar', 'foo'); + $builder->setAlias('foobar', 'foo'); + $this->assertEquals($builder->getAliases(), array('bar' => 'foo', 'foobar' => 'foo'), '->getAliases() returns all service aliases'); + $builder->register('bar', 'stdClass'); + $this->assertEquals($builder->getAliases(), array('foobar' => 'foo'), '->getAliases() does not return aliased services that have been overridden'); + $builder->setService('foobar', 'stdClass'); + $this->assertEquals($builder->getAliases(), array(), '->getAliases() does not return aliased services that have been overridden'); + } + + public function testCreateService() + { + $builder = new Builder(); + $builder->register('foo1', 'FooClass')->setFile(self::$fixturesPath.'/includes/foo.php'); + $this->assertTrue($builder->getService('foo1') instanceof \FooClass, '->createService() requires the file defined by the service definition'); + $builder->register('foo2', 'FooClass')->setFile(self::$fixturesPath.'/includes/%file%.php'); + $builder->setParameter('file', 'foo'); + $this->assertTrue($builder->getService('foo2') instanceof \FooClass, '->createService() replaces parameters in the file provided by the service definition'); + } + + public function testCreateServiceClass() + { + $builder = new Builder(); + $builder->register('foo1', '%class%'); + $builder->setParameter('class', 'stdClass'); + $this->assertTrue($builder->getService('foo1') instanceof \stdClass, '->createService() replaces parameters in the class provided by the service definition'); + } + + public function testCreateServiceArguments() + { + $builder = new Builder(); + $builder->register('bar', 'stdClass'); + $builder->register('foo1', 'FooClass')->addArgument(array('foo' => '%value%', '%value%' => 'foo', new Reference('bar'))); + $builder->setParameter('value', 'bar'); + $this->assertEquals($builder->getService('foo1')->arguments, array('foo' => 'bar', 'bar' => 'foo', $builder->getService('bar')), '->createService() replaces parameters and service references in the arguments provided by the service definition'); + } + + public function testCreateServiceConstructor() + { + $builder = new Builder(); + $builder->register('bar', 'stdClass'); + $builder->register('foo1', 'FooClass')->setConstructor('getInstance')->addArgument(array('foo' => '%value%', '%value%' => 'foo', new Reference('bar'))); + $builder->setParameter('value', 'bar'); + $this->assertTrue($builder->getService('foo1')->called, '->createService() calls the constructor to create the service instance'); + $this->assertEquals($builder->getService('foo1')->arguments, array('foo' => 'bar', 'bar' => 'foo', $builder->getService('bar')), '->createService() passes the arguments to the constructor'); + } + + public function testCreateServiceMethodCalls() + { + $builder = new Builder(); + $builder->register('bar', 'stdClass'); + $builder->register('foo1', 'FooClass')->addMethodCall('setBar', array(array('%value%', new Reference('bar')))); + $builder->setParameter('value', 'bar'); + $this->assertEquals($builder->getService('foo1')->bar, array('bar', $builder->getService('bar')), '->createService() replaces the values in the method calls arguments'); + } + + public function testCreateServiceConfigurator() + { + require_once self::$fixturesPath.'/includes/classes.php'; + + $builder = new Builder(); + $builder->register('foo1', 'FooClass')->setConfigurator('sc_configure'); + $this->assertTrue($builder->getService('foo1')->configured, '->createService() calls the configurator'); + + $builder->register('foo2', 'FooClass')->setConfigurator(array('%class%', 'configureStatic')); + $builder->setParameter('class', 'BazClass'); + $this->assertTrue($builder->getService('foo2')->configured, '->createService() calls the configurator'); + + $builder->register('baz', 'BazClass'); + $builder->register('foo3', 'FooClass')->setConfigurator(array(new Reference('baz'), 'configure')); + $this->assertTrue($builder->getService('foo3')->configured, '->createService() calls the configurator'); + + $builder->register('foo4', 'FooClass')->setConfigurator('foo'); + try + { + $builder->getService('foo4'); + $this->fail('->createService() throws an InvalidArgumentException if the configure callable is not a valid callable'); + } + catch (\InvalidArgumentException $e) + { + } + } + + public function testResolveValue() + { + $this->assertEquals(Builder::resolveValue('foo', array()), 'foo', '->resolveValue() returns its argument unmodified if no placeholders are found'); + $this->assertEquals(Builder::resolveValue('I\'m a %foo%', array('foo' => 'bar')), 'I\'m a bar', '->resolveValue() replaces placeholders by their values'); + $this->assertTrue(Builder::resolveValue('%foo%', array('foo' => true)) === true, '->resolveValue() replaces arguments that are just a placeholder by their value without casting them to strings'); + + $this->assertEquals(Builder::resolveValue(array('%foo%' => '%foo%'), array('foo' => 'bar')), array('bar' => 'bar'), '->resolveValue() replaces placeholders in keys and values of arrays'); + + $this->assertEquals(Builder::resolveValue(array('%foo%' => array('%foo%' => array('%foo%' => '%foo%'))), array('foo' => 'bar')), array('bar' => array('bar' => array('bar' => 'bar'))), '->resolveValue() replaces placeholders in nested arrays'); + + $this->assertEquals(Builder::resolveValue('I\'m a %%foo%%', array('foo' => 'bar')), 'I\'m a %foo%', '->resolveValue() supports % escaping by doubling it'); + $this->assertEquals(Builder::resolveValue('I\'m a %foo% %%foo %foo%', array('foo' => 'bar')), 'I\'m a bar %foo bar', '->resolveValue() supports % escaping by doubling it'); + + try + { + Builder::resolveValue('%foobar%', array()); + $this->fail('->resolveValue() throws a RuntimeException if a placeholder references a non-existant parameter'); + } + catch (\RuntimeException $e) + { + } + + try + { + Builder::resolveValue('foo %foobar% bar', array()); + $this->fail('->resolveValue() throws a RuntimeException if a placeholder references a non-existant parameter'); + } + catch (\RuntimeException $e) + { + } + } + + public function testResolveServices() + { + $builder = new Builder(); + $builder->register('foo', 'FooClass'); + $this->assertEquals($builder->resolveServices(new Reference('foo')), $builder->getService('foo'), '->resolveServices() resolves service references to service instances'); + $this->assertEquals($builder->resolveServices(array('foo' => array('foo', new Reference('foo')))), array('foo' => array('foo', $builder->getService('foo'))), '->resolveServices() resolves service references to service instances in nested arrays'); + } + + public function testMerge() + { + $container = new Builder(); + $container->merge(null); + $this->assertEquals($container->getParameters(), array(), '->merge() accepts null as an argument'); + $this->assertEquals($container->getDefinitions(), array(), '->merge() accepts null as an argument'); + + $container = new Builder(array('bar' => 'foo')); + $config = new BuilderConfiguration(); + $config->setParameters(array('foo' => 'bar')); + $container->merge($config); + $this->assertEquals($container->getParameters(), array('bar' => 'foo', 'foo' => 'bar'), '->merge() merges current parameters with the loaded ones'); + + $container = new Builder(array('bar' => 'foo', 'foo' => 'baz')); + $config = new BuilderConfiguration(); + $config->setParameters(array('foo' => 'bar')); + $container->merge($config); + $this->assertEquals($container->getParameters(), array('bar' => 'foo', 'foo' => 'baz'), '->merge() does not change the already defined parameters'); + + $container = new Builder(array('bar' => 'foo')); + $config = new BuilderConfiguration(); + $config->setParameters(array('foo' => '%bar%')); + $container->merge($config); + $this->assertEquals($container->getParameters(), array('bar' => 'foo', 'foo' => 'foo'), '->merge() evaluates the values of the parameters towards already defined ones'); + + $container = new Builder(array('bar' => 'foo')); + $config = new BuilderConfiguration(); + $config->setParameters(array('foo' => '%bar%', 'baz' => '%foo%')); + $container->merge($config); + $this->assertEquals($container->getParameters(), array('bar' => 'foo', 'foo' => 'foo', 'baz' => 'foo'), '->merge() evaluates the values of the parameters towards already defined ones'); + + $container = new Builder(); + $container->register('foo', 'FooClass'); + $container->register('bar', 'BarClass'); + $config = new BuilderConfiguration(); + $config->setDefinition('baz', new Definition('BazClass')); + $config->setAlias('alias_for_foo', 'foo'); + $container->merge($config); + $this->assertEquals(array_keys($container->getDefinitions()), array('foo', 'bar', 'baz'), '->merge() merges definitions already defined ones'); + $this->assertEquals($container->getAliases(), array('alias_for_foo' => 'foo'), '->merge() registers defined aliases'); + + $container = new Builder(); + $container->register('foo', 'FooClass'); + $config->setDefinition('foo', new Definition('BazClass')); + $container->merge($config); + $this->assertEquals($container->getDefinition('foo')->getClass(), 'BazClass', '->merge() overrides already defined services'); + } + + public function testFindAnnotatedServiceIds() + { + $builder = new Builder(); + $builder + ->register('foo', 'FooClass') + ->addAnnotation('foo', array('foo' => 'foo')) + ->addAnnotation('bar', array('bar' => 'bar')) + ->addAnnotation('foo', array('foofoo' => 'foofoo')) + ; + $this->assertEquals($builder->findAnnotatedServiceIds('foo'), array( + 'foo' => array( + array('foo' => 'foo'), + array('foofoo' => 'foofoo'), + ) + ), '->findAnnotatedServiceIds() returns an array of service ids and its annotation attributes'); + $this->assertEquals($builder->findAnnotatedServiceIds('foobar'), array(), '->findAnnotatedServiceIds() returns an empty array if there is annotated services'); + } +} diff --git a/tests/Symfony/Tests/Components/DependencyInjection/ContainerTest.php b/tests/Symfony/Tests/Components/DependencyInjection/ContainerTest.php new file mode 100644 index 0000000000..c550b3d4ea --- /dev/null +++ b/tests/Symfony/Tests/Components/DependencyInjection/ContainerTest.php @@ -0,0 +1,208 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Tests\Components\DependencyInjection; + +require_once __DIR__.'/../../bootstrap.php'; + +use Symfony\Components\DependencyInjection\Container; + +class ContainerTest extends \PHPUnit_Framework_TestCase +{ + public function testConstructor() + { + $sc = new Container(); + $this->assertEquals(spl_object_hash($sc->getService('service_container')), spl_object_hash($sc), '__construct() automatically registers itself as a service'); + + $sc = new Container(array('foo' => 'bar')); + $this->assertEquals($sc->getParameters(), array('foo' => 'bar'), '__construct() takes an array of parameters as its first argument'); + } + + public function testGetSetParameters() + { + $sc = new Container(); + $this->assertEquals($sc->getParameters(), array(), '->getParameters() returns an empty array if no parameter has been defined'); + + $sc->setParameters(array('foo' => 'bar')); + $this->assertEquals($sc->getParameters(), array('foo' => 'bar'), '->setParameters() sets the parameters'); + + $sc->setParameters(array('bar' => 'foo')); + $this->assertEquals($sc->getParameters(), array('bar' => 'foo'), '->setParameters() overrides the previous defined parameters'); + + $sc->setParameters(array('Bar' => 'foo')); + $this->assertEquals($sc->getParameters(), array('bar' => 'foo'), '->setParameters() converts the key to lowercase'); + } + + public function testGetSetParameter() + { + $sc = new Container(array('foo' => 'bar')); + $sc->setParameter('bar', 'foo'); + $this->assertEquals($sc->getParameter('bar'), 'foo', '->setParameter() sets the value of a new parameter'); + $this->assertEquals($sc['bar'], 'foo', '->offsetGet() gets the value of a parameter'); + + $sc['bar1'] = 'foo1'; + $this->assertEquals($sc['bar1'], 'foo1', '->offsetset() sets the value of a parameter'); + + unset($sc['bar1']); + $this->assertTrue(!isset($sc['bar1']), '->offsetUnset() removes a parameter'); + + $sc->setParameter('foo', 'baz'); + $this->assertEquals($sc->getParameter('foo'), 'baz', '->setParameter() overrides previously set parameter'); + + $sc->setParameter('Foo', 'baz1'); + $this->assertEquals($sc->getParameter('foo'), 'baz1', '->setParameter() converts the key to lowercase'); + $this->assertEquals($sc->getParameter('FOO'), 'baz1', '->getParameter() converts the key to lowercase'); + $this->assertEquals($sc['FOO'], 'baz1', '->offsetGet() converts the key to lowercase'); + + try + { + $sc->getParameter('baba'); + $this->fail('->getParameter() thrown an \InvalidArgumentException if the key does not exist'); + } + catch (\InvalidArgumentException $e) + { + } + + try + { + $sc['baba']; + $this->fail('->offsetGet() thrown an \InvalidArgumentException if the key does not exist'); + } + catch (\InvalidArgumentException $e) + { + } + } + + public function testHasParameter() + { + $sc = new Container(array('foo' => 'bar')); + $this->assertTrue($sc->hasParameter('foo'), '->hasParameter() returns true if a parameter is defined'); + $this->assertTrue($sc->hasParameter('Foo'), '->hasParameter() converts the key to lowercase'); + $this->assertTrue(isset($sc['Foo']), '->offsetExists() converts the key to lowercase'); + $this->assertTrue(!$sc->hasParameter('bar'), '->hasParameter() returns false if a parameter is not defined'); + $this->assertTrue(isset($sc['foo']), '->offsetExists() returns true if a parameter is defined'); + $this->assertTrue(!isset($sc['bar']), '->offsetExists() returns false if a parameter is not defined'); + } + + public function testAddParameters() + { + $sc = new Container(array('foo' => 'bar')); + $sc->addParameters(array('bar' => 'foo')); + $this->assertEquals($sc->getParameters(), array('foo' => 'bar', 'bar' => 'foo'), '->addParameters() adds parameters to the existing ones'); + $sc->addParameters(array('Bar' => 'fooz')); + $this->assertEquals($sc->getParameters(), array('foo' => 'bar', 'bar' => 'fooz'), '->addParameters() converts keys to lowercase'); + } + + public function testServices() + { + $sc = new Container(); + $sc->setService('foo', $obj = new \stdClass()); + $this->assertEquals(spl_object_hash($sc->getService('foo')), spl_object_hash($obj), '->setService() registers a service under a key name'); + + $sc->foo1 = $obj1 = new \stdClass(); + $this->assertEquals(spl_object_hash($sc->foo1), spl_object_hash($obj1), '->__set() sets a service'); + + $this->assertEquals(spl_object_hash($sc->foo), spl_object_hash($obj), '->__get() gets a service by name'); + $this->assertTrue($sc->hasService('foo'), '->hasService() returns true if the service is defined'); + $this->assertTrue(isset($sc->foo), '->__isset() returns true if the service is defined'); + $this->assertTrue(!$sc->hasService('bar'), '->hasService() returns false if the service is not defined'); + $this->assertTrue(!isset($sc->bar), '->__isset() returns false if the service is not defined'); + } + + public function testGetServiceIds() + { + $sc = new Container(); + $sc->setService('foo', $obj = new \stdClass()); + $sc->setService('bar', $obj = new \stdClass()); + $this->assertEquals($sc->getServiceIds(), array('service_container', 'foo', 'bar'), '->getServiceIds() returns all defined service ids'); + + $sc = new ProjectServiceContainer(); + $this->assertEquals(spl_object_hash($sc->getService('bar')), spl_object_hash($sc->__bar), '->getService() looks for a getXXXService() method'); + $this->assertTrue($sc->hasService('bar'), '->hasService() returns true if the service has been defined as a getXXXService() method'); + + $sc->setService('bar', $bar = new \stdClass()); + $this->assertNotEquals(spl_object_hash($sc->getService('bar')), spl_object_hash($bar), '->getService() prefers to return a service defined with a getXXXService() method than one defined with setService()'); + + try + { + $sc->getService('baba'); + $this->fail('->getService() thrown an \InvalidArgumentException if the service does not exist'); + } + catch (\InvalidArgumentException $e) + { + } + + try + { + $sc->baba; + $this->fail('->__get() thrown an \InvalidArgumentException if the service does not exist'); + } + catch (\InvalidArgumentException $e) + { + } + + try + { + unset($sc->baba); + $this->fail('->__unset() thrown an LogicException if you try to remove a service'); + } + catch (\LogicException $e) + { + } + + $this->assertEquals(spl_object_hash($sc->getService('foo_bar')), spl_object_hash($sc->__foo_bar), '->getService() camelizes the service id when looking for a method'); + $this->assertEquals(spl_object_hash($sc->getService('foo.baz')), spl_object_hash($sc->__foo_baz), '->getService() camelizes the service id when looking for a method'); + } + + public function testMagicCall() + { + $sc = new Container(); + $sc->setService('foo_bar.foo', $foo = new \stdClass()); + $this->assertEquals($sc->getFooBar_FooService(), $foo, '__call() finds services is the method is getXXXService()'); + + try + { + $sc->getFooBar_Foo(); + $this->fail('__call() throws a \RuntimeException exception if the method is not a service method'); + } + catch (\RuntimeException $e) + { + } + } +} + +class ProjectServiceContainer extends Container +{ + public $__bar, $__foo_bar, $__foo_baz; + + public function __construct() + { + parent::__construct(); + + $this->__bar = new \stdClass(); + $this->__foo_bar = new \stdClass(); + $this->__foo_baz = new \stdClass(); + } + + protected function getBarService() + { + return $this->__bar; + } + + protected function getFooBarService() + { + return $this->__foo_bar; + } + + protected function getFoo_BazService() + { + return $this->__foo_baz; + } +} diff --git a/tests/Symfony/Tests/Components/DependencyInjection/CrossCheckTest.php b/tests/Symfony/Tests/Components/DependencyInjection/CrossCheckTest.php new file mode 100644 index 0000000000..fc25578598 --- /dev/null +++ b/tests/Symfony/Tests/Components/DependencyInjection/CrossCheckTest.php @@ -0,0 +1,88 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Tests\Components\DependencyInjection; + +require_once __DIR__.'/../../bootstrap.php'; + +use Symfony\Components\DependencyInjection\Builder; + +class CrossCheckTest extends \PHPUnit_Framework_TestCase +{ + static protected $fixturesPath; + + static public function setUpBeforeClass() + { + self::$fixturesPath = __DIR__.'/../../../../fixtures/Symfony/Components/DependencyInjection/'; + + require_once self::$fixturesPath.'/includes/classes.php'; + require_once self::$fixturesPath.'/includes/foo.php'; + } + + public function testCrossCheck() + { + // cross-check loaders/dumpers + + $fixtures = array( + 'services1.xml' => 'xml', + 'services2.xml' => 'xml', + 'services6.xml' => 'xml', + 'services8.xml' => 'xml', + 'services9.xml' => 'xml', + + 'services1.yml' => 'yaml', + 'services2.yml' => 'yaml', + 'services6.yml' => 'yaml', + 'services8.yml' => 'yaml', + 'services9.yml' => 'yaml', + ); + + foreach ($fixtures as $fixture => $type) + { + $loaderClass = 'Symfony\\Components\\DependencyInjection\\Loader\\'.ucfirst($type).'FileLoader'; + $dumperClass = 'Symfony\\Components\\DependencyInjection\\Dumper\\'.ucfirst($type).'Dumper'; + + $container1 = new Builder(); + $loader1 = new $loaderClass($container1); + $loader1->load(self::$fixturesPath.'/'.$type.'/'.$fixture); + $container1->setParameter('path', self::$fixturesPath.'/includes'); + + $dumper = new $dumperClass($container1); + $tmp = tempnam('sf_service_container', 'sf'); + file_put_contents($tmp, $dumper->dump()); + + $container2 = new Builder(); + $loader2 = new $loaderClass($container2); + $loader2->load($tmp); + $container2->setParameter('path', self::$fixturesPath.'/includes'); + + unlink($tmp); + + $this->assertEquals(serialize($container1), serialize($container2), 'loading a dump from a previously loaded container returns the same container'); + + $this->assertEquals($container1->getParameters(), $container2->getParameters(), '->getParameters() returns the same value for both containers'); + + $services1 = array(); + foreach ($container1 as $id => $service) + { + $services1[$id] = serialize($service); + } + $services2 = array(); + foreach ($container2 as $id => $service) + { + $services2[$id] = serialize($service); + } + + unset($services1['service_container'], $services2['service_container']); + + $this->assertEquals($services1, $services2, 'Iterator on the containers returns the same services'); + } + } +} diff --git a/tests/Symfony/Tests/Components/DependencyInjection/DefinitionTest.php b/tests/Symfony/Tests/Components/DependencyInjection/DefinitionTest.php new file mode 100644 index 0000000000..d6a948a4a1 --- /dev/null +++ b/tests/Symfony/Tests/Components/DependencyInjection/DefinitionTest.php @@ -0,0 +1,95 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Tests\Components\DependencyInjection; + +require_once __DIR__.'/../../bootstrap.php'; + +use Symfony\Components\DependencyInjection\Definition; + +class DefinitionTest extends \PHPUnit_Framework_TestCase +{ + public function testConstructor() + { + $def = new Definition('stdClass'); + $this->assertEquals($def->getClass(), 'stdClass', '__construct() takes the class name as its first argument'); + + $def = new Definition('stdClass', array('foo')); + $this->assertEquals($def->getArguments(), array('foo'), '__construct() takes an optional array of arguments as its second argument'); + } + + public function testSetGetConstructor() + { + $def = new Definition('stdClass'); + $this->assertEquals(spl_object_hash($def->setConstructor('foo')), spl_object_hash($def), '->setConstructor() implements a fluent interface'); + $this->assertEquals($def->getConstructor(), 'foo', '->getConstructor() returns the constructor name'); + } + + public function testSetGetClass() + { + $def = new Definition('stdClass'); + $this->assertEquals(spl_object_hash($def->setClass('foo')), spl_object_hash($def), '->setClass() implements a fluent interface'); + $this->assertEquals($def->getClass(), 'foo', '->getClass() returns the class name'); + } + + public function testArguments() + { + $def = new Definition('stdClass'); + $this->assertEquals(spl_object_hash($def->setArguments(array('foo'))), spl_object_hash($def), '->setArguments() implements a fluent interface'); + $this->assertEquals($def->getArguments(), array('foo'), '->getArguments() returns the arguments'); + $this->assertEquals(spl_object_hash($def->addArgument('bar')), spl_object_hash($def), '->addArgument() implements a fluent interface'); + $this->assertEquals($def->getArguments(), array('foo', 'bar'), '->addArgument() adds an argument'); + } + + public function testMethodCalls() + { + $def = new Definition('stdClass'); + $this->assertEquals(spl_object_hash($def->setMethodCalls(array(array('foo', array('foo'))))), spl_object_hash($def), '->setMethodCalls() implements a fluent interface'); + $this->assertEquals($def->getMethodCalls(), array(array('foo', array('foo'))), '->getMethodCalls() returns the methods to call'); + $this->assertEquals(spl_object_hash($def->addMethodCall('bar', array('bar'))), spl_object_hash($def), '->addMethodCall() implements a fluent interface'); + $this->assertEquals($def->getMethodCalls(), array(array('foo', array('foo')), array('bar', array('bar'))), '->addMethodCall() adds a method to call'); + } + + public function testSetGetFile() + { + $def = new Definition('stdClass'); + $this->assertEquals(spl_object_hash($def->setFile('foo')), spl_object_hash($def), '->setFile() implements a fluent interface'); + $this->assertEquals($def->getFile(), 'foo', '->getFile() returns the file to include'); + } + + public function testSetIsShared() + { + $def = new Definition('stdClass'); + $this->assertEquals($def->isShared(), true, '->isShared() returns true by default'); + $this->assertEquals(spl_object_hash($def->setShared(false)), spl_object_hash($def), '->setShared() implements a fluent interface'); + $this->assertEquals($def->isShared(), false, '->isShared() returns false if the instance must not be shared'); + } + + public function testSetGetConfigurator() + { + $def = new Definition('stdClass'); + $this->assertEquals(spl_object_hash($def->setConfigurator('foo')), spl_object_hash($def), '->setConfigurator() implements a fluent interface'); + $this->assertEquals($def->getConfigurator(), 'foo', '->getConfigurator() returns the configurator'); + } + + public function testAnnotations() + { + $def = new Definition('stdClass'); + $this->assertEquals(spl_object_hash($def->addAnnotation('foo')), spl_object_hash($def), '->addAnnotation() implements a fluent interface'); + $this->assertEquals($def->getAnnotation('foo'), array(array()), '->getAnnotation() returns attributes for an annotation name'); + $def->addAnnotation('foo', array('foo' => 'bar')); + $this->assertEquals($def->getAnnotation('foo'), array(array(), array('foo' => 'bar')), '->addAnnotation() can adds the same annotation several times'); + $def->addAnnotation('bar', array('bar' => 'bar')); + $this->assertEquals($def->getAnnotations(), array( + 'foo' => array(array(), array('foo' => 'bar')), + 'bar' => array(array('bar' => 'bar')), + ), '->getAnnotations() returns all annotations'); + } +} diff --git a/tests/Symfony/Tests/Components/DependencyInjection/Dumper/DumperTest.php b/tests/Symfony/Tests/Components/DependencyInjection/Dumper/DumperTest.php new file mode 100644 index 0000000000..7894a31ae5 --- /dev/null +++ b/tests/Symfony/Tests/Components/DependencyInjection/Dumper/DumperTest.php @@ -0,0 +1,37 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Tests\Components\DependencyInjection\Dumper; + +require_once __DIR__.'/../../../bootstrap.php'; + +use Symfony\Components\DependencyInjection\Builder; +use Symfony\Components\DependencyInjection\Dumper\Dumper; + +class DumperTest extends \PHPUnit_Framework_TestCase +{ + public function testDump() + { + $builder = new Builder(); + $dumper = new ProjectDumper($builder); + try + { + $dumper->dump(); + $this->fail('->dump() returns a LogicException if the dump() method has not been overriden by a children class'); + } + catch (\LogicException $e) + { + } + } +} + +class ProjectDumper extends Dumper +{ +} diff --git a/tests/Symfony/Tests/Components/DependencyInjection/Dumper/GraphvizDumperTest.php b/tests/Symfony/Tests/Components/DependencyInjection/Dumper/GraphvizDumperTest.php new file mode 100644 index 0000000000..8863f17886 --- /dev/null +++ b/tests/Symfony/Tests/Components/DependencyInjection/Dumper/GraphvizDumperTest.php @@ -0,0 +1,55 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Tests\Components\DependencyInjection\Dumper; + +require_once __DIR__.'/../../../bootstrap.php'; + +use Symfony\Components\DependencyInjection\Builder; +use Symfony\Components\DependencyInjection\Dumper\GraphvizDumper; + +class GraphvizDumperTest extends \PHPUnit_Framework_TestCase +{ + static protected $fixturesPath; + + static public function setUpBeforeClass() + { + self::$fixturesPath = __DIR__.'/../../../../../fixtures/Symfony/Components/DependencyInjection/'; + } + + public function testDump() + { + $dumper = new GraphvizDumper($container = new Builder()); + + $this->assertEquals($dumper->dump(), file_get_contents(self::$fixturesPath.'/graphviz/services1.dot'), '->dump() dumps an empty container as an empty dot file'); + + $container = new Builder(); + $dumper = new GraphvizDumper($container); + + $container = include self::$fixturesPath.'/containers/container9.php'; + $dumper = new GraphvizDumper($container); + $this->assertEquals($dumper->dump(), str_replace('%path%', __DIR__, file_get_contents(self::$fixturesPath.'/graphviz/services9.dot')), '->dump() dumps services'); + + $container = include self::$fixturesPath.'/containers/container10.php'; + $dumper = new GraphvizDumper($container); + $this->assertEquals($dumper->dump(), str_replace('%path%', __DIR__, file_get_contents(self::$fixturesPath.'/graphviz/services10.dot')), '->dump() dumps services'); + + $container = include self::$fixturesPath.'/containers/container10.php'; + $dumper = new GraphvizDumper($container); + $this->assertEquals($dumper->dump(array( + 'graph' => array('ratio' => 'normal'), + 'node' => array('fontsize' => 13, 'fontname' => 'Verdana', 'shape' => 'square'), + 'edge' => array('fontsize' => 12, 'fontname' => 'Verdana', 'color' => 'white', 'arrowhead' => 'closed', 'arrowsize' => 1), + 'node.instance' => array('fillcolor' => 'green', 'style' => 'empty'), + 'node.definition' => array('fillcolor' => 'grey'), + 'node.missing' => array('fillcolor' => 'red', 'style' => 'empty'), + )), str_replace('%path%', __DIR__, file_get_contents(self::$fixturesPath.'/graphviz/services10-1.dot')), '->dump() dumps services'); + } +} diff --git a/tests/Symfony/Tests/Components/DependencyInjection/Dumper/PhpDumperTest.php b/tests/Symfony/Tests/Components/DependencyInjection/Dumper/PhpDumperTest.php new file mode 100644 index 0000000000..8ffca976f8 --- /dev/null +++ b/tests/Symfony/Tests/Components/DependencyInjection/Dumper/PhpDumperTest.php @@ -0,0 +1,62 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Tests\Components\DependencyInjection\Dumper; + +require_once __DIR__.'/../../../bootstrap.php'; + +use Symfony\Components\DependencyInjection\Builder; +use Symfony\Components\DependencyInjection\Dumper\PhpDumper; + +class PhpDumperTest extends \PHPUnit_Framework_TestCase +{ + static protected $fixturesPath; + + static public function setUpBeforeClass() + { + self::$fixturesPath = realpath(__DIR__.'/../../../../../fixtures/Symfony/Components/DependencyInjection/'); + } + + public function testDump() + { + $dumper = new PhpDumper($container = new Builder()); + + $this->assertEquals($dumper->dump(), file_get_contents(self::$fixturesPath.'/php/services1.php'), '->dump() dumps an empty container as an empty PHP class'); + $this->assertEquals($dumper->dump(array('class' => 'Container', 'base_class' => 'AbstractContainer')), file_get_contents(self::$fixturesPath.'/php/services1-1.php'), '->dump() takes a class and a base_class options'); + + $container = new Builder(); + $dumper = new PhpDumper($container); + } + + public function testAddParameters() + { + $container = include self::$fixturesPath.'/containers/container8.php'; + $dumper = new PhpDumper($container); + $this->assertEquals($dumper->dump(), file_get_contents(self::$fixturesPath.'/php/services8.php'), '->dump() dumps parameters'); + } + + public function testAddService() + { + $container = include self::$fixturesPath.'/containers/container9.php'; + $dumper = new PhpDumper($container); + $this->assertEquals($dumper->dump(), str_replace('%path%', self::$fixturesPath.'/includes', file_get_contents(self::$fixturesPath.'/php/services9.php')), '->dump() dumps services'); + + $dumper = new PhpDumper($container = new Builder()); + $container->register('foo', 'FooClass')->addArgument(new \stdClass()); + try + { + $dumper->dump(); + $this->fail('->dump() throws a RuntimeException if the container to be dumped has reference to objects or resources'); + } + catch (\RuntimeException $e) + { + } + } +} diff --git a/tests/Symfony/Tests/Components/DependencyInjection/Dumper/XmlDumperTest.php b/tests/Symfony/Tests/Components/DependencyInjection/Dumper/XmlDumperTest.php new file mode 100644 index 0000000000..24f0c7f8d0 --- /dev/null +++ b/tests/Symfony/Tests/Components/DependencyInjection/Dumper/XmlDumperTest.php @@ -0,0 +1,61 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Tests\Components\DependencyInjection\Dumper; + +require_once __DIR__.'/../../../bootstrap.php'; + +use Symfony\Components\DependencyInjection\Builder; +use Symfony\Components\DependencyInjection\Dumper\XmlDumper; + +class XmlDumperTest extends \PHPUnit_Framework_TestCase +{ + static protected $fixturesPath; + + static public function setUpBeforeClass() + { + self::$fixturesPath = realpath(__DIR__.'/../../../../../fixtures/Symfony/Components/DependencyInjection/'); + } + + public function testDump() + { + $dumper = new XmlDumper($container = new Builder()); + + $this->assertEquals($dumper->dump(), file_get_contents(self::$fixturesPath.'/xml/services1.xml'), '->dump() dumps an empty container as an empty XML file'); + + $container = new Builder(); + $dumper = new XmlDumper($container); + } + + public function testAddParemeters() + { + $container = include self::$fixturesPath.'//containers/container8.php'; + $dumper = new XmlDumper($container); + $this->assertEquals($dumper->dump(), file_get_contents(self::$fixturesPath.'/xml/services8.xml'), '->dump() dumps parameters'); + } + + public function testAddService() + { + $container = include self::$fixturesPath.'/containers/container9.php'; + $dumper = new XmlDumper($container); + $this->assertEquals($dumper->dump(), str_replace('%path%', self::$fixturesPath.'/includes', file_get_contents(self::$fixturesPath.'/xml/services9.xml')), '->dump() dumps services'); + + $dumper = new XmlDumper($container = new Builder()); + $container->register('foo', 'FooClass')->addArgument(new \stdClass()); + try + { + $dumper->dump(); + $this->fail('->dump() throws a RuntimeException if the container to be dumped has reference to objects or resources'); + } + catch (\RuntimeException $e) + { + } + } +} diff --git a/tests/Symfony/Tests/Components/DependencyInjection/Dumper/YamlDumperTest.php b/tests/Symfony/Tests/Components/DependencyInjection/Dumper/YamlDumperTest.php new file mode 100644 index 0000000000..fb8778f55d --- /dev/null +++ b/tests/Symfony/Tests/Components/DependencyInjection/Dumper/YamlDumperTest.php @@ -0,0 +1,61 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Tests\Components\DependencyInjection\Dumper; + +require_once __DIR__.'/../../../bootstrap.php'; + +use Symfony\Components\DependencyInjection\Builder; +use Symfony\Components\DependencyInjection\Dumper\YamlDumper; + +class YamlDumperTest extends \PHPUnit_Framework_TestCase +{ + static protected $fixturesPath; + + static public function setUpBeforeClass() + { + self::$fixturesPath = realpath(__DIR__.'/../../../../../fixtures/Symfony/Components/DependencyInjection/'); + } + + public function testDump() + { + $dumper = new YamlDumper($container = new Builder()); + + $this->assertEquals($dumper->dump(), file_get_contents(self::$fixturesPath.'/yaml/services1.yml'), '->dump() dumps an empty container as an empty YAML file'); + + $container = new Builder(); + $dumper = new YamlDumper($container); + } + + public function testAddParameters() + { + $container = include self::$fixturesPath.'/containers/container8.php'; + $dumper = new YamlDumper($container); + $this->assertEquals($dumper->dump(), file_get_contents(self::$fixturesPath.'/yaml/services8.yml'), '->dump() dumps parameters'); + } + + public function testAddService() + { + $container = include self::$fixturesPath.'/containers/container9.php'; + $dumper = new YamlDumper($container); + $this->assertEquals($dumper->dump(), str_replace('%path%', self::$fixturesPath.'/includes', file_get_contents(self::$fixturesPath.'/yaml/services9.yml')), '->dump() dumps services'); + + $dumper = new YamlDumper($container = new Builder()); + $container->register('foo', 'FooClass')->addArgument(new \stdClass()); + try + { + $dumper->dump(); + $this->fail('->dump() throws a RuntimeException if the container to be dumped has reference to objects or resources'); + } + catch (\RuntimeException $e) + { + } + } +} diff --git a/tests/Symfony/Tests/Components/DependencyInjection/FileResourceTest.php b/tests/Symfony/Tests/Components/DependencyInjection/FileResourceTest.php new file mode 100644 index 0000000000..f0663126df --- /dev/null +++ b/tests/Symfony/Tests/Components/DependencyInjection/FileResourceTest.php @@ -0,0 +1,47 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Tests\Components\DependencyInjection; + +require_once __DIR__.'/../../bootstrap.php'; + +use Symfony\Components\DependencyInjection\FileResource; + +class FileResourceTest extends \PHPUnit_Framework_TestCase +{ + protected $resource; + protected $file; + + public function setUp() + { + $this->file = sys_get_temp_dir().'/tmp.xml'; + touch($this->file); + $this->resource = new FileResource($this->file); + } + + public function tearDown() + { + unlink($this->file); + } + + public function testGetResource() + { + $this->assertEquals($this->resource->getResource(), $this->file, '->getResource() returns the path to the resource'); + } + + public function testIsUptodate() + { + $this->assertTrue($this->resource->isUptodate(time() + 10), '->isUptodate() returns true if the resource has not changed'); + $this->assertTrue(!$this->resource->isUptodate(time() - 86400), '->isUptodate() returns false if the resource has been updated'); + + $resource = new FileResource('/____foo/foobar'.rand(1, 999999)); + $this->assertTrue(!$resource->isUptodate(time()), '->isUptodate() returns false if the resource does not exist'); + } +} diff --git a/tests/Symfony/Tests/Components/DependencyInjection/Loader/FileLoaderTest.php b/tests/Symfony/Tests/Components/DependencyInjection/Loader/FileLoaderTest.php new file mode 100644 index 0000000000..c24610b9d4 --- /dev/null +++ b/tests/Symfony/Tests/Components/DependencyInjection/Loader/FileLoaderTest.php @@ -0,0 +1,57 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Tests\Components\DependencyInjection\Loader; + +require_once __DIR__.'/../../../bootstrap.php'; + +use Symfony\Components\DependencyInjection\Builder; +use Symfony\Components\DependencyInjection\Loader\FileLoader; + +class XmlDumperTest extends \PHPUnit_Framework_TestCase +{ + public function testConstructor() + { + $loader = new ProjectLoader(__DIR__); + $this->assertEquals($loader->paths, array(__DIR__), '__construct() takes a path as its second argument'); + + $loader = new ProjectLoader(array(__DIR__, __DIR__)); + $this->assertEquals($loader->paths, array(__DIR__, __DIR__), '__construct() takes an array of paths as its second argument'); + } + + public function testGetAbsolutePath() + { + $loader = new ProjectLoader(array(__DIR__.'/../../../../../bin')); + $this->assertEquals($loader->getAbsolutePath('/foo.xml'), '/foo.xml', '->getAbsolutePath() return the path unmodified if it is already an absolute path'); + $this->assertEquals($loader->getAbsolutePath('c:\\\\foo.xml'), 'c:\\\\foo.xml', '->getAbsolutePath() return the path unmodified if it is already an absolute path'); + $this->assertEquals($loader->getAbsolutePath('c:/foo.xml'), 'c:/foo.xml', '->getAbsolutePath() return the path unmodified if it is already an absolute path'); + $this->assertEquals($loader->getAbsolutePath('\\server\\foo.xml'), '\\server\\foo.xml', '->getAbsolutePath() return the path unmodified if it is already an absolute path'); + + $this->assertEquals($loader->getAbsolutePath('FileLoaderTest.php', __DIR__), __DIR__.'/FileLoaderTest.php', '->getAbsolutePath() returns an absolute filename if the file exists in the current path'); + + $this->assertEquals($loader->getAbsolutePath('prove.php', __DIR__), __DIR__.'/../../../../../bin/prove.php', '->getAbsolutePath() returns an absolute filename if the file exists in one of the paths given in the constructor'); + + $this->assertEquals($loader->getAbsolutePath('foo.xml', __DIR__), 'foo.xml', '->getAbsolutePath() returns the path unmodified if it is unable to find it in the given paths'); + } +} + +class ProjectLoader extends FileLoader +{ + public $paths; + + public function load($resource) + { + } + + public function getAbsolutePath($file, $currentPath = null) + { + return parent::getAbsolutePath($file, $currentPath); + } +} diff --git a/tests/Symfony/Tests/Components/DependencyInjection/Loader/IniFileLoaderTest.php b/tests/Symfony/Tests/Components/DependencyInjection/Loader/IniFileLoaderTest.php new file mode 100644 index 0000000000..579af40af9 --- /dev/null +++ b/tests/Symfony/Tests/Components/DependencyInjection/Loader/IniFileLoaderTest.php @@ -0,0 +1,51 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Tests\Components\DependencyInjection\Loader; + +require_once __DIR__.'/../../../bootstrap.php'; + +use Symfony\Components\DependencyInjection\Builder; +use Symfony\Components\DependencyInjection\Loader\IniFileLoader; + +class IniLoaderTest extends \PHPUnit_Framework_TestCase +{ + static protected $fixturesPath; + + static public function setUpBeforeClass() + { + self::$fixturesPath = realpath(__DIR__.'/../../../../../fixtures/Symfony/Components/DependencyInjection/'); + } + + public function testLoader() + { + $loader = new IniFileLoader(self::$fixturesPath.'/ini'); + $config = $loader->load('parameters.ini'); + $this->assertEquals($config->getParameters(), array('foo' => 'bar', 'bar' => '%foo%'), '->load() takes a single file name as its first argument'); + + try + { + $loader->load('foo.ini'); + $this->fail('->load() throws an InvalidArgumentException if the loaded file does not exist'); + } + catch (\InvalidArgumentException $e) + { + } + + try + { + @$loader->load('nonvalid.ini'); + $this->fail('->load() throws an InvalidArgumentException if the loaded file is not parseable'); + } + catch (\InvalidArgumentException $e) + { + } + } +} diff --git a/tests/Symfony/Tests/Components/DependencyInjection/Loader/LoaderExtensionTest.php b/tests/Symfony/Tests/Components/DependencyInjection/Loader/LoaderExtensionTest.php new file mode 100644 index 0000000000..5ae2a5b15b --- /dev/null +++ b/tests/Symfony/Tests/Components/DependencyInjection/Loader/LoaderExtensionTest.php @@ -0,0 +1,35 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Tests\Components\DependencyInjection\Loader; + +require_once __DIR__.'/../../../bootstrap.php'; + +require_once __DIR__.'/../../../../../fixtures/Symfony/Components/DependencyInjection/includes/ProjectExtension.php'; + +class LoaderExtensionTest extends \PHPUnit_Framework_TestCase +{ + public function testLoad() + { + $extension = new \ProjectExtension(); + + try + { + $extension->load('foo', array()); + $this->fail('->load() throws an InvalidArgumentException if the tag does not exist'); + } + catch (\InvalidArgumentException $e) + { + } + + $config = $extension->load('bar', array('foo' => 'bar')); + $this->assertEquals($config->getParameters(), array('project.parameter.bar' => 'bar'), '->load() calls the method tied to the given tag'); + } +} diff --git a/tests/unit/Symfony/Components/DependencyInjection/Loader/LoaderTest.php b/tests/Symfony/Tests/Components/DependencyInjection/Loader/LoaderTest.php similarity index 51% rename from tests/unit/Symfony/Components/DependencyInjection/Loader/LoaderTest.php rename to tests/Symfony/Tests/Components/DependencyInjection/Loader/LoaderTest.php index 59df8aab0a..986af59917 100644 --- a/tests/unit/Symfony/Components/DependencyInjection/Loader/LoaderTest.php +++ b/tests/Symfony/Tests/Components/DependencyInjection/Loader/LoaderTest.php @@ -8,22 +8,26 @@ * file that was distributed with this source code. */ -require_once __DIR__.'/../../../../bootstrap.php'; +namespace Symfony\Tests\Components\DependencyInjection\Loader; -use Symfony\Components\DependencyInjection\Loader\Loader; +require_once __DIR__.'/../../../bootstrap.php'; require_once __DIR__.'/../../../../../fixtures/Symfony/Components/DependencyInjection/includes/ProjectExtension.php'; -class ProjectLoader extends Loader +use Symfony\Components\DependencyInjection\Loader\Loader; + +class ProjectLoader1 extends Loader { public function load($resource) { } } -$t = new LimeTest(1); - -// ::registerExtension() ::getExtension() -$t->diag('::registerExtension() ::getExtension()'); -ProjectLoader::registerExtension($extension = new ProjectExtension()); -$t->ok(ProjectLoader::getExtension('project') === $extension, '::registerExtension() registers an extension'); +class LoaderTest extends \PHPUnit_Framework_TestCase +{ + public function testExtension() + { + ProjectLoader1::registerExtension($extension = new \ProjectExtension()); + $this->assertTrue(ProjectLoader1::getExtension('project') === $extension, '::registerExtension() registers an extension'); + } +} diff --git a/tests/Symfony/Tests/Components/DependencyInjection/Loader/XmlFileLoaderTest.php b/tests/Symfony/Tests/Components/DependencyInjection/Loader/XmlFileLoaderTest.php new file mode 100644 index 0000000000..94fb11c84b --- /dev/null +++ b/tests/Symfony/Tests/Components/DependencyInjection/Loader/XmlFileLoaderTest.php @@ -0,0 +1,203 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Tests\Components\DependencyInjection\Loader; + +require_once __DIR__.'/../../../bootstrap.php'; + +use Symfony\Components\DependencyInjection\Builder; +use Symfony\Components\DependencyInjection\Reference; +use Symfony\Components\DependencyInjection\Definition; +use Symfony\Components\DependencyInjection\Loader\Loader; +use Symfony\Components\DependencyInjection\Loader\XmlFileLoader; + +class XmlFileLoaderTest extends \PHPUnit_Framework_TestCase +{ + static protected $fixturesPath; + + static public function setUpBeforeClass() + { + self::$fixturesPath = realpath(__DIR__.'/../../../../../fixtures/Symfony/Components/DependencyInjection/'); + require_once self::$fixturesPath.'/includes/ProjectExtension.php'; + } + + public function testLoad() + { + $loader = new ProjectLoader2(self::$fixturesPath.'/ini'); + + try + { + $loader->load('foo.xml'); + $this->fail('->load() throws an InvalidArgumentException if the loaded file does not exist'); + } + catch (\InvalidArgumentException $e) + { + } + } + + public function testParseFile() + { + $loader = new ProjectLoader2(self::$fixturesPath.'/ini'); + + try + { + $loader->parseFile(self::$fixturesPath.'/ini/parameters.ini'); + $this->fail('->parseFile() throws an InvalidArgumentException if the loaded file is not a valid XML file'); + } + catch (\InvalidArgumentException $e) + { + } + + $loader = new ProjectLoader2(self::$fixturesPath.'/xml'); + + try + { + $loader->parseFile(self::$fixturesPath.'/xml/nonvalid.xml'); + $this->fail('->parseFile() throws an InvalidArgumentException if the loaded file does not validate the XSD'); + } + catch (\InvalidArgumentException $e) + { + } + + $xml = $loader->parseFile(self::$fixturesPath.'/xml/services1.xml'); + $this->assertEquals(get_class($xml), 'Symfony\\Components\\DependencyInjection\\SimpleXMLElement', '->parseFile() returns an SimpleXMLElement object'); + } + + public function testLoadParameters() + { + $loader = new ProjectLoader2(self::$fixturesPath.'/xml'); + $config = $loader->load('services2.xml'); + + $actual = $config->getParameters(); + $expected = array('a string', 'foo' => 'bar', 'values' => array(0, 'integer' => 4, 100 => null, 'true', true, false, 'on', 'off', 'float' => 1.3, 1000.3, 'a string', array('foo', 'bar')), 'foo_bar' => new Reference('foo_bar')); + + $this->assertEquals($actual, $expected, '->load() converts XML values to PHP ones'); + } + + public function testLoadImports() + { + $loader = new ProjectLoader2(self::$fixturesPath.'/xml'); + $config = $loader->load('services4.xml'); + + $actual = $config->getParameters(); + $expected = array('a string', 'foo' => 'bar', 'values' => array(true, false), 'foo_bar' => new Reference('foo_bar'), 'bar' => '%foo%', 'imported_from_ini' => true, 'imported_from_yaml' => true); + + $this->assertEquals(array_keys($actual), array_keys($expected), '->load() imports and merges imported files'); + } + + public function testLoadAnonymousServices() + { + $loader = new ProjectLoader2(self::$fixturesPath.'/xml'); + $config = $loader->load('services5.xml'); + $services = $config->getDefinitions(); + $this->assertEquals(count($services), 3, '->load() attributes unique ids to anonymous services'); + $args = $services['foo']->getArguments(); + $this->assertEquals(count($args), 1, '->load() references anonymous services as "normal" ones'); + $this->assertEquals(get_class($args[0]), 'Symfony\\Components\\DependencyInjection\\Reference', '->load() converts anonymous services to references to "normal" services'); + $this->assertTrue(isset($services[(string) $args[0]]), '->load() makes a reference to the created ones'); + $inner = $services[(string) $args[0]]; + $this->assertEquals($inner->getClass(), 'BarClass', '->load() uses the same configuration as for the anonymous ones'); + + $args = $inner->getArguments(); + $this->assertEquals(count($args), 1, '->load() references anonymous services as "normal" ones'); + $this->assertEquals(get_class($args[0]), 'Symfony\\Components\\DependencyInjection\\Reference', '->load() converts anonymous services to references to "normal" services'); + $this->assertTrue(isset($services[(string) $args[0]]), '->load() makes a reference to the created ones'); + $inner = $services[(string) $args[0]]; + $this->assertEquals($inner->getClass(), 'BazClass', '->load() uses the same configuration as for the anonymous ones'); + } + + public function testLoadServices() + { + $loader = new ProjectLoader2(self::$fixturesPath.'/xml'); + $config = $loader->load('services6.xml'); + $services = $config->getDefinitions(); + $this->assertTrue(isset($services['foo']), '->load() parses elements'); + $this->assertEquals(get_class($services['foo']), 'Symfony\\Components\\DependencyInjection\\Definition', '->load() converts element to Definition instances'); + $this->assertEquals($services['foo']->getClass(), 'FooClass', '->load() parses the class attribute'); + $this->assertTrue($services['shared']->isShared(), '->load() parses the shared attribute'); + $this->assertTrue(!$services['non_shared']->isShared(), '->load() parses the shared attribute'); + $this->assertEquals($services['constructor']->getConstructor(), 'getInstance', '->load() parses the constructor attribute'); + $this->assertEquals($services['file']->getFile(), '%path%/foo.php', '->load() parses the file tag'); + $this->assertEquals($services['arguments']->getArguments(), array('foo', new Reference('foo'), array(true, false)), '->load() parses the argument tags'); + $this->assertEquals($services['configurator1']->getConfigurator(), 'sc_configure', '->load() parses the configurator tag'); + $this->assertEquals($services['configurator2']->getConfigurator(), array(new Reference('baz'), 'configure'), '->load() parses the configurator tag'); + $this->assertEquals($services['configurator3']->getConfigurator(), array('BazClass', 'configureStatic'), '->load() parses the configurator tag'); + $this->assertEquals($services['method_call1']->getMethodCalls(), array(array('setBar', array())), '->load() parses the method_call tag'); + $this->assertEquals($services['method_call2']->getMethodCalls(), array(array('setBar', array('foo', new Reference('foo'), array(true, false)))), '->load() parses the method_call tag'); + $aliases = $config->getAliases(); + $this->assertTrue(isset($aliases['alias_for_foo']), '->load() parses elements'); + $this->assertEquals($aliases['alias_for_foo'], 'foo', '->load() parses aliases'); + } + + public function testConvertDomElementToArray() + { + $doc = new \DOMDocument("1.0"); + $doc->loadXML('bar'); + $this->assertEquals(ProjectLoader2::convertDomElementToArray($doc->documentElement), 'bar', '::convertDomElementToArray() converts a \DomElement to an array'); + + $doc = new \DOMDocument("1.0"); + $doc->loadXML(''); + $this->assertEquals(ProjectLoader2::convertDomElementToArray($doc->documentElement), array('foo' => 'bar'), '::convertDomElementToArray() converts a \DomElement to an array'); + + $doc = new \DOMDocument("1.0"); + $doc->loadXML('bar'); + $this->assertEquals(ProjectLoader2::convertDomElementToArray($doc->documentElement), array('foo' => 'bar'), '::convertDomElementToArray() converts a \DomElement to an array'); + + $doc = new \DOMDocument("1.0"); + $doc->loadXML('barbar'); + $this->assertEquals(ProjectLoader2::convertDomElementToArray($doc->documentElement), array('foo' => array('value' => 'bar', 'foo' => 'bar')), '::convertDomElementToArray() converts a \DomElement to an array'); + + $doc = new \DOMDocument("1.0"); + $doc->loadXML(''); + $this->assertEquals(ProjectLoader2::convertDomElementToArray($doc->documentElement), array('foo' => null), '::convertDomElementToArray() converts a \DomElement to an array'); + + $doc = new \DOMDocument("1.0"); + $doc->loadXML(''); + $this->assertEquals(ProjectLoader2::convertDomElementToArray($doc->documentElement), array('foo' => null), '::convertDomElementToArray() converts a \DomElement to an array'); + } + + public function testExtensions() + { + Loader::registerExtension(new \ProjectExtension()); + $loader = new ProjectLoader2(self::$fixturesPath.'/xml'); + + $config = $loader->load('services10.xml'); + $services = $config->getDefinitions(); + $parameters = $config->getParameters(); + $this->assertTrue(isset($services['project.service.bar']), '->load() parses extension elements'); + $this->assertTrue(isset($parameters['project.parameter.bar']), '->load() parses extension elements'); + + try + { + $config = $loader->load('services11.xml'); + $this->fail('->load() throws an InvalidArgumentException if the tag is not valid'); + } + catch (\InvalidArgumentException $e) + { + } + + try + { + $config = $loader->load('services12.xml'); + $this->fail('->load() throws an InvalidArgumentException if an extension is not loaded'); + } + catch (\InvalidArgumentException $e) + { + } + } +} + +class ProjectLoader2 extends XmlFileLoader +{ + public function parseFile($file) + { + return parent::parseFile($file); + } +} diff --git a/tests/Symfony/Tests/Components/DependencyInjection/Loader/YamlFileLoaderTest.php b/tests/Symfony/Tests/Components/DependencyInjection/Loader/YamlFileLoaderTest.php new file mode 100644 index 0000000000..6de3e76d01 --- /dev/null +++ b/tests/Symfony/Tests/Components/DependencyInjection/Loader/YamlFileLoaderTest.php @@ -0,0 +1,145 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Tests\Components\DependencyInjection\Loader; + +require_once __DIR__.'/../../../bootstrap.php'; + +use Symfony\Components\DependencyInjection\Builder; +use Symfony\Components\DependencyInjection\Reference; +use Symfony\Components\DependencyInjection\Definition; +use Symfony\Components\DependencyInjection\Loader\Loader; +use Symfony\Components\DependencyInjection\Loader\YamlFileLoader; + +class YamlFileLoaderTest extends \PHPUnit_Framework_TestCase +{ + static protected $fixturesPath; + + static public function setUpBeforeClass() + { + self::$fixturesPath = realpath(__DIR__.'/../../../../../fixtures/Symfony/Components/DependencyInjection/'); + require_once self::$fixturesPath.'/includes/ProjectExtension.php'; + } + + public function testLoadFile() + { + $loader = new ProjectLoader3(self::$fixturesPath.'/ini'); + + try + { + $loader->loadFile('foo.yml'); + $this->fail('->load() throws an InvalidArgumentException if the loaded file does not exist'); + } + catch (\InvalidArgumentException $e) + { + } + + try + { + $loader->loadFile('parameters.ini'); + $this->fail('->load() throws an InvalidArgumentException if the loaded file is not a valid YAML file'); + } + catch (\InvalidArgumentException $e) + { + } + + $loader = new ProjectLoader3(self::$fixturesPath.'/yaml'); + + foreach (array('nonvalid1', 'nonvalid2') as $fixture) + { + try + { + $loader->loadFile($fixture.'.yml'); + $this->fail('->load() throws an InvalidArgumentException if the loaded file does not validate'); + } + catch (\InvalidArgumentException $e) + { + } + } + } + + public function testLoadParameters() + { + $loader = new ProjectLoader3(self::$fixturesPath.'/yaml'); + $config = $loader->load('services2.yml'); + $this->assertEquals($config->getParameters(), array('foo' => 'bar', 'values' => array(true, false, 0, 1000.3), 'bar' => 'foo', 'foo_bar' => new Reference('foo_bar')), '->load() converts YAML keys to lowercase'); + } + + public function testLoadImports() + { + $loader = new ProjectLoader3(self::$fixturesPath.'/yaml'); + $config = $loader->load('services4.yml'); + + $actual = $config->getParameters(); + $expected = array('foo' => 'bar', 'values' => array(true, false), 'bar' => '%foo%', 'foo_bar' => new Reference('foo_bar'), 'imported_from_ini' => true, 'imported_from_xml' => true); + $this->assertEquals(array_keys($actual), array_keys($expected), '->load() imports and merges imported files'); + } + + public function testLoadServices() + { + $loader = new ProjectLoader3(self::$fixturesPath.'/yaml'); + $config = $loader->load('services6.yml'); + $services = $config->getDefinitions(); + $this->assertTrue(isset($services['foo']), '->load() parses service elements'); + $this->assertEquals(get_class($services['foo']), 'Symfony\\Components\\DependencyInjection\\Definition', '->load() converts service element to Definition instances'); + $this->assertEquals($services['foo']->getClass(), 'FooClass', '->load() parses the class attribute'); + $this->assertTrue($services['shared']->isShared(), '->load() parses the shared attribute'); + $this->assertTrue(!$services['non_shared']->isShared(), '->load() parses the shared attribute'); + $this->assertEquals($services['constructor']->getConstructor(), 'getInstance', '->load() parses the constructor attribute'); + $this->assertEquals($services['file']->getFile(), '%path%/foo.php', '->load() parses the file tag'); + $this->assertEquals($services['arguments']->getArguments(), array('foo', new Reference('foo'), array(true, false)), '->load() parses the argument tags'); + $this->assertEquals($services['configurator1']->getConfigurator(), 'sc_configure', '->load() parses the configurator tag'); + $this->assertEquals($services['configurator2']->getConfigurator(), array(new Reference('baz'), 'configure'), '->load() parses the configurator tag'); + $this->assertEquals($services['configurator3']->getConfigurator(), array('BazClass', 'configureStatic'), '->load() parses the configurator tag'); + $this->assertEquals($services['method_call1']->getMethodCalls(), array(array('setBar', array())), '->load() parses the method_call tag'); + $this->assertEquals($services['method_call2']->getMethodCalls(), array(array('setBar', array('foo', new Reference('foo'), array(true, false)))), '->load() parses the method_call tag'); + $aliases = $config->getAliases(); + $this->assertTrue(isset($aliases['alias_for_foo']), '->load() parses aliases'); + $this->assertEquals($aliases['alias_for_foo'], 'foo', '->load() parses aliases'); + } + + public function testExtensions() + { + Loader::registerExtension(new \ProjectExtension()); + $loader = new ProjectLoader3(self::$fixturesPath.'/yaml'); + + $config = $loader->load('services10.yml'); + $services = $config->getDefinitions(); + $parameters = $config->getParameters(); + $this->assertTrue(isset($services['project.service.bar']), '->load() parses extension elements'); + $this->assertTrue(isset($parameters['project.parameter.bar']), '->load() parses extension elements'); + + try + { + $config = $loader->load('services11.yml'); + $this->fail('->load() throws an InvalidArgumentException if the tag is not valid'); + } + catch (\InvalidArgumentException $e) + { + } + + try + { + $config = $loader->load('services12.yml'); + $this->fail('->load() throws an InvalidArgumentException if an extension is not loaded'); + } + catch (\InvalidArgumentException $e) + { + } + } +} + +class ProjectLoader3 extends YamlFileLoader +{ + public function loadFile($file) + { + return parent::loadFile($file); + } +} diff --git a/tests/Symfony/Tests/Components/DependencyInjection/ParameterTest.php b/tests/Symfony/Tests/Components/DependencyInjection/ParameterTest.php new file mode 100644 index 0000000000..39d79f479f --- /dev/null +++ b/tests/Symfony/Tests/Components/DependencyInjection/ParameterTest.php @@ -0,0 +1,24 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Tests\Components\DependencyInjection; + +require_once __DIR__.'/../../bootstrap.php'; + +use Symfony\Components\DependencyInjection\Parameter; + +class ParameterTest extends \PHPUnit_Framework_TestCase +{ + public function testConstructor() + { + $ref = new Parameter('foo'); + $this->assertEquals((string) $ref, 'foo', '__construct() sets the id of the parameter, which is used for the __toString() method'); + } +} diff --git a/tests/Symfony/Tests/Components/DependencyInjection/ReferenceTest.php b/tests/Symfony/Tests/Components/DependencyInjection/ReferenceTest.php new file mode 100644 index 0000000000..2528ae5a3d --- /dev/null +++ b/tests/Symfony/Tests/Components/DependencyInjection/ReferenceTest.php @@ -0,0 +1,24 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Tests\Components\DependencyInjection; + +require_once __DIR__.'/../../bootstrap.php'; + +use Symfony\Components\DependencyInjection\Reference; + +class ReferenceTest extends \PHPUnit_Framework_TestCase +{ + public function testConstructor() + { + $ref = new Reference('foo'); + $this->assertEquals((string) $ref, 'foo', '__construct() sets the id of the reference, which is used for the __toString() method'); + } +} diff --git a/tests/Symfony/Tests/Components/EventDispatcher/EventDispatcherTest.php b/tests/Symfony/Tests/Components/EventDispatcher/EventDispatcherTest.php new file mode 100644 index 0000000000..62892ecb4a --- /dev/null +++ b/tests/Symfony/Tests/Components/EventDispatcher/EventDispatcherTest.php @@ -0,0 +1,143 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Tests\Components\EventDispatcher; + +require_once __DIR__.'/../../bootstrap.php'; + +use Symfony\Components\EventDispatcher\Event; +use Symfony\Components\EventDispatcher\EventDispatcher; + +class EventDispatcherTest extends \PHPUnit_Framework_TestCase +{ + public function testConnectAndDisconnect() + { + $dispatcher = new EventDispatcher(); + + $dispatcher->connect('bar', 'listenToBar'); + $this->assertEquals($dispatcher->getListeners('bar'), array('listenToBar'), '->connect() connects a listener to an event name'); + $dispatcher->connect('bar', 'listenToBarBar'); + $this->assertEquals($dispatcher->getListeners('bar'), array('listenToBar', 'listenToBarBar'), '->connect() can connect several listeners for the same event name'); + + $dispatcher->connect('barbar', 'listenToBarBar'); + $dispatcher->disconnect('bar', 'listenToBarBar'); + $this->assertEquals($dispatcher->getListeners('bar'), array('listenToBar'), '->disconnect() disconnects a listener for an event name'); + $this->assertEquals($dispatcher->getListeners('barbar'), array('listenToBarBar'), '->disconnect() disconnects a listener for an event name'); + + $this->assertTrue($dispatcher->disconnect('foobar', 'listen') === false, '->disconnect() returns false if the listener does not exist'); + } + + public function testGetHasListeners() + { + $dispatcher = new EventDispatcher(); + + $this->assertEquals($dispatcher->hasListeners('foo'), false, '->hasListeners() returns false if the event has no listener'); + $dispatcher->connect('foo', 'listenToFoo'); + $this->assertEquals($dispatcher->hasListeners('foo'), true, '->hasListeners() returns true if the event has some listeners'); + $dispatcher->disconnect('foo', 'listenToFoo'); + $this->assertEquals($dispatcher->hasListeners('foo'), false, '->hasListeners() returns false if the event has no listener'); + + $dispatcher->connect('bar', 'listenToBar'); + $this->assertEquals($dispatcher->getListeners('bar'), array('listenToBar'), '->getListeners() returns an array of listeners connected to the given event name'); + $this->assertEquals($dispatcher->getListeners('foobar'), array(), '->getListeners() returns an empty array if no listener are connected to the given event name'); + } + + public function testNotify() + { + $listener = new Listener(); + $dispatcher = new EventDispatcher(); + $dispatcher->connect('foo', array($listener, 'listenToFoo')); + $dispatcher->connect('foo', array($listener, 'listenToFooBis')); + $e = $dispatcher->notify($event = new Event(new \stdClass(), 'foo')); + $this->assertEquals($listener->getValue(), 'listenToFoolistenToFooBis', '->notify() notifies all registered listeners in order'); + $this->assertEquals($e, $event, '->notify() returns the event object'); + + $listener->reset(); + $dispatcher = new EventDispatcher(); + $dispatcher->connect('foo', array($listener, 'listenToFooBis')); + $dispatcher->connect('foo', array($listener, 'listenToFoo')); + $dispatcher->notify(new Event(new \stdClass(), 'foo')); + $this->assertEquals($listener->getValue(), 'listenToFooBislistenToFoo', '->notify() notifies all registered listeners in order'); + } + + public function testNotifyUntil() + { + $listener = new Listener(); + $dispatcher = new EventDispatcher(); + $dispatcher->connect('foo', array($listener, 'listenToFoo')); + $dispatcher->connect('foo', array($listener, 'listenToFooBis')); + $e = $dispatcher->notifyUntil($event = new Event(new \stdClass(), 'foo')); + $this->assertEquals($listener->getValue(), 'listenToFoolistenToFooBis', '->notifyUntil() notifies all registered listeners in order and stops if it returns true'); + $this->assertEquals($e, $event, '->notifyUntil() returns the event object'); + + $listener->reset(); + $dispatcher = new EventDispatcher(); + $dispatcher->connect('foo', array($listener, 'listenToFooBis')); + $dispatcher->connect('foo', array($listener, 'listenToFoo')); + $e = $dispatcher->notifyUntil($event = new Event(new \stdClass(), 'foo')); + $this->assertEquals($listener->getValue(), 'listenToFooBis', '->notifyUntil() notifies all registered listeners in order and stops if it returns true'); + } + + public function testFilter() + { + $listener = new Listener(); + $dispatcher = new EventDispatcher(); + $dispatcher->connect('foo', array($listener, 'filterFoo')); + $dispatcher->connect('foo', array($listener, 'filterFooBis')); + $e = $dispatcher->filter($event = new Event(new \stdClass(), 'foo'), 'foo'); + $this->assertEquals($e->getReturnValue(), '-*foo*-', '->filter() filters a value'); + $this->assertEquals($e, $event, '->filter() returns the event object'); + + $listener->reset(); + $dispatcher = new EventDispatcher(); + $dispatcher->connect('foo', array($listener, 'filterFooBis')); + $dispatcher->connect('foo', array($listener, 'filterFoo')); + $e = $dispatcher->filter($event = new Event(new \stdClass(), 'foo'), 'foo'); + $this->assertEquals($e->getReturnValue(), '*-foo-*', '->filter() filters a value'); + } +} + +class Listener +{ + protected + $value = ''; + + function filterFoo(Event $event, $foo) + { + return "*$foo*"; + } + + function filterFooBis(Event $event, $foo) + { + return "-$foo-"; + } + + function listenToFoo(Event $event) + { + $this->value .= 'listenToFoo'; + } + + function listenToFooBis(Event $event) + { + $this->value .= 'listenToFooBis'; + + return true; + } + + function getValue() + { + return $this->value; + } + + function reset() + { + $this->value = ''; + } +} diff --git a/tests/Symfony/Tests/Components/EventDispatcher/EventTest.php b/tests/Symfony/Tests/Components/EventDispatcher/EventTest.php new file mode 100644 index 0000000000..a1ff17764a --- /dev/null +++ b/tests/Symfony/Tests/Components/EventDispatcher/EventTest.php @@ -0,0 +1,100 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Tests\Components\EventDispatcher; + +require_once __DIR__.'/../../bootstrap.php'; + +use Symfony\Components\EventDispatcher\Event; + +class EventTest extends \PHPUnit_Framework_TestCase +{ + protected $subject; + protected $parameters; + + public function testGetSubject() + { + $this->assertEquals($this->createEvent()->getSubject(), $this->subject, '->getSubject() returns the event subject'); + } + + public function testGetName() + { + $this->assertEquals($this->createEvent()->getName(), 'name', '->getName() returns the event name'); + } + + public function testParameters() + { + $event = $this->createEvent(); + + $this->assertEquals($event->getParameters(), $this->parameters, '->getParameters() returns the event parameters'); + $this->assertEquals($event->getParameter('foo'), 'bar', '->getParameter() returns the value of a parameter'); + $event->setParameter('foo', 'foo'); + $this->assertEquals($event->getParameter('foo'), 'foo', '->setParameter() changes the value of a parameter'); + $this->assertTrue($event->hasParameter('foo'), '->hasParameter() returns true if the parameter is defined'); + unset($event['foo']); + $this->assertTrue(!$event->hasParameter('foo'), '->hasParameter() returns false if the parameter is not defined'); + + try + { + $event->getParameter('foobar'); + $this->fail('->getParameter() throws an \InvalidArgumentException exception when the parameter does not exist'); + } + catch (\InvalidArgumentException $e) + { + } + $event = new Event($this->subject, 'name', $this->parameters); + } + + public function testSetGetReturnValue() + { + $event = $this->createEvent(); + $event->setReturnValue('foo'); + $this->assertEquals($event->getReturnValue(), 'foo', '->getReturnValue() returns the return value of the event'); + } + + public function testSetIsProcessed() + { + $event = $this->createEvent(); + $event->setProcessed(true); + $this->assertEquals($event->isProcessed(), true, '->isProcessed() returns true if the event has been processed'); + $event->setProcessed(false); + $this->assertEquals($event->isProcessed(), false, '->setProcessed() changes the processed status'); + } + + public function testArrayAccessInterface() + { + $event = $this->createEvent(); + + $this->assertEquals($event['foo'], 'bar', 'Event implements the ArrayAccess interface'); + $event['foo'] = 'foo'; + $this->assertEquals($event['foo'], 'foo', 'Event implements the ArrayAccess interface'); + + try + { + $event['foobar']; + $this->fail('::offsetGet() throws an \InvalidArgumentException exception when the parameter does not exist'); + } + catch (\InvalidArgumentException $e) + { + } + + $this->assertTrue(isset($event['foo']), 'Event implements the ArrayAccess interface'); + unset($event['foo']); + $this->assertTrue(!isset($event['foo']), 'Event implements the ArrayAccess interface'); + } + + protected function createEvent() + { + $this->subject = new \stdClass(); + $this->parameters = array('foo' => 'bar'); + + return new Event($this->subject, 'name', $this->parameters); + } +} diff --git a/tests/Symfony/Tests/Components/OutputEscaper/ArrayDecoratorTest.php b/tests/Symfony/Tests/Components/OutputEscaper/ArrayDecoratorTest.php new file mode 100644 index 0000000000..e86e6e9d1c --- /dev/null +++ b/tests/Symfony/Tests/Components/OutputEscaper/ArrayDecoratorTest.php @@ -0,0 +1,90 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Tests\Components\OutputEscaper; + +require_once __DIR__.'/../../bootstrap.php'; + +use Symfony\Components\OutputEscaper\Escaper; + +class ArrayDecoratorTest extends \PHPUnit_Framework_TestCase +{ + static protected $escaped; + + static public function setUpBeforeClass() + { + $a = array('escaped!', 1, null, array(2, 'escaped!')); + + self::$escaped = Escaper::escape('entities', $a); + } + + public function testGetRaw() + { + $this->assertEquals(self::$escaped->getRaw(0), 'escaped!', '->getRaw() returns the raw value'); + } + + public function testArrayAccessInterface() + { + $this->assertEquals(self::$escaped[0], '<strong>escaped!</strong>', 'The escaped object behaves like an array'); + $this->assertEquals(self::$escaped[2], null, 'The escaped object behaves like an array'); + $this->assertEquals(self::$escaped[3][1], '<strong>escaped!</strong>', 'The escaped object behaves like an array'); + + $this->assertTrue(isset(self::$escaped[1]), 'The escaped object behaves like an array (isset)'); + + try + { + unset(self::$escaped[0]); + + $this->fail('The escaped object is read only (unset)'); + } + catch (\LogicException $e) + { + } + + try + { + self::$escaped[0] = 12; + + $this->fail('The escaped object is read only (set)'); + } + catch (\LogicException $e) + { + } + } + + public function testIteratorInterface() + { + foreach (self::$escaped as $key => $value) + { + switch ($key) + { + case 0: + $this->assertEquals($value, '<strong>escaped!</strong>', 'The escaped object behaves like an array'); + break; + case 1: + $this->assertEquals($value, 1, 'The escaped object behaves like an array'); + break; + case 2: + $this->assertEquals($value, null, 'The escaped object behaves like an array'); + break; + case 3: + break; + default: + $this->fail('The escaped object behaves like an array'); + } + } + } + + public function testCountableInterface() + { + $this->assertEquals(count(self::$escaped), 4, 'The escaped object implements the Countable interface'); + } +} diff --git a/tests/Symfony/Tests/Components/OutputEscaper/EscaperTest.php b/tests/Symfony/Tests/Components/OutputEscaper/EscaperTest.php new file mode 100644 index 0000000000..c63cba990f --- /dev/null +++ b/tests/Symfony/Tests/Components/OutputEscaper/EscaperTest.php @@ -0,0 +1,182 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Tests\Components\OutputEscaper; + +require_once __DIR__.'/../../bootstrap.php'; + +use Symfony\Components\OutputEscaper\Escaper; +use Symfony\Components\OutputEscaper\SafeDecorator; +use Symfony\Components\OutputEscaper\IteratorDecorator; +use Symfony\Components\OutputEscaper\ArrayDecorator; +use Symfony\Components\OutputEscaper\ObjectDecorator; + +class EscaperTest extends \PHPUnit_Framework_TestCase +{ + public function testEscapeDoesNotEscapeSpecialValues() + { + $this->assertSame(Escaper::escape('entities', null), null, '::escape() returns null if the value to escape is null'); + $this->assertSame(Escaper::escape('entities', false), false, '::escape() returns false if the value to escape is false'); + $this->assertSame(Escaper::escape('entities', true), true, '::escape() returns true if the value to escape is true'); + } + + public function testEscapeDoesNotEscapeAValueWhenEscapingMethodIsRAW() + { + $this->assertEquals(Escaper::escape('raw', 'escaped!'), 'escaped!', '::escape() takes an escaping strategy function name as its first argument'); + } + + public function testEscapeEscapesStrings() + { + $this->assertEquals(Escaper::escape('entities', 'escaped!'), '<strong>escaped!</strong>', '::escape() returns an escaped string if the value to escape is a string'); + $this->assertEquals(Escaper::escape('entities', 'échappé'), '<strong>échappé</strong>', '::escape() returns an escaped string if the value to escape is a string'); + } + + public function testEscapeEscapesArrays() + { + $input = array( + 'foo' => 'escaped!', + 'bar' => array('foo' => 'escaped!'), + ); + $output = Escaper::escape('entities', $input); + $this->assertTrue($output instanceof ArrayDecorator, '::escape() returns a ArrayDecorator object if the value to escape is an array'); + $this->assertEquals($output['foo'], '<strong>escaped!</strong>', '::escape() escapes all elements of the original array'); + $this->assertEquals($output['bar']['foo'], '<strong>escaped!</strong>', '::escape() is recursive'); + $this->assertEquals($output->getRawValue(), $input, '->getRawValue() returns the unescaped value'); + } + + public function testEscapeEscapesObjects() + { + $input = new OutputEscaperTestClass(); + $output = Escaper::escape('entities', $input); + $this->assertTrue($output instanceof ObjectDecorator, '::escape() returns a ObjectDecorator object if the value to escape is an object'); + $this->assertEquals($output->getTitle(), '<strong>escaped!</strong>', '::escape() escapes all methods of the original object'); + $this->assertEquals($output->title, '<strong>escaped!</strong>', '::escape() escapes all properties of the original object'); + $this->assertEquals($output->getTitleTitle(), '<strong>escaped!</strong>', '::escape() is recursive'); + $this->assertEquals($output->getRawValue(), $input, '->getRawValue() returns the unescaped value'); + + $this->assertEquals(Escaper::escape('entities', $output)->getTitle(), '<strong>escaped!</strong>', '::escape() does not double escape an object'); + $this->assertTrue(Escaper::escape('entities', new \DirectoryIterator('.')) instanceof IteratorDecorator, '::escape() returns a IteratorDecorator object if the value to escape is an object that implements the ArrayAccess interface'); + } + + public function testEscapeDoesNotEscapeObjectMarkedAsBeingSafe() + { + $this->assertTrue(Escaper::escape('entities', new SafeDecorator(new OutputEscaperTestClass())) instanceof OutputEscaperTestClass, '::escape() returns the original value if it is marked as being safe'); + + Escaper::markClassAsSafe('Symfony\Tests\Components\OutputEscaper\OutputEscaperTestClass'); + $this->assertTrue(Escaper::escape('entities', new OutputEscaperTestClass()) instanceof OutputEscaperTestClass, '::escape() returns the original value if the object class is marked as being safe'); + $this->assertTrue(Escaper::escape('entities', new OutputEscaperTestClassChild()) instanceof OutputEscaperTestClassChild, '::escape() returns the original value if one of the object parent class is marked as being safe'); + } + + public function testEscapeCannotEscapeResources() + { + $fh = fopen(__FILE__, 'r'); + try + { + Escaper::escape('entities', $fh); + + $this->fail('::escape() throws an InvalidArgumentException if the value cannot be escaped'); + } + catch (\InvalidArgumentException $e) + { + } + fclose($fh); + } + + public function testUnescapeDoesNotUnescapeSpecialValues() + { + $this->assertTrue(Escaper::unescape(null) === null, '::unescape() returns null if the value to unescape is null'); + $this->assertTrue(Escaper::unescape(false) === false, '::unescape() returns false if the value to unescape is false'); + $this->assertTrue(Escaper::unescape(true) === true, '::unescape() returns true if the value to unescape is true'); + } + + public function testUnescapeUnescapesStrings() + { + $this->assertEquals(Escaper::unescape('<strong>escaped!</strong>'), 'escaped!', '::unescape() returns an unescaped string if the value to unescape is a string'); + $this->assertEquals(Escaper::unescape('<strong>échappé</strong>'), 'échappé', '::unescape() returns an unescaped string if the value to unescape is a string'); + } + + public function testUnescapeUnescapesArrays() + { + $input = Escaper::escape('entities', array( + 'foo' => 'escaped!', + 'bar' => array('foo' => 'escaped!'), + )); + $output = Escaper::unescape($input); + $this->assertTrue(is_array($output), '::unescape() returns an array if the input is a ArrayDecorator object'); + $this->assertEquals($output['foo'], 'escaped!', '::unescape() unescapes all elements of the original array'); + $this->assertEquals($output['bar']['foo'], 'escaped!', '::unescape() is recursive'); + } + + public function testUnescapeUnescapesObjects() + { + $object = new OutputEscaperTestClass(); + $input = Escaper::escape('entities', $object); + $output = Escaper::unescape($input); + $this->assertTrue($output instanceof OutputEscaperTestClass, '::unescape() returns the original object when a ObjectDecorator object is passed'); + $this->assertEquals($output->getTitle(), 'escaped!', '::unescape() unescapes all methods of the original object'); + $this->assertEquals($output->title, 'escaped!', '::unescape() unescapes all properties of the original object'); + $this->assertEquals($output->getTitleTitle(), 'escaped!', '::unescape() is recursive'); + + $this->assertTrue(IteratorDecorator::unescape(Escaper::escape('entities', new \DirectoryIterator('.'))) instanceof \DirectoryIterator, '::unescape() unescapes IteratorDecorator objects'); + } + + public function testUnescapeDoesNotUnescapeObjectMarkedAsBeingSafe() + { + $this->assertTrue(Escaper::unescape(Escaper::escape('entities', new SafeDecorator(new OutputEscaperTestClass()))) instanceof OutputEscaperTestClass, '::unescape() returns the original value if it is marked as being safe'); + + Escaper::markClassAsSafe('OutputEscaperTestClass'); + $this->assertTrue(Escaper::unescape(Escaper::escape('entities', new OutputEscaperTestClass())) instanceof OutputEscaperTestClass, '::unescape() returns the original value if the object class is marked as being safe'); + $this->assertTrue(Escaper::unescape(Escaper::escape('entities', new OutputEscaperTestClassChild())) instanceof OutputEscaperTestClassChild, '::unescape() returns the original value if one of the object parent class is marked as being safe'); + } + + public function testUnescapeDoesNothingToResources() + { + $fh = fopen(__FILE__, 'r'); + $this->assertEquals(Escaper::unescape($fh), $fh, '::unescape() do nothing to resources'); + } + + public function testUnescapeUnescapesMixedArrays() + { + $object = new OutputEscaperTestClass(); + $input = array( + 'foo' => 'bar', + 'bar' => Escaper::escape('entities', 'bar'), + 'foobar' => Escaper::escape('entities', $object), + ); + $output = array( + 'foo' => 'bar', + 'bar' => 'bar', + 'foobar' => $object, + ); + $this->assertEquals(Escaper::unescape($input), $output, '::unescape() unescapes values with some escaped and unescaped values'); + } +} + +class OutputEscaperTestClass +{ + public $title = 'escaped!'; + + public function getTitle() + { + return $this->title; + } + + public function getTitleTitle() + { + $o = new self; + + return $o->getTitle(); + } +} + +class OutputEscaperTestClassChild extends OutputEscaperTestClass +{ +} diff --git a/tests/Symfony/Tests/Components/OutputEscaper/ObjectDecoratorTest.php b/tests/Symfony/Tests/Components/OutputEscaper/ObjectDecoratorTest.php new file mode 100644 index 0000000000..c269ea63e5 --- /dev/null +++ b/tests/Symfony/Tests/Components/OutputEscaper/ObjectDecoratorTest.php @@ -0,0 +1,59 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Tests\Components\OutputEscaper; + +require_once __DIR__.'/../../bootstrap.php'; + +use Symfony\Components\OutputEscaper\Escaper; + +class ObjectDecoratorTest extends \PHPUnit_Framework_TestCase +{ + static protected $escaped; + + static public function setUpBeforeClass() + { + $object = new OutputEscaperTest(); + + self::$escaped = Escaper::escape('entities', $object); + } + + public function testGenericBehavior() + { + $this->assertEquals(self::$escaped->getTitle(), '<strong>escaped!</strong>', 'The escaped object behaves like the real object'); + + $array = self::$escaped->getTitles(); + $this->assertEquals($array[2], '<strong>escaped!</strong>', 'The escaped object behaves like the real object'); + } + + public function testMagicToString() + { + $this->assertEquals(self::$escaped->__toString(), '<strong>escaped!</strong>', 'The escaped object behaves like the real object'); + } +} + +class OutputEscaperTest +{ + public function __toString() + { + return $this->getTitle(); + } + + public function getTitle() + { + return 'escaped!'; + } + + public function getTitles() + { + return array(1, 2, 'escaped!'); + } +} diff --git a/tests/Symfony/Tests/Components/OutputEscaper/SafeDecoratorTest.php b/tests/Symfony/Tests/Components/OutputEscaper/SafeDecoratorTest.php new file mode 100644 index 0000000000..97c91718d6 --- /dev/null +++ b/tests/Symfony/Tests/Components/OutputEscaper/SafeDecoratorTest.php @@ -0,0 +1,98 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Tests\Components\OutputEscaper; + +require_once __DIR__.'/../../bootstrap.php'; + +use Symfony\Components\OutputEscaper\SafeDecorator; + +class SafeDecoratorTest extends \PHPUnit_Framework_TestCase +{ + public function testGetValue() + { + $safe = new SafeDecorator('foo'); + $this->assertEquals($safe->getValue(), 'foo', '->getValue() returns the embedded value'); + } + + public function testMagicGetAndSet() + { + $safe = new SafeDecorator(new TestClass1()); + + $this->assertEquals($safe->foo, 'bar', '->__get() returns the object parameter'); + $safe->foo = 'baz'; + $this->assertEquals($safe->foo, 'baz', '->__set() sets the object parameter'); + } + + public function testMagicCall() + { + $safe = new SafeDecorator(new TestClass2()); + + $this->assertEquals($safe->doSomething(), 'ok', '->__call() invokes the embedded method'); + } + + public function testMagicIssetAndUnset() + { + $safe = new SafeDecorator(new TestClass3()); + + $this->assertEquals(isset($safe->boolValue), true, '->__isset() returns true if the property is not null'); + $this->assertEquals(isset($safe->nullValue), false, '->__isset() returns false if the property is null'); + $this->assertEquals(isset($safe->undefinedValue), false, '->__isset() returns false if the property does not exist'); + + unset($safe->boolValue); + $this->assertEquals(isset($safe->boolValue), false, '->__unset() unsets the embedded property'); + } + + public function testIteratorInterface() + { + $input = array('one' => 1, 'two' => 2, 'three' => 3, 'children' => array(1, 2, 3)); + $output = array(); + + $safe = new SafeDecorator($input); + foreach ($safe as $key => $value) + { + $output[$key] = $value; + } + $this->assertSame($output, $input, '"Iterator" implementation imitates an array'); + } + + public function testArrayAccessIterator() + { + $safe = new SafeDecorator(array('foo' => 'bar')); + + $this->assertEquals($safe['foo'], 'bar', '"ArrayAccess" implementation returns a value from the embedded array'); + $safe['foo'] = 'baz'; + $this->assertEquals($safe['foo'], 'baz', '"ArrayAccess" implementation sets a value on the embedded array'); + $this->assertEquals(isset($safe['foo']), true, '"ArrayAccess" checks if a value is set on the embedded array'); + unset($safe['foo']); + $this->assertEquals(isset($safe['foo']), false, '"ArrayAccess" unsets a value on the embedded array'); + } +} + +class TestClass1 +{ + public $foo = 'bar'; +} + +class TestClass2 +{ + public function doSomething() + { + return 'ok'; + } +} + +class TestClass3 +{ + public + $boolValue = true, + $nullValue = null; +} diff --git a/tests/Symfony/Tests/Components/Yaml/DumperTest.php b/tests/Symfony/Tests/Components/Yaml/DumperTest.php new file mode 100644 index 0000000000..7220e182da --- /dev/null +++ b/tests/Symfony/Tests/Components/Yaml/DumperTest.php @@ -0,0 +1,173 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Tests\Components\OutputEscaper; + +require_once __DIR__.'/../../bootstrap.php'; + +use Symfony\Components\Yaml\Yaml; +use Symfony\Components\Yaml\Parser; +use Symfony\Components\Yaml\Dumper; + +class DumperTest extends \PHPUnit_Framework_TestCase +{ + protected $parser; + protected $dumper; + protected $path; + + static public function setUpBeforeClass() + { + Yaml::setSpecVersion('1.1'); + } + + public function setUp() + { + $this->parser = new Parser(); + $this->dumper = new Dumper(); + $this->path = __DIR__.'/../../../../fixtures/Symfony/Components/Yaml'; + } + + public function testSpecifications() + { + $files = $this->parser->parse(file_get_contents($this->path.'/index.yml')); + foreach ($files as $file) + { + $yamls = file_get_contents($this->path.'/'.$file.'.yml'); + + // split YAMLs documents + foreach (preg_split('/^---( %YAML\:1\.0)?/m', $yamls) as $yaml) + { + if (!$yaml) + { + continue; + } + + $test = $this->parser->parse($yaml); + if (isset($test['dump_skip']) && $test['dump_skip']) + { + continue; + } + else if (isset($test['todo']) && $test['todo']) + { + // TODO + } + else + { + $expected = eval('return '.trim($test['php']).';'); + + $this->assertEquals($this->parser->parse($this->dumper->dump($expected, 10)), $expected, $test['test']); + } + } + } + } + + public function testInlineLevel() + { + // inline level + $array = array( + '' => 'bar', + 'foo' => '#bar', + 'foo\'bar' => array(), + 'bar' => array(1, 'foo'), + 'foobar' => array( + 'foo' => 'bar', + 'bar' => array(1, 'foo'), + 'foobar' => array( + 'foo' => 'bar', + 'bar' => array(1, 'foo'), + ), + ), + ); + + $expected = <<assertEquals($this->dumper->dump($array, -10), $expected, '->dump() takes an inline level argument'); +$this->assertEquals($this->dumper->dump($array, 0), $expected, '->dump() takes an inline level argument'); + +$expected = <<assertEquals($this->dumper->dump($array, 1), $expected, '->dump() takes an inline level argument'); + + $expected = <<assertEquals($this->dumper->dump($array, 2), $expected, '->dump() takes an inline level argument'); + + $expected = <<assertEquals($this->dumper->dump($array, 3), $expected, '->dump() takes an inline level argument'); + + $expected = <<assertEquals($this->dumper->dump($array, 4), $expected, '->dump() takes an inline level argument'); + $this->assertEquals($this->dumper->dump($array, 10), $expected, '->dump() takes an inline level argument'); + } + + public function testObjectsSupport() + { + $a = array('foo' => new A(), 'bar' => 1); + + $this->assertEquals($this->dumper->dump($a), '{ foo: !!php/object:O:40:"Symfony\Tests\Components\OutputEscaper\A":1:{s:1:"a";s:3:"foo";}, bar: 1 }', '->dump() is able to dump objects'); + } +} + +class A +{ + public $a = 'foo'; +} diff --git a/tests/Symfony/Tests/Components/Yaml/InlineTest.php b/tests/Symfony/Tests/Components/Yaml/InlineTest.php new file mode 100644 index 0000000000..47501a1f62 --- /dev/null +++ b/tests/Symfony/Tests/Components/Yaml/InlineTest.php @@ -0,0 +1,163 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Tests\Components\OutputEscaper; + +require_once __DIR__.'/../../bootstrap.php'; + +use Symfony\Components\Yaml\Yaml; +use Symfony\Components\Yaml\Inline; + +class InlineTest extends \PHPUnit_Framework_TestCase +{ + static public function setUpBeforeClass() + { + Yaml::setSpecVersion('1.1'); + } + + public function testLoad() + { + foreach ($this->getTestsForLoad() as $yaml => $value) + { + $this->assertEquals(Inline::load($yaml), $value, sprintf('::load() converts an inline YAML to a PHP structure (%s)', $yaml)); + } + } + + public function testDump() + { + $testsForDump = $this->getTestsForDump(); + + foreach ($testsForDump as $yaml => $value) + { + $this->assertEquals(Inline::dump($value), $yaml, sprintf('::dump() converts a PHP structure to an inline YAML (%s)', $yaml)); + } + + foreach ($this->getTestsForLoad() as $yaml => $value) + { + if ($value == 1230) + { + continue; + } + + $this->assertEquals(Inline::load(Inline::dump($value)), $value, 'check consistency'); + } + + foreach ($testsForDump as $yaml => $value) + { + if ($value == 1230) + { + continue; + } + + $this->assertEquals(Inline::load(Inline::dump($value)), $value, 'check consistency'); + } + } + + protected function getTestsForLoad() + { + return array( + '' => '', + 'null' => null, + 'false' => false, + 'true' => true, + '12' => 12, + '"quoted string"' => 'quoted string', + "'quoted string'" => 'quoted string', + '12.30e+02' => 12.30e+02, + '0x4D2' => 0x4D2, + '02333' => 02333, + '.Inf' => -log(0), + '-.Inf' => log(0), + '123456789123456789' => '123456789123456789', + '"foo\r\nbar"' => "foo\r\nbar", + "'foo#bar'" => 'foo#bar', + "'foo # bar'" => 'foo # bar', + "'#cfcfcf'" => '#cfcfcf', + + '2007-10-30' => mktime(0, 0, 0, 10, 30, 2007), + '2007-10-30T02:59:43Z' => gmmktime(2, 59, 43, 10, 30, 2007), + '2007-10-30 02:59:43 Z' => gmmktime(2, 59, 43, 10, 30, 2007), + + '"a \\"string\\" with \'quoted strings inside\'"' => 'a "string" with \'quoted strings inside\'', + "'a \"string\" with ''quoted strings inside'''" => 'a "string" with \'quoted strings inside\'', + + // sequences + // urls are no key value mapping. see #3609. Valid yaml "key: value" mappings require a space after the colon + '[foo, http://urls.are/no/mappings, false, null, 12]' => array('foo', 'http://urls.are/no/mappings', false, null, 12), + '[ foo , bar , false , null , 12 ]' => array('foo', 'bar', false, null, 12), + '[\'foo,bar\', \'foo bar\']' => array('foo,bar', 'foo bar'), + + // mappings + '{foo:bar,bar:foo,false:false,null:null,integer:12}' => array('foo' => 'bar', 'bar' => 'foo', 'false' => false, 'null' => null, 'integer' => 12), + '{ foo : bar, bar : foo, false : false, null : null, integer : 12 }' => array('foo' => 'bar', 'bar' => 'foo', 'false' => false, 'null' => null, 'integer' => 12), + '{foo: \'bar\', bar: \'foo: bar\'}' => array('foo' => 'bar', 'bar' => 'foo: bar'), + '{\'foo\': \'bar\', "bar": \'foo: bar\'}' => array('foo' => 'bar', 'bar' => 'foo: bar'), + '{\'foo\'\'\': \'bar\', "bar\"": \'foo: bar\'}' => array('foo\'' => 'bar', "bar\"" => 'foo: bar'), + '{\'foo: \': \'bar\', "bar: ": \'foo: bar\'}' => array('foo: ' => 'bar', "bar: " => 'foo: bar'), + + // nested sequences and mappings + '[foo, [bar, foo]]' => array('foo', array('bar', 'foo')), + '[foo, {bar: foo}]' => array('foo', array('bar' => 'foo')), + '{ foo: {bar: foo} }' => array('foo' => array('bar' => 'foo')), + '{ foo: [bar, foo] }' => array('foo' => array('bar', 'foo')), + + '[ foo, [ bar, foo ] ]' => array('foo', array('bar', 'foo')), + + '[{ foo: {bar: foo} }]' => array(array('foo' => array('bar' => 'foo'))), + + '[foo, [bar, [foo, [bar, foo]], foo]]' => array('foo', array('bar', array('foo', array('bar', 'foo')), 'foo')), + + '[foo, {bar: foo, foo: [foo, {bar: foo}]}, [foo, {bar: foo}]]' => array('foo', array('bar' => 'foo', 'foo' => array('foo', array('bar' => 'foo'))), array('foo', array('bar' => 'foo'))), + + '[foo, bar: { foo: bar }]' => array('foo', '1' => array('bar' => array('foo' => 'bar'))), + ); + } + + protected function getTestsForDump() + { + return array( + 'null' => null, + 'false' => false, + 'true' => true, + '12' => 12, + "'quoted string'" => 'quoted string', + '12.30e+02' => 12.30e+02, + '1234' => 0x4D2, + '1243' => 02333, + '.Inf' => -log(0), + '-.Inf' => log(0), + '"foo\r\nbar"' => "foo\r\nbar", + "'foo#bar'" => 'foo#bar', + "'foo # bar'" => 'foo # bar', + "'#cfcfcf'" => '#cfcfcf', + + "'a \"string\" with ''quoted strings inside'''" => 'a "string" with \'quoted strings inside\'', + + // sequences + '[foo, bar, false, null, 12]' => array('foo', 'bar', false, null, 12), + '[\'foo,bar\', \'foo bar\']' => array('foo,bar', 'foo bar'), + + // mappings + '{ foo: bar, bar: foo, \'false\': false, \'null\': null, integer: 12 }' => array('foo' => 'bar', 'bar' => 'foo', 'false' => false, 'null' => null, 'integer' => 12), + '{ foo: bar, bar: \'foo: bar\' }' => array('foo' => 'bar', 'bar' => 'foo: bar'), + + // nested sequences and mappings + '[foo, [bar, foo]]' => array('foo', array('bar', 'foo')), + + '[foo, [bar, [foo, [bar, foo]], foo]]' => array('foo', array('bar', array('foo', array('bar', 'foo')), 'foo')), + + '{ foo: { bar: foo } }' => array('foo' => array('bar' => 'foo')), + + '[foo, { bar: foo }]' => array('foo', array('bar' => 'foo')), + + '[foo, { bar: foo, foo: [foo, { bar: foo }] }, [foo, { bar: foo }]]' => array('foo', array('bar' => 'foo', 'foo' => array('foo', array('bar' => 'foo'))), array('foo', array('bar' => 'foo'))), + ); + } +} diff --git a/tests/Symfony/Tests/Components/Yaml/ParserTest.php b/tests/Symfony/Tests/Components/Yaml/ParserTest.php new file mode 100644 index 0000000000..5e1d4ed100 --- /dev/null +++ b/tests/Symfony/Tests/Components/Yaml/ParserTest.php @@ -0,0 +1,103 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ + +namespace Symfony\Tests\Components\OutputEscaper; + +require_once __DIR__.'/../../bootstrap.php'; + +use Symfony\Components\Yaml\Yaml; +use Symfony\Components\Yaml\Parser; +use Symfony\Components\Yaml\ParserException; + +class ParserTest extends \PHPUnit_Framework_TestCase +{ + protected $parser; + protected $path; + + static public function setUpBeforeClass() + { + Yaml::setSpecVersion('1.1'); + } + + public function setUp() + { + $this->parser = new Parser(); + $this->path = __DIR__.'/../../../../fixtures/Symfony/Components/Yaml'; + } + + public function testSpecifications() + { + $files = $this->parser->parse(file_get_contents($this->path.'/index.yml')); + foreach ($files as $file) + { + $yamls = file_get_contents($this->path.'/'.$file.'.yml'); + + // split YAMLs documents + foreach (preg_split('/^---( %YAML\:1\.0)?/m', $yamls) as $yaml) + { + if (!$yaml) + { + continue; + } + + $test = $this->parser->parse($yaml); + if (isset($test['todo']) && $test['todo']) + { + // TODO + } + else + { + $expected = var_export(eval('return '.trim($test['php']).';'), true); + + $this->assertEquals(var_export($this->parser->parse($test['yaml']), true), $expected, $test['test']); + } + } + } + } + + public function testTabsInYaml() + { + // test tabs in YAML + $yamls = array( + "foo:\n bar", + "foo:\n bar", + "foo:\n bar", + "foo:\n bar", + ); + + foreach ($yamls as $yaml) + { + try + { + $content = $this->parser->parse($yaml); + + $this->fail('YAML files must not contain tabs'); + } + catch (ParserException $e) + { + } + } + } + + public function testObjectsSupport() + { + $b = array('foo' => new B(), 'bar' => 1); + $this->assertEquals($this->parser->parse(<<parse() is able to dump objects'); + } +} + +class B +{ + public $b = 'foo'; +} diff --git a/tests/unit/Symfony/Components/Console/Output/NullOutputTest.php b/tests/Symfony/Tests/bootstrap.php similarity index 50% rename from tests/unit/Symfony/Components/Console/Output/NullOutputTest.php rename to tests/Symfony/Tests/bootstrap.php index 096a1cf78f..5eefbe7ebb 100644 --- a/tests/unit/Symfony/Components/Console/Output/NullOutputTest.php +++ b/tests/Symfony/Tests/bootstrap.php @@ -2,18 +2,16 @@ /* * This file is part of the symfony package. + * * (c) Fabien Potencier * * For the full copyright and license information, please view the LICENSE * file that was distributed with this source code. */ -require_once __DIR__.'/../../../../bootstrap.php'; +require_once __DIR__.'/../../../src/Symfony/Foundation/ClassLoader.php'; +require_once 'PHPUnit/Framework.php'; -use Symfony\Components\Console\Output\NullOutput; - -$t = new LimeTest(1); - -$output = new NullOutput(); -$output->write('foo'); -$t->pass('->write() does nothing'); +$loader = new Symfony\Foundation\ClassLoader(); +$loader->registerNamespace('Symfony', __DIR__.'/../../../src'); +$loader->register(); diff --git a/tests/unit/Symfony/Components/Console/ApplicationTest.php b/tests/unit/Symfony/Components/Console/ApplicationTest.php deleted file mode 100644 index ad5b752266..0000000000 --- a/tests/unit/Symfony/Components/Console/ApplicationTest.php +++ /dev/null @@ -1,312 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -require_once __DIR__.'/../../../bootstrap.php'; - -use Symfony\Components\Console\Application; -use Symfony\Components\Console\Input\ArrayInput; -use Symfony\Components\Console\Output\Output; -use Symfony\Components\Console\Output\StreamOutput; -use Symfony\Components\Console\Tester\ApplicationTester; - -$fixtures = __DIR__.'/../../../../fixtures/Symfony/Components/Console'; - -require_once $fixtures.'/FooCommand.php'; -require_once $fixtures.'/Foo1Command.php'; -require_once $fixtures.'/Foo2Command.php'; - -$t = new LimeTest(52); - -// __construct() -$t->diag('__construct()'); -$application = new Application('foo', 'bar'); -$t->is($application->getName(), 'foo', '__construct() takes the application name as its first argument'); -$t->is($application->getVersion(), 'bar', '__construct() takes the application version as its first argument'); -$t->is(array_keys($application->getCommands()), array('help', 'list'), '__construct() registered the help and list commands by default'); - -// ->setName() ->getName() -$t->diag('->setName() ->getName()'); -$application = new Application(); -$application->setName('foo'); -$t->is($application->getName(), 'foo', '->setName() sets the name of the application'); - -// ->getVersion() ->getVersion() -$t->diag('->getVersion() ->getVersion()'); -$application = new Application(); -$application->setVersion('bar'); -$t->is($application->getVersion(), 'bar', '->setVersion() sets the version of the application'); - -// ->getLongVersion() -$t->diag('->getLongVersion()'); -$application = new Application('foo', 'bar'); -$t->is($application->getLongVersion(), 'foo version bar', '->getLongVersion() returns the long version of the application'); - -// ->getHelp() -$t->diag('->getHelp()'); -$application = new Application(); -$t->is($application->getHelp(), file_get_contents($fixtures.'/application_gethelp.txt'), '->setHelp() returns a help message'); - -// ->getCommands() -$t->diag('->getCommands()'); -$application = new Application(); -$commands = $application->getCommands(); -$t->is(get_class($commands['help']), 'Symfony\\Components\\Console\\Command\\HelpCommand', '->getCommands() returns the registered commands'); - -$application->addCommand(new FooCommand()); -$commands = $application->getCommands('foo'); -$t->is(count($commands), 1, '->getCommands() takes a namespace as its first argument'); - -// ->register() -$t->diag('->register()'); -$application = new Application(); -$command = $application->register('foo'); -$t->is($command->getName(), 'foo', '->register() regiters a new command'); - -// ->addCommand() ->addCommands() -$t->diag('->addCommand() ->addCommands()'); -$application = new Application(); -$application->addCommand($foo = new FooCommand()); -$commands = $application->getCommands(); -$t->is($commands['foo:bar'], $foo, '->addCommand() registers a command'); - -$application = new Application(); -$application->addCommands(array($foo = new FooCommand(), $foo1 = new Foo1Command())); -$commands = $application->getCommands(); -$t->is(array($commands['foo:bar'], $commands['foo:bar1']), array($foo, $foo1), '->addCommands() registers an array of commands'); - -// ->hasCommand() ->getCommand() -$t->diag('->hasCommand() ->getCommand()'); -$application = new Application(); -$t->ok($application->hasCommand('list'), '->hasCommand() returns true if a named command is registered'); -$t->ok(!$application->hasCommand('afoobar'), '->hasCommand() returns false if a named command is not registered'); - -$application->addCommand($foo = new FooCommand()); -$t->ok($application->hasCommand('afoobar'), '->hasCommand() returns true if an alias is registered'); -$t->is($application->getCommand('foo:bar'), $foo, '->getCommand() returns a command by name'); -$t->is($application->getCommand('afoobar'), $foo, '->getCommand() returns a command by alias'); - -try -{ - $application->getCommand('foofoo'); - $t->fail('->getCommand() throws an \InvalidArgumentException if the command does not exist'); -} -catch (\InvalidArgumentException $e) -{ - $t->pass('->getCommand() throws an \InvalidArgumentException if the command does not exist'); -} - -class TestApplication extends Application -{ - public function setWantHelps() - { - $this->wantHelps = true; - } -} -$application = new TestApplication(); -$application->addCommand($foo = new FooCommand()); -$application->setWantHelps(); -$command = $application->getCommand('foo:bar'); -$t->is(get_class($command), 'Symfony\Components\Console\Command\HelpCommand', '->getCommand() returns the help command if --help is provided as the input'); - -// ->getNamespaces() -$t->diag('->getNamespaces()'); -$application = new TestApplication(); -$application->addCommand(new FooCommand()); -$application->addCommand(new Foo1Command()); -$t->is($application->getNamespaces(), array('foo'), '->getNamespaces() returns an array of unique used namespaces'); - -// ->findNamespace() -$t->diag('->findNamespace()'); -$application = new TestApplication(); -$application->addCommand(new FooCommand()); -$t->is($application->findNamespace('foo'), 'foo', '->findNamespace() returns the given namespace if it exists'); -$t->is($application->findNamespace('f'), 'foo', '->findNamespace() finds a namespace given an abbreviation'); -$application->addCommand(new Foo2Command()); -$t->is($application->findNamespace('foo'), 'foo', '->findNamespace() returns the given namespace if it exists'); -try -{ - $application->findNamespace('f'); - $t->fail('->findNamespace() throws an \InvalidArgumentException if the abbreviation is ambiguous'); -} -catch (\InvalidArgumentException $e) -{ - $t->pass('->findNamespace() throws an \InvalidArgumentException if the abbreviation is ambiguous'); -} - -try -{ - $application->findNamespace('bar'); - $t->fail('->findNamespace() throws an \InvalidArgumentException if no command is in the given namespace'); -} -catch (\InvalidArgumentException $e) -{ - $t->pass('->findNamespace() throws an \InvalidArgumentException if no command is in the given namespace'); -} - -// ->findCommand() -$t->diag('->findCommand()'); -$application = new TestApplication(); -$application->addCommand(new FooCommand()); -$t->is(get_class($application->findCommand('foo:bar')), 'FooCommand', '->findCommand() returns a command if its name exists'); -$t->is(get_class($application->findCommand('h')), 'Symfony\Components\Console\Command\HelpCommand', '->findCommand() returns a command if its name exists'); -$t->is(get_class($application->findCommand('f:bar')), 'FooCommand', '->findCommand() returns a command if the abbreviation for the namespace exists'); -$t->is(get_class($application->findCommand('f:b')), 'FooCommand', '->findCommand() returns a command if the abbreviation for the namespace and the command name exist'); -$t->is(get_class($application->findCommand('a')), 'FooCommand', '->findCommand() returns a command if the abbreviation exists for an alias'); - -$application->addCommand(new Foo1Command()); -$application->addCommand(new Foo2Command()); - -try -{ - $application->findCommand('f'); - $t->fail('->findCommand() throws an \InvalidArgumentException if the abbreviation is ambiguous for a namespace'); -} -catch (\InvalidArgumentException $e) -{ - $t->pass('->findCommand() throws an \InvalidArgumentException if the abbreviation is ambiguous for a namespace'); -} - -try -{ - $application->findCommand('a'); - $t->fail('->findCommand() throws an \InvalidArgumentException if the abbreviation is ambiguous for an alias'); -} -catch (\InvalidArgumentException $e) -{ - $t->pass('->findCommand() throws an \InvalidArgumentException if the abbreviation is ambiguous for an alias'); -} - -try -{ - $application->findCommand('foo:b'); - $t->fail('->findCommand() throws an \InvalidArgumentException if the abbreviation is ambiguous for a command'); -} -catch (\InvalidArgumentException $e) -{ - $t->pass('->findCommand() throws an \InvalidArgumentException if the abbreviation is ambiguous for a taks'); -} - -// ->setCatchExceptions() -$t->diag('->setCatchExceptions()'); -$application = new Application(); -$application->setAutoExit(false); -$tester = new ApplicationTester($application); - -$application->setCatchExceptions(true); -$tester->run(array('command' => 'foo')); -$t->is($tester->getDisplay(), file_get_contents($fixtures.'/application_renderexception1.txt'), '->setCatchExceptions() sets the catch exception flag'); - -$application->setCatchExceptions(false); -try -{ - $tester->run(array('command' => 'foo')); - $t->fail('->setCatchExceptions() sets the catch exception flag'); -} -catch (\Exception $e) -{ - $t->pass('->setCatchExceptions() sets the catch exception flag'); -} - -// ->asText() -$t->diag('->asText()'); -$application = new Application(); -$application->addCommand(new FooCommand); -$t->is($application->asText(), file_get_contents($fixtures.'/application_astext1.txt'), '->asText() returns a text representation of the application'); -$t->is($application->asText('foo'), file_get_contents($fixtures.'/application_astext2.txt'), '->asText() returns a text representation of the application'); - -// ->asXml() -$t->diag('->asXml()'); -$application = new Application(); -$application->addCommand(new FooCommand); -$t->is($application->asXml(), file_get_contents($fixtures.'/application_asxml1.txt'), '->asXml() returns an XML representation of the application'); -$t->is($application->asXml('foo'), file_get_contents($fixtures.'/application_asxml2.txt'), '->asXml() returns an XML representation of the application'); - -// ->renderException() -$t->diag('->renderException()'); -$application = new Application(); -$application->setAutoExit(false); -$tester = new ApplicationTester($application); - -$tester->run(array('command' => 'foo')); -$t->is($tester->getDisplay(), file_get_contents($fixtures.'/application_renderexception1.txt'), '->renderException() renders a pretty exception'); - -$tester->run(array('command' => 'foo'), array('verbosity' => Output::VERBOSITY_VERBOSE)); -$t->like($tester->getDisplay(), '/Exception trace/', '->renderException() renders a pretty exception with a stack trace when verbosity is verbose'); - -$tester->run(array('command' => 'list', '--foo' => true)); -$t->is($tester->getDisplay(), file_get_contents($fixtures.'/application_renderexception2.txt'), '->renderException() renders the command synopsis when an exception occurs in the context of a command'); - -// ->run() -$t->diag('->run()'); -$application = new Application(); -$application->setAutoExit(false); -$application->setCatchExceptions(false); -$application->addCommand($command = new Foo1Command()); -$_SERVER['argv'] = array('cli.php', 'foo:bar1'); - -ob_start(); -$application->run(); -ob_end_clean(); - -$t->is(get_class($command->input), 'Symfony\Components\Console\Input\ArgvInput', '->run() creates an ArgvInput by default if none is given'); -$t->is(get_class($command->output), 'Symfony\Components\Console\Output\ConsoleOutput', '->run() creates a ConsoleOutput by default if none is given'); - -$application = new Application(); -$application->setAutoExit(false); -$application->setCatchExceptions(false); -$tester = new ApplicationTester($application); -$tester->run(array()); -$t->is($tester->getDisplay(), file_get_contents($fixtures.'/application_run1.txt'), '->run() runs the list command if no argument is passed'); - -$tester->run(array('--help' => true)); -$t->is($tester->getDisplay(), file_get_contents($fixtures.'/application_run2.txt'), '->run() runs the help command if --help is passed'); - -$application = new Application(); -$application->setAutoExit(false); -$application->setCatchExceptions(false); -$tester = new ApplicationTester($application); -$tester->run(array('command' => 'list', '--help' => true)); -$t->is($tester->getDisplay(), file_get_contents($fixtures.'/application_run3.txt'), '->run() displays the help if --help is passed'); - -$application = new Application(); -$application->setAutoExit(false); -$application->setCatchExceptions(false); -$tester = new ApplicationTester($application); -$tester->run(array('--color' => true)); -$t->ok($tester->getOutput()->isDecorated(), '->run() forces color output if --color is passed'); - -$application = new Application(); -$application->setAutoExit(false); -$application->setCatchExceptions(false); -$tester = new ApplicationTester($application); -$tester->run(array('--version' => true)); -$t->is($tester->getDisplay(), file_get_contents($fixtures.'/application_run4.txt'), '->run() displays the program version if --version is passed'); - -$application = new Application(); -$application->setAutoExit(false); -$application->setCatchExceptions(false); -$tester = new ApplicationTester($application); -$tester->run(array('command' => 'list', '--quiet' => true)); -$t->is($tester->getDisplay(), '', '->run() removes all output if --quiet is passed'); - -$application = new Application(); -$application->setAutoExit(false); -$application->setCatchExceptions(false); -$tester = new ApplicationTester($application); -$tester->run(array('command' => 'list', '--verbose' => true)); -$t->is($tester->getOutput()->getVerbosity(), Output::VERBOSITY_VERBOSE, '->run() sets the output to verbose is --verbose is passed'); - -$application = new Application(); -$application->setAutoExit(false); -$application->setCatchExceptions(false); -$application->addCommand(new FooCommand()); -$tester = new ApplicationTester($application); -$tester->run(array('command' => 'foo:bar', '--no-interaction' => true)); -$t->is($tester->getDisplay(), "called\n", '->run() does not called interact() if --no-interaction is passed'); diff --git a/tests/unit/Symfony/Components/Console/Command/CommandTest.php b/tests/unit/Symfony/Components/Console/Command/CommandTest.php deleted file mode 100644 index 7753969419..0000000000 --- a/tests/unit/Symfony/Components/Console/Command/CommandTest.php +++ /dev/null @@ -1,206 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -require_once __DIR__.'/../../../../bootstrap.php'; - -use Symfony\Components\Console\Command\Command; -use Symfony\Components\Console\Application; -use Symfony\Components\Console\Input\InputDefinition; -use Symfony\Components\Console\Input\InputArgument; -use Symfony\Components\Console\Input\InputOption; -use Symfony\Components\Console\Input\InputInterface; -use Symfony\Components\Console\Input\StringInput; -use Symfony\Components\Console\Output\OutputInterface; -use Symfony\Components\Console\Output\NullOutput; -use Symfony\Components\Console\Output\StreamOutput; -use Symfony\Components\Console\Tester\CommandTester; - -$fixtures = __DIR__.'/../../../../../fixtures/Symfony/Components/Console'; - -$t = new LimeTest(46); - -require_once $fixtures.'/TestCommand.php'; - -$application = new Application(); - -// __construct() -$t->diag('__construct()'); -try -{ - $command = new Command(); - $t->fail('__construct() throws a \LogicException if the name is null'); -} -catch (\LogicException $e) -{ - $t->pass('__construct() throws a \LogicException if the name is null'); -} -$command = new Command('foo:bar'); -$t->is($command->getFullName(), 'foo:bar', '__construct() takes the command name as its first argument'); - -// ->setApplication() -$t->diag('->setApplication()'); -$command = new TestCommand(); -$command->setApplication($application); -$t->is($command->getApplication(), $application, '->setApplication() sets the current application'); - -// ->setDefinition() ->getDefinition() -$t->diag('->setDefinition() ->getDefinition()'); -$ret = $command->setDefinition($definition = new InputDefinition()); -$t->is($ret, $command, '->setDefinition() implements a fluent interface'); -$t->is($command->getDefinition(), $definition, '->setDefinition() sets the current InputDefinition instance'); -$command->setDefinition(array(new InputArgument('foo'), new InputOption('bar'))); -$t->ok($command->getDefinition()->hasArgument('foo'), '->setDefinition() also takes an array of InputArguments and InputOptions as an argument'); -$t->ok($command->getDefinition()->hasOption('bar'), '->setDefinition() also takes an array of InputArguments and InputOptions as an argument'); -$command->setDefinition(new InputDefinition()); - -// ->addArgument() -$t->diag('->addArgument()'); -$ret = $command->addArgument('foo'); -$t->is($ret, $command, '->addArgument() implements a fluent interface'); -$t->ok($command->getDefinition()->hasArgument('foo'), '->addArgument() adds an argument to the command'); - -// ->addOption() -$t->diag('->addOption()'); -$ret = $command->addOption('foo'); -$t->is($ret, $command, '->addOption() implements a fluent interface'); -$t->ok($command->getDefinition()->hasOption('foo'), '->addOption() adds an option to the command'); - -// ->getNamespace() ->getName() ->getFullName() ->setName() -$t->diag('->getNamespace() ->getName() ->getFullName()'); -$t->is($command->getNamespace(), 'namespace', '->getNamespace() returns the command namespace'); -$t->is($command->getName(), 'name', '->getName() returns the command name'); -$t->is($command->getFullName(), 'namespace:name', '->getNamespace() returns the full command name'); -$command->setName('foo'); -$t->is($command->getName(), 'foo', '->setName() sets the command name'); - -$command->setName(':bar'); -$t->is($command->getName(), 'bar', '->setName() sets the command name'); -$t->is($command->getNamespace(), '', '->setName() can set the command namespace'); - -$ret = $command->setName('foobar:bar'); -$t->is($ret, $command, '->setName() implements a fluent interface'); -$t->is($command->getName(), 'bar', '->setName() sets the command name'); -$t->is($command->getNamespace(), 'foobar', '->setName() can set the command namespace'); - -try -{ - $command->setName(''); - $t->fail('->setName() throws an \InvalidArgumentException if the name is empty'); -} -catch (\InvalidArgumentException $e) -{ - $t->pass('->setName() throws an \InvalidArgumentException if the name is empty'); -} - -try -{ - $command->setName('foo:'); - $t->fail('->setName() throws an \InvalidArgumentException if the name is empty'); -} -catch (\InvalidArgumentException $e) -{ - $t->pass('->setName() throws an \InvalidArgumentException if the name is empty'); -} - -// ->getDescription() ->setDescription() -$t->diag('->getDescription() ->setDescription()'); -$t->is($command->getDescription(), 'description', '->getDescription() returns the description'); -$ret = $command->setDescription('description1'); -$t->is($ret, $command, '->setDescription() implements a fluent interface'); -$t->is($command->getDescription(), 'description1', '->setDescription() sets the description'); - -// ->getHelp() ->setHelp() -$t->diag('->getHelp() ->setHelp()'); -$t->is($command->getHelp(), 'help', '->getHelp() returns the help'); -$ret = $command->setHelp('help1'); -$t->is($ret, $command, '->setHelp() implements a fluent interface'); -$t->is($command->getHelp(), 'help1', '->setHelp() sets the help'); - -// ->getAliases() ->setAliases() -$t->diag('->getAliases() ->setAliases()'); -$t->is($command->getAliases(), array('name'), '->getAliases() returns the aliases'); -$ret = $command->setAliases(array('name1')); -$t->is($ret, $command, '->setAliases() implements a fluent interface'); -$t->is($command->getAliases(), array('name1'), '->setAliases() sets the aliases'); - -// ->getSynopsis() -$t->diag('->getSynopsis()'); -$t->is($command->getSynopsis(), 'foobar:bar [--foo] [foo]', '->getSynopsis() returns the synopsis'); - -// ->mergeApplicationDefinition() -$t->diag('->mergeApplicationDefinition()'); -$application1 = new Application(); -$application1->getDefinition()->addArguments(array(new InputArgument('foo'))); -$application1->getDefinition()->addOptions(array(new InputOption('bar'))); -$command = new TestCommand(); -$command->setApplication($application1); -$command->setDefinition($definition = new InputDefinition(array(new InputArgument('bar'), new InputOption('foo')))); -$command->mergeApplicationDefinition(); -$t->ok($command->getDefinition()->hasArgument('foo'), '->mergeApplicationDefinition() merges the application arguments and the command arguments'); -$t->ok($command->getDefinition()->hasArgument('bar'), '->mergeApplicationDefinition() merges the application arguments and the command arguments'); -$t->ok($command->getDefinition()->hasOption('foo'), '->mergeApplicationDefinition() merges the application options and the command options'); -$t->ok($command->getDefinition()->hasOption('bar'), '->mergeApplicationDefinition() merges the application options and the command options'); - -$command->mergeApplicationDefinition(); -$t->is($command->getDefinition()->getArgumentCount(), 3, '->mergeApplicationDefinition() does not try to merge twice the application arguments and options'); - -$command = new TestCommand(); -$command->mergeApplicationDefinition(); -$t->pass('->mergeApplicationDefinition() does nothing if application is not set'); - -// ->run() -$t->diag('->run()'); -$command = new TestCommand(); -$command->setApplication($application); -$tester = new CommandTester($command); -try -{ - $tester->execute(array('--bar' => true)); - $t->fail('->run() throws a \RuntimeException when the input does not validate the current InputDefinition'); -} -catch (\RuntimeException $e) -{ - $t->pass('->run() throws a \RuntimeException when the input does not validate the current InputDefinition'); -} - -$t->is($tester->execute(array(), array('interactive' => true)), "interact called\nexecute called\n", '->run() calls the interact() method if the input is interactive'); -$t->is($tester->execute(array(), array('interactive' => false)), "execute called\n", '->run() does not call the interact() method if the input is not interactive'); - -$command = new Command('foo'); -try -{ - $command->run(new StringInput(''), new NullOutput()); - $t->fail('->run() throws a \LogicException if the execute() method has not been overriden and no code has been provided'); -} -catch (\LogicException $e) -{ - $t->pass('->run() throws a \LogicException if the execute() method has not been overriden and no code has been provided'); -} - -// ->setCode() -$t->diag('->setCode()'); -$command = new TestCommand(); -$command->setApplication($application); -$ret = $command->setCode(function (InputInterface $input, OutputInterface $output) -{ - $output->writeln('from the code...'); -}); -$t->is($ret, $command, '->setCode() implements a fluent interface'); -$tester = new CommandTester($command); -$tester->execute(array()); -$t->is($tester->getDisplay(), "interact called\nfrom the code...\n"); - -// ->asText() -$t->diag('->asText()'); -$t->is($command->asText(), file_get_contents($fixtures.'/command_astext.txt'), '->asText() returns a text representation of the command'); - -// ->asXml() -$t->diag('->asXml()'); -$t->is($command->asXml(), file_get_contents($fixtures.'/command_asxml.txt'), '->asXml() returns an XML representation of the command'); diff --git a/tests/unit/Symfony/Components/Console/Command/HelpCommandTest.php b/tests/unit/Symfony/Components/Console/Command/HelpCommandTest.php deleted file mode 100644 index 8951043ec9..0000000000 --- a/tests/unit/Symfony/Components/Console/Command/HelpCommandTest.php +++ /dev/null @@ -1,39 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -require_once __DIR__.'/../../../../bootstrap.php'; - -use Symfony\Components\Console\Tester\CommandTester; -use Symfony\Components\Console\Command\HelpCommand; -use Symfony\Components\Console\Command\ListCommand; -use Symfony\Components\Console\Application; - -$t = new LimeTest(4); - -// ->execute() -$t->diag('->execute()'); - -$command = new HelpCommand(); -$command->setCommand(new ListCommand()); - -$commandTester = new CommandTester($command); -$commandTester->execute(array()); -$t->like($commandTester->getDisplay(), '/list \[--xml\] \[namespace\]/', '->execute() returns a text help for the given command'); - -$commandTester->execute(array('--xml' => true)); -$t->like($commandTester->getDisplay(), '/execute() returns an XML help text if --xml is passed'); - -$application = new Application(); -$commandTester = new CommandTester($application->getCommand('help')); -$commandTester->execute(array('command_name' => 'list')); -$t->like($commandTester->getDisplay(), '/list \[--xml\] \[namespace\]/', '->execute() returns a text help for the given command'); - -$commandTester->execute(array('command_name' => 'list', '--xml' => true)); -$t->like($commandTester->getDisplay(), '/execute() returns an XML help text if --xml is passed'); diff --git a/tests/unit/Symfony/Components/Console/Command/ListCommandTest.php b/tests/unit/Symfony/Components/Console/Command/ListCommandTest.php deleted file mode 100644 index 9ed10cc6f9..0000000000 --- a/tests/unit/Symfony/Components/Console/Command/ListCommandTest.php +++ /dev/null @@ -1,28 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -require_once __DIR__.'/../../../../bootstrap.php'; - -use Symfony\Components\Console\Tester\CommandTester; -use Symfony\Components\Console\Application; - -$t = new LimeTest(2); - -$application = new Application(); - -// ->execute() -$t->diag('->execute()'); - -$commandTester = new CommandTester($application->getCommand('list')); -$commandTester->execute(array()); -$t->like($commandTester->getDisplay(), '/help Displays help for a command/', '->execute() returns a list of available commands'); - -$commandTester->execute(array('--xml' => true)); -$t->like($commandTester->getDisplay(), '//', '->execute() returns a list of available commands in XML if --xml is passed'); diff --git a/tests/unit/Symfony/Components/Console/Helper/FormatterHelperTest.php b/tests/unit/Symfony/Components/Console/Helper/FormatterHelperTest.php deleted file mode 100644 index 0fd2199b61..0000000000 --- a/tests/unit/Symfony/Components/Console/Helper/FormatterHelperTest.php +++ /dev/null @@ -1,28 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -require_once __DIR__.'/../../../../bootstrap.php'; - -use Symfony\Components\Console\Helper\FormatterHelper; - -$formatter = new FormatterHelper(); - -$t = new LimeTest(4); - -// ::formatSection() -$t->diag('::formatSection()'); -$t->is($formatter->formatSection('cli', 'Some text to display'), '[cli] Some text to display', '::formatSection() formats a message in a section'); - -// ::formatBlock() -$t->diag('::formatBlock()'); -$t->is($formatter->formatBlock('Some text to display', 'error'), ' Some text to display ', '::formatBlock() formats a message in a block'); -$t->is($formatter->formatBlock(array('Some text to display', 'foo bar'), 'error'), " Some text to display \n foo bar ", '::formatBlock() formats a message in a block'); - -$t->is($formatter->formatBlock('Some text to display', 'error', true), " \n Some text to display \n ", '::formatBlock() formats a message in a block'); diff --git a/tests/unit/Symfony/Components/Console/Input/ArgvInputTest.php b/tests/unit/Symfony/Components/Console/Input/ArgvInputTest.php deleted file mode 100644 index ee9ebea055..0000000000 --- a/tests/unit/Symfony/Components/Console/Input/ArgvInputTest.php +++ /dev/null @@ -1,174 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -require_once __DIR__.'/../../../../bootstrap.php'; - -use Symfony\Components\Console\Input\ArgvInput; -use Symfony\Components\Console\Input\InputDefinition; -use Symfony\Components\Console\Input\InputArgument; -use Symfony\Components\Console\Input\InputOption; - -class TestInput extends ArgvInput -{ - public function getTokens() - { - return $this->tokens; - } -} - -$t = new LimeTest(26); - -// __construct() -$t->diag('__construct()'); -$_SERVER['argv'] = array('cli.php', 'foo'); -$input = new TestInput(); -$t->is($input->getTokens(), array('foo'), '__construct() automatically get its input from the argv server variable'); - -// ->parse() -$t->diag('->parse()'); -$input = new TestInput(array('cli.php', 'foo')); -$input->bind(new InputDefinition(array(new InputArgument('name')))); -$t->is($input->getArguments(), array('name' => 'foo'), '->parse() parses required arguments'); - -$input->bind(new InputDefinition(array(new InputArgument('name')))); -$t->is($input->getArguments(), array('name' => 'foo'), '->parse() is stateless'); - -$input = new TestInput(array('cli.php', '--foo')); -$input->bind(new InputDefinition(array(new InputOption('foo')))); -$t->is($input->getOptions(), array('foo' => true), '->parse() parses long options without parameter'); - -$input = new TestInput(array('cli.php', '--foo=bar')); -$input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::PARAMETER_REQUIRED)))); -$t->is($input->getOptions(), array('foo' => 'bar'), '->parse() parses long options with a required parameter (with a = separator)'); - -$input = new TestInput(array('cli.php', '--foo', 'bar')); -$input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::PARAMETER_REQUIRED)))); -$t->is($input->getOptions(), array('foo' => 'bar'), '->parse() parses long options with a required parameter (with a space separator)'); - -try -{ - $input = new TestInput(array('cli.php', '--foo')); - $input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::PARAMETER_REQUIRED)))); - $t->fail('->parse() throws a \RuntimeException if no parameter is passed to an option when it is required'); -} -catch (\RuntimeException $e) -{ - $t->pass('->parse() throws a \RuntimeException if no parameter is passed to an option when it is required'); -} - -$input = new TestInput(array('cli.php', '-f')); -$input->bind(new InputDefinition(array(new InputOption('foo', 'f')))); -$t->is($input->getOptions(), array('foo' => true), '->parse() parses short options without parameter'); - -$input = new TestInput(array('cli.php', '-fbar')); -$input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::PARAMETER_REQUIRED)))); -$t->is($input->getOptions(), array('foo' => 'bar'), '->parse() parses short options with a required parameter (with no separator)'); - -$input = new TestInput(array('cli.php', '-f', 'bar')); -$input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::PARAMETER_REQUIRED)))); -$t->is($input->getOptions(), array('foo' => 'bar'), '->parse() parses short options with a required parameter (with a space separator)'); - -$input = new TestInput(array('cli.php', '-f', '-b', 'foo')); -$input->bind(new InputDefinition(array(new InputArgument('name'), new InputOption('foo', 'f', InputOption::PARAMETER_OPTIONAL), new InputOption('bar', 'b')))); -$t->is($input->getOptions(), array('foo' => null, 'bar' => true), '->parse() parses short options with an optional parameter which is not present'); - -try -{ - $input = new TestInput(array('cli.php', '-f')); - $input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::PARAMETER_REQUIRED)))); - $t->fail('->parse() throws a \RuntimeException if no parameter is passed to an option when it is required'); -} -catch (\RuntimeException $e) -{ - $t->pass('->parse() throws a \RuntimeException if no parameter is passed to an option when it is required'); -} - -try -{ - $input = new TestInput(array('cli.php', '-ffoo')); - $input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::PARAMETER_NONE)))); - $t->fail('->parse() throws a \RuntimeException if a value is passed to an option which does not take one'); -} -catch (\RuntimeException $e) -{ - $t->pass('->parse() throws a \RuntimeException if a value is passed to an option which does not take one'); -} - -try -{ - $input = new TestInput(array('cli.php', 'foo', 'bar')); - $input->bind(new InputDefinition()); - $t->fail('->parse() throws a \RuntimeException if too many arguments are passed'); -} -catch (\RuntimeException $e) -{ - $t->pass('->parse() throws a \RuntimeException if too many arguments are passed'); -} - -try -{ - $input = new TestInput(array('cli.php', '--foo')); - $input->bind(new InputDefinition()); - $t->fail('->parse() throws a \RuntimeException if an unknown long option is passed'); -} -catch (\RuntimeException $e) -{ - $t->pass('->parse() throws a \RuntimeException if an unknown long option is passed'); -} - -try -{ - $input = new TestInput(array('cli.php', '-f')); - $input->bind(new InputDefinition()); - $t->fail('->parse() throws a \RuntimeException if an unknown short option is passed'); -} -catch (\RuntimeException $e) -{ - $t->pass('->parse() throws a \RuntimeException if an unknown short option is passed'); -} - -$input = new TestInput(array('cli.php', '-fb')); -$input->bind(new InputDefinition(array(new InputOption('foo', 'f'), new InputOption('bar', 'b')))); -$t->is($input->getOptions(), array('foo' => true, 'bar' => true), '->parse() parses short options when they are aggregated as a single one'); - -$input = new TestInput(array('cli.php', '-fb', 'bar')); -$input->bind(new InputDefinition(array(new InputOption('foo', 'f'), new InputOption('bar', 'b', InputOption::PARAMETER_REQUIRED)))); -$t->is($input->getOptions(), array('foo' => true, 'bar' => 'bar'), '->parse() parses short options when they are aggregated as a single one and the last one has a required parameter'); - -$input = new TestInput(array('cli.php', '-fb', 'bar')); -$input->bind(new InputDefinition(array(new InputOption('foo', 'f'), new InputOption('bar', 'b', InputOption::PARAMETER_OPTIONAL)))); -$t->is($input->getOptions(), array('foo' => true, 'bar' => 'bar'), '->parse() parses short options when they are aggregated as a single one and the last one has an optional parameter'); - -$input = new TestInput(array('cli.php', '-fbbar')); -$input->bind(new InputDefinition(array(new InputOption('foo', 'f'), new InputOption('bar', 'b', InputOption::PARAMETER_OPTIONAL)))); -$t->is($input->getOptions(), array('foo' => true, 'bar' => 'bar'), '->parse() parses short options when they are aggregated as a single one and the last one has an optional parameter with no separator'); - -$input = new TestInput(array('cli.php', '-fbbar')); -$input->bind(new InputDefinition(array(new InputOption('foo', 'f', InputOption::PARAMETER_OPTIONAL), new InputOption('bar', 'b', InputOption::PARAMETER_OPTIONAL)))); -$t->is($input->getOptions(), array('foo' => 'bbar', 'bar' => null), '->parse() parses short options when they are aggregated as a single one and one of them takes a parameter'); - -// ->getFirstArgument() -$t->diag('->getFirstArgument()'); -$input = new TestInput(array('cli.php', '-fbbar')); -$t->is($input->getFirstArgument(), '', '->getFirstArgument() returns the first argument from the raw input'); - -$input = new TestInput(array('cli.php', '-fbbar', 'foo')); -$t->is($input->getFirstArgument(), 'foo', '->getFirstArgument() returns the first argument from the raw input'); - -// ->hasParameterOption() -$t->diag('->hasParameterOption()'); -$input = new TestInput(array('cli.php', '-f', 'foo')); -$t->ok($input->hasParameterOption('-f'), '->hasParameterOption() returns true if the given short option is in the raw input'); - -$input = new TestInput(array('cli.php', '--foo', 'foo')); -$t->ok($input->hasParameterOption('--foo'), '->hasParameterOption() returns true if the given short option is in the raw input'); - -$input = new TestInput(array('cli.php', 'foo')); -$t->ok(!$input->hasParameterOption('--foo'), '->hasParameterOption() returns false if the given short option is not in the raw input'); diff --git a/tests/unit/Symfony/Components/Console/Input/ArrayInputTest.php b/tests/unit/Symfony/Components/Console/Input/ArrayInputTest.php deleted file mode 100644 index 2d68a7c660..0000000000 --- a/tests/unit/Symfony/Components/Console/Input/ArrayInputTest.php +++ /dev/null @@ -1,93 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -require_once __DIR__.'/../../../../bootstrap.php'; - -use Symfony\Components\Console\Input\ArrayInput; -use Symfony\Components\Console\Input\InputDefinition; -use Symfony\Components\Console\Input\InputArgument; -use Symfony\Components\Console\Input\InputOption; - -$t = new LimeTest(15); - -// ->getFirstArgument() -$t->diag('->getFirstArgument()'); -$input = new ArrayInput(array()); -$t->is($input->getFirstArgument(), null, '->getFirstArgument() returns null if no argument were passed'); -$input = new ArrayInput(array('name' => 'Fabien')); -$t->is($input->getFirstArgument(), 'Fabien', '->getFirstArgument() returns the first passed argument'); -$input = new ArrayInput(array('--foo' => 'bar', 'name' => 'Fabien')); -$t->is($input->getFirstArgument(), 'Fabien', '->getFirstArgument() returns the first passed argument'); - -// ->hasParameterOption() -$t->diag('->hasParameterOption()'); -$input = new ArrayInput(array('name' => 'Fabien', '--foo' => 'bar')); -$t->ok($input->hasParameterOption('--foo'), '->hasParameterOption() returns true if an option is present in the passed parameters'); -$t->ok(!$input->hasParameterOption('--bar'), '->hasParameterOption() returns false if an option is not present in the passed parameters'); - -$input = new ArrayInput(array('--foo')); -$t->ok($input->hasParameterOption('--foo'), '->hasParameterOption() returns true if an option is present in the passed parameters'); - -// ->parse() -$t->diag('->parse()'); -$input = new ArrayInput(array('name' => 'foo'), new InputDefinition(array(new InputArgument('name')))); -$t->is($input->getArguments(), array('name' => 'foo'), '->parse() parses required arguments'); - -try -{ - $input = new ArrayInput(array('foo' => 'foo'), new InputDefinition(array(new InputArgument('name')))); - $t->fail('->parse() throws an \InvalidArgumentException exception if an invalid argument is passed'); -} -catch (\RuntimeException $e) -{ - $t->pass('->parse() throws an \InvalidArgumentException exception if an invalid argument is passed'); -} - -$input = new ArrayInput(array('--foo' => 'bar'), new InputDefinition(array(new InputOption('foo')))); -$t->is($input->getOptions(), array('foo' => 'bar'), '->parse() parses long options'); - -$input = new ArrayInput(array('--foo' => 'bar'), new InputDefinition(array(new InputOption('foo', 'f', InputOption::PARAMETER_OPTIONAL, '', 'default')))); -$t->is($input->getOptions(), array('foo' => 'bar'), '->parse() parses long options with a default value'); - -$input = new ArrayInput(array('--foo' => null), new InputDefinition(array(new InputOption('foo', 'f', InputOption::PARAMETER_OPTIONAL, '', 'default')))); -$t->is($input->getOptions(), array('foo' => 'default'), '->parse() parses long options with a default value'); - -try -{ - $input = new ArrayInput(array('--foo' => null), new InputDefinition(array(new InputOption('foo', 'f', InputOption::PARAMETER_REQUIRED)))); - $t->fail('->parse() throws an \InvalidArgumentException exception if a required option is passed without a value'); -} -catch (\RuntimeException $e) -{ - $t->pass('->parse() throws an \InvalidArgumentException exception if a required option is passed without a value'); -} - -try -{ - $input = new ArrayInput(array('--foo' => 'foo'), new InputDefinition()); - $t->fail('->parse() throws an \InvalidArgumentException exception if an invalid option is passed'); -} -catch (\RuntimeException $e) -{ - $t->pass('->parse() throws an \InvalidArgumentException exception if an invalid option is passed'); -} - -$input = new ArrayInput(array('-f' => 'bar'), new InputDefinition(array(new InputOption('foo', 'f')))); -$t->is($input->getOptions(), array('foo' => 'bar'), '->parse() parses short options'); - -try -{ - $input = new ArrayInput(array('-o' => 'foo'), new InputDefinition()); - $t->fail('->parse() throws an \InvalidArgumentException exception if an invalid option is passed'); -} -catch (\RuntimeException $e) -{ - $t->pass('->parse() throws an \InvalidArgumentException exception if an invalid option is passed'); -} diff --git a/tests/unit/Symfony/Components/Console/Input/InputArgumentTest.php b/tests/unit/Symfony/Components/Console/Input/InputArgumentTest.php deleted file mode 100644 index 07736e39b1..0000000000 --- a/tests/unit/Symfony/Components/Console/Input/InputArgumentTest.php +++ /dev/null @@ -1,97 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -require_once __DIR__.'/../../../../bootstrap.php'; - -use Symfony\Components\Console\Input\InputArgument; -use Symfony\Components\Console\Exception; - -$t = new LimeTest(16); - -// __construct() -$t->diag('__construct()'); -$argument = new InputArgument('foo'); -$t->is($argument->getName(), 'foo', '__construct() takes a name as its first argument'); - -// mode argument -$argument = new InputArgument('foo'); -$t->is($argument->isRequired(), false, '__construct() gives a "Argument::OPTIONAL" mode by default'); - -$argument = new InputArgument('foo', null); -$t->is($argument->isRequired(), false, '__construct() can take "Argument::OPTIONAL" as its mode'); - -$argument = new InputArgument('foo', InputArgument::OPTIONAL); -$t->is($argument->isRequired(), false, '__construct() can take "Argument::PARAMETER_OPTIONAL" as its mode'); - -$argument = new InputArgument('foo', InputArgument::REQUIRED); -$t->is($argument->isRequired(), true, '__construct() can take "Argument::PARAMETER_REQUIRED" as its mode'); - -try -{ - $argument = new InputArgument('foo', 'ANOTHER_ONE'); - $t->fail('__construct() throws an Exception if the mode is not valid'); -} -catch (\Exception $e) -{ - $t->pass('__construct() throws an Exception if the mode is not valid'); -} - -// ->isArray() -$t->diag('->isArray()'); -$argument = new InputArgument('foo', InputArgument::IS_ARRAY); -$t->ok($argument->isArray(), '->isArray() returns true if the argument can be an array'); -$argument = new InputArgument('foo', InputArgument::OPTIONAL | InputArgument::IS_ARRAY); -$t->ok($argument->isArray(), '->isArray() returns true if the argument can be an array'); -$argument = new InputArgument('foo', InputArgument::OPTIONAL); -$t->ok(!$argument->isArray(), '->isArray() returns false if the argument can not be an array'); - -// ->getDescription() -$t->diag('->getDescription()'); -$argument = new InputArgument('foo', null, 'Some description'); -$t->is($argument->getDescription(), 'Some description', '->getDescription() return the message description'); - -// ->getDefault() -$t->diag('->getDefault()'); -$argument = new InputArgument('foo', InputArgument::OPTIONAL, '', 'default'); -$t->is($argument->getDefault(), 'default', '->getDefault() return the default value'); - -// ->setDefault() -$t->diag('->setDefault()'); -$argument = new InputArgument('foo', InputArgument::OPTIONAL, '', 'default'); -$argument->setDefault(null); -$t->ok(is_null($argument->getDefault()), '->setDefault() can reset the default value by passing null'); -$argument->setDefault('another'); -$t->is($argument->getDefault(), 'another', '->setDefault() changes the default value'); - -$argument = new InputArgument('foo', InputArgument::OPTIONAL | InputArgument::IS_ARRAY); -$argument->setDefault(array(1, 2)); -$t->is($argument->getDefault(), array(1, 2), '->setDefault() changes the default value'); - -try -{ - $argument = new InputArgument('foo', InputArgument::REQUIRED); - $argument->setDefault('default'); - $t->fail('->setDefault() throws an Exception if you give a default value for a required argument'); -} -catch (\Exception $e) -{ - $t->pass('->setDefault() throws an Exception if you give a default value for a required argument'); -} - -try -{ - $argument = new InputArgument('foo', InputArgument::IS_ARRAY); - $argument->setDefault('default'); - $t->fail('->setDefault() throws an Exception if you give a default value which is not an array for a IS_ARRAY option'); -} -catch (\Exception $e) -{ - $t->pass('->setDefault() throws an Exception if you give a default value which is not an array for a IS_ARRAY option'); -} diff --git a/tests/unit/Symfony/Components/Console/Input/InputDefinitionTest.php b/tests/unit/Symfony/Components/Console/Input/InputDefinitionTest.php deleted file mode 100644 index 124ddfaa5d..0000000000 --- a/tests/unit/Symfony/Components/Console/Input/InputDefinitionTest.php +++ /dev/null @@ -1,305 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -require_once __DIR__.'/../../../../bootstrap.php'; - -use Symfony\Components\Console\Input\InputDefinition; -use Symfony\Components\Console\Input\InputArgument; -use Symfony\Components\Console\Input\InputOption; -use Symfony\Components\Console\Exception; - -$fixtures = __DIR__.'/../../../../../fixtures/Symfony/Components/Console'; - -$t = new LimeTest(51); - -$foo = new InputArgument('foo'); -$bar = new InputArgument('bar'); -$foo1 = new InputArgument('foo'); -$foo2 = new InputArgument('foo2', InputArgument::REQUIRED); - -// __construct() -$t->diag('__construct()'); -$definition = new InputDefinition(); -$t->is($definition->getArguments(), array(), '__construct() creates a new InputDefinition object'); - -$definition = new InputDefinition(array($foo, $bar)); -$t->is($definition->getArguments(), array('foo' => $foo, 'bar' => $bar), '__construct() takes an array of InputArgument objects as its first argument'); - -// ->setArguments() -$t->diag('->setArguments()'); -$definition = new InputDefinition(); -$definition->setArguments(array($foo)); -$t->is($definition->getArguments(), array('foo' => $foo), '->setArguments() sets the array of InputArgument objects'); -$definition->setArguments(array($bar)); - -$t->is($definition->getArguments(), array('bar' => $bar), '->setArguments() clears all InputArgument objects'); - -// ->addArguments() -$t->diag('->addArguments()'); -$definition = new InputDefinition(); -$definition->addArguments(array($foo)); -$t->is($definition->getArguments(), array('foo' => $foo), '->addArguments() adds an array of InputArgument objects'); -$definition->addArguments(array($bar)); -$t->is($definition->getArguments(), array('foo' => $foo, 'bar' => $bar), '->addArguments() does not clear existing InputArgument objects'); - -// ->addArgument() -$t->diag('->addArgument()'); -$definition = new InputDefinition(); -$definition->addArgument($foo); -$t->is($definition->getArguments(), array('foo' => $foo), '->addArgument() adds a InputArgument object'); -$definition->addArgument($bar); -$t->is($definition->getArguments(), array('foo' => $foo, 'bar' => $bar), '->addArgument() adds a InputArgument object'); - -// arguments must have different names -try -{ - $definition->addArgument($foo1); - $t->fail('->addArgument() throws a Exception if another argument is already registered with the same name'); -} -catch (\Exception $e) -{ - $t->pass('->addArgument() throws a Exception if another argument is already registered with the same name'); -} - -// cannot add a parameter after an array parameter -$definition->addArgument(new InputArgument('fooarray', InputArgument::IS_ARRAY)); -try -{ - $definition->addArgument(new InputArgument('anotherbar')); - $t->fail('->addArgument() throws a Exception if there is an array parameter already registered'); -} -catch (\Exception $e) -{ - $t->pass('->addArgument() throws a Exception if there is an array parameter already registered'); -} - -// cannot add a required argument after an optional one -$definition = new InputDefinition(); -$definition->addArgument($foo); -try -{ - $definition->addArgument($foo2); - $t->fail('->addArgument() throws an exception if you try to add a required argument after an optional one'); -} -catch (\Exception $e) -{ - $t->pass('->addArgument() throws an exception if you try to add a required argument after an optional one'); -} - -// ->getArgument() -$t->diag('->getArgument()'); -$definition = new InputDefinition(); -$definition->addArguments(array($foo)); -$t->is($definition->getArgument('foo'), $foo, '->getArgument() returns a InputArgument by its name'); -try -{ - $definition->getArgument('bar'); - $t->fail('->getArgument() throws an exception if the InputArgument name does not exist'); -} -catch (\Exception $e) -{ - $t->pass('->getArgument() throws an exception if the InputArgument name does not exist'); -} - -// ->hasArgument() -$t->diag('->hasArgument()'); -$definition = new InputDefinition(); -$definition->addArguments(array($foo)); -$t->is($definition->hasArgument('foo'), true, '->hasArgument() returns true if a InputArgument exists for the given name'); -$t->is($definition->hasArgument('bar'), false, '->hasArgument() returns false if a InputArgument exists for the given name'); - -// ->getArgumentRequiredCount() -$t->diag('->getArgumentRequiredCount()'); -$definition = new InputDefinition(); -$definition->addArgument($foo2); -$t->is($definition->getArgumentRequiredCount(), 1, '->getArgumentRequiredCount() returns the number of required arguments'); -$definition->addArgument($foo); -$t->is($definition->getArgumentRequiredCount(), 1, '->getArgumentRequiredCount() returns the number of required arguments'); - -// ->getArgumentCount() -$t->diag('->getArgumentCount()'); -$definition = new InputDefinition(); -$definition->addArgument($foo2); -$t->is($definition->getArgumentCount(), 1, '->getArgumentCount() returns the number of arguments'); -$definition->addArgument($foo); -$t->is($definition->getArgumentCount(), 2, '->getArgumentCount() returns the number of arguments'); - -// ->getArgumentDefaults() -$t->diag('->getArgumentDefaults()'); -$definition = new InputDefinition(array( - new InputArgument('foo1', InputArgument::OPTIONAL), - new InputArgument('foo2', InputArgument::OPTIONAL, '', 'default'), - new InputArgument('foo3', InputArgument::OPTIONAL | InputArgument::IS_ARRAY), -// new InputArgument('foo4', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, '', array(1, 2)), -)); -$t->is($definition->getArgumentDefaults(), array('foo1' => null, 'foo2' => 'default', 'foo3' => array()), '->getArgumentDefaults() return the default values for each argument'); - -$definition = new InputDefinition(array( - new InputArgument('foo4', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, '', array(1, 2)), -)); -$t->is($definition->getArgumentDefaults(), array('foo4' => array(1, 2)), '->getArgumentDefaults() return the default values for each argument'); - -$foo = new InputOption('foo', 'f'); -$bar = new InputOption('bar', 'b'); -$foo1 = new InputOption('fooBis', 'f'); -$foo2 = new InputOption('foo', 'p'); - -// __construct() -$t->diag('__construct()'); -$definition = new InputDefinition(); -$t->is($definition->getOptions(), array(), '__construct() creates a new InputDefinition object'); - -$definition = new InputDefinition(array($foo, $bar)); -$t->is($definition->getOptions(), array('foo' => $foo, 'bar' => $bar), '__construct() takes an array of InputOption objects as its first argument'); - -// ->setOptions() -$t->diag('->setOptions()'); -$definition = new InputDefinition(array($foo)); -$t->is($definition->getOptions(), array('foo' => $foo), '->setOptions() sets the array of InputOption objects'); -$definition->setOptions(array($bar)); -$t->is($definition->getOptions(), array('bar' => $bar), '->setOptions() clears all InputOption objects'); -try -{ - $definition->getOptionForShortcut('f'); - $t->fail('->setOptions() clears all InputOption objects'); -} -catch (\Exception $e) -{ - $t->pass('->setOptions() clears all InputOption objects'); -} - -// ->addOptions() -$t->diag('->addOptions()'); -$definition = new InputDefinition(array($foo)); -$t->is($definition->getOptions(), array('foo' => $foo), '->addOptions() adds an array of InputOption objects'); -$definition->addOptions(array($bar)); -$t->is($definition->getOptions(), array('foo' => $foo, 'bar' => $bar), '->addOptions() does not clear existing InputOption objects'); - -// ->addOption() -$t->diag('->addOption()'); -$definition = new InputDefinition(); -$definition->addOption($foo); -$t->is($definition->getOptions(), array('foo' => $foo), '->addOption() adds a InputOption object'); -$definition->addOption($bar); -$t->is($definition->getOptions(), array('foo' => $foo, 'bar' => $bar), '->addOption() adds a InputOption object'); -try -{ - $definition->addOption($foo2); - $t->fail('->addOption() throws a Exception if the another option is already registered with the same name'); -} -catch (\Exception $e) -{ - $t->pass('->addOption() throws a Exception if the another option is already registered with the same name'); -} -try -{ - $definition->addOption($foo1); - $t->fail('->addOption() throws a Exception if the another option is already registered with the same shortcut'); -} -catch (\Exception $e) -{ - $t->pass('->addOption() throws a Exception if the another option is already registered with the same shortcut'); -} - -// ->getOption() -$t->diag('->getOption()'); -$definition = new InputDefinition(array($foo)); -$t->is($definition->getOption('foo'), $foo, '->getOption() returns a InputOption by its name'); -try -{ - $definition->getOption('bar'); - $t->fail('->getOption() throws an exception if the option name does not exist'); -} -catch (\Exception $e) -{ - $t->pass('->getOption() throws an exception if the option name does not exist'); -} - -// ->hasOption() -$t->diag('->hasOption()'); -$definition = new InputDefinition(array($foo)); -$t->is($definition->hasOption('foo'), true, '->hasOption() returns true if a InputOption exists for the given name'); -$t->is($definition->hasOption('bar'), false, '->hasOption() returns false if a InputOption exists for the given name'); - -// ->hasShortcut() -$t->diag('->hasShortcut()'); -$definition = new InputDefinition(array($foo)); -$t->is($definition->hasShortcut('f'), true, '->hasShortcut() returns true if a InputOption exists for the given shortcut'); -$t->is($definition->hasShortcut('b'), false, '->hasShortcut() returns false if a InputOption exists for the given shortcut'); - -// ->getOptionForShortcut() -$t->diag('->getOptionForShortcut()'); -$definition = new InputDefinition(array($foo)); -$t->is($definition->getOptionForShortcut('f'), $foo, '->getOptionForShortcut() returns a InputOption by its shortcut'); -try -{ - $definition->getOptionForShortcut('l'); - $t->fail('->getOption() throws an exception if the shortcut does not exist'); -} -catch (\Exception $e) -{ - $t->pass('->getOption() throws an exception if the shortcut does not exist'); -} - -// ->getOptionDefaults() -$t->diag('->getOptionDefaults()'); -$definition = new InputDefinition(array( - new InputOption('foo1', null, InputOption::PARAMETER_NONE), - new InputOption('foo2', null, InputOption::PARAMETER_REQUIRED), - new InputOption('foo3', null, InputOption::PARAMETER_REQUIRED, '', 'default'), - new InputOption('foo4', null, InputOption::PARAMETER_OPTIONAL), - new InputOption('foo5', null, InputOption::PARAMETER_OPTIONAL, '', 'default'), - new InputOption('foo6', null, InputOption::PARAMETER_OPTIONAL | InputOption::PARAMETER_IS_ARRAY), - new InputOption('foo7', null, InputOption::PARAMETER_OPTIONAL | InputOption::PARAMETER_IS_ARRAY, '', array(1, 2)), -)); -$defaults = array( - 'foo1' => null, - 'foo2' => null, - 'foo3' => 'default', - 'foo4' => null, - 'foo5' => 'default', - 'foo6' => array(), - 'foo7' => array(1, 2), -); -$t->is($definition->getOptionDefaults(), $defaults, '->getOptionDefaults() returns the default values for all options'); - -// ->getSynopsis() -$t->diag('->getSynopsis()'); -$definition = new InputDefinition(array(new InputOption('foo'))); -$t->is($definition->getSynopsis(), '[--foo]', '->getSynopsis() returns a synopsis of arguments and options'); -$definition = new InputDefinition(array(new InputOption('foo', 'f'))); -$t->is($definition->getSynopsis(), '[-f|--foo]', '->getSynopsis() returns a synopsis of arguments and options'); -$definition = new InputDefinition(array(new InputOption('foo', 'f', InputOption::PARAMETER_REQUIRED))); -$t->is($definition->getSynopsis(), '[-f|--foo="..."]', '->getSynopsis() returns a synopsis of arguments and options'); -$definition = new InputDefinition(array(new InputOption('foo', 'f', InputOption::PARAMETER_OPTIONAL))); -$t->is($definition->getSynopsis(), '[-f|--foo[="..."]]', '->getSynopsis() returns a synopsis of arguments and options'); - -$definition = new InputDefinition(array(new InputArgument('foo'))); -$t->is($definition->getSynopsis(), '[foo]', '->getSynopsis() returns a synopsis of arguments and options'); -$definition = new InputDefinition(array(new InputArgument('foo', InputArgument::REQUIRED))); -$t->is($definition->getSynopsis(), 'foo', '->getSynopsis() returns a synopsis of arguments and options'); -$definition = new InputDefinition(array(new InputArgument('foo', InputArgument::IS_ARRAY))); -$t->is($definition->getSynopsis(), '[foo1] ... [fooN]', '->getSynopsis() returns a synopsis of arguments and options'); -$definition = new InputDefinition(array(new InputArgument('foo', InputArgument::REQUIRED | InputArgument::IS_ARRAY))); -$t->is($definition->getSynopsis(), 'foo1 ... [fooN]', '->getSynopsis() returns a synopsis of arguments and options'); - -// ->asText() -$t->diag('->asText()'); -$definition = new InputDefinition(array( - new InputArgument('foo', InputArgument::OPTIONAL, 'The bar argument'), - new InputArgument('bar', InputArgument::OPTIONAL | InputArgument::IS_ARRAY, 'The foo argument', array('bar')), - new InputOption('foo', 'f', InputOption::PARAMETER_REQUIRED, 'The foo option'), - new InputOption('bar', 'b', InputOption::PARAMETER_OPTIONAL, 'The foo option', 'bar'), -)); -$t->is($definition->asText(), file_get_contents($fixtures.'/definition_astext.txt'), '->asText() returns a textual representation of the InputDefinition'); - -// ->asXml() -$t->diag('->asXml()'); -$t->is($definition->asXml(), file_get_contents($fixtures.'/definition_asxml.txt'), '->asText() returns a textual representation of the InputDefinition'); diff --git a/tests/unit/Symfony/Components/Console/Input/InputOptionTest.php b/tests/unit/Symfony/Components/Console/Input/InputOptionTest.php deleted file mode 100644 index 85509c672d..0000000000 --- a/tests/unit/Symfony/Components/Console/Input/InputOptionTest.php +++ /dev/null @@ -1,138 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -require_once __DIR__.'/../../../../bootstrap.php'; - -use Symfony\Components\Console\Input\InputOption; -use Symfony\Components\Console\Exception; - -$t = new LimeTest(34); - -// __construct() -$t->diag('__construct()'); -$option = new InputOption('foo'); -$t->is($option->getName(), 'foo', '__construct() takes a name as its first argument'); -$option = new InputOption('--foo'); -$t->is($option->getName(), 'foo', '__construct() removes the leading -- of the option name'); - -try -{ - $option = new InputOption('foo', 'f', InputOption::PARAMETER_IS_ARRAY); - $t->fail('->setDefault() throws an Exception if PARAMETER_IS_ARRAY option is used when an option does not accept a value'); -} -catch (\Exception $e) -{ - $t->pass('->setDefault() throws an Exception if PARAMETER_IS_ARRAY option is used when an option does not accept a value'); -} - -// shortcut argument -$option = new InputOption('foo', 'f'); -$t->is($option->getShortcut(), 'f', '__construct() can take a shortcut as its second argument'); -$option = new InputOption('foo', '-f'); -$t->is($option->getShortcut(), 'f', '__construct() removes the leading - of the shortcut'); - -// mode argument -$option = new InputOption('foo', 'f'); -$t->is($option->acceptParameter(), false, '__construct() gives a "Option::PARAMETER_NONE" mode by default'); -$t->is($option->isParameterRequired(), false, '__construct() gives a "Option::PARAMETER_NONE" mode by default'); -$t->is($option->isParameterOptional(), false, '__construct() gives a "Option::PARAMETER_NONE" mode by default'); - -$option = new InputOption('foo', 'f', null); -$t->is($option->acceptParameter(), false, '__construct() can take "Option::PARAMETER_NONE" as its mode'); -$t->is($option->isParameterRequired(), false, '__construct() can take "Option::PARAMETER_NONE" as its mode'); -$t->is($option->isParameterOptional(), false, '__construct() can take "Option::PARAMETER_NONE" as its mode'); - -$option = new InputOption('foo', 'f', InputOption::PARAMETER_NONE); -$t->is($option->acceptParameter(), false, '__construct() can take "Option::PARAMETER_NONE" as its mode'); -$t->is($option->isParameterRequired(), false, '__construct() can take "Option::PARAMETER_NONE" as its mode'); -$t->is($option->isParameterOptional(), false, '__construct() can take "Option::PARAMETER_NONE" as its mode'); - -$option = new InputOption('foo', 'f', InputOption::PARAMETER_REQUIRED); -$t->is($option->acceptParameter(), true, '__construct() can take "Option::PARAMETER_REQUIRED" as its mode'); -$t->is($option->isParameterRequired(), true, '__construct() can take "Option::PARAMETER_REQUIRED" as its mode'); -$t->is($option->isParameterOptional(), false, '__construct() can take "Option::PARAMETER_REQUIRED" as its mode'); - -$option = new InputOption('foo', 'f', InputOption::PARAMETER_OPTIONAL); -$t->is($option->acceptParameter(), true, '__construct() can take "Option::PARAMETER_OPTIONAL" as its mode'); -$t->is($option->isParameterRequired(), false, '__construct() can take "Option::PARAMETER_OPTIONAL" as its mode'); -$t->is($option->isParameterOptional(), true, '__construct() can take "Option::PARAMETER_OPTIONAL" as its mode'); - -try -{ - $option = new InputOption('foo', 'f', 'ANOTHER_ONE'); - $t->fail('__construct() throws an Exception if the mode is not valid'); -} -catch (\Exception $e) -{ - $t->pass('__construct() throws an Exception if the mode is not valid'); -} - -// ->isArray() -$t->diag('->isArray()'); -$option = new InputOption('foo', null, InputOption::PARAMETER_OPTIONAL | InputOption::PARAMETER_IS_ARRAY); -$t->ok($option->isArray(), '->isArray() returns true if the option can be an array'); -$option = new InputOption('foo', null, InputOption::PARAMETER_NONE); -$t->ok(!$option->isArray(), '->isArray() returns false if the option can not be an array'); - -// ->getDescription() -$t->diag('->getDescription()'); -$option = new InputOption('foo', 'f', null, 'Some description'); -$t->is($option->getDescription(), 'Some description', '->getDescription() returns the description message'); - -// ->getDefault() -$t->diag('->getDefault()'); -$option = new InputOption('foo', null, InputOption::PARAMETER_OPTIONAL, '', 'default'); -$t->is($option->getDefault(), 'default', '->getDefault() returns the default value'); - -$option = new InputOption('foo', null, InputOption::PARAMETER_REQUIRED, '', 'default'); -$t->is($option->getDefault(), 'default', '->getDefault() returns the default value'); - -$option = new InputOption('foo', null, InputOption::PARAMETER_REQUIRED); -$t->ok(is_null($option->getDefault()), '->getDefault() returns null if no default value is configured'); - -$option = new InputOption('foo', null, InputOption::PARAMETER_OPTIONAL | InputOption::PARAMETER_IS_ARRAY); -$t->is($option->getDefault(), array(), '->getDefault() returns an empty array if option is an array'); - -$option = new InputOption('foo', null, InputOption::PARAMETER_NONE); -$t->ok($option->getDefault() === false, '->getDefault() returns false if the option does not take a parameter'); - -// ->setDefault() -$t->diag('->setDefault()'); -$option = new InputOption('foo', null, InputOption::PARAMETER_REQUIRED, '', 'default'); -$option->setDefault(null); -$t->ok(is_null($option->getDefault()), '->setDefault() can reset the default value by passing null'); -$option->setDefault('another'); -$t->is($option->getDefault(), 'another', '->setDefault() changes the default value'); - -$option = new InputOption('foo', null, InputOption::PARAMETER_REQUIRED | InputOption::PARAMETER_IS_ARRAY); -$option->setDefault(array(1, 2)); -$t->is($option->getDefault(), array(1, 2), '->setDefault() changes the default value'); - -$option = new InputOption('foo', 'f', InputOption::PARAMETER_NONE); -try -{ - $option->setDefault('default'); - $t->fail('->setDefault() throws an Exception if you give a default value for a PARAMETER_NONE option'); -} -catch (\Exception $e) -{ - $t->pass('->setDefault() throws an Exception if you give a default value for a PARAMETER_NONE option'); -} - -$option = new InputOption('foo', 'f', InputOption::PARAMETER_OPTIONAL | InputOption::PARAMETER_IS_ARRAY); -try -{ - $option->setDefault('default'); - $t->fail('->setDefault() throws an Exception if you give a default value which is not an array for a PARAMETER_IS_ARRAY option'); -} -catch (\Exception $e) -{ - $t->pass('->setDefault() throws an Exception if you give a default value which is not an array for a PARAMETER_IS_ARRAY option'); -} diff --git a/tests/unit/Symfony/Components/Console/Input/InputTest.php b/tests/unit/Symfony/Components/Console/Input/InputTest.php deleted file mode 100644 index cb175feceb..0000000000 --- a/tests/unit/Symfony/Components/Console/Input/InputTest.php +++ /dev/null @@ -1,124 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -require_once __DIR__.'/../../../../bootstrap.php'; - -use Symfony\Components\Console\Input\ArrayInput; -use Symfony\Components\Console\Input\InputDefinition; -use Symfony\Components\Console\Input\InputArgument; -use Symfony\Components\Console\Input\InputOption; - -$t = new LimeTest(19); - -// __construct() -$t->diag('__construct()'); -$input = new ArrayInput(array('name' => 'foo'), new InputDefinition(array(new InputArgument('name')))); -$t->is($input->getArgument('name'), 'foo', '->__construct() takes a InputDefinition as an argument'); - -// ->getOption() ->setOption() ->getOptions() -$t->diag('->getOption() ->setOption() ->getOptions()'); -$input = new ArrayInput(array('--name' => 'foo'), new InputDefinition(array(new InputOption('name')))); -$t->is($input->getOption('name'), 'foo', '->getOption() returns the value for the given option'); - -$input->setOption('name', 'bar'); -$t->is($input->getOption('name'), 'bar', '->setOption() sets the value for a given option'); -$t->is($input->getOptions(), array('name' => 'bar'), '->getOptions() returns all option values'); - -$input = new ArrayInput(array('--name' => 'foo'), new InputDefinition(array(new InputOption('name'), new InputOption('bar', '', InputOption::PARAMETER_OPTIONAL, '', 'default')))); -$t->is($input->getOption('bar'), 'default', '->getOption() returns the default value for optional options'); -$t->is($input->getOptions(), array('name' => 'foo', 'bar' => 'default'), '->getOptions() returns all option values, even optional ones'); - -try -{ - $input->setOption('foo', 'bar'); - $t->fail('->setOption() throws a \InvalidArgumentException if the option does not exist'); -} -catch (\InvalidArgumentException $e) -{ - $t->pass('->setOption() throws a \InvalidArgumentException if the option does not exist'); -} - -try -{ - $input->getOption('foo'); - $t->fail('->getOption() throws a \InvalidArgumentException if the option does not exist'); -} -catch (\InvalidArgumentException $e) -{ - $t->pass('->getOption() throws a \InvalidArgumentException if the option does not exist'); -} - -// ->getArgument() ->setArgument() ->getArguments() -$t->diag('->getArgument() ->setArgument() ->getArguments()'); -$input = new ArrayInput(array('name' => 'foo'), new InputDefinition(array(new InputArgument('name')))); -$t->is($input->getArgument('name'), 'foo', '->getArgument() returns the value for the given argument'); - -$input->setArgument('name', 'bar'); -$t->is($input->getArgument('name'), 'bar', '->setArgument() sets the value for a given argument'); -$t->is($input->getArguments(), array('name' => 'bar'), '->getArguments() returns all argument values'); - -$input = new ArrayInput(array('name' => 'foo'), new InputDefinition(array(new InputArgument('name'), new InputArgument('bar', InputArgument::OPTIONAL, '', 'default')))); -$t->is($input->getArgument('bar'), 'default', '->getArgument() returns the default value for optional arguments'); -$t->is($input->getArguments(), array('name' => 'foo', 'bar' => 'default'), '->getArguments() returns all argument values, even optional ones'); - -try -{ - $input->setArgument('foo', 'bar'); - $t->fail('->setArgument() throws a \InvalidArgumentException if the argument does not exist'); -} -catch (\InvalidArgumentException $e) -{ - $t->pass('->setArgument() throws a \InvalidArgumentException if the argument does not exist'); -} - -try -{ - $input->getArgument('foo'); - $t->fail('->getArgument() throws a \InvalidArgumentException if the argument does not exist'); -} -catch (\InvalidArgumentException $e) -{ - $t->pass('->getArgument() throws a \InvalidArgumentException if the argument does not exist'); -} - -// ->validate() -$t->diag('->validate()'); -$input = new ArrayInput(array()); -$input->bind(new InputDefinition(array(new InputArgument('name', InputArgument::REQUIRED)))); - -try -{ - $input->validate(); - $t->fail('->validate() throws a \RuntimeException if not enough arguments are given'); -} -catch (\RuntimeException $e) -{ - $t->pass('->validate() throws a \RuntimeException if not enough arguments are given'); -} - -$input = new ArrayInput(array('name' => 'foo')); -$input->bind(new InputDefinition(array(new InputArgument('name', InputArgument::REQUIRED)))); - -try -{ - $input->validate(); - $t->pass('->validate() does not throw a \RuntimeException if enough arguments are given'); -} -catch (\RuntimeException $e) -{ - $t->fail('->validate() does not throw a \RuntimeException if enough arguments are given'); -} - -// ->setInteractive() ->isInteractive() -$t->diag('->setInteractive() ->isInteractive()'); -$input = new ArrayInput(array()); -$t->ok($input->isInteractive(), '->isInteractive() returns whether the input should be interactive or not'); -$input->setInteractive(false); -$t->ok(!$input->isInteractive(), '->setInteractive() changes the interactive flag'); diff --git a/tests/unit/Symfony/Components/Console/Input/StringInputTest.php b/tests/unit/Symfony/Components/Console/Input/StringInputTest.php deleted file mode 100644 index d5bf981c13..0000000000 --- a/tests/unit/Symfony/Components/Console/Input/StringInputTest.php +++ /dev/null @@ -1,94 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -require_once __DIR__.'/../../../../bootstrap.php'; - -use Symfony\Components\Console\Input\StringInput; - -class TestInput extends StringInput -{ - public function getTokens() - { - return $this->tokens; - } -} - -$t = new LimeTest(23); - -// ->tokenize() -$t->diag('->tokenize()'); -$input = new TestInput(''); -$t->is($input->getTokens(), array(), '->tokenize() parses an empty string'); - -$input = new TestInput('foo'); -$t->is($input->getTokens(), array('foo'), '->tokenize() parses arguments'); - -$input = new TestInput(' foo bar '); -$t->is($input->getTokens(), array('foo', 'bar'), '->tokenize() ignores whitespaces between arguments'); - -$input = new TestInput('"quoted"'); -$t->is($input->getTokens(), array('quoted'), '->tokenize() parses quoted arguments'); - -$input = new TestInput("'quoted'"); -$t->is($input->getTokens(), array('quoted'), '->tokenize() parses quoted arguments'); - -$input = new TestInput('\"quoted\"'); -$t->is($input->getTokens(), array('"quoted"'), '->tokenize() parses escaped-quoted arguments'); - -$input = new TestInput("\'quoted\'"); -$t->is($input->getTokens(), array('\'quoted\''), '->tokenize() parses escaped-quoted arguments'); - -$input = new TestInput('-a'); -$t->is($input->getTokens(), array('-a'), '->tokenize() parses short options'); - -$input = new TestInput('-azc'); -$t->is($input->getTokens(), array('-azc'), '->tokenize() parses aggregated short options'); - -$input = new TestInput('-awithavalue'); -$t->is($input->getTokens(), array('-awithavalue'), '->tokenize() parses short options with a value'); - -$input = new TestInput('-a"foo bar"'); -$t->is($input->getTokens(), array('-afoo bar'), '->tokenize() parses short options with a value'); - -$input = new TestInput('-a"foo bar""foo bar"'); -$t->is($input->getTokens(), array('-afoo barfoo bar'), '->tokenize() parses short options with a value'); - -$input = new TestInput('-a\'foo bar\''); -$t->is($input->getTokens(), array('-afoo bar'), '->tokenize() parses short options with a value'); - -$input = new TestInput('-a\'foo bar\'\'foo bar\''); -$t->is($input->getTokens(), array('-afoo barfoo bar'), '->tokenize() parses short options with a value'); - -$input = new TestInput('-a\'foo bar\'"foo bar"'); -$t->is($input->getTokens(), array('-afoo barfoo bar'), '->tokenize() parses short options with a value'); - -$input = new TestInput('--long-option'); -$t->is($input->getTokens(), array('--long-option'), '->tokenize() parses long options'); - -$input = new TestInput('--long-option=foo'); -$t->is($input->getTokens(), array('--long-option=foo'), '->tokenize() parses long options with a value'); - -$input = new TestInput('--long-option="foo bar"'); -$t->is($input->getTokens(), array('--long-option=foo bar'), '->tokenize() parses long options with a value'); - -$input = new TestInput('--long-option="foo bar""another"'); -$t->is($input->getTokens(), array('--long-option=foo baranother'), '->tokenize() parses long options with a value'); - -$input = new TestInput('--long-option=\'foo bar\''); -$t->is($input->getTokens(), array('--long-option=foo bar'), '->tokenize() parses long options with a value'); - -$input = new TestInput("--long-option='foo bar''another'"); -$t->is($input->getTokens(), array("--long-option=foo baranother"), '->tokenize() parses long options with a value'); - -$input = new TestInput("--long-option='foo bar'\"another\""); -$t->is($input->getTokens(), array("--long-option=foo baranother"), '->tokenize() parses long options with a value'); - -$input = new TestInput('foo -a -ffoo --long bar'); -$t->is($input->getTokens(), array('foo', '-a', '-ffoo', '--long', 'bar'), '->tokenize() parses when several arguments and options'); diff --git a/tests/unit/Symfony/Components/Console/Output/ConsoleOutputTest.php b/tests/unit/Symfony/Components/Console/Output/ConsoleOutputTest.php deleted file mode 100644 index 2c61dde745..0000000000 --- a/tests/unit/Symfony/Components/Console/Output/ConsoleOutputTest.php +++ /dev/null @@ -1,21 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -require_once __DIR__.'/../../../../bootstrap.php'; - -use Symfony\Components\Console\Output\ConsoleOutput; -use Symfony\Components\Console\Output\Output; - -$t = new LimeTest(1); - -// __construct() -$t->diag('__construct()'); -$output = new ConsoleOutput(Output::VERBOSITY_QUIET, true); -$t->is($output->getVerbosity(), Output::VERBOSITY_QUIET, '__construct() takes the verbosity as its first argument'); diff --git a/tests/unit/Symfony/Components/Console/Output/OutputTest.php b/tests/unit/Symfony/Components/Console/Output/OutputTest.php deleted file mode 100644 index 03bed431b7..0000000000 --- a/tests/unit/Symfony/Components/Console/Output/OutputTest.php +++ /dev/null @@ -1,100 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -require_once __DIR__.'/../../../../bootstrap.php'; - -use Symfony\Components\Console\Output\Output; - -$t = new LimeTest(13); - -class TestOutput extends Output -{ - public $output = ''; - - static public function getStyle($name) - { - return static::$styles[$name]; - } - - public function doWrite($message, $newline) - { - $this->output .= $message.($newline ? "\n" : ''); - } -} - -// __construct() -$t->diag('__construct()'); -$output = new TestOutput(Output::VERBOSITY_QUIET, true); -$t->is($output->getVerbosity(), Output::VERBOSITY_QUIET, '__construct() takes the verbosity as its first argument'); -$t->is($output->isDecorated(), true, '__construct() takes the decorated flag as its second argument'); - -// ->setDecorated() ->isDecorated() -$t->diag('->setDecorated() ->isDecorated()'); -$output = new TestOutput(); -$output->setDecorated(true); -$t->is($output->isDecorated(), true, 'setDecorated() sets the decorated flag'); - -// ->setVerbosity() ->getVerbosity() -$t->diag('->setVerbosity() ->getVerbosity()'); -$output->setVerbosity(Output::VERBOSITY_QUIET); -$t->is($output->getVerbosity(), Output::VERBOSITY_QUIET, '->setVerbosity() sets the verbosity'); - -// ::setStyle() -$t->diag('::setStyle()'); -Output::setStyle('FOO', array('bg' => 'red', 'fg' => 'yellow', 'blink' => true)); -$t->is(TestOutput::getStyle('foo'), array('bg' => 'red', 'fg' => 'yellow', 'blink' => true), '::setStyle() sets a new style'); - -// ->writeln() -$t->diag('->writeln()'); -$output = new TestOutput(Output::VERBOSITY_QUIET); -$output->writeln('foo'); -$t->is($output->output, '', '->writeln() outputs nothing if verbosity is set to VERBOSITY_QUIET'); - -$output = new TestOutput(); -$output->writeln(array('foo', 'bar')); -$t->is($output->output, "foo\nbar\n", '->writeln() can take an array of messages to output'); - -$output = new TestOutput(); -$output->writeln('foo', Output::OUTPUT_RAW); -$t->is($output->output, "foo\n", '->writeln() outputs the raw message if OUTPUT_RAW is specified'); - -$output = new TestOutput(); -$output->writeln('foo', Output::OUTPUT_PLAIN); -$t->is($output->output, "foo\n", '->writeln() strips decoration tags if OUTPUT_PLAIN is specified'); - -$output = new TestOutput(); -$output->setDecorated(false); -$output->writeln('foo'); -$t->is($output->output, "foo\n", '->writeln() strips decoration tags if decoration is set to false'); - -$output = new TestOutput(); -$output->setDecorated(true); -$output->writeln('foo'); -$t->is($output->output, "\033[33;41;5mfoo\033[0m\n", '->writeln() decorates the output'); - -try -{ - $output->writeln('foo', 24); - $t->fail('->writeln() throws an \InvalidArgumentException when the type does not exist'); -} -catch (\InvalidArgumentException $e) -{ - $t->pass('->writeln() throws an \InvalidArgumentException when the type does not exist'); -} - -try -{ - $output->writeln('foo'); - $t->fail('->writeln() throws an \InvalidArgumentException when a style does not exist'); -} -catch (\InvalidArgumentException $e) -{ - $t->pass('->writeln() throws an \InvalidArgumentException when a style does not exist'); -} diff --git a/tests/unit/Symfony/Components/Console/Output/StreamOutputTest.php b/tests/unit/Symfony/Components/Console/Output/StreamOutputTest.php deleted file mode 100644 index e8db804a20..0000000000 --- a/tests/unit/Symfony/Components/Console/Output/StreamOutputTest.php +++ /dev/null @@ -1,47 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -require_once __DIR__.'/../../../../bootstrap.php'; - -use Symfony\Components\Console\Output\Output; -use Symfony\Components\Console\Output\StreamOutput; - -$t = new LimeTest(5); - -$stream = fopen('php://memory', 'a', false); - -// __construct() -$t->diag('__construct()'); - -try -{ - $output = new StreamOutput('foo'); - $t->fail('__construct() throws an \InvalidArgumentException if the first argument is not a stream'); -} -catch (\InvalidArgumentException $e) -{ - $t->pass('__construct() throws an \InvalidArgumentException if the first argument is not a stream'); -} - -$output = new StreamOutput($stream, Output::VERBOSITY_QUIET, true); -$t->is($output->getVerbosity(), Output::VERBOSITY_QUIET, '__construct() takes the verbosity as its first argument'); -$t->is($output->isDecorated(), true, '__construct() takes the decorated flag as its second argument'); - -// ->getStream() -$t->diag('->getStream()'); -$output = new StreamOutput($stream); -$t->is($output->getStream(), $stream, '->getStream() returns the current stream'); - -// ->doWrite() -$t->diag('->doWrite()'); -$output = new StreamOutput($stream); -$output->writeln('foo'); -rewind($output->getStream()); -$t->is(stream_get_contents($output->getStream()), "foo\n", '->doWrite() writes to the stream'); diff --git a/tests/unit/Symfony/Components/Console/Tester/ApplicationTesterTest.php b/tests/unit/Symfony/Components/Console/Tester/ApplicationTesterTest.php deleted file mode 100644 index d2cdb4883f..0000000000 --- a/tests/unit/Symfony/Components/Console/Tester/ApplicationTesterTest.php +++ /dev/null @@ -1,47 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -require_once __DIR__.'/../../../../bootstrap.php'; - -use Symfony\Components\Console\Application; -use Symfony\Components\Console\Output\Output; -use Symfony\Components\Console\Tester\ApplicationTester; - -$t = new LimeTest(6); - -$application = new Application(); -$application->setAutoExit(false); -$application->register('foo') - ->addArgument('command') - ->addArgument('foo') - ->setCode(function ($input, $output) { $output->writeln('foo'); }) -; - -$tester = new ApplicationTester($application); -$tester->run(array('command' => 'foo', 'foo' => 'bar'), array('interactive' => false, 'decorated' => false, 'verbosity' => Output::VERBOSITY_VERBOSE)); - -// ->run() -$t->diag('->run()'); -$t->is($tester->getInput()->isInteractive(), false, '->execute() takes an interactive option'); -$t->is($tester->getOutput()->isDecorated(), false, '->execute() takes a decorated option'); -$t->is($tester->getOutput()->getVerbosity(), Output::VERBOSITY_VERBOSE, '->execute() takes a verbosity option'); - -// ->getInput() -$t->diag('->getInput()'); -$t->is($tester->getInput()->getArgument('foo'), 'bar', '->getInput() returns the current input instance'); - -// ->getOutput() -$t->diag('->getOutput()'); -rewind($tester->getOutput()->getStream()); -$t->is(stream_get_contents($tester->getOutput()->getStream()), "foo\n", '->getOutput() returns the current output instance'); - -// ->getDisplay() -$t->diag('->getDisplay()'); -$t->is($tester->getDisplay(), "foo\n", '->getDisplay() returns the display of the last execution'); diff --git a/tests/unit/Symfony/Components/Console/Tester/CommandTesterTest.php b/tests/unit/Symfony/Components/Console/Tester/CommandTesterTest.php deleted file mode 100644 index aa48b83366..0000000000 --- a/tests/unit/Symfony/Components/Console/Tester/CommandTesterTest.php +++ /dev/null @@ -1,44 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -require_once __DIR__.'/../../../../bootstrap.php'; - -use Symfony\Components\Console\Command\Command; -use Symfony\Components\Console\Output\Output; -use Symfony\Components\Console\Tester\CommandTester; - -$t = new LimeTest(6); - -$command = new Command('foo'); -$command->addArgument('command'); -$command->addArgument('foo'); -$command->setCode(function ($input, $output) { $output->writeln('foo'); }); - -$tester = new CommandTester($command); -$tester->execute(array('foo' => 'bar'), array('interactive' => false, 'decorated' => false, 'verbosity' => Output::VERBOSITY_VERBOSE)); - -// ->execute() -$t->diag('->execute()'); -$t->is($tester->getInput()->isInteractive(), false, '->execute() takes an interactive option'); -$t->is($tester->getOutput()->isDecorated(), false, '->execute() takes a decorated option'); -$t->is($tester->getOutput()->getVerbosity(), Output::VERBOSITY_VERBOSE, '->execute() takes a verbosity option'); - -// ->getInput() -$t->diag('->getInput()'); -$t->is($tester->getInput()->getArgument('foo'), 'bar', '->getInput() returns the current input instance'); - -// ->getOutput() -$t->diag('->getOutput()'); -rewind($tester->getOutput()->getStream()); -$t->is(stream_get_contents($tester->getOutput()->getStream()), "foo\n", '->getOutput() returns the current output instance'); - -// ->getDisplay() -$t->diag('->getDisplay()'); -$t->is($tester->getDisplay(), "foo\n", '->getDisplay() returns the display of the last execution'); diff --git a/tests/unit/Symfony/Components/DependencyInjection/BuilderConfigurationTest.php b/tests/unit/Symfony/Components/DependencyInjection/BuilderConfigurationTest.php deleted file mode 100644 index 414b7cdbfd..0000000000 --- a/tests/unit/Symfony/Components/DependencyInjection/BuilderConfigurationTest.php +++ /dev/null @@ -1,192 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -require_once __DIR__.'/../../../bootstrap.php'; - -use Symfony\Components\DependencyInjection\Builder; -use Symfony\Components\DependencyInjection\BuilderConfiguration; -use Symfony\Components\DependencyInjection\Definition; -use Symfony\Components\DependencyInjection\Reference; -use Symfony\Components\DependencyInjection\FileResource; - -$fixturesPath = __DIR__.'/../../../../fixtures/Symfony/Components/DependencyInjection/'; - -$t = new LimeTest(39); - -// __construct() -$t->diag('__construct()'); -$definitions = array( - 'foo' => new Definition('FooClass'), - 'bar' => new Definition('BarClass'), -); -$parameters = array( - 'foo' => 'foo', - 'bar' => 'bar', -); -$configuration = new BuilderConfiguration($definitions, $parameters); -$t->is($configuration->getDefinitions(), $definitions, '__construct() takes an array of definitions as its first argument'); -$t->is($configuration->getParameters(), $parameters, '__construct() takes an array of parameters as its second argument'); - -// ->merge() -$t->diag('->merge()'); -$configuration = new BuilderConfiguration(); -$configuration->merge(null); -$t->is($configuration->getParameters(), array(), '->merge() accepts null as an argument'); -$t->is($configuration->getDefinitions(), array(), '->merge() accepts null as an argument'); - -$configuration = new BuilderConfiguration(array(), array('bar' => 'foo')); -$configuration1 = new BuilderConfiguration(array(), array('foo' => 'bar')); -$configuration->merge($configuration1); -$t->is($configuration->getParameters(), array('bar' => 'foo', 'foo' => 'bar'), '->merge() merges current parameters with the loaded ones'); - -$configuration = new BuilderConfiguration(array(), array('bar' => 'foo', 'foo' => 'baz')); -$config = new BuilderConfiguration(array(), array('foo' => 'bar')); -$configuration->merge($config); -$t->is($configuration->getParameters(), array('bar' => 'foo', 'foo' => 'bar'), '->merge() overrides existing parameters'); - -$configuration = new BuilderConfiguration(array('foo' => new Definition('FooClass'), 'bar' => new Definition('BarClass'))); -$config = new BuilderConfiguration(array('baz' => new Definition('BazClass'))); -$config->setAlias('alias_for_foo', 'foo'); -$configuration->merge($config); -$t->is(array_keys($configuration->getDefinitions()), array('foo', 'bar', 'baz'), '->merge() merges definitions already defined ones'); -$t->is($configuration->getAliases(), array('alias_for_foo' => 'foo'), '->merge() registers defined aliases'); - -$configuration = new BuilderConfiguration(array('foo' => new Definition('FooClass'))); -$config->setDefinition('foo', new Definition('BazClass')); -$configuration->merge($config); -$t->is($configuration->getDefinition('foo')->getClass(), 'BazClass', '->merge() overrides already defined services'); - -$configuration = new BuilderConfiguration(); -$configuration->addResource($a = new FileResource('foo.xml')); -$config = new BuilderConfiguration(); -$config->addResource($b = new FileResource('foo.yml')); -$configuration->merge($config); -$t->is($configuration->getResources(), array($a, $b), '->merge() merges resources'); - -// ->setParameters() ->getParameters() -$t->diag('->setParameters() ->getParameters()'); - -$configuration = new BuilderConfiguration(); -$t->is($configuration->getParameters(), array(), '->getParameters() returns an empty array if no parameter has been defined'); - -$configuration->setParameters(array('foo' => 'bar')); -$t->is($configuration->getParameters(), array('foo' => 'bar'), '->setParameters() sets the parameters'); - -$configuration->setParameters(array('bar' => 'foo')); -$t->is($configuration->getParameters(), array('bar' => 'foo'), '->setParameters() overrides the previous defined parameters'); - -$configuration->setParameters(array('Bar' => 'foo')); -$t->is($configuration->getParameters(), array('bar' => 'foo'), '->setParameters() converts the key to lowercase'); - -// ->setParameter() ->getParameter() -$t->diag('->setParameter() ->getParameter() '); - -$configuration = new BuilderConfiguration(array(), array('foo' => 'bar')); -$configuration->setParameter('bar', 'foo'); -$t->is($configuration->getParameter('bar'), 'foo', '->setParameter() sets the value of a new parameter'); - -$configuration->setParameter('foo', 'baz'); -$t->is($configuration->getParameter('foo'), 'baz', '->setParameter() overrides previously set parameter'); - -$configuration->setParameter('Foo', 'baz1'); -$t->is($configuration->getParameter('foo'), 'baz1', '->setParameter() converts the key to lowercase'); -$t->is($configuration->getParameter('FOO'), 'baz1', '->getParameter() converts the key to lowercase'); - -try -{ - $configuration->getParameter('baba'); - $t->fail('->getParameter() throws an \InvalidArgumentException if the key does not exist'); -} -catch (\InvalidArgumentException $e) -{ - $t->pass('->getParameter() throws an \InvalidArgumentException if the key does not exist'); -} - -// ->hasParameter() -$t->diag('->hasParameter()'); -$configuration = new BuilderConfiguration(array(), array('foo' => 'bar')); -$t->ok($configuration->hasParameter('foo'), '->hasParameter() returns true if a parameter is defined'); -$t->ok($configuration->hasParameter('Foo'), '->hasParameter() converts the key to lowercase'); -$t->ok(!$configuration->hasParameter('bar'), '->hasParameter() returns false if a parameter is not defined'); - -// ->addParameters() -$t->diag('->addParameters()'); -$configuration = new BuilderConfiguration(array(), array('foo' => 'bar')); -$configuration->addParameters(array('bar' => 'foo')); -$t->is($configuration->getParameters(), array('foo' => 'bar', 'bar' => 'foo'), '->addParameters() adds parameters to the existing ones'); -$configuration->addParameters(array('Bar' => 'fooz')); -$t->is($configuration->getParameters(), array('foo' => 'bar', 'bar' => 'fooz'), '->addParameters() converts keys to lowercase'); - -// ->setAlias() ->getAlias() ->hasAlias() ->getAliases() ->addAliases() -$t->diag('->setAlias() ->getAlias() ->hasAlias()'); -$configuration = new BuilderConfiguration(); -$configuration->setAlias('bar', 'foo'); -$t->is($configuration->getAlias('bar'), 'foo', '->setAlias() defines a new alias'); -$t->ok($configuration->hasAlias('bar'), '->hasAlias() returns true if the alias is defined'); -$t->ok(!$configuration->hasAlias('baba'), '->hasAlias() returns false if the alias is not defined'); - -try -{ - $configuration->getAlias('baba'); - $t->fail('->getAlias() throws an \InvalidArgumentException if the alias does not exist'); -} -catch (\InvalidArgumentException $e) -{ - $t->pass('->getAlias() throws an \InvalidArgumentException if the alias does not exist'); -} - -$configuration->setAlias('barbar', 'foofoo'); -$t->is($configuration->getAliases(), array('bar' => 'foo', 'barbar' => 'foofoo'), '->getAliases() returns an array of all defined aliases'); - -$configuration->addAliases(array('foo' => 'bar')); -$t->is($configuration->getAliases(), array('bar' => 'foo', 'barbar' => 'foofoo', 'foo' => 'bar'), '->addAliases() adds some aliases'); - -// ->setDefinitions() ->addDefinitions() ->getDefinitions() ->setDefinition() ->getDefinition() ->hasDefinition() -$t->diag('->setDefinitions() ->addDefinitions() ->getDefinitions() ->setDefinition() ->getDefinition() ->hasDefinition()'); -$configuration = new BuilderConfiguration(); -$definitions = array( - 'foo' => new Definition('FooClass'), - 'bar' => new Definition('BarClass'), -); -$configuration->setDefinitions($definitions); -$t->is($configuration->getDefinitions(), $definitions, '->setDefinitions() sets the service definitions'); -$t->ok($configuration->hasDefinition('foo'), '->hasDefinition() returns true if a service definition exists'); -$t->ok(!$configuration->hasDefinition('foobar'), '->hasDefinition() returns false if a service definition does not exist'); - -$configuration->setDefinition('foobar', $foo = new Definition('FooBarClass')); -$t->is($configuration->getDefinition('foobar'), $foo, '->getDefinition() returns a service definition if defined'); -$t->ok($configuration->setDefinition('foobar', $foo = new Definition('FooBarClass')) === $foo, '->setDefinition() implements a fuild interface by returning the service reference'); - -$configuration->addDefinitions($defs = array('foobar' => new Definition('FooBarClass'))); -$t->is($configuration->getDefinitions(), array_merge($definitions, $defs), '->addDefinitions() adds the service definitions'); - -try -{ - $configuration->getDefinition('baz'); - $t->fail('->getDefinition() throws an InvalidArgumentException if the service definition does not exist'); -} -catch (InvalidArgumentException $e) -{ - $t->pass('->getDefinition() throws an InvalidArgumentException if the service definition does not exist'); -} - -// ->findDefinition() -$t->diag('->findDefinition()'); -$configuration = new BuilderConfiguration(array('foo' => $definition = new Definition('FooClass'))); -$configuration->setAlias('bar', 'foo'); -$configuration->setAlias('foobar', 'bar'); -$t->is($configuration->findDefinition('foobar'), $definition, '->findDefinition() returns a Definition'); - -// ->addResource() ->getResources() -$t->diag('->addResource() ->getResources()'); -$configuration = new BuilderConfiguration(); -$configuration->addResource($a = new FileResource('foo.xml')); -$configuration->addResource($b = new FileResource('foo.yml')); -$t->is($configuration->getResources(), array($a, $b), '->getResources() returns an array of resources read for the current configuration'); diff --git a/tests/unit/Symfony/Components/DependencyInjection/BuilderTest.php b/tests/unit/Symfony/Components/DependencyInjection/BuilderTest.php deleted file mode 100644 index 216082f2a0..0000000000 --- a/tests/unit/Symfony/Components/DependencyInjection/BuilderTest.php +++ /dev/null @@ -1,309 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -require_once __DIR__.'/../../../bootstrap.php'; - -use Symfony\Components\DependencyInjection\Builder; -use Symfony\Components\DependencyInjection\BuilderConfiguration; -use Symfony\Components\DependencyInjection\Definition; -use Symfony\Components\DependencyInjection\Reference; - -$fixturesPath = __DIR__.'/../../../../fixtures/Symfony/Components/DependencyInjection/'; - -$t = new LimeTest(61); - -// ->setDefinitions() ->addDefinitions() ->getDefinitions() ->setDefinition() ->getDefinition() ->hasDefinition() -$t->diag('->setDefinitions() ->addDefinitions() ->getDefinitions() ->setDefinition() ->getDefinition() ->hasDefinition()'); -$builder = new Builder(); -$definitions = array( - 'foo' => new Definition('FooClass'), - 'bar' => new Definition('BarClass'), -); -$builder->setDefinitions($definitions); -$t->is($builder->getDefinitions(), $definitions, '->setDefinitions() sets the service definitions'); -$t->ok($builder->hasDefinition('foo'), '->hasDefinition() returns true if a service definition exists'); -$t->ok(!$builder->hasDefinition('foobar'), '->hasDefinition() returns false if a service definition does not exist'); - -$builder->setDefinition('foobar', $foo = new Definition('FooBarClass')); -$t->is($builder->getDefinition('foobar'), $foo, '->getDefinition() returns a service definition if defined'); -$t->ok($builder->setDefinition('foobar', $foo = new Definition('FooBarClass')) === $foo, '->setDefinition() implements a fuild interface by returning the service reference'); - -$builder->addDefinitions($defs = array('foobar' => new Definition('FooBarClass'))); -$t->is($builder->getDefinitions(), array_merge($definitions, $defs), '->addDefinitions() adds the service definitions'); - -try -{ - $builder->getDefinition('baz'); - $t->fail('->getDefinition() throws an InvalidArgumentException if the service definition does not exist'); -} -catch (InvalidArgumentException $e) -{ - $t->pass('->getDefinition() throws an InvalidArgumentException if the service definition does not exist'); -} - -// ->register() -$t->diag('->register()'); -$builder = new Builder(); -$builder->register('foo', 'FooClass'); -$t->ok($builder->hasDefinition('foo'), '->register() registers a new service definition'); -$t->ok($builder->getDefinition('foo') instanceof Definition, '->register() returns the newly created Definition instance'); - -// ->hasService() -$t->diag('->hasService()'); -$builder = new Builder(); -$t->ok(!$builder->hasService('foo'), '->hasService() returns false if the service does not exist'); -$builder->register('foo', 'FooClass'); -$t->ok($builder->hasService('foo'), '->hasService() returns true if a service definition exists'); -$builder->bar = new stdClass(); -$t->ok($builder->hasService('bar'), '->hasService() returns true if a service exists'); - -// ->getService() -$t->diag('->getService()'); -$builder = new Builder(); -try -{ - $builder->getService('foo'); - $t->fail('->getService() throws an InvalidArgumentException if the service does not exist'); -} -catch (InvalidArgumentException $e) -{ - $t->pass('->getService() throws an InvalidArgumentException if the service does not exist'); -} -$builder->register('foo', 'stdClass'); -$t->ok(is_object($builder->getService('foo')), '->getService() returns the service definition associated with the id'); -$builder->bar = $bar = new stdClass(); -$t->is($builder->getService('bar'), $bar, '->getService() returns the service associated with the id'); -$builder->register('bar', 'stdClass'); -$t->is($builder->getService('bar'), $bar, '->getService() returns the service associated with the id even if a definition has been defined'); - -$builder->register('baz', 'stdClass')->setArguments(array(new Reference('baz'))); -try -{ - @$builder->getService('baz'); - $t->fail('->getService() throws a LogicException if the service has a circular reference to itself'); -} -catch (\LogicException $e) -{ - $t->pass('->getService() throws a LogicException if the service has a circular reference to itself'); -} - -$builder->register('foobar', 'stdClass')->setShared(true); -$t->ok($builder->getService('bar') === $builder->getService('bar'), '->getService() always returns the same instance if the service is shared'); - -// ->getServiceIds() -$t->diag('->getServiceIds()'); -$builder = new Builder(); -$builder->register('foo', 'stdClass'); -$builder->bar = $bar = new stdClass(); -$builder->register('bar', 'stdClass'); -$t->is($builder->getServiceIds(), array('foo', 'bar', 'service_container'), '->getServiceIds() returns all defined service ids'); - -// ->setAlias() ->getAlias() ->hasAlias() -$t->diag('->setAlias() ->getAlias() ->hasAlias()'); -$builder = new Builder(); -$builder->register('foo', 'stdClass'); -$builder->setAlias('bar', 'foo'); -$t->ok($builder->hasAlias('bar'), '->hasAlias() returns true if the alias exists'); -$t->ok(!$builder->hasAlias('foobar'), '->hasAlias() returns false if the alias does not exist'); -$t->is($builder->getAlias('bar'), 'foo', '->getAlias() returns the aliased service'); -$t->ok($builder->hasService('bar'), '->setAlias() defines a new service'); -$t->ok($builder->getService('bar') === $builder->getService('foo'), '->setAlias() creates a service that is an alias to another one'); - -try -{ - $builder->getAlias('foobar'); - $t->fail('->getAlias() throws an InvalidArgumentException if the alias does not exist'); -} -catch (\InvalidArgumentException $e) -{ - $t->pass('->getAlias() throws an InvalidArgumentException if the alias does not exist'); -} - -// ->getAliases() -$t->diag('->getAliases()'); -$builder = new Builder(); -$builder->setAlias('bar', 'foo'); -$builder->setAlias('foobar', 'foo'); -$t->is($builder->getAliases(), array('bar' => 'foo', 'foobar' => 'foo'), '->getAliases() returns all service aliases'); -$builder->register('bar', 'stdClass'); -$t->is($builder->getAliases(), array('foobar' => 'foo'), '->getAliases() does not return aliased services that have been overridden'); -$builder->setService('foobar', 'stdClass'); -$t->is($builder->getAliases(), array(), '->getAliases() does not return aliased services that have been overridden'); - -// ->createService() # file -$t->diag('->createService() # file'); -$builder = new Builder(); -$builder->register('foo1', 'FooClass')->setFile($fixturesPath.'/includes/foo.php'); -$t->ok($builder->getService('foo1'), '->createService() requires the file defined by the service definition'); -$builder->register('foo2', 'FooClass')->setFile($fixturesPath.'/includes/%file%.php'); -$builder->setParameter('file', 'foo'); -$t->ok($builder->getService('foo2'), '->createService() replaces parameters in the file provided by the service definition'); - -// ->createService() # class -$t->diag('->createService() # class'); -$builder = new Builder(); -$builder->register('foo1', '%class%'); -$builder->setParameter('class', 'stdClass'); -$t->ok($builder->getService('foo1') instanceof stdClass, '->createService() replaces parameters in the class provided by the service definition'); - -// ->createService() # arguments -$t->diag('->createService() # arguments'); -$builder = new Builder(); -$builder->register('bar', 'stdClass'); -$builder->register('foo1', 'FooClass')->addArgument(array('foo' => '%value%', '%value%' => 'foo', new Reference('bar'))); -$builder->setParameter('value', 'bar'); -$t->is($builder->getService('foo1')->arguments, array('foo' => 'bar', 'bar' => 'foo', $builder->getService('bar')), '->createService() replaces parameters and service references in the arguments provided by the service definition'); - -// ->createService() # constructor -$t->diag('->createService() # constructor'); -$builder = new Builder(); -$builder->register('bar', 'stdClass'); -$builder->register('foo1', 'FooClass')->setConstructor('getInstance')->addArgument(array('foo' => '%value%', '%value%' => 'foo', new Reference('bar'))); -$builder->setParameter('value', 'bar'); -$t->ok($builder->getService('foo1')->called, '->createService() calls the constructor to create the service instance'); -$t->is($builder->getService('foo1')->arguments, array('foo' => 'bar', 'bar' => 'foo', $builder->getService('bar')), '->createService() passes the arguments to the constructor'); - -// ->createService() # method calls -$t->diag('->createService() # method calls'); -$builder = new Builder(); -$builder->register('bar', 'stdClass'); -$builder->register('foo1', 'FooClass')->addMethodCall('setBar', array(array('%value%', new Reference('bar')))); -$builder->setParameter('value', 'bar'); -$t->is($builder->getService('foo1')->bar, array('bar', $builder->getService('bar')), '->createService() replaces the values in the method calls arguments'); - -// ->createService() # configurator -require_once $fixturesPath.'/includes/classes.php'; -$t->diag('->createService() # configurator'); -$builder = new Builder(); -$builder->register('foo1', 'FooClass')->setConfigurator('sc_configure'); -$t->ok($builder->getService('foo1')->configured, '->createService() calls the configurator'); - -$builder->register('foo2', 'FooClass')->setConfigurator(array('%class%', 'configureStatic')); -$builder->setParameter('class', 'BazClass'); -$t->ok($builder->getService('foo2')->configured, '->createService() calls the configurator'); - -$builder->register('baz', 'BazClass'); -$builder->register('foo3', 'FooClass')->setConfigurator(array(new Reference('baz'), 'configure')); -$t->ok($builder->getService('foo3')->configured, '->createService() calls the configurator'); - -$builder->register('foo4', 'FooClass')->setConfigurator('foo'); -try -{ - $builder->getService('foo4'); - $t->fail('->createService() throws an InvalidArgumentException if the configure callable is not a valid callable'); -} -catch (\InvalidArgumentException $e) -{ - $t->pass('->createService() throws an InvalidArgumentException if the configure callable is not a valid callable'); -} - -// ::resolveValue() -$t->diag('::resolveValue()'); -$t->is(Builder::resolveValue('foo', array()), 'foo', '->resolveValue() returns its argument unmodified if no placeholders are found'); -$t->is(Builder::resolveValue('I\'m a %foo%', array('foo' => 'bar')), 'I\'m a bar', '->resolveValue() replaces placeholders by their values'); -$t->ok(Builder::resolveValue('%foo%', array('foo' => true)) === true, '->resolveValue() replaces arguments that are just a placeholder by their value without casting them to strings'); - -$t->is(Builder::resolveValue(array('%foo%' => '%foo%'), array('foo' => 'bar')), array('bar' => 'bar'), '->resolveValue() replaces placeholders in keys and values of arrays'); - -$t->is(Builder::resolveValue(array('%foo%' => array('%foo%' => array('%foo%' => '%foo%'))), array('foo' => 'bar')), array('bar' => array('bar' => array('bar' => 'bar'))), '->resolveValue() replaces placeholders in nested arrays'); - -$t->is(Builder::resolveValue('I\'m a %%foo%%', array('foo' => 'bar')), 'I\'m a %foo%', '->resolveValue() supports % escaping by doubling it'); -$t->is(Builder::resolveValue('I\'m a %foo% %%foo %foo%', array('foo' => 'bar')), 'I\'m a bar %foo bar', '->resolveValue() supports % escaping by doubling it'); - -try -{ - Builder::resolveValue('%foobar%', array()); - $t->fail('->resolveValue() throws a RuntimeException if a placeholder references a non-existant parameter'); -} -catch (RuntimeException $e) -{ - $t->pass('->resolveValue() throws a RuntimeException if a placeholder references a non-existant parameter'); -} - -try -{ - Builder::resolveValue('foo %foobar% bar', array()); - $t->fail('->resolveValue() throws a RuntimeException if a placeholder references a non-existant parameter'); -} -catch (RuntimeException $e) -{ - $t->pass('->resolveValue() throws a RuntimeException if a placeholder references a non-existant parameter'); -} - -// ->resolveServices() -$t->diag('->resolveServices()'); -$builder = new Builder(); -$builder->register('foo', 'FooClass'); -$t->is($builder->resolveServices(new Reference('foo')), $builder->getService('foo'), '->resolveServices() resolves service references to service instances'); -$t->is($builder->resolveServices(array('foo' => array('foo', new Reference('foo')))), array('foo' => array('foo', $builder->getService('foo'))), '->resolveServices() resolves service references to service instances in nested arrays'); - -// ->merge() -$t->diag('->merge()'); -$container = new Builder(); -$container->merge(null); -$t->is($container->getParameters(), array(), '->merge() accepts null as an argument'); -$t->is($container->getDefinitions(), array(), '->merge() accepts null as an argument'); - -$container = new Builder(array('bar' => 'foo')); -$config = new BuilderConfiguration(); -$config->setParameters(array('foo' => 'bar')); -$container->merge($config); -$t->is($container->getParameters(), array('bar' => 'foo', 'foo' => 'bar'), '->merge() merges current parameters with the loaded ones'); - -$container = new Builder(array('bar' => 'foo', 'foo' => 'baz')); -$config = new BuilderConfiguration(); -$config->setParameters(array('foo' => 'bar')); -$container->merge($config); -$t->is($container->getParameters(), array('bar' => 'foo', 'foo' => 'baz'), '->merge() does not change the already defined parameters'); - -$container = new Builder(array('bar' => 'foo')); -$config = new BuilderConfiguration(); -$config->setParameters(array('foo' => '%bar%')); -$container->merge($config); -$t->is($container->getParameters(), array('bar' => 'foo', 'foo' => 'foo'), '->merge() evaluates the values of the parameters towards already defined ones'); - -$container = new Builder(array('bar' => 'foo')); -$config = new BuilderConfiguration(); -$config->setParameters(array('foo' => '%bar%', 'baz' => '%foo%')); -$container->merge($config); -$t->is($container->getParameters(), array('bar' => 'foo', 'foo' => 'foo', 'baz' => 'foo'), '->merge() evaluates the values of the parameters towards already defined ones'); - -$container = new Builder(); -$container->register('foo', 'FooClass'); -$container->register('bar', 'BarClass'); -$config = new BuilderConfiguration(); -$config->setDefinition('baz', new Definition('BazClass')); -$config->setAlias('alias_for_foo', 'foo'); -$container->merge($config); -$t->is(array_keys($container->getDefinitions()), array('foo', 'bar', 'baz'), '->merge() merges definitions already defined ones'); -$t->is($container->getAliases(), array('alias_for_foo' => 'foo'), '->merge() registers defined aliases'); - -$container = new Builder(); -$container->register('foo', 'FooClass'); -$config->setDefinition('foo', new Definition('BazClass')); -$container->merge($config); -$t->is($container->getDefinition('foo')->getClass(), 'BazClass', '->merge() overrides already defined services'); - -// ->findAnnotatedServiceIds() -$t->diag('->findAnnotatedServiceIds()'); -$builder = new Builder(); -$builder - ->register('foo', 'FooClass') - ->addAnnotation('foo', array('foo' => 'foo')) - ->addAnnotation('bar', array('bar' => 'bar')) - ->addAnnotation('foo', array('foofoo' => 'foofoo')) -; -$t->is($builder->findAnnotatedServiceIds('foo'), array( - 'foo' => array( - array('foo' => 'foo'), - array('foofoo' => 'foofoo'), - ) -), '->findAnnotatedServiceIds() returns an array of service ids and its annotation attributes'); -$t->is($builder->findAnnotatedServiceIds('foobar'), array(), '->findAnnotatedServiceIds() returns an empty array if there is annotated services'); diff --git a/tests/unit/Symfony/Components/DependencyInjection/ContainerTest.php b/tests/unit/Symfony/Components/DependencyInjection/ContainerTest.php deleted file mode 100644 index 6cd5bb3373..0000000000 --- a/tests/unit/Symfony/Components/DependencyInjection/ContainerTest.php +++ /dev/null @@ -1,207 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -require_once __DIR__.'/../../../bootstrap.php'; - -use Symfony\Components\DependencyInjection\Container; - -$fixturesPath = __DIR__.'/../../../../fixtures/Symfony/Components/DependencyInjection/'; - -$t = new LimeTest(42); - -// __construct() -$t->diag('__construct()'); -$sc = new Container(); -$t->is(spl_object_hash($sc->getService('service_container')), spl_object_hash($sc), '__construct() automatically registers itself as a service'); - -$sc = new Container(array('foo' => 'bar')); -$t->is($sc->getParameters(), array('foo' => 'bar'), '__construct() takes an array of parameters as its first argument'); - -// ->setParameters() ->getParameters() -$t->diag('->setParameters() ->getParameters()'); - -$sc = new Container(); -$t->is($sc->getParameters(), array(), '->getParameters() returns an empty array if no parameter has been defined'); - -$sc->setParameters(array('foo' => 'bar')); -$t->is($sc->getParameters(), array('foo' => 'bar'), '->setParameters() sets the parameters'); - -$sc->setParameters(array('bar' => 'foo')); -$t->is($sc->getParameters(), array('bar' => 'foo'), '->setParameters() overrides the previous defined parameters'); - -$sc->setParameters(array('Bar' => 'foo')); -$t->is($sc->getParameters(), array('bar' => 'foo'), '->setParameters() converts the key to lowercase'); - -// ->setParameter() ->getParameter() -$t->diag('->setParameter() ->getParameter() '); - -$sc = new Container(array('foo' => 'bar')); -$sc->setParameter('bar', 'foo'); -$t->is($sc->getParameter('bar'), 'foo', '->setParameter() sets the value of a new parameter'); -$t->is($sc['bar'], 'foo', '->offsetGet() gets the value of a parameter'); - -$sc['bar1'] = 'foo1'; -$t->is($sc['bar1'], 'foo1', '->offsetset() sets the value of a parameter'); - -unset($sc['bar1']); -$t->ok(!isset($sc['bar1']), '->offsetUnset() removes a parameter'); - -$sc->setParameter('foo', 'baz'); -$t->is($sc->getParameter('foo'), 'baz', '->setParameter() overrides previously set parameter'); - -$sc->setParameter('Foo', 'baz1'); -$t->is($sc->getParameter('foo'), 'baz1', '->setParameter() converts the key to lowercase'); -$t->is($sc->getParameter('FOO'), 'baz1', '->getParameter() converts the key to lowercase'); -$t->is($sc['FOO'], 'baz1', '->offsetGet() converts the key to lowercase'); - -try -{ - $sc->getParameter('baba'); - $t->fail('->getParameter() thrown an \InvalidArgumentException if the key does not exist'); -} -catch (\InvalidArgumentException $e) -{ - $t->pass('->getParameter() thrown an \InvalidArgumentException if the key does not exist'); -} - -try -{ - $sc['baba']; - $t->fail('->offsetGet() thrown an \InvalidArgumentException if the key does not exist'); -} -catch (\InvalidArgumentException $e) -{ - $t->pass('->offsetGet() thrown an \InvalidArgumentException if the key does not exist'); -} - -// ->hasParameter() -$t->diag('->hasParameter()'); -$sc = new Container(array('foo' => 'bar')); -$t->ok($sc->hasParameter('foo'), '->hasParameter() returns true if a parameter is defined'); -$t->ok($sc->hasParameter('Foo'), '->hasParameter() converts the key to lowercase'); -$t->ok(isset($sc['Foo']), '->offsetExists() converts the key to lowercase'); -$t->ok(!$sc->hasParameter('bar'), '->hasParameter() returns false if a parameter is not defined'); -$t->ok(isset($sc['foo']), '->offsetExists() returns true if a parameter is defined'); -$t->ok(!isset($sc['bar']), '->offsetExists() returns false if a parameter is not defined'); - -// ->addParameters() -$t->diag('->addParameters()'); -$sc = new Container(array('foo' => 'bar')); -$sc->addParameters(array('bar' => 'foo')); -$t->is($sc->getParameters(), array('foo' => 'bar', 'bar' => 'foo'), '->addParameters() adds parameters to the existing ones'); -$sc->addParameters(array('Bar' => 'fooz')); -$t->is($sc->getParameters(), array('foo' => 'bar', 'bar' => 'fooz'), '->addParameters() converts keys to lowercase'); - -// ->setService() ->hasService() ->getService() -$t->diag('->setService() ->hasService() ->getService()'); -$sc = new Container(); -$sc->setService('foo', $obj = new stdClass()); -$t->is(spl_object_hash($sc->getService('foo')), spl_object_hash($obj), '->setService() registers a service under a key name'); - -$sc->foo1 = $obj1 = new stdClass(); -$t->is(spl_object_hash($sc->foo1), spl_object_hash($obj1), '->__set() sets a service'); - -$t->is(spl_object_hash($sc->foo), spl_object_hash($obj), '->__get() gets a service by name'); -$t->ok($sc->hasService('foo'), '->hasService() returns true if the service is defined'); -$t->ok(isset($sc->foo), '->__isset() returns true if the service is defined'); -$t->ok(!$sc->hasService('bar'), '->hasService() returns false if the service is not defined'); -$t->ok(!isset($sc->bar), '->__isset() returns false if the service is not defined'); - -// ->getServiceIds() -$t->diag('->getServiceIds()'); -$sc = new Container(); -$sc->setService('foo', $obj = new stdClass()); -$sc->setService('bar', $obj = new stdClass()); -$t->is($sc->getServiceIds(), array('service_container', 'foo', 'bar'), '->getServiceIds() returns all defined service ids'); - -class ProjectServiceContainer extends Container -{ - public $__bar, $__foo_bar, $__foo_baz; - - public function __construct() - { - parent::__construct(); - - $this->__bar = new stdClass(); - $this->__foo_bar = new stdClass(); - $this->__foo_baz = new stdClass(); - } - - protected function getBarService() - { - return $this->__bar; - } - - protected function getFooBarService() - { - return $this->__foo_bar; - } - - protected function getFoo_BazService() - { - return $this->__foo_baz; - } -} - -$sc = new ProjectServiceContainer(); -$t->is(spl_object_hash($sc->getService('bar')), spl_object_hash($sc->__bar), '->getService() looks for a getXXXService() method'); -$t->ok($sc->hasService('bar'), '->hasService() returns true if the service has been defined as a getXXXService() method'); - -$sc->setService('bar', $bar = new stdClass()); -$t->isnt(spl_object_hash($sc->getService('bar')), spl_object_hash($bar), '->getService() prefers to return a service defined with a getXXXService() method than one defined with setService()'); - -try -{ - $sc->getService('baba'); - $t->fail('->getService() thrown an \InvalidArgumentException if the service does not exist'); -} -catch (\InvalidArgumentException $e) -{ - $t->pass('->getService() thrown an \InvalidArgumentException if the service does not exist'); -} - -try -{ - $sc->baba; - $t->fail('->__get() thrown an \InvalidArgumentException if the service does not exist'); -} -catch (\InvalidArgumentException $e) -{ - $t->pass('->__get() thrown an \InvalidArgumentException if the service does not exist'); -} - -try -{ - unset($sc->baba); - $t->fail('->__unset() thrown an LogicException if you try to remove a service'); -} -catch (LogicException $e) -{ - $t->pass('->__unset() thrown an LogicException if you try to remove a service'); -} - -$t->is(spl_object_hash($sc->getService('foo_bar')), spl_object_hash($sc->__foo_bar), '->getService() camelizes the service id when looking for a method'); -$t->is(spl_object_hash($sc->getService('foo.baz')), spl_object_hash($sc->__foo_baz), '->getService() camelizes the service id when looking for a method'); - -// __call() -$t->diag('__call()'); -$sc = new Container(); -$sc->setService('foo_bar.foo', $foo = new stdClass()); -$t->is($sc->getFooBar_FooService(), $foo, '__call() finds services is the method is getXXXService()'); - -try -{ - $sc->getFooBar_Foo(); - $t->fail('__call() throws a \RuntimeException exception if the method is not a service method'); -} -catch (\RuntimeException $e) -{ - $t->pass('__call() throws a \RuntimeException exception if the method is not a service method'); -} diff --git a/tests/unit/Symfony/Components/DependencyInjection/CrossCheckTest.php b/tests/unit/Symfony/Components/DependencyInjection/CrossCheckTest.php deleted file mode 100644 index 4c55dc08af..0000000000 --- a/tests/unit/Symfony/Components/DependencyInjection/CrossCheckTest.php +++ /dev/null @@ -1,78 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -require_once __DIR__.'/../../../bootstrap.php'; - -use Symfony\Components\DependencyInjection\Builder; - -$fixturesPath = realpath(__DIR__.'/../../../../fixtures/Symfony/Components/DependencyInjection/'); - -require_once $fixturesPath.'/includes/classes.php'; -require_once $fixturesPath.'/includes/foo.php'; - -$t = new LimeTest(30); - -// cross-check loaders/dumpers -$t->diag('cross-check loaders/dumpers'); - -$fixtures = array( - 'services1.xml' => 'xml', - 'services2.xml' => 'xml', - 'services6.xml' => 'xml', - 'services8.xml' => 'xml', - 'services9.xml' => 'xml', - - 'services1.yml' => 'yaml', - 'services2.yml' => 'yaml', - 'services6.yml' => 'yaml', - 'services8.yml' => 'yaml', - 'services9.yml' => 'yaml', -); - -foreach ($fixtures as $fixture => $type) -{ - $loaderClass = 'Symfony\\Components\\DependencyInjection\\Loader\\'.ucfirst($type).'FileLoader'; - $dumperClass = 'Symfony\\Components\\DependencyInjection\\Dumper\\'.ucfirst($type).'Dumper'; - - $container1 = new Builder(); - $loader1 = new $loaderClass($container1); - $loader1->load($fixturesPath.'/'.$type.'/'.$fixture); - $container1->setParameter('path', $fixturesPath.'/includes'); - - $dumper = new $dumperClass($container1); - $tmp = tempnam('sf_service_container', 'sf'); - file_put_contents($tmp, $dumper->dump()); - - $container2 = new Builder(); - $loader2 = new $loaderClass($container2); - $loader2->load($tmp); - $container2->setParameter('path', $fixturesPath.'/includes'); - - unlink($tmp); - - $t->is(serialize($container1), serialize($container2), 'loading a dump from a previously loaded container returns the same container'); - - $t->is($container1->getParameters(), $container2->getParameters(), '->getParameters() returns the same value for both containers'); - - $services1 = array(); - foreach ($container1 as $id => $service) - { - $services1[$id] = serialize($service); - } - $services2 = array(); - foreach ($container2 as $id => $service) - { - $services2[$id] = serialize($service); - } - - unset($services1['service_container'], $services2['service_container']); - - $t->is($services1, $services2, 'Iterator on the containers returns the same services'); -} diff --git a/tests/unit/Symfony/Components/DependencyInjection/DefinitionTest.php b/tests/unit/Symfony/Components/DependencyInjection/DefinitionTest.php deleted file mode 100644 index 1cb1f8d777..0000000000 --- a/tests/unit/Symfony/Components/DependencyInjection/DefinitionTest.php +++ /dev/null @@ -1,84 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -require_once __DIR__.'/../../../bootstrap.php'; - -use Symfony\Components\DependencyInjection\Definition; - -$t = new LimeTest(25); - -// __construct() -$t->diag('__construct()'); - -$def = new Definition('stdClass'); -$t->is($def->getClass(), 'stdClass', '__construct() takes the class name as its first argument'); - -$def = new Definition('stdClass', array('foo')); -$t->is($def->getArguments(), array('foo'), '__construct() takes an optional array of arguments as its second argument'); - -// ->setConstructor() ->getConstructor() -$t->diag('->setConstructor() ->getConstructor()'); -$def = new Definition('stdClass'); -$t->is(spl_object_hash($def->setConstructor('foo')), spl_object_hash($def), '->setConstructor() implements a fluent interface'); -$t->is($def->getConstructor(), 'foo', '->getConstructor() returns the constructor name'); - -// ->setClass() ->getClass() -$t->diag('->setClass() ->getClass()'); -$def = new Definition('stdClass'); -$t->is(spl_object_hash($def->setClass('foo')), spl_object_hash($def), '->setClass() implements a fluent interface'); -$t->is($def->getClass(), 'foo', '->getClass() returns the class name'); - -// ->setArguments() ->getArguments() ->addArgument() -$t->diag('->setArguments() ->getArguments() ->addArgument()'); -$def = new Definition('stdClass'); -$t->is(spl_object_hash($def->setArguments(array('foo'))), spl_object_hash($def), '->setArguments() implements a fluent interface'); -$t->is($def->getArguments(), array('foo'), '->getArguments() returns the arguments'); -$t->is(spl_object_hash($def->addArgument('bar')), spl_object_hash($def), '->addArgument() implements a fluent interface'); -$t->is($def->getArguments(), array('foo', 'bar'), '->addArgument() adds an argument'); - -// ->setMethodCalls() ->getMethodCalls() ->addMethodCall() -$t->diag('->setMethodCalls() ->getMethodCalls() ->addMethodCall()'); -$def = new Definition('stdClass'); -$t->is(spl_object_hash($def->setMethodCalls(array(array('foo', array('foo'))))), spl_object_hash($def), '->setMethodCalls() implements a fluent interface'); -$t->is($def->getMethodCalls(), array(array('foo', array('foo'))), '->getMethodCalls() returns the methods to call'); -$t->is(spl_object_hash($def->addMethodCall('bar', array('bar'))), spl_object_hash($def), '->addMethodCall() implements a fluent interface'); -$t->is($def->getMethodCalls(), array(array('foo', array('foo')), array('bar', array('bar'))), '->addMethodCall() adds a method to call'); - -// ->setFile() ->getFile() -$t->diag('->setFile() ->getFile()'); -$def = new Definition('stdClass'); -$t->is(spl_object_hash($def->setFile('foo')), spl_object_hash($def), '->setFile() implements a fluent interface'); -$t->is($def->getFile(), 'foo', '->getFile() returns the file to include'); - -// ->setShared() ->isShared() -$t->diag('->setShared() ->isShared()'); -$def = new Definition('stdClass'); -$t->is($def->isShared(), true, '->isShared() returns true by default'); -$t->is(spl_object_hash($def->setShared(false)), spl_object_hash($def), '->setShared() implements a fluent interface'); -$t->is($def->isShared(), false, '->isShared() returns false if the instance must not be shared'); - -// ->setConfigurator() ->getConfigurator() -$t->diag('->setConfigurator() ->getConfigurator()'); -$def = new Definition('stdClass'); -$t->is(spl_object_hash($def->setConfigurator('foo')), spl_object_hash($def), '->setConfigurator() implements a fluent interface'); -$t->is($def->getConfigurator(), 'foo', '->getConfigurator() returns the configurator'); - -// ->getAnnotations() ->getAnnotation() ->addAnnotation() -$t->diag('->getAnnotations() ->getAnnotation() ->addAnnotation()'); -$def = new Definition('stdClass'); -$t->is(spl_object_hash($def->addAnnotation('foo')), spl_object_hash($def), '->addAnnotation() implements a fluent interface'); -$t->is($def->getAnnotation('foo'), array(array()), '->getAnnotation() returns attributes for an annotation name'); -$def->addAnnotation('foo', array('foo' => 'bar')); -$t->is($def->getAnnotation('foo'), array(array(), array('foo' => 'bar')), '->addAnnotation() can adds the same annotation several times'); -$def->addAnnotation('bar', array('bar' => 'bar')); -$t->is($def->getAnnotations(), array( - 'foo' => array(array(), array('foo' => 'bar')), - 'bar' => array(array('bar' => 'bar')), -), '->getAnnotations() returns all annotations'); diff --git a/tests/unit/Symfony/Components/DependencyInjection/Dumper/DumperTest.php b/tests/unit/Symfony/Components/DependencyInjection/Dumper/DumperTest.php deleted file mode 100644 index e1b5ac5123..0000000000 --- a/tests/unit/Symfony/Components/DependencyInjection/Dumper/DumperTest.php +++ /dev/null @@ -1,32 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -require_once __DIR__.'/../../../../bootstrap.php'; - -use Symfony\Components\DependencyInjection\Builder; -use Symfony\Components\DependencyInjection\Dumper\Dumper; - -$t = new LimeTest(1); - -class ProjectDumper extends Dumper -{ -} - -$builder = new Builder(); -$dumper = new ProjectDumper($builder); -try -{ - $dumper->dump(); - $t->fail('->dump() returns a LogicException if the dump() method has not been overriden by a children class'); -} -catch (LogicException $e) -{ - $t->pass('->dump() returns a LogicException if the dump() method has not been overriden by a children class'); -} diff --git a/tests/unit/Symfony/Components/DependencyInjection/Dumper/GraphvizDumperTest.php b/tests/unit/Symfony/Components/DependencyInjection/Dumper/GraphvizDumperTest.php deleted file mode 100644 index a46fa1e731..0000000000 --- a/tests/unit/Symfony/Components/DependencyInjection/Dumper/GraphvizDumperTest.php +++ /dev/null @@ -1,46 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -require_once __DIR__.'/../../../../bootstrap.php'; - -use Symfony\Components\DependencyInjection\Builder; -use Symfony\Components\DependencyInjection\Dumper\GraphvizDumper; - -$t = new LimeTest(4); - -$fixturesPath = __DIR__.'/../../../../../fixtures/Symfony/Components/DependencyInjection/'; - -// ->dump() -$t->diag('->dump()'); -$dumper = new GraphvizDumper($container = new Builder()); - -$t->is($dumper->dump(), file_get_contents($fixturesPath.'/graphviz/services1.dot'), '->dump() dumps an empty container as an empty dot file'); - -$container = new Builder(); -$dumper = new GraphvizDumper($container); - -$container = include $fixturesPath.'/containers/container9.php'; -$dumper = new GraphvizDumper($container); -$t->is($dumper->dump(), str_replace('%path%', __DIR__, file_get_contents($fixturesPath.'/graphviz/services9.dot')), '->dump() dumps services'); - -$container = include $fixturesPath.'/containers/container10.php'; -$dumper = new GraphvizDumper($container); -$t->is($dumper->dump(), str_replace('%path%', __DIR__, file_get_contents($fixturesPath.'/graphviz/services10.dot')), '->dump() dumps services'); - -$container = include $fixturesPath.'/containers/container10.php'; -$dumper = new GraphvizDumper($container); -$t->is($dumper->dump(array( - 'graph' => array('ratio' => 'normal'), - 'node' => array('fontsize' => 13, 'fontname' => 'Verdana', 'shape' => 'square'), - 'edge' => array('fontsize' => 12, 'fontname' => 'Verdana', 'color' => 'white', 'arrowhead' => 'closed', 'arrowsize' => 1), - 'node.instance' => array('fillcolor' => 'green', 'style' => 'empty'), - 'node.definition' => array('fillcolor' => 'grey'), - 'node.missing' => array('fillcolor' => 'red', 'style' => 'empty'), -)), str_replace('%path%', __DIR__, file_get_contents($fixturesPath.'/graphviz/services10-1.dot')), '->dump() dumps services'); diff --git a/tests/unit/Symfony/Components/DependencyInjection/Dumper/PhpDumperTest.php b/tests/unit/Symfony/Components/DependencyInjection/Dumper/PhpDumperTest.php deleted file mode 100644 index 326b85c4a3..0000000000 --- a/tests/unit/Symfony/Components/DependencyInjection/Dumper/PhpDumperTest.php +++ /dev/null @@ -1,52 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -require_once __DIR__.'/../../../../bootstrap.php'; - -use Symfony\Components\DependencyInjection\Builder; -use Symfony\Components\DependencyInjection\Dumper\PhpDumper; - -$t = new LimeTest(5); - -$fixturesPath = realpath(__DIR__.'/../../../../../fixtures/Symfony/Components/DependencyInjection/'); - -// ->dump() -$t->diag('->dump()'); -$dumper = new PhpDumper($container = new Builder()); - -$t->is($dumper->dump(), file_get_contents($fixturesPath.'/php/services1.php'), '->dump() dumps an empty container as an empty PHP class'); -$t->is($dumper->dump(array('class' => 'Container', 'base_class' => 'AbstractContainer')), file_get_contents($fixturesPath.'/php/services1-1.php'), '->dump() takes a class and a base_class options'); - -$container = new Builder(); -$dumper = new PhpDumper($container); - -// ->addParameters() -$t->diag('->addParameters()'); -$container = include $fixturesPath.'/containers/container8.php'; -$dumper = new PhpDumper($container); -$t->is($dumper->dump(), file_get_contents($fixturesPath.'/php/services8.php'), '->dump() dumps parameters'); - -// ->addService() -$t->diag('->addService()'); -$container = include $fixturesPath.'/containers/container9.php'; -$dumper = new PhpDumper($container); -$t->is($dumper->dump(), str_replace('%path%', $fixturesPath.'/includes', file_get_contents($fixturesPath.'/php/services9.php')), '->dump() dumps services'); - -$dumper = new PhpDumper($container = new Builder()); -$container->register('foo', 'FooClass')->addArgument(new stdClass()); -try -{ - $dumper->dump(); - $t->fail('->dump() throws a RuntimeException if the container to be dumped has reference to objects or resources'); -} -catch (RuntimeException $e) -{ - $t->pass('->dump() throws a RuntimeException if the container to be dumped has reference to objects or resources'); -} diff --git a/tests/unit/Symfony/Components/DependencyInjection/Dumper/XmlDumperTest.php b/tests/unit/Symfony/Components/DependencyInjection/Dumper/XmlDumperTest.php deleted file mode 100644 index 9f49c8ee54..0000000000 --- a/tests/unit/Symfony/Components/DependencyInjection/Dumper/XmlDumperTest.php +++ /dev/null @@ -1,51 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -require_once __DIR__.'/../../../../bootstrap.php'; - -use Symfony\Components\DependencyInjection\Builder; -use Symfony\Components\DependencyInjection\Dumper\XmlDumper; - -$t = new LimeTest(4); - -$fixturesPath = realpath(__DIR__.'/../../../../../fixtures/Symfony/Components/DependencyInjection/'); - -// ->dump() -$t->diag('->dump()'); -$dumper = new XmlDumper($container = new Builder()); - -$t->is($dumper->dump(), file_get_contents($fixturesPath.'/xml/services1.xml'), '->dump() dumps an empty container as an empty XML file'); - -$container = new Builder(); -$dumper = new XmlDumper($container); - -// ->addParameters() -$t->diag('->addParameters()'); -$container = include $fixturesPath.'//containers/container8.php'; -$dumper = new XmlDumper($container); -$t->is($dumper->dump(), file_get_contents($fixturesPath.'/xml/services8.xml'), '->dump() dumps parameters'); - -// ->addService() -$t->diag('->addService()'); -$container = include $fixturesPath.'/containers/container9.php'; -$dumper = new XmlDumper($container); -$t->is($dumper->dump(), str_replace('%path%', $fixturesPath.'/includes', file_get_contents($fixturesPath.'/xml/services9.xml')), '->dump() dumps services'); - -$dumper = new XmlDumper($container = new Builder()); -$container->register('foo', 'FooClass')->addArgument(new stdClass()); -try -{ - $dumper->dump(); - $t->fail('->dump() throws a RuntimeException if the container to be dumped has reference to objects or resources'); -} -catch (RuntimeException $e) -{ - $t->pass('->dump() throws a RuntimeException if the container to be dumped has reference to objects or resources'); -} diff --git a/tests/unit/Symfony/Components/DependencyInjection/Dumper/YamlDumperTest.php b/tests/unit/Symfony/Components/DependencyInjection/Dumper/YamlDumperTest.php deleted file mode 100644 index 19d5764f37..0000000000 --- a/tests/unit/Symfony/Components/DependencyInjection/Dumper/YamlDumperTest.php +++ /dev/null @@ -1,51 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -require_once __DIR__.'/../../../../bootstrap.php'; - -use Symfony\Components\DependencyInjection\Builder; -use Symfony\Components\DependencyInjection\Dumper\YamlDumper; - -$t = new LimeTest(4); - -$fixturesPath = realpath(__DIR__.'/../../../../../fixtures/Symfony/Components/DependencyInjection/'); - -// ->dump() -$t->diag('->dump()'); -$dumper = new YamlDumper($container = new Builder()); - -$t->is($dumper->dump(), file_get_contents($fixturesPath.'/yaml/services1.yml'), '->dump() dumps an empty container as an empty YAML file'); - -$container = new Builder(); -$dumper = new YamlDumper($container); - -// ->addParameters() -$t->diag('->addParameters()'); -$container = include $fixturesPath.'/containers/container8.php'; -$dumper = new YamlDumper($container); -$t->is($dumper->dump(), file_get_contents($fixturesPath.'/yaml/services8.yml'), '->dump() dumps parameters'); - -// ->addService() -$t->diag('->addService()'); -$container = include $fixturesPath.'/containers/container9.php'; -$dumper = new YamlDumper($container); -$t->is($dumper->dump(), str_replace('%path%', $fixturesPath.'/includes', file_get_contents($fixturesPath.'/yaml/services9.yml')), '->dump() dumps services'); - -$dumper = new YamlDumper($container = new Builder()); -$container->register('foo', 'FooClass')->addArgument(new stdClass()); -try -{ - $dumper->dump(); - $t->fail('->dump() throws a RuntimeException if the container to be dumped has reference to objects or resources'); -} -catch (RuntimeException $e) -{ - $t->pass('->dump() throws a RuntimeException if the container to be dumped has reference to objects or resources'); -} diff --git a/tests/unit/Symfony/Components/DependencyInjection/FileResourceTest.php b/tests/unit/Symfony/Components/DependencyInjection/FileResourceTest.php deleted file mode 100644 index 8113de5152..0000000000 --- a/tests/unit/Symfony/Components/DependencyInjection/FileResourceTest.php +++ /dev/null @@ -1,31 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -require_once __DIR__.'/../../../bootstrap.php'; - -use Symfony\Components\DependencyInjection\FileResource; - -$t = new LimeTest(4); - -// ->getResource() -$t->diag('->getResource()'); -$file = sys_get_temp_dir().'/tmp.xml'; -touch($file); -$resource = new FileResource($file); -$t->is($resource->getResource(), $file, '->getResource() returns the path to the resource'); - -// ->isUptodate() -$t->diag('->isUptodate()'); -$t->ok($resource->isUptodate(time() + 10), '->isUptodate() returns true if the resource has not changed'); -$t->ok(!$resource->isUptodate(time() - 86400), '->isUptodate() returns false if the resource has been updated'); -unlink($file); - -$resource = new FileResource('/____foo/foobar'.rand(1, 999999)); -$t->ok(!$resource->isUptodate(time()), '->isUptodate() returns false if the resource does not exist'); diff --git a/tests/unit/Symfony/Components/DependencyInjection/Loader/FileLoaderTest.php b/tests/unit/Symfony/Components/DependencyInjection/Loader/FileLoaderTest.php deleted file mode 100644 index b0e6ed5b74..0000000000 --- a/tests/unit/Symfony/Components/DependencyInjection/Loader/FileLoaderTest.php +++ /dev/null @@ -1,52 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -require_once __DIR__.'/../../../../bootstrap.php'; - -use Symfony\Components\DependencyInjection\Builder; -use Symfony\Components\DependencyInjection\Loader\FileLoader; - -$t = new LimeTest(9); - -class ProjectLoader extends FileLoader -{ - public $paths; - - public function load($resource) - { - } - - public function getAbsolutePath($file, $currentPath = null) - { - return parent::getAbsolutePath($file, $currentPath); - } -} - -// __construct() -$t->diag('__construct()'); -$loader = new ProjectLoader(__DIR__); -$t->is($loader->paths, array(__DIR__), '__construct() takes a path as its second argument'); - -$loader = new ProjectLoader(array(__DIR__, __DIR__)); -$t->is($loader->paths, array(__DIR__, __DIR__), '__construct() takes an array of paths as its second argument'); - -// ->getAbsolutePath() -$t->diag('->getAbsolutePath()'); -$loader = new ProjectLoader(array(__DIR__.'/../../../../../bin')); -$t->is($loader->getAbsolutePath('/foo.xml'), '/foo.xml', '->getAbsolutePath() return the path unmodified if it is already an absolute path'); -$t->is($loader->getAbsolutePath('c:\\\\foo.xml'), 'c:\\\\foo.xml', '->getAbsolutePath() return the path unmodified if it is already an absolute path'); -$t->is($loader->getAbsolutePath('c:/foo.xml'), 'c:/foo.xml', '->getAbsolutePath() return the path unmodified if it is already an absolute path'); -$t->is($loader->getAbsolutePath('\\server\\foo.xml'), '\\server\\foo.xml', '->getAbsolutePath() return the path unmodified if it is already an absolute path'); - -$t->is($loader->getAbsolutePath('FileLoaderTest.php', __DIR__), __DIR__.'/FileLoaderTest.php', '->getAbsolutePath() returns an absolute filename if the file exists in the current path'); - -$t->is($loader->getAbsolutePath('prove.php', __DIR__), __DIR__.'/../../../../../bin/prove.php', '->getAbsolutePath() returns an absolute filename if the file exists in one of the paths given in the constructor'); - -$t->is($loader->getAbsolutePath('foo.xml', __DIR__), 'foo.xml', '->getAbsolutePath() returns the path unmodified if it is unable to find it in the given paths'); diff --git a/tests/unit/Symfony/Components/DependencyInjection/Loader/IniLoaderTest.php b/tests/unit/Symfony/Components/DependencyInjection/Loader/IniLoaderTest.php deleted file mode 100644 index 9a44ea9e7f..0000000000 --- a/tests/unit/Symfony/Components/DependencyInjection/Loader/IniLoaderTest.php +++ /dev/null @@ -1,42 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -require_once __DIR__.'/../../../../bootstrap.php'; - -use Symfony\Components\DependencyInjection\Builder; -use Symfony\Components\DependencyInjection\Loader\IniFileLoader; - -$t = new LimeTest(3); - -$fixturesPath = realpath(__DIR__.'/../../../../../fixtures/Symfony/Components/DependencyInjection/'); - -$loader = new IniFileLoader($fixturesPath.'/ini'); -$config = $loader->load('parameters.ini'); -$t->is($config->getParameters(), array('foo' => 'bar', 'bar' => '%foo%'), '->load() takes a single file name as its first argument'); - -try -{ - $loader->load('foo.ini'); - $t->fail('->load() throws an InvalidArgumentException if the loaded file does not exist'); -} -catch (InvalidArgumentException $e) -{ - $t->pass('->load() throws an InvalidArgumentException if the loaded file does not exist'); -} - -try -{ - @$loader->load('nonvalid.ini'); - $t->fail('->load() throws an InvalidArgumentException if the loaded file is not parseable'); -} -catch (InvalidArgumentException $e) -{ - $t->pass('->load() throws an InvalidArgumentException if the loaded file is not parseable'); -} diff --git a/tests/unit/Symfony/Components/DependencyInjection/Loader/LoaderExtensionTest.php b/tests/unit/Symfony/Components/DependencyInjection/Loader/LoaderExtensionTest.php deleted file mode 100644 index 3f0361d4d9..0000000000 --- a/tests/unit/Symfony/Components/DependencyInjection/Loader/LoaderExtensionTest.php +++ /dev/null @@ -1,32 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -require_once __DIR__.'/../../../../bootstrap.php'; - -require_once __DIR__.'/../../../../../fixtures/Symfony/Components/DependencyInjection/includes/ProjectExtension.php'; - -$t = new LimeTest(2); - -// ->load() -$t->diag('->load()'); -$extension = new ProjectExtension(); - -try -{ - $extension->load('foo', array()); - $t->fail('->load() throws an InvalidArgumentException if the tag does not exist'); -} -catch (InvalidArgumentException $e) -{ - $t->pass('->load() throws an InvalidArgumentException if the tag does not exist'); -} - -$config = $extension->load('bar', array('foo' => 'bar')); -$t->is($config->getParameters(), array('project.parameter.bar' => 'bar'), '->load() calls the method tied to the given tag'); diff --git a/tests/unit/Symfony/Components/DependencyInjection/Loader/XmlFileLoaderTest.php b/tests/unit/Symfony/Components/DependencyInjection/Loader/XmlFileLoaderTest.php deleted file mode 100644 index 19aabf0248..0000000000 --- a/tests/unit/Symfony/Components/DependencyInjection/Loader/XmlFileLoaderTest.php +++ /dev/null @@ -1,182 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -require_once __DIR__.'/../../../../bootstrap.php'; - -use Symfony\Components\DependencyInjection\Builder; -use Symfony\Components\DependencyInjection\Reference; -use Symfony\Components\DependencyInjection\Definition; -use Symfony\Components\DependencyInjection\Loader\Loader; -use Symfony\Components\DependencyInjection\Loader\XmlFileLoader; - -$t = new LimeTest(40); - -$fixturesPath = realpath(__DIR__.'/../../../../../fixtures/Symfony/Components/DependencyInjection/'); - -require_once $fixturesPath.'/includes/ProjectExtension.php'; - -class ProjectLoader extends XmlFileLoader -{ - public function parseFile($file) - { - return parent::parseFile($file); - } -} - -// ->load() -$t->diag('->load()'); - -$loader = new ProjectLoader($fixturesPath.'/ini'); - -try -{ - $loader->load('foo.xml'); - $t->fail('->load() throws an InvalidArgumentException if the loaded file does not exist'); -} -catch (InvalidArgumentException $e) -{ - $t->pass('->load() throws an InvalidArgumentException if the loaded file does not exist'); -} - -// ->parseFile() -$t->diag('->parseFile()'); - -try -{ - $loader->parseFile($fixturesPath.'/ini/parameters.ini'); - $t->fail('->parseFile() throws an InvalidArgumentException if the loaded file is not a valid XML file'); -} -catch (InvalidArgumentException $e) -{ - $t->pass('->parseFile() throws an InvalidArgumentException if the loaded file is not a valid XML file'); -} - -$loader = new ProjectLoader($fixturesPath.'/xml'); - -try -{ - $loader->parseFile($fixturesPath.'/xml/nonvalid.xml'); - $t->fail('->parseFile() throws an InvalidArgumentException if the loaded file does not validate the XSD'); -} -catch (InvalidArgumentException $e) -{ - $t->pass('->parseFile() throws an InvalidArgumentException if the loaded file does not validate the XSD'); -} - -$xml = $loader->parseFile($fixturesPath.'/xml/services1.xml'); -$t->is(get_class($xml), 'Symfony\\Components\\DependencyInjection\\SimpleXMLElement', '->parseFile() returns an SimpleXMLElement object'); - -// ->load() # parameters -$t->diag('->load() # parameters'); -$loader = new ProjectLoader($fixturesPath.'/xml'); -$config = $loader->load('services2.xml'); -$t->is($config->getParameters(), array('a string', 'foo' => 'bar', 'values' => array(0, 'integer' => 4, 100 => null, 'true', true, false, 'on', 'off', 'float' => 1.3, 1000.3, 'a string', array('foo', 'bar')), 'foo_bar' => new Reference('foo_bar')), '->load() converts XML values to PHP ones'); - -// ->load() # imports -$t->diag('->load() # imports'); -$config = $loader->load('services4.xml'); -$t->is($config->getParameters(), array('a string', 'foo' => 'bar', 'bar' => '%foo%', 'values' => array(true, false), 'foo_bar' => new Reference('foo_bar'), 'imported_from_yaml' => true, 'imported_from_ini' => true), '->load() imports and merges imported files'); - -// ->load() # anonymous services -$t->diag('->load() # anonymous services'); -$config = $loader->load('services5.xml'); -$services = $config->getDefinitions(); -$t->is(count($services), 3, '->load() attributes unique ids to anonymous services'); -$args = $services['foo']->getArguments(); -$t->is(count($args), 1, '->load() references anonymous services as "normal" ones'); -$t->is(get_class($args[0]), 'Symfony\\Components\\DependencyInjection\\Reference', '->load() converts anonymous services to references to "normal" services'); -$t->ok(isset($services[(string) $args[0]]), '->load() makes a reference to the created ones'); -$inner = $services[(string) $args[0]]; -$t->is($inner->getClass(), 'BarClass', '->load() uses the same configuration as for the anonymous ones'); - -$args = $inner->getArguments(); -$t->is(count($args), 1, '->load() references anonymous services as "normal" ones'); -$t->is(get_class($args[0]), 'Symfony\\Components\\DependencyInjection\\Reference', '->load() converts anonymous services to references to "normal" services'); -$t->ok(isset($services[(string) $args[0]]), '->load() makes a reference to the created ones'); -$inner = $services[(string) $args[0]]; -$t->is($inner->getClass(), 'BazClass', '->load() uses the same configuration as for the anonymous ones'); - -// ->load() # services -$t->diag('->load() # services'); -$config = $loader->load('services6.xml'); -$services = $config->getDefinitions(); -$t->ok(isset($services['foo']), '->load() parses elements'); -$t->is(get_class($services['foo']), 'Symfony\\Components\\DependencyInjection\\Definition', '->load() converts element to Definition instances'); -$t->is($services['foo']->getClass(), 'FooClass', '->load() parses the class attribute'); -$t->ok($services['shared']->isShared(), '->load() parses the shared attribute'); -$t->ok(!$services['non_shared']->isShared(), '->load() parses the shared attribute'); -$t->is($services['constructor']->getConstructor(), 'getInstance', '->load() parses the constructor attribute'); -$t->is($services['file']->getFile(), '%path%/foo.php', '->load() parses the file tag'); -$t->is($services['arguments']->getArguments(), array('foo', new Reference('foo'), array(true, false)), '->load() parses the argument tags'); -$t->is($services['configurator1']->getConfigurator(), 'sc_configure', '->load() parses the configurator tag'); -$t->is($services['configurator2']->getConfigurator(), array(new Reference('baz'), 'configure'), '->load() parses the configurator tag'); -$t->is($services['configurator3']->getConfigurator(), array('BazClass', 'configureStatic'), '->load() parses the configurator tag'); -$t->is($services['method_call1']->getMethodCalls(), array(array('setBar', array())), '->load() parses the method_call tag'); -$t->is($services['method_call2']->getMethodCalls(), array(array('setBar', array('foo', new Reference('foo'), array(true, false)))), '->load() parses the method_call tag'); -$aliases = $config->getAliases(); -$t->ok(isset($aliases['alias_for_foo']), '->load() parses elements'); -$t->is($aliases['alias_for_foo'], 'foo', '->load() parses aliases'); - -// ::convertDomElementToArray() -$t->diag('::convertDomElementToArray()'); -$doc = new DOMDocument("1.0"); -$doc->loadXML('bar'); -$t->is(ProjectLoader::convertDomElementToArray($doc->documentElement), 'bar', '::convertDomElementToArray() converts a \DomElement to an array'); - -$doc = new DOMDocument("1.0"); -$doc->loadXML(''); -$t->is(ProjectLoader::convertDomElementToArray($doc->documentElement), array('foo' => 'bar'), '::convertDomElementToArray() converts a \DomElement to an array'); - -$doc = new DOMDocument("1.0"); -$doc->loadXML('bar'); -$t->is(ProjectLoader::convertDomElementToArray($doc->documentElement), array('foo' => 'bar'), '::convertDomElementToArray() converts a \DomElement to an array'); - -$doc = new DOMDocument("1.0"); -$doc->loadXML('barbar'); -$t->is(ProjectLoader::convertDomElementToArray($doc->documentElement), array('foo' => array('value' => 'bar', 'foo' => 'bar')), '::convertDomElementToArray() converts a \DomElement to an array'); - -$doc = new DOMDocument("1.0"); -$doc->loadXML(''); -$t->is(ProjectLoader::convertDomElementToArray($doc->documentElement), array('foo' => null), '::convertDomElementToArray() converts a \DomElement to an array'); - -$doc = new DOMDocument("1.0"); -$doc->loadXML(''); -$t->is(ProjectLoader::convertDomElementToArray($doc->documentElement), array('foo' => null), '::convertDomElementToArray() converts a \DomElement to an array'); - -// extensions -$t->diag('extensions'); -Loader::registerExtension(new ProjectExtension()); -$loader = new ProjectLoader($fixturesPath.'/xml'); - -$config = $loader->load('services10.xml'); -$services = $config->getDefinitions(); -$parameters = $config->getParameters(); -$t->ok(isset($services['project.service.bar']), '->load() parses extension elements'); -$t->ok(isset($parameters['project.parameter.bar']), '->load() parses extension elements'); - -try -{ - $config = $loader->load('services11.xml'); - $t->fail('->load() throws an InvalidArgumentException if the tag is not valid'); -} -catch (InvalidArgumentException $e) -{ - $t->pass('->load() throws an InvalidArgumentException if the tag is not valid'); -} - -try -{ - $config = $loader->load('services12.xml'); - $t->fail('->load() throws an InvalidArgumentException if an extension is not loaded'); -} -catch (InvalidArgumentException $e) -{ - $t->pass('->load() throws an InvalidArgumentException if an extension is not loaded'); -} diff --git a/tests/unit/Symfony/Components/DependencyInjection/Loader/YamlFileLoaderTest.php b/tests/unit/Symfony/Components/DependencyInjection/Loader/YamlFileLoaderTest.php deleted file mode 100644 index dd59663c0d..0000000000 --- a/tests/unit/Symfony/Components/DependencyInjection/Loader/YamlFileLoaderTest.php +++ /dev/null @@ -1,134 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -require_once __DIR__.'/../../../../bootstrap.php'; - -use Symfony\Components\DependencyInjection\Builder; -use Symfony\Components\DependencyInjection\Reference; -use Symfony\Components\DependencyInjection\Definition; -use Symfony\Components\DependencyInjection\Loader\Loader; -use Symfony\Components\DependencyInjection\Loader\YamlFileLoader; - -$t = new LimeTest(25); - -$fixturesPath = realpath(__DIR__.'/../../../../../fixtures/Symfony/Components/DependencyInjection/'); - -require_once $fixturesPath.'/includes/ProjectExtension.php'; - -class ProjectLoader extends YamlFileLoader -{ - public function loadFile($file) - { - return parent::loadFile($file); - } -} - -// ->loadFile() -$t->diag('->loadFile()'); - -$loader = new ProjectLoader($fixturesPath.'/ini'); - -try -{ - $loader->loadFile('foo.yml'); - $t->fail('->load() throws an InvalidArgumentException if the loaded file does not exist'); -} -catch (InvalidArgumentException $e) -{ - $t->pass('->load() throws an InvalidArgumentException if the loaded file does not exist'); -} - -try -{ - $loader->loadFile('parameters.ini'); - $t->fail('->load() throws an InvalidArgumentException if the loaded file is not a valid YAML file'); -} -catch (InvalidArgumentException $e) -{ - $t->pass('->load() throws an InvalidArgumentException if the loaded file is not a valid YAML file'); -} - -$loader = new ProjectLoader($fixturesPath.'/yaml'); - -foreach (array('nonvalid1', 'nonvalid2') as $fixture) -{ - try - { - $loader->loadFile($fixture.'.yml'); - $t->fail('->load() throws an InvalidArgumentException if the loaded file does not validate'); - } - catch (InvalidArgumentException $e) - { - $t->pass('->load() throws an InvalidArgumentException if the loaded file does not validate'); - } -} - -// ->load() # parameters -$t->diag('->load() # parameters'); -$loader = new ProjectLoader($fixturesPath.'/yaml'); -$config = $loader->load('services2.yml'); -$t->is($config->getParameters(), array('foo' => 'bar', 'values' => array(true, false, 0, 1000.3), 'bar' => 'foo', 'foo_bar' => new Reference('foo_bar')), '->load() converts YAML keys to lowercase'); - -// ->load() # imports -$t->diag('->load() # imports'); -$config = $loader->load('services4.yml'); -$t->is($config->getParameters(), array('foo' => 'bar', 'bar' => '%foo%', 'values' => array(true, false), 'foo_bar' => new Reference('foo_bar'), 'imported_from_xml' => true, 'imported_from_ini' => true), '->load() imports and merges imported files'); - -// ->load() # services -$t->diag('->load() # services'); -$config = $loader->load('services6.yml'); -$services = $config->getDefinitions(); -$t->ok(isset($services['foo']), '->load() parses service elements'); -$t->is(get_class($services['foo']), 'Symfony\\Components\\DependencyInjection\\Definition', '->load() converts service element to Definition instances'); -$t->is($services['foo']->getClass(), 'FooClass', '->load() parses the class attribute'); -$t->ok($services['shared']->isShared(), '->load() parses the shared attribute'); -$t->ok(!$services['non_shared']->isShared(), '->load() parses the shared attribute'); -$t->is($services['constructor']->getConstructor(), 'getInstance', '->load() parses the constructor attribute'); -$t->is($services['file']->getFile(), '%path%/foo.php', '->load() parses the file tag'); -$t->is($services['arguments']->getArguments(), array('foo', new Reference('foo'), array(true, false)), '->load() parses the argument tags'); -$t->is($services['configurator1']->getConfigurator(), 'sc_configure', '->load() parses the configurator tag'); -$t->is($services['configurator2']->getConfigurator(), array(new Reference('baz'), 'configure'), '->load() parses the configurator tag'); -$t->is($services['configurator3']->getConfigurator(), array('BazClass', 'configureStatic'), '->load() parses the configurator tag'); -$t->is($services['method_call1']->getMethodCalls(), array(array('setBar', array())), '->load() parses the method_call tag'); -$t->is($services['method_call2']->getMethodCalls(), array(array('setBar', array('foo', new Reference('foo'), array(true, false)))), '->load() parses the method_call tag'); -$aliases = $config->getAliases(); -$t->ok(isset($aliases['alias_for_foo']), '->load() parses aliases'); -$t->is($aliases['alias_for_foo'], 'foo', '->load() parses aliases'); - -// extensions -$t->diag('extensions'); -Loader::registerExtension(new ProjectExtension()); -$loader = new ProjectLoader($fixturesPath.'/yaml'); - -$config = $loader->load('services10.yml'); -$services = $config->getDefinitions(); -$parameters = $config->getParameters(); -$t->ok(isset($services['project.service.bar']), '->load() parses extension elements'); -$t->ok(isset($parameters['project.parameter.bar']), '->load() parses extension elements'); - -try -{ - $config = $loader->load('services11.yml'); - $t->fail('->load() throws an InvalidArgumentException if the tag is not valid'); -} -catch (InvalidArgumentException $e) -{ - $t->pass('->load() throws an InvalidArgumentException if the tag is not valid'); -} - -try -{ - $config = $loader->load('services12.yml'); - $t->fail('->load() throws an InvalidArgumentException if an extension is not loaded'); -} -catch (InvalidArgumentException $e) -{ - $t->pass('->load() throws an InvalidArgumentException if an extension is not loaded'); -} diff --git a/tests/unit/Symfony/Components/DependencyInjection/ParameterTest.php b/tests/unit/Symfony/Components/DependencyInjection/ParameterTest.php deleted file mode 100644 index 482caf78d0..0000000000 --- a/tests/unit/Symfony/Components/DependencyInjection/ParameterTest.php +++ /dev/null @@ -1,21 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -require_once __DIR__.'/../../../bootstrap.php'; - -use Symfony\Components\DependencyInjection\Parameter; - -$t = new LimeTest(1); - -// __construct() ->__toString() -$t->diag('__construct() ->__toString()'); - -$ref = new Parameter('foo'); -$t->is((string) $ref, 'foo', '__construct() sets the id of the parameter, which is used for the __toString() method'); diff --git a/tests/unit/Symfony/Components/DependencyInjection/ReferenceTest.php b/tests/unit/Symfony/Components/DependencyInjection/ReferenceTest.php deleted file mode 100644 index 993711ccd6..0000000000 --- a/tests/unit/Symfony/Components/DependencyInjection/ReferenceTest.php +++ /dev/null @@ -1,21 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -require_once __DIR__.'/../../../bootstrap.php'; - -use Symfony\Components\DependencyInjection\Reference; - -$t = new LimeTest(1); - -// __construct() ->__toString() -$t->diag('__construct() ->__toString()'); - -$ref = new Reference('foo'); -$t->is((string) $ref, 'foo', '__construct() sets the id of the reference, which is used for the __toString() method'); diff --git a/tests/unit/Symfony/Components/EventDispatcher/EventDispatcherTest.php b/tests/unit/Symfony/Components/EventDispatcher/EventDispatcherTest.php deleted file mode 100644 index 0aae435969..0000000000 --- a/tests/unit/Symfony/Components/EventDispatcher/EventDispatcherTest.php +++ /dev/null @@ -1,134 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -require_once __DIR__.'/../../../bootstrap.php'; - -use Symfony\Components\EventDispatcher\Event; -use Symfony\Components\EventDispatcher\EventDispatcher; - -$t = new LimeTest(19); - -$dispatcher = new EventDispatcher(); - -// ->connect() ->disconnect() -$t->diag('->connect() ->disconnect()'); -$dispatcher->connect('bar', 'listenToBar'); -$t->is($dispatcher->getListeners('bar'), array('listenToBar'), '->connect() connects a listener to an event name'); -$dispatcher->connect('bar', 'listenToBarBar'); -$t->is($dispatcher->getListeners('bar'), array('listenToBar', 'listenToBarBar'), '->connect() can connect several listeners for the same event name'); - -$dispatcher->connect('barbar', 'listenToBarBar'); -$dispatcher->disconnect('bar', 'listenToBarBar'); -$t->is($dispatcher->getListeners('bar'), array('listenToBar'), '->disconnect() disconnects a listener for an event name'); -$t->is($dispatcher->getListeners('barbar'), array('listenToBarBar'), '->disconnect() disconnects a listener for an event name'); - -$t->ok($dispatcher->disconnect('foobar', 'listen') === false, '->disconnect() returns false if the listener does not exist'); - -// ->getListeners() ->hasListeners() -$t->diag('->getListeners() ->hasListeners()'); -$t->is($dispatcher->hasListeners('foo'), false, '->hasListeners() returns false if the event has no listener'); -$dispatcher->connect('foo', 'listenToFoo'); -$t->is($dispatcher->hasListeners('foo'), true, '->hasListeners() returns true if the event has some listeners'); -$dispatcher->disconnect('foo', 'listenToFoo'); -$t->is($dispatcher->hasListeners('foo'), false, '->hasListeners() returns false if the event has no listener'); - -$t->is($dispatcher->getListeners('bar'), array('listenToBar'), '->getListeners() returns an array of listeners connected to the given event name'); -$t->is($dispatcher->getListeners('foobar'), array(), '->getListeners() returns an empty array if no listener are connected to the given event name'); - -$listener = new Listener(); - -// ->notify() -$t->diag('->notify()'); -$listener->reset(); -$dispatcher = new EventDispatcher(); -$dispatcher->connect('foo', array($listener, 'listenToFoo')); -$dispatcher->connect('foo', array($listener, 'listenToFooBis')); -$e = $dispatcher->notify($event = new Event(new stdClass(), 'foo')); -$t->is($listener->getValue(), 'listenToFoolistenToFooBis', '->notify() notifies all registered listeners in order'); -$t->is($e, $event, '->notify() returns the event object'); - -$listener->reset(); -$dispatcher = new EventDispatcher(); -$dispatcher->connect('foo', array($listener, 'listenToFooBis')); -$dispatcher->connect('foo', array($listener, 'listenToFoo')); -$dispatcher->notify(new Event(new stdClass(), 'foo')); -$t->is($listener->getValue(), 'listenToFooBislistenToFoo', '->notify() notifies all registered listeners in order'); - -// ->notifyUntil() -$t->diag('->notifyUntil()'); -$listener->reset(); -$dispatcher = new EventDispatcher(); -$dispatcher->connect('foo', array($listener, 'listenToFoo')); -$dispatcher->connect('foo', array($listener, 'listenToFooBis')); -$e = $dispatcher->notifyUntil($event = new Event(new stdClass(), 'foo')); -$t->is($listener->getValue(), 'listenToFoolistenToFooBis', '->notifyUntil() notifies all registered listeners in order and stops if it returns true'); -$t->is($e, $event, '->notifyUntil() returns the event object'); - -$listener->reset(); -$dispatcher = new EventDispatcher(); -$dispatcher->connect('foo', array($listener, 'listenToFooBis')); -$dispatcher->connect('foo', array($listener, 'listenToFoo')); -$e = $dispatcher->notifyUntil($event = new Event(new stdClass(), 'foo')); -$t->is($listener->getValue(), 'listenToFooBis', '->notifyUntil() notifies all registered listeners in order and stops if it returns true'); - -// ->filter() -$t->diag('->filter()'); -$listener->reset(); -$dispatcher = new EventDispatcher(); -$dispatcher->connect('foo', array($listener, 'filterFoo')); -$dispatcher->connect('foo', array($listener, 'filterFooBis')); -$e = $dispatcher->filter($event = new Event(new stdClass(), 'foo'), 'foo'); -$t->is($e->getReturnValue(), '-*foo*-', '->filter() filters a value'); -$t->is($e, $event, '->filter() returns the event object'); - -$listener->reset(); -$dispatcher = new EventDispatcher(); -$dispatcher->connect('foo', array($listener, 'filterFooBis')); -$dispatcher->connect('foo', array($listener, 'filterFoo')); -$e = $dispatcher->filter($event = new Event(new stdClass(), 'foo'), 'foo'); -$t->is($e->getReturnValue(), '*-foo-*', '->filter() filters a value'); - -class Listener -{ - protected - $value = ''; - - function filterFoo(Event $event, $foo) - { - return "*$foo*"; - } - - function filterFooBis(Event $event, $foo) - { - return "-$foo-"; - } - - function listenToFoo(Event $event) - { - $this->value .= 'listenToFoo'; - } - - function listenToFooBis(Event $event) - { - $this->value .= 'listenToFooBis'; - - return true; - } - - function getValue() - { - return $this->value; - } - - function reset() - { - $this->value = ''; - } -} diff --git a/tests/unit/Symfony/Components/EventDispatcher/EventTest.php b/tests/unit/Symfony/Components/EventDispatcher/EventTest.php deleted file mode 100644 index 0d4961437c..0000000000 --- a/tests/unit/Symfony/Components/EventDispatcher/EventTest.php +++ /dev/null @@ -1,80 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -require_once __DIR__.'/../../../bootstrap.php'; - -use Symfony\Components\EventDispatcher\Event; - -$t = new LimeTest(16); - -$subject = new stdClass(); -$parameters = array('foo' => 'bar'); -$event = new Event($subject, 'name', $parameters); - -// ->getSubject() -$t->diag('->getSubject()'); -$t->is($event->getSubject(), $subject, '->getSubject() returns the event subject'); - -// ->getName() -$t->diag('->getName()'); -$t->is($event->getName(), 'name', '->getName() returns the event name'); - -// ->getParameters() ->setParameter() ->hasParameter() ->getParameter() -$t->diag('->getParameters()'); -$t->is($event->getParameters(), $parameters, '->getParameters() returns the event parameters'); -$t->is($event->getParameter('foo'), 'bar', '->getParameter() returns the value of a parameter'); -$event->setParameter('foo', 'foo'); -$t->is($event->getParameter('foo'), 'foo', '->setParameter() changes the value of a parameter'); -$t->ok($event->hasParameter('foo'), '->hasParameter() returns true if the parameter is defined'); -unset($event['foo']); -$t->ok(!$event->hasParameter('foo'), '->hasParameter() returns false if the parameter is not defined'); - -try -{ - $event->getParameter('foobar'); - $t->fail('->getParameter() throws an \InvalidArgumentException exception when the parameter does not exist'); -} -catch (\InvalidArgumentException $e) -{ - $t->pass('->getParameter() throws an \InvalidArgumentException exception when the parameter does not exist'); -} -$event = new Event($subject, 'name', $parameters); - -// ->getReturnValue() ->setReturnValue() -$t->diag('->getReturnValue() ->setReturnValue()'); -$event->setReturnValue('foo'); -$t->is($event->getReturnValue(), 'foo', '->getReturnValue() returns the return value of the event'); - -// ->setProcessed() ->isProcessed() -$t->diag('->setProcessed() ->isProcessed()'); -$event->setProcessed(true); -$t->is($event->isProcessed(), true, '->isProcessed() returns true if the event has been processed'); -$event->setProcessed(false); -$t->is($event->isProcessed(), false, '->setProcessed() changes the processed status'); - -// ArrayAccess interface -$t->diag('ArrayAccess interface'); -$t->is($event['foo'], 'bar', 'Event implements the ArrayAccess interface'); -$event['foo'] = 'foo'; -$t->is($event['foo'], 'foo', 'Event implements the ArrayAccess interface'); - -try -{ - $event['foobar']; - $t->fail('::offsetGet() throws an \InvalidArgumentException exception when the parameter does not exist'); -} -catch (\InvalidArgumentException $e) -{ - $t->pass('::offsetGet() throws an \InvalidArgumentException exception when the parameter does not exist'); -} - -$t->ok(isset($event['foo']), 'Event implements the ArrayAccess interface'); -unset($event['foo']); -$t->ok(!isset($event['foo']), 'Event implements the ArrayAccess interface'); diff --git a/tests/unit/Symfony/Components/OutputEscaper/ArrayDecoratorTest.php b/tests/unit/Symfony/Components/OutputEscaper/ArrayDecoratorTest.php deleted file mode 100644 index af7c828750..0000000000 --- a/tests/unit/Symfony/Components/OutputEscaper/ArrayDecoratorTest.php +++ /dev/null @@ -1,78 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -require_once __DIR__.'/../../../bootstrap.php'; - -use Symfony\Components\OutputEscaper\Escaper; - -$t = new LimeTest(11); - -$a = array('escaped!', 1, null, array(2, 'escaped!')); -$escaped = Escaper::escape('entities', $a); - -// ->getRaw() -$t->diag('->getRaw()'); -$t->is($escaped->getRaw(0), 'escaped!', '->getRaw() returns the raw value'); - -// ArrayAccess interface -$t->diag('ArrayAccess interface'); -$t->is($escaped[0], '<strong>escaped!</strong>', 'The escaped object behaves like an array'); -$t->is($escaped[2], null, 'The escaped object behaves like an array'); -$t->is($escaped[3][1], '<strong>escaped!</strong>', 'The escaped object behaves like an array'); - -$t->ok(isset($escaped[1]), 'The escaped object behaves like an array (isset)'); - -$t->diag('ArrayAccess interface is read only'); -try -{ - unset($escaped[0]); - $t->fail('The escaped object is read only (unset)'); -} -catch (\LogicException $e) -{ - $t->pass('The escaped object is read only (unset)'); -} - -try -{ - $escaped[0] = 12; - $t->fail('The escaped object is read only (set)'); -} -catch (\LogicException $e) -{ - $t->pass('The escaped object is read only (set)'); -} - -// Iterator interface -$t->diag('Iterator interface'); -foreach ($escaped as $key => $value) -{ - switch ($key) - { - case 0: - $t->is($value, '<strong>escaped!</strong>', 'The escaped object behaves like an array'); - break; - case 1: - $t->is($value, 1, 'The escaped object behaves like an array'); - break; - case 2: - $t->is($value, null, 'The escaped object behaves like an array'); - break; - case 3: - break; - default: - $t->fail('The escaped object behaves like an array'); - } -} - -// Coutable interface -$t->diag('Countable interface'); -$t->is(count($escaped), 4, 'The escaped object implements the Countable interface'); diff --git a/tests/unit/Symfony/Components/OutputEscaper/EscaperTest.php b/tests/unit/Symfony/Components/OutputEscaper/EscaperTest.php deleted file mode 100644 index d2fbe4db0f..0000000000 --- a/tests/unit/Symfony/Components/OutputEscaper/EscaperTest.php +++ /dev/null @@ -1,154 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -require_once __DIR__.'/../../../bootstrap.php'; - -use Symfony\Components\OutputEscaper\Escaper; -use Symfony\Components\OutputEscaper\SafeDecorator; -use Symfony\Components\OutputEscaper\IteratorDecorator; -use Symfony\Components\OutputEscaper\ArrayDecorator; -use Symfony\Components\OutputEscaper\ObjectDecorator; - -$t = new LimeTest(39); - -class OutputEscaperTestClass -{ - public $title = 'escaped!'; - - public function getTitle() - { - return $this->title; - } - - public function getTitleTitle() - { - $o = new self; - - return $o->getTitle(); - } -} - -class OutputEscaperTestClassChild extends OutputEscaperTestClass -{ -} - -// ::escape() -$t->diag('::escape()'); -$t->diag('::escape() does not escape special values'); -$t->ok(Escaper::escape('entities', null) === null, '::escape() returns null if the value to escape is null'); -$t->ok(Escaper::escape('entities', false) === false, '::escape() returns false if the value to escape is false'); -$t->ok(Escaper::escape('entities', true) === true, '::escape() returns true if the value to escape is true'); - -$t->diag('::escape() does not escape a value when escaping method is RAW'); -$t->is(Escaper::escape('raw', 'escaped!'), 'escaped!', '::escape() takes an escaping strategy function name as its first argument'); - -$t->diag('::escape() escapes strings'); -$t->is(Escaper::escape('entities', 'escaped!'), '<strong>escaped!</strong>', '::escape() returns an escaped string if the value to escape is a string'); -$t->is(Escaper::escape('entities', 'échappé'), '<strong>échappé</strong>', '::escape() returns an escaped string if the value to escape is a string'); - -$t->diag('::escape() escapes arrays'); -$input = array( - 'foo' => 'escaped!', - 'bar' => array('foo' => 'escaped!'), -); -$output = Escaper::escape('entities', $input); -$t->ok($output instanceof ArrayDecorator, '::escape() returns a ArrayDecorator object if the value to escape is an array'); -$t->is($output['foo'], '<strong>escaped!</strong>', '::escape() escapes all elements of the original array'); -$t->is($output['bar']['foo'], '<strong>escaped!</strong>', '::escape() is recursive'); -$t->is($output->getRawValue(), $input, '->getRawValue() returns the unescaped value'); - -$t->diag('::escape() escapes objects'); -$input = new OutputEscaperTestClass(); -$output = Escaper::escape('entities', $input); -$t->ok($output instanceof ObjectDecorator, '::escape() returns a ObjectDecorator object if the value to escape is an object'); -$t->is($output->getTitle(), '<strong>escaped!</strong>', '::escape() escapes all methods of the original object'); -$t->is($output->title, '<strong>escaped!</strong>', '::escape() escapes all properties of the original object'); -$t->is($output->getTitleTitle(), '<strong>escaped!</strong>', '::escape() is recursive'); -$t->is($output->getRawValue(), $input, '->getRawValue() returns the unescaped value'); - -$t->is(Escaper::escape('entities', $output)->getTitle(), '<strong>escaped!</strong>', '::escape() does not double escape an object'); -$t->ok(Escaper::escape('entities', new \DirectoryIterator('.')) instanceof IteratorDecorator, '::escape() returns a IteratorDecorator object if the value to escape is an object that implements the ArrayAccess interface'); - -$t->diag('::escape() does not escape object marked as being safe'); -$t->ok(Escaper::escape('entities', new SafeDecorator(new OutputEscaperTestClass())) instanceof OutputEscaperTestClass, '::escape() returns the original value if it is marked as being safe'); - -Escaper::markClassAsSafe('OutputEscaperTestClass'); -$t->ok(Escaper::escape('entities', new OutputEscaperTestClass()) instanceof OutputEscaperTestClass, '::escape() returns the original value if the object class is marked as being safe'); -$t->ok(Escaper::escape('entities', new OutputEscaperTestClassChild()) instanceof OutputEscaperTestClassChild, '::escape() returns the original value if one of the object parent class is marked as being safe'); - -$t->diag('::escape() cannot escape resources'); -$fh = fopen(__FILE__, 'r'); -try -{ - Escaper::escape('entities', $fh); - $t->fail('::escape() throws an InvalidArgumentException if the value cannot be escaped'); -} -catch (InvalidArgumentException $e) -{ - $t->pass('::escape() throws an InvalidArgumentException if the value cannot be escaped'); -} - -// ::unescape() -$t->diag('::unescape()'); -$t->diag('::unescape() does not unescape special values'); -$t->ok(Escaper::unescape(null) === null, '::unescape() returns null if the value to unescape is null'); -$t->ok(Escaper::unescape(false) === false, '::unescape() returns false if the value to unescape is false'); -$t->ok(Escaper::unescape(true) === true, '::unescape() returns true if the value to unescape is true'); - -$t->diag('::unescape() unescapes strings'); -$t->is(Escaper::unescape('<strong>escaped!</strong>'), 'escaped!', '::unescape() returns an unescaped string if the value to unescape is a string'); -$t->is(Escaper::unescape('<strong>échappé</strong>'), 'échappé', '::unescape() returns an unescaped string if the value to unescape is a string'); - -$t->diag('::unescape() unescapes arrays'); -$input = Escaper::escape('entities', array( - 'foo' => 'escaped!', - 'bar' => array('foo' => 'escaped!'), -)); -$output = Escaper::unescape($input); -$t->ok(is_array($output), '::unescape() returns an array if the input is a ArrayDecorator object'); -$t->is($output['foo'], 'escaped!', '::unescape() unescapes all elements of the original array'); -$t->is($output['bar']['foo'], 'escaped!', '::unescape() is recursive'); - -$t->diag('::unescape() unescapes objects'); -$object = new OutputEscaperTestClass(); -$input = Escaper::escape('entities', $object); -$output = Escaper::unescape($input); -$t->ok($output instanceof OutputEscaperTestClass, '::unescape() returns the original object when a ObjectDecorator object is passed'); -$t->is($output->getTitle(), 'escaped!', '::unescape() unescapes all methods of the original object'); -$t->is($output->title, 'escaped!', '::unescape() unescapes all properties of the original object'); -$t->is($output->getTitleTitle(), 'escaped!', '::unescape() is recursive'); - -$t->ok(IteratorDecorator::unescape(Escaper::escape('entities', new DirectoryIterator('.'))) instanceof DirectoryIterator, '::unescape() unescapes IteratorDecorator objects'); - -$t->diag('::unescape() does not unescape object marked as being safe'); -$t->ok(Escaper::unescape(Escaper::escape('entities', new SafeDecorator(new OutputEscaperTestClass()))) instanceof OutputEscaperTestClass, '::unescape() returns the original value if it is marked as being safe'); - -Escaper::markClassAsSafe('OutputEscaperTestClass'); -$t->ok(Escaper::unescape(Escaper::escape('entities', new OutputEscaperTestClass())) instanceof OutputEscaperTestClass, '::unescape() returns the original value if the object class is marked as being safe'); -$t->ok(Escaper::unescape(Escaper::escape('entities', new OutputEscaperTestClassChild())) instanceof OutputEscaperTestClassChild, '::unescape() returns the original value if one of the object parent class is marked as being safe'); - -$t->diag('::unescape() do nothing to resources'); -$fh = fopen(__FILE__, 'r'); -$t->is(Escaper::unescape($fh), $fh, '::unescape() do nothing to resources'); - -$t->diag('::unescape() unescapes mixed arrays'); -$object = new OutputEscaperTestClass(); -$input = array( - 'foo' => 'bar', - 'bar' => Escaper::escape('entities', 'bar'), - 'foobar' => Escaper::escape('entities', $object), -); -$output = array( - 'foo' => 'bar', - 'bar' => 'bar', - 'foobar' => $object, -); -$t->is(Escaper::unescape($input), $output, '::unescape() unescapes values with some escaped and unescaped values'); diff --git a/tests/unit/Symfony/Components/OutputEscaper/ObjectDecoratorTest.php b/tests/unit/Symfony/Components/OutputEscaper/ObjectDecoratorTest.php deleted file mode 100644 index 993b9dc9e8..0000000000 --- a/tests/unit/Symfony/Components/OutputEscaper/ObjectDecoratorTest.php +++ /dev/null @@ -1,47 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -require_once __DIR__.'/../../../bootstrap.php'; - -use Symfony\Components\OutputEscaper\Escaper; - -$t = new LimeTest(3); - -class OutputEscaperTest -{ - public function __toString() - { - return $this->getTitle(); - } - - public function getTitle() - { - return 'escaped!'; - } - - public function getTitles() - { - return array(1, 2, 'escaped!'); - } -} - -$object = new OutputEscaperTest(); -$escaped = Escaper::escape('entities', $object); - -$t->is($escaped->getTitle(), '<strong>escaped!</strong>', 'The escaped object behaves like the real object'); - -$array = $escaped->getTitles(); -$t->is($array[2], '<strong>escaped!</strong>', 'The escaped object behaves like the real object'); - -// __toString() -$t->diag('__toString()'); - -$t->is($escaped->__toString(), '<strong>escaped!</strong>', 'The escaped object behaves like the real object'); diff --git a/tests/unit/Symfony/Components/OutputEscaper/SafeDecoratorTest.php b/tests/unit/Symfony/Components/OutputEscaper/SafeDecoratorTest.php deleted file mode 100644 index 6a543df25e..0000000000 --- a/tests/unit/Symfony/Components/OutputEscaper/SafeDecoratorTest.php +++ /dev/null @@ -1,93 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -require_once __DIR__.'/../../../bootstrap.php'; - -use Symfony\Components\OutputEscaper\SafeDecorator; - -$t = new LimeTest(13); - -// ->getValue() -$t->diag('->getValue()'); -$safe = new SafeDecorator('foo'); -$t->is($safe->getValue(), 'foo', '->getValue() returns the embedded value'); - -// ->__set() ->__get() -$t->diag('->__set() ->__get()'); - -class TestClass1 -{ - public $foo = 'bar'; -} - -$safe = new SafeDecorator(new TestClass1()); - -$t->is($safe->foo, 'bar', '->__get() returns the object parameter'); -$safe->foo = 'baz'; -$t->is($safe->foo, 'baz', '->__set() sets the object parameter'); - -// ->__call() -$t->diag('->__call()'); - -class TestClass2 -{ - public function doSomething() - { - return 'ok'; - } -} - -$safe = new SafeDecorator(new TestClass2()); -$t->is($safe->doSomething(), 'ok', '->__call() invokes the embedded method'); - -// ->__isset() ->__unset() -$t->diag('->__isset() ->__unset()'); - -class TestClass3 -{ - public - $boolValue = true, - $nullValue = null; -} - -$safe = new SafeDecorator(new TestClass3()); - -$t->is(isset($safe->boolValue), true, '->__isset() returns true if the property is not null'); -$t->is(isset($safe->nullValue), false, '->__isset() returns false if the property is null'); -$t->is(isset($safe->undefinedValue), false, '->__isset() returns false if the property does not exist'); - -unset($safe->boolValue); -$t->is(isset($safe->boolValue), false, '->__unset() unsets the embedded property'); - -// Iterator -$t->diag('Iterator'); - -$input = array('one' => 1, 'two' => 2, 'three' => 3, 'children' => array(1, 2, 3)); -$output = array(); - -$safe = new SafeDecorator($input); -foreach ($safe as $key => $value) -{ - $output[$key] = $value; -} -$t->same($output, $input, '"Iterator" implementation imitates an array'); - -// ArrayAccess -$t->diag('ArrayAccess'); - -$safe = new SafeDecorator(array('foo' => 'bar')); - -$t->is($safe['foo'], 'bar', '"ArrayAccess" implementation returns a value from the embedded array'); -$safe['foo'] = 'baz'; -$t->is($safe['foo'], 'baz', '"ArrayAccess" implementation sets a value on the embedded array'); -$t->is(isset($safe['foo']), true, '"ArrayAccess" checks if a value is set on the embedded array'); -unset($safe['foo']); -$t->is(isset($safe['foo']), false, '"ArrayAccess" unsets a value on the embedded array'); diff --git a/tests/unit/Symfony/Components/Yaml/DumperTest.php b/tests/unit/Symfony/Components/Yaml/DumperTest.php deleted file mode 100644 index bdb7514e42..0000000000 --- a/tests/unit/Symfony/Components/Yaml/DumperTest.php +++ /dev/null @@ -1,153 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -require_once __DIR__.'/../../../bootstrap.php'; - -use Symfony\Components\Yaml\Yaml; -use Symfony\Components\Yaml\Parser; -use Symfony\Components\Yaml\Dumper; - -Yaml::setSpecVersion('1.1'); - -$t = new LimeTest(151); - -$parser = new Parser(); -$dumper = new Dumper(); - -$path = __DIR__.'/../../../../fixtures/Symfony/Components/Yaml'; -$files = $parser->parse(file_get_contents($path.'/index.yml')); -foreach ($files as $file) -{ - $t->diag($file); - - $yamls = file_get_contents($path.'/'.$file.'.yml'); - - // split YAMLs documents - foreach (preg_split('/^---( %YAML\:1\.0)?/m', $yamls) as $yaml) - { - if (!$yaml) - { - continue; - } - - $test = $parser->parse($yaml); - if (isset($test['dump_skip']) && $test['dump_skip']) - { - continue; - } - else if (isset($test['todo']) && $test['todo']) - { - $t->todo($test['test']); - } - else - { - $expected = eval('return '.trim($test['php']).';'); - - $t->is($parser->parse($dumper->dump($expected, 10)), $expected, $test['test']); - } - } -} - -// inline level -$array = array( - '' => 'bar', - 'foo' => '#bar', - 'foo\'bar' => array(), - 'bar' => array(1, 'foo'), - 'foobar' => array( - 'foo' => 'bar', - 'bar' => array(1, 'foo'), - 'foobar' => array( - 'foo' => 'bar', - 'bar' => array(1, 'foo'), - ), - ), -); - -$expected = <<is($dumper->dump($array, -10), $expected, '->dump() takes an inline level argument'); -$t->is($dumper->dump($array, 0), $expected, '->dump() takes an inline level argument'); - -$expected = <<is($dumper->dump($array, 1), $expected, '->dump() takes an inline level argument'); - -$expected = <<is($dumper->dump($array, 2), $expected, '->dump() takes an inline level argument'); - -$expected = <<is($dumper->dump($array, 3), $expected, '->dump() takes an inline level argument'); - -$expected = <<is($dumper->dump($array, 4), $expected, '->dump() takes an inline level argument'); -$t->is($dumper->dump($array, 10), $expected, '->dump() takes an inline level argument'); - -// objects -$t->diag('Objects support'); -class A -{ - public $a = 'foo'; -} -$a = array('foo' => new A(), 'bar' => 1); -$t->is($dumper->dump($a), '{ foo: !!php/object:O:1:"A":1:{s:1:"a";s:3:"foo";}, bar: 1 }', '->dump() is able to dump objects'); diff --git a/tests/unit/Symfony/Components/Yaml/InlineTest.php b/tests/unit/Symfony/Components/Yaml/InlineTest.php deleted file mode 100644 index 3a591c8f29..0000000000 --- a/tests/unit/Symfony/Components/Yaml/InlineTest.php +++ /dev/null @@ -1,148 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -require_once __DIR__.'/../../../bootstrap.php'; - -use Symfony\Components\Yaml\Yaml; -use Symfony\Components\Yaml\Inline; - -Yaml::setSpecVersion('1.1'); - -$t = new LimeTest(124); - -// ::load() -$t->diag('::load()'); - -$testsForLoad = array( - '' => '', - 'null' => null, - 'false' => false, - 'true' => true, - '12' => 12, - '"quoted string"' => 'quoted string', - "'quoted string'" => 'quoted string', - '12.30e+02' => 12.30e+02, - '0x4D2' => 0x4D2, - '02333' => 02333, - '.Inf' => -log(0), - '-.Inf' => log(0), - '123456789123456789' => '123456789123456789', - '"foo\r\nbar"' => "foo\r\nbar", - "'foo#bar'" => 'foo#bar', - "'foo # bar'" => 'foo # bar', - "'#cfcfcf'" => '#cfcfcf', - - '2007-10-30' => mktime(0, 0, 0, 10, 30, 2007), - '2007-10-30T02:59:43Z' => gmmktime(2, 59, 43, 10, 30, 2007), - '2007-10-30 02:59:43 Z' => gmmktime(2, 59, 43, 10, 30, 2007), - - '"a \\"string\\" with \'quoted strings inside\'"' => 'a "string" with \'quoted strings inside\'', - "'a \"string\" with ''quoted strings inside'''" => 'a "string" with \'quoted strings inside\'', - - // sequences - // urls are no key value mapping. see #3609. Valid yaml "key: value" mappings require a space after the colon - '[foo, http://urls.are/no/mappings, false, null, 12]' => array('foo', 'http://urls.are/no/mappings', false, null, 12), - '[ foo , bar , false , null , 12 ]' => array('foo', 'bar', false, null, 12), - '[\'foo,bar\', \'foo bar\']' => array('foo,bar', 'foo bar'), - - // mappings - '{foo:bar,bar:foo,false:false,null:null,integer:12}' => array('foo' => 'bar', 'bar' => 'foo', 'false' => false, 'null' => null, 'integer' => 12), - '{ foo : bar, bar : foo, false : false, null : null, integer : 12 }' => array('foo' => 'bar', 'bar' => 'foo', 'false' => false, 'null' => null, 'integer' => 12), - '{foo: \'bar\', bar: \'foo: bar\'}' => array('foo' => 'bar', 'bar' => 'foo: bar'), - '{\'foo\': \'bar\', "bar": \'foo: bar\'}' => array('foo' => 'bar', 'bar' => 'foo: bar'), - '{\'foo\'\'\': \'bar\', "bar\"": \'foo: bar\'}' => array('foo\'' => 'bar', "bar\"" => 'foo: bar'), - '{\'foo: \': \'bar\', "bar: ": \'foo: bar\'}' => array('foo: ' => 'bar', "bar: " => 'foo: bar'), - - // nested sequences and mappings - '[foo, [bar, foo]]' => array('foo', array('bar', 'foo')), - '[foo, {bar: foo}]' => array('foo', array('bar' => 'foo')), - '{ foo: {bar: foo} }' => array('foo' => array('bar' => 'foo')), - '{ foo: [bar, foo] }' => array('foo' => array('bar', 'foo')), - - '[ foo, [ bar, foo ] ]' => array('foo', array('bar', 'foo')), - - '[{ foo: {bar: foo} }]' => array(array('foo' => array('bar' => 'foo'))), - - '[foo, [bar, [foo, [bar, foo]], foo]]' => array('foo', array('bar', array('foo', array('bar', 'foo')), 'foo')), - - '[foo, {bar: foo, foo: [foo, {bar: foo}]}, [foo, {bar: foo}]]' => array('foo', array('bar' => 'foo', 'foo' => array('foo', array('bar' => 'foo'))), array('foo', array('bar' => 'foo'))), - - '[foo, bar: { foo: bar }]' => array('foo', '1' => array('bar' => array('foo' => 'bar'))), -); - -foreach ($testsForLoad as $yaml => $value) -{ - $t->is(Inline::load($yaml), $value, sprintf('::load() converts an inline YAML to a PHP structure (%s)', $yaml)); -} - -$testsForDump = array( - 'null' => null, - 'false' => false, - 'true' => true, - '12' => 12, - "'quoted string'" => 'quoted string', - '12.30e+02' => 12.30e+02, - '1234' => 0x4D2, - '1243' => 02333, - '.Inf' => -log(0), - '-.Inf' => log(0), - '"foo\r\nbar"' => "foo\r\nbar", - "'foo#bar'" => 'foo#bar', - "'foo # bar'" => 'foo # bar', - "'#cfcfcf'" => '#cfcfcf', - - "'a \"string\" with ''quoted strings inside'''" => 'a "string" with \'quoted strings inside\'', - - // sequences - '[foo, bar, false, null, 12]' => array('foo', 'bar', false, null, 12), - '[\'foo,bar\', \'foo bar\']' => array('foo,bar', 'foo bar'), - - // mappings - '{ foo: bar, bar: foo, \'false\': false, \'null\': null, integer: 12 }' => array('foo' => 'bar', 'bar' => 'foo', 'false' => false, 'null' => null, 'integer' => 12), - '{ foo: bar, bar: \'foo: bar\' }' => array('foo' => 'bar', 'bar' => 'foo: bar'), - - // nested sequences and mappings - '[foo, [bar, foo]]' => array('foo', array('bar', 'foo')), - - '[foo, [bar, [foo, [bar, foo]], foo]]' => array('foo', array('bar', array('foo', array('bar', 'foo')), 'foo')), - - '{ foo: { bar: foo } }' => array('foo' => array('bar' => 'foo')), - - '[foo, { bar: foo }]' => array('foo', array('bar' => 'foo')), - - '[foo, { bar: foo, foo: [foo, { bar: foo }] }, [foo, { bar: foo }]]' => array('foo', array('bar' => 'foo', 'foo' => array('foo', array('bar' => 'foo'))), array('foo', array('bar' => 'foo'))), -); - -// ::dump() -$t->diag('::dump()'); -foreach ($testsForDump as $yaml => $value) -{ - $t->is(Inline::dump($value), $yaml, sprintf('::dump() converts a PHP structure to an inline YAML (%s)', $yaml)); -} - -foreach ($testsForLoad as $yaml => $value) -{ - if ($value == 1230) - { - continue; - } - - $t->is(Inline::load(Inline::dump($value)), $value, 'check consistency'); -} - -foreach ($testsForDump as $yaml => $value) -{ - if ($value == 1230) - { - continue; - } - - $t->is(Inline::load(Inline::dump($value)), $value, 'check consistency'); -} diff --git a/tests/unit/Symfony/Components/Yaml/ParserTest.php b/tests/unit/Symfony/Components/Yaml/ParserTest.php deleted file mode 100644 index b5b8c7f668..0000000000 --- a/tests/unit/Symfony/Components/Yaml/ParserTest.php +++ /dev/null @@ -1,85 +0,0 @@ - - * - * For the full copyright and license information, please view the LICENSE - * file that was distributed with this source code. - */ - -require_once __DIR__.'/../../../bootstrap.php'; - -use Symfony\Components\Yaml\Yaml; -use Symfony\Components\Yaml\Parser; -use Symfony\Components\Yaml\ParserException; - -Yaml::setSpecVersion('1.1'); - -$t = new LimeTest(151); - -$parser = new Parser(); - -$path = __DIR__.'/../../../../fixtures/Symfony/Components/Yaml'; -$files = $parser->parse(file_get_contents($path.'/index.yml')); -foreach ($files as $file) -{ - $t->diag($file); - - $yamls = file_get_contents($path.'/'.$file.'.yml'); - - // split YAMLs documents - foreach (preg_split('/^---( %YAML\:1\.0)?/m', $yamls) as $yaml) - { - if (!$yaml) - { - continue; - } - - $test = $parser->parse($yaml); - if (isset($test['todo']) && $test['todo']) - { - $t->todo($test['test']); - } - else - { - $expected = var_export(eval('return '.trim($test['php']).';'), true); - - $t->is(var_export($parser->parse($test['yaml']), true), $expected, $test['test']); - } - } -} - -// test tabs in YAML -$yamls = array( - "foo:\n bar", - "foo:\n bar", - "foo:\n bar", - "foo:\n bar", -); - -foreach ($yamls as $yaml) -{ - try - { - $content = $parser->parse($yaml); - $t->fail('YAML files must not contain tabs'); - } - catch (ParserException $e) - { - $t->pass('YAML files must not contain tabs'); - } -} - -// objects -$t->diag('Objects support'); -class A -{ - public $a = 'foo'; -} -$a = array('foo' => new A(), 'bar' => 1); -$t->is($parser->parse(<<parse() is able to dump objects');