BaseWidgetTest.php 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. <?php
  2. /*
  3. * This file is part of the Sonata Project package.
  4. *
  5. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  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 Sonata\AdminBundle\Tests\Form\Widget;
  11. use Sonata\CoreBundle\Test\AbstractWidgetTestCase;
  12. use Symfony\Bridge\Twig\Extension\TranslationExtension;
  13. use Symfony\Bridge\Twig\Form\TwigRendererEngine;
  14. use Symfony\Bundle\FrameworkBundle\Tests\Templating\Helper\Fixtures\StubTranslator;
  15. /**
  16. * Class BaseWidgetTest.
  17. *
  18. * Base class for tests checking rendering of form widgets with form_admin_fields.html.twig and
  19. * filter_admin_fields.html.twig. Template to use is defined by $this->type variable, that needs to be overridden in
  20. * child classes.
  21. */
  22. abstract class BaseWidgetTest extends AbstractWidgetTestCase
  23. {
  24. /**
  25. * Current template type, form or filter.
  26. *
  27. * @var string
  28. */
  29. protected $type = null;
  30. /**
  31. * @var array
  32. */
  33. protected $sonataAdmin = array(
  34. 'name' => null,
  35. 'admin' => null,
  36. 'value' => null,
  37. 'edit' => 'standard',
  38. 'inline' => 'natural',
  39. 'field_description' => null,
  40. 'block_name' => false,
  41. 'options' => array(
  42. 'form_type' => 'vertical',
  43. 'use_icheck' => true,
  44. ),
  45. );
  46. /**
  47. * {@inheritdoc}
  48. */
  49. protected function getEnvironment()
  50. {
  51. $environment = parent::getEnvironment();
  52. $environment->addGlobal('sonata_admin', $this->getSonataAdmin());
  53. if (!$environment->hasExtension('translator')) {
  54. $environment->addExtension(new TranslationExtension(new StubTranslator()));
  55. }
  56. return $environment;
  57. }
  58. /**
  59. * {@inheritdoc}
  60. */
  61. protected function getRenderingEngine()
  62. {
  63. if (!in_array($this->type, array('form', 'filter'))) {
  64. throw new \Exception('Please override $this->type in your test class specifying template to use (either form or filter)');
  65. }
  66. return new TwigRendererEngine(array(
  67. $this->type.'_admin_fields.html.twig',
  68. ));
  69. }
  70. /**
  71. * {@inheritdoc}
  72. */
  73. protected function getSonataAdmin()
  74. {
  75. return $this->sonataAdmin;
  76. }
  77. /**
  78. * {@inheritdoc}
  79. */
  80. protected function getTemplatePaths()
  81. {
  82. return array_merge(parent::getTemplatePaths(), array(
  83. __DIR__.'/../../../Resources/views/Form',
  84. ));
  85. }
  86. }