SlotsHelperTest.php 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Tests\Components\Templating\Helper;
  11. use Symfony\Components\Templating\Helper\SlotsHelper;
  12. class SlotsHelperTest extends \PHPUnit_Framework_TestCase
  13. {
  14. public function testHasGetSet()
  15. {
  16. $helper = new SlotsHelper();
  17. $helper->set('foo', 'bar');
  18. $this->assertEquals('bar', $helper->get('foo'), '->set() sets a slot value');
  19. $this->assertEquals('bar', $helper->get('bar', 'bar'), '->get() takes a default value to return if the slot does not exist');
  20. $this->assertTrue($helper->has('foo'), '->has() returns true if the slot exists');
  21. $this->assertTrue(!$helper->has('bar'), '->has() returns false if the slot does not exist');
  22. }
  23. public function testOutput()
  24. {
  25. $helper = new SlotsHelper();
  26. $helper->set('foo', 'bar');
  27. ob_start();
  28. $ret = $helper->output('foo');
  29. $output = ob_get_clean();
  30. $this->assertEquals('bar', $output, '->output() outputs the content of a slot');
  31. $this->assertEquals(true, $ret, '->output() returns true if the slot exists');
  32. ob_start();
  33. $ret = $helper->output('bar', 'bar');
  34. $output = ob_get_clean();
  35. $this->assertEquals('bar', $output, '->output() takes a default value to return if the slot does not exist');
  36. $this->assertEquals(true, $ret, '->output() returns true if the slot does not exist but a default value is provided');
  37. ob_start();
  38. $ret = $helper->output('bar');
  39. $output = ob_get_clean();
  40. $this->assertEquals('', $output, '->output() outputs nothing if the slot does not exist');
  41. $this->assertEquals(false, $ret, '->output() returns false if the slot does not exist');
  42. }
  43. public function testStartStop()
  44. {
  45. $helper = new SlotsHelper();
  46. $helper->start('bar');
  47. echo 'foo';
  48. $helper->stop();
  49. $this->assertEquals('foo', $helper->get('bar'), '->start() starts a slot');
  50. $this->assertTrue($helper->has('bar'), '->starts() starts a slot');
  51. $helper->start('bar');
  52. try
  53. {
  54. $helper->start('bar');
  55. $helper->stop();
  56. $this->fail('->start() throws an InvalidArgumentException if a slot with the same name is already started');
  57. }
  58. catch (\InvalidArgumentException $e)
  59. {
  60. $helper->stop();
  61. }
  62. try
  63. {
  64. $helper->stop();
  65. $this->fail('->stop() throws an LogicException if no slot is started');
  66. }
  67. catch (\LogicException $e)
  68. {
  69. }
  70. }
  71. }