SlotsHelperTest.php 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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->assertFalse($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->assertTrue($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->assertTrue($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->assertFalse($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 (\Exception $e)
  59. {
  60. $helper->stop();
  61. $this->assertType('\InvalidArgumentException', $e, '->start() throws an InvalidArgumentException if a slot with the same name is already started');
  62. $this->assertEquals('A slot named "bar" is already started.', $e->getMessage(), '->start() throws an InvalidArgumentException if a slot with the same name is already started');
  63. }
  64. try
  65. {
  66. $helper->stop();
  67. $this->fail('->stop() throws an LogicException if no slot is started');
  68. }
  69. catch (\Exception $e)
  70. {
  71. $this->assertType('\LogicException', $e, '->stop() throws an LogicException if no slot is started');
  72. $this->assertEquals('No slot started.', $e->getMessage(), '->stop() throws an LogicException if no slot is started');
  73. }
  74. }
  75. }