$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 = 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');
<?php /* * 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'; use Symfony\Components\CLI\Output\NullOutput; $t = new LimeTest(1); $output = new NullOutput(); $output->write('foo'); $t->pass('->write() does nothing');
} } // ->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)
$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\tbar", "foo:\n \tbar", "foo:\n\t bar", "foo:\n \t 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(<<<EOF foo: !!php/object:O:1:"A":1:{s:1:"a";s:3:"foo";} bar: 1 EOF ), $a, '->parse() is able to dump objects');
$t->is($input->getArgument('name'), 'foo', '->__construct() takes a Definition as an argument'); // ->getOption() ->setOption() ->getOptions() $t->diag('->getOption() ->setOption() ->getOptions()'); $input = new ArrayInput(array('--name' => 'foo'), new Definition(array(new Option('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 Definition(array(new Option('name'), new Option('bar', '', Option::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 Definition(array(new Argument('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 Definition(array(new Argument('name'), new Argument('bar', Argument::OPTIONAL, '', 'default'))));
*/ require_once __DIR__ . '/../../../../bootstrap.php'; use Symfony\Components\CLI\Input\Option; use Symfony\Components\CLI\Exception; $t = new LimeTest(34); // __construct() $t->diag('__construct()'); $option = new Option('foo'); $t->is($option->getName(), 'foo', '__construct() takes a name as its first argument'); $option = new Option('--foo'); $t->is($option->getName(), 'foo', '__construct() removes the leading -- of the option name'); try { $option = new Option('foo', 'f', Option::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 Option('foo', 'f'); $t->is($option->getShortcut(), 'f', '__construct() can take a shortcut as its second argument'); $option = new Option('foo', '-f'); $t->is($option->getShortcut(), 'f', '__construct() removes the leading - of the shortcut'); // mode argument $option = new Option('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 Option('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');
<?php /* * 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__ . '/../../../../../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');
$output->write('foo'); $t->is($output->output, '', '->write() outputs nothing if verbosity is set to VERBOSITY_QUIET'); $output = new TestOutput(); $output->write(array('foo', 'bar')); $t->is($output->output, "foo\nbar\n", '->write() can take an array of messages to output'); $output = new TestOutput(); $output->write('<info>foo</info>', Output::OUTPUT_RAW); $t->is($output->output, "<info>foo</info>\n", '->write() outputs the raw message if OUTPUT_RAW is specified'); $output = new TestOutput(); $output->write('<info>foo</info>', Output::OUTPUT_PLAIN); $t->is($output->output, "foo\n", '->write() strips decoration tags if OUTPUT_PLAIN is specified'); $output = new TestOutput(); $output->setDecorated(false); $output->write('<info>foo</info>'); $t->is($output->output, "foo\n", '->write() strips decoration tags if decoration is set to false'); $output = new TestOutput(); $output->setDecorated(true); $output->write('<foo>foo</foo>'); $t->is($output->output, "[33;41;5mfoo[0m\n", '->write() decorates the output'); try { $output->write('<foo>foo</foo>', 24); $t->fail('->write() throws an \\InvalidArgumentException when the type does not exist'); } catch (\InvalidArgumentException $e) { $t->pass('->write() throws an \\InvalidArgumentException when the type does not exist'); } try { $output->write('<bar>foo</bar>'); $t->fail('->write() throws an \\InvalidArgumentException when a style does not exist'); } catch (\InvalidArgumentException $e) { $t->pass('->write() throws an \\InvalidArgumentException when a style does not exist'); }
// ->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 Definition(array(new Argument('name')))); $t->is($input->getArguments(), array('name' => 'foo'), '->parse() parses required arguments'); try { $input = new ArrayInput(array('foo' => 'foo'), new Definition(array(new Argument('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 Definition(array(new Option('foo')))); $t->is($input->getOptions(), array('foo' => 'bar'), '->parse() parses long options'); $input = new ArrayInput(array('--foo' => 'bar'), new Definition(array(new Option('foo', 'f', Option::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 Definition(array(new Option('foo', 'f', Option::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 Definition(array(new Option('foo', 'f', Option::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 Definition());
$t->ok($engine->getRenderers() === array('php' => $renderer), '__construct() can overridde the default PHP renderer'); $engine = new ProjectTemplateEngine($loader, array(), $helperSet = new HelperSet()); $t->ok($engine->getHelperSet() === $helperSet, '__construct() takes a helper set as its third argument'); // ->getHelperSet() ->setHelperSet() $t->diag('->getHelperSet() ->setHelperSet()'); $engine = new ProjectTemplateEngine($loader); $engine->setHelperSet(new HelperSet(array('foo' => $helper = new SimpleHelper('bar')))); $t->is((string) $engine->getHelperSet()->get('foo'), 'bar', '->setHelperSet() sets a helper set'); // __get() $t->diag('__get()'); $t->is($engine->foo, $helper, '->__get() returns the value of a helper'); try { $engine->bar; $t->fail('->__get() throws an InvalidArgumentException if the helper is not defined'); } catch (InvalidArgumentException $e) { $t->pass('->__get() throws an InvalidArgumentException if the helper is not defined'); } // ->get() ->set() ->has() $t->diag('->get() ->set() ->has()'); $engine = new ProjectTemplateEngine($loader); $engine->set('foo', 'bar'); $t->is($engine->get('foo'), 'bar', '->set() sets a slot value'); $t->is($engine->get('bar', 'bar'), 'bar', '->get() takes a default value to return if the slot does not exist'); $t->ok($engine->has('foo'), '->has() returns true if the slot exists'); $t->ok(!$engine->has('bar'), '->has() returns false if the slot does not exist'); // ->output() $t->diag('->output()'); ob_start(); $ret = $engine->output('foo'); $output = ob_get_clean(); $t->is($output, 'bar', '->output() outputs the content of a slot');
$t->is($input->getArguments(), array('name' => 'foo'), '->parse() is stateless'); $input = new TestInput(array('cli.php', '--foo')); $input->bind(new Definition(array(new Option('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 Definition(array(new Option('foo', 'f', Option::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 Definition(array(new Option('foo', 'f', Option::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 Definition(array(new Option('foo', 'f', Option::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 Definition(array(new Option('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 Definition(array(new Option('foo', 'f', Option::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 Definition(array(new Option('foo', 'f', Option::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 Definition(array(new Argument('name'), new Option('foo', 'f', Option::PARAMETER_OPTIONAL), new Option('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'));
* file that was distributed with this source code. */ require_once __DIR__ . '/../../../../bootstrap.php'; require_once __DIR__ . '/../../../../../lib/SymfonyTests/Components/Templating/SimpleHelper.php'; use Symfony\Components\Templating\Helper\HelperSet; use Symfony\Components\Templating\Engine; use Symfony\Components\Templating\Loader\FilesystemLoader; $t = new LimeTest(7); $engine = new Engine(new FilesystemLoader('/')); // __construct() $t->diag('__construct()'); $helperSet = new HelperSet(array('foo' => $helper = new SimpleHelper('foo'))); $t->ok($helperSet->has('foo'), '__construct() takes an array of helpers as its first argument'); // ->setEngine() $t->diag('->getEngine()'); $helperSet = new HelperSet(array('foo' => $helper = new SimpleHelper('foo'))); $t->ok($helper->getHelperSet() === $helperSet, '->__construct() changes the embedded helper set of the given helpers'); // ->get() ->set() ->has() $t->diag('->getHelper() ->setHelper() ->has()'); $helperSet = new HelperSet(); $helperSet->set($helper = new SimpleHelper('bar')); $t->ok($helper->getHelperSet() === $helperSet, '->set() changes the embedded helper set of the helper'); $t->is((string) $helperSet->get('foo'), 'bar', '->set() sets a helper value'); $t->ok($helperSet->has('foo'), '->has() returns true if the helper is defined'); $t->ok(!$helperSet->has('bar'), '->has() returns false if the helper is not defined'); try { $helperSet->get('bar'); $t->fail('->get() throws an InvalidArgumentException if the helper is not defined'); } catch (InvalidArgumentException $e) { $t->pass('->get() throws an InvalidArgumentException if the helper is not defined'); }
$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');
<?php /* * 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'; 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'); }
$responseMock = $t->mock('Sonata_Response'); $varHolderMock = $t->mock('Sonata_ParameterHolder'); $templateVewMock = $t->mock('Sonata_TemplateView'); $fooController = new FooController($requestMock, $responseMock, $varHolderMock); // @After unset($requestMock); unset($responseMock); unset($varHolderMock); unset($templateVewMock); // @Test: ->dispatch() // @Test: general try { $fooController->dispatch('fail', $templateVewMock); $t->fail('No code should be executed after calling non-existing actions'); } catch (Sonata_Exception_Controller_Action $ex) { $t->pass('An exception is thrown for non-existing actions'); } try { $fooController->dispatch('myBar', $templateVewMock); $t->pass('Existing methods are executed correctly'); } catch (Sonata_Exception_Controller_Action $ex) { $t->fail('No exception should be thrown for existing methods'); } try { $fooController->dispatch('myBaz', $templateVewMock); $t->fail('No code should be executed after calling non-existing methods'); } catch (Sonata_Exception_Controller_Action $ex) { $t->pass('An exception is thrown for non-existing methods'); } // @Test: action returning Sonata_Controller_Action::ACTION_SUCCESS $requestMock->getParameter('resource')->returns('article')->once();
* 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'); }
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'); }
$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() $t->diag('->getParameters()'); $t->is($event->getParameters(), $parameters, '->getParameters() returns the event 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');
$t->is($output->getTitleTitle(), '<strong>escaped!</strong>', '::escape() is recursive'); $t->is($output->getRawValue(), $input, '->getRawValue() returns the unescaped value'); $t->is(Escaper::escape('esc_entities', $output)->getTitle(), '<strong>escaped!</strong>', '::escape() does not double escape an object'); $t->ok(Escaper::escape('esc_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('esc_entities', new Safe(new OutputEscaperTestClass())) instanceof OutputEscaperTestClass, '::escape() returns the original value if it is marked as being safe'); Escaper::markClassAsSafe('OutputEscaperTestClass'); $t->ok(Escaper::escape('esc_entities', new OutputEscaperTestClass()) instanceof OutputEscaperTestClass, '::escape() returns the original value if the object class is marked as being safe'); $t->ok(Escaper::escape('esc_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('esc_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>'), '<strong>escaped!</strong>', '::unescape() returns an unescaped string if the value to unescape is a string'); $t->is(Escaper::unescape('<strong>échappé</strong>'), '<strong>échappé</strong>', '::unescape() returns an unescaped string if the value to unescape is a string'); $t->diag('::unescape() unescapes arrays'); $input = Escaper::escape('esc_entities', array('foo' => '<strong>escaped!</strong>', 'bar' => array('foo' => '<strong>escaped!</strong>'))); $output = Escaper::unescape($input); $t->ok(is_array($output), '::unescape() returns an array if the input is a ArrayDecorator object'); $t->is($output['foo'], '<strong>escaped!</strong>', '::unescape() unescapes all elements of the original array');
$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');
$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');
{ return 'foo'; } } $params = array('name' => 'Fabien', 'obj' => new Object()); $templates = array('1_basic1' => '{{ obj.foo }}', '1_basic2' => '{{ name|upper }}', '1_basic3' => '{% if name %}foo{% endif %}', '1_basic' => '{% if obj.foo %}{{ obj.foo|upper }}{% endif %}'); $t = new LimeTest(9); $t->diag('Sandbox globally set'); $twig = get_environment(false, $templates); $t->is($twig->loadTemplate('1_basic')->render($params), 'FOO', 'Sandbox does nothing if it is disabled globally'); $twig = get_environment(true, $templates); try { $twig->loadTemplate('1_basic1')->render($params); $t->fail('Sandbox throws a SecurityError exception if an unallowed method is called'); } catch (Twig_Sandbox_SecurityError $e) { $t->pass('Sandbox throws a SecurityError exception if an unallowed method is called'); } $twig = get_environment(true, $templates); try { $twig->loadTemplate('1_basic2')->render($params); $t->fail('Sandbox throws a SecurityError exception if an unallowed filter is called'); } catch (Twig_Sandbox_SecurityError $e) { $t->pass('Sandbox throws a SecurityError exception if an unallowed filter is called'); } $twig = get_environment(true, $templates); try { $twig->loadTemplate('1_basic3')->render($params); $t->fail('Sandbox throws a SecurityError exception if an unallowed tag is used in the template'); } catch (Twig_Sandbox_SecurityError $e) { $t->pass('Sandbox throws a SecurityError exception if an unallowed tag is used in the template'); }
$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
$requestMock->getParameter('resource')->returns(null); $requestMock->replay(); $t->is($dispatcher->getControllerClassName($requestMock), null, 'Returns NULL for non-existing request parameter \'resource\''); // @Test: ->getControllerClassName() - existing request parameter 'resource' $requestMock->getParameter('resource')->returns('article'); $requestMock->replay(); $t->is($dispatcher->getControllerClassName($requestMock), 'ArticleController', 'Returns the correct controller name for existing request parameter \'resource\''); $requestMock->verify(); // @Test: ->loadControllerClass // @Test: non-existing controller class $dispatcher->setControllersDir(dirname(__FILE__) . '/../fixtures/controllers'); try { $dispatcher->loadControllerClass('NonExistingController'); $t->fail('No code should be executed after this'); } catch (Sonata_Exception_Dispatcher $ex) { $t->pass('Throws an exception for non-existing controller classes'); } // @Test: existing controller class $dispatcher->setControllersDir(dirname(__FILE__) . '/../fixtures/controllers'); try { $dispatcher->loadControllerClass('ArticleController'); $t->is(class_exists('ArticleController'), true, 'The controller class was loaded correctly'); } catch (Sonata_Exception_Dispatcher $ex) { $t->pass('Throws an exception for non-existing controller classes'); } // @Test: ->dispatch() // @Test: simulating a route string that cannot be resolved $requestMock->getParameter('format')->returns('xml'); $requestMock->getParameter('route')->returns('foo/bar.xml'); $requestMock->replay(); $routeMapMock->resolveRouteString('foo/bar.xml')->returns(false);
$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'));
$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\\CLI\\Command\\HelpCommand', '->getCommand() returns the help command if --help is provided as the input'); // ->getNamespaces() $t->diag('->getNamespaces()');
$templateView->assign('bar', 4711); $t->is($templateView->getTemplateVars(), array('foo' => 42, 'bar' => 4711), 'Template vars are assigned correctly (single)'); // @Test: ->assign() - with array as argument $templateView->assign(array('foo' => 42, 'bar' => 4711)); $t->is($templateView->getTemplateVars(), array('foo' => 42, 'bar' => 4711), 'Template vars are assigned correctly (array)'); // @Test: ->__get() $templateView->assign('foo', 42); $t->is($templateView->foo, 42, 'Template vars retrieved correctly via PHP\'s magic \'__get\' method'); // @Test: ->render() // @Test: non-existing templates $templateView->setDir(dirname(__FILE__) . '/../fixtures/templates'); try { $templateView->render('foo', 'article', 'xml'); $t->fail('No code should be executed after this'); } catch (Sonata_Exception_Template $ex) { $t->pass('Throws an exception for non-existing templates'); } // @Test: existing templates $templateView->setDir(dirname(__FILE__) . '/../fixtures/templates'); $expectedData = <<<EOF <?xml version="1.0" encoding="utf-8" ?> <rsp stat="ok"> <article id="123"> <title>Example</title> <body>My article example</body> <author>John Doe</author> </article> </rsp> EOF; $t->is($templateView->render('list', 'article', 'xml'), $expectedData, 'The template is rendered correctly');
$configFile = dirname(__FILE__) . '/../fixtures/config/foo.yml'; $cp = new Sonata_Parser_Config(new MyParserDriver()); // @After unlink($configFile); unset($cp); // @Test: ->getDriver() $t->is($cp->getDriver() instanceof MyParserDriver, true, 'The driver was returned correctly'); // @Test: ->setDriver() $cp->setDriver(new MyOtherParserDriver()); $t->is($cp->getDriver() instanceof MyOtherParserDriver, true, 'The new driver was set correctly'); // @Test: ->parse() try { $cp->parse('some_non_existing_file.yml'); $t->fail('No code should be executed after this'); } catch (RuntimeException $ex) { $t->pass('An exception is thrown if the config file cannot be found'); } $driverStub = $t->stub('MyParserDriver'); $driverStub->doParse($configFile)->throws('InvalidArgumentException'); $driverStub->replay(); $cp->setDriver($driverStub); try { $cp->parse($configFile); $t->fail('No code should be executed after this'); } catch (InvalidArgumentException $ex) { $t->pass('An exception thrown by the driver will also be thrown by the config parser'); } $driverStub = $t->stub('MyParserDriver'); $driverStub->doParse($configFile)->returns(array("foo" => array("bar" => 42, "baz" => 4711))); $driverStub->replay(); $cp->setDriver($driverStub);