SlotsHelperTest.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. require_once __DIR__.'/../../../bootstrap.php';
  12. use Symfony\Components\Templating\Helper\SlotsHelper;
  13. class SlotsHelperTest extends \PHPUnit_Framework_TestCase
  14. {
  15. public function testHasGetSet()
  16. {
  17. $helper = new SlotsHelper();
  18. $helper->set('foo', 'bar');
  19. $this->assertEquals($helper->get('foo'), 'bar', '->set() sets a slot value');
  20. $this->assertEquals($helper->get('bar', 'bar'), 'bar', '->get() takes a default value to return if the slot does not exist');
  21. $this->assertTrue($helper->has('foo'), '->has() returns true if the slot exists');
  22. $this->assertTrue(!$helper->has('bar'), '->has() returns false if the slot does not exist');
  23. }
  24. public function testOutput()
  25. {
  26. $helper = new SlotsHelper();
  27. $helper->set('foo', 'bar');
  28. ob_start();
  29. $ret = $helper->output('foo');
  30. $output = ob_get_clean();
  31. $this->assertEquals($output, 'bar', '->output() outputs the content of a slot');
  32. $this->assertEquals($ret, true, '->output() returns true if the slot exists');
  33. ob_start();
  34. $ret = $helper->output('bar', 'bar');
  35. $output = ob_get_clean();
  36. $this->assertEquals($output, 'bar', '->output() takes a default value to return if the slot does not exist');
  37. $this->assertEquals($ret, true, '->output() returns true if the slot does not exist but a default value is provided');
  38. ob_start();
  39. $ret = $helper->output('bar');
  40. $output = ob_get_clean();
  41. $this->assertEquals($output, '', '->output() outputs nothing if the slot does not exist');
  42. $this->assertEquals($ret, false, '->output() returns false if the slot does not exist');
  43. }
  44. public function testStartStop()
  45. {
  46. $helper = new SlotsHelper();
  47. $helper->start('bar');
  48. echo 'foo';
  49. $helper->stop();
  50. $this->assertEquals($helper->get('bar'), 'foo', '->start() starts a slot');
  51. $this->assertTrue($helper->has('bar'), '->starts() starts a slot');
  52. $helper->start('bar');
  53. try
  54. {
  55. $helper->start('bar');
  56. $helper->stop();
  57. $this->fail('->start() throws an InvalidArgumentException if a slot with the same name is already started');
  58. }
  59. catch (\InvalidArgumentException $e)
  60. {
  61. $helper->stop();
  62. }
  63. try
  64. {
  65. $helper->stop();
  66. $this->fail('->stop() throws an LogicException if no slot is started');
  67. }
  68. catch (\LogicException $e)
  69. {
  70. }
  71. }
  72. }