Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
| 1 | <?php |
||
| 26 | class FormExtraExtensionTest extends \PHPUnit_Framework_TestCase |
||
| 27 | { |
||
| 28 | /** @var \Twig_Environment */ |
||
| 29 | private $twig; |
||
| 30 | |||
| 31 | /** @var \Symfony\Component\Form\FormFactoryInterface */ |
||
| 32 | private $formFactory; |
||
| 33 | |||
| 34 | /** @var \Symfony\Bridge\Twig\Form\TwigRenderer */ |
||
| 35 | private $formRenderer; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * {@inheritdoc} |
||
| 39 | */ |
||
| 40 | protected function setUp() |
||
| 41 | { |
||
| 42 | $this->formFactory = Forms::createFormFactory(); |
||
| 43 | $this->formRenderer = new TwigRenderer(new TwigRendererEngine(array( |
||
| 44 | 'javascript.html.twig', |
||
| 45 | 'stylesheet.html.twig', |
||
| 46 | ))); |
||
| 47 | |||
| 48 | $this->twig = new \Twig_Environment(new \Twig_Loader_Filesystem(array( |
||
| 49 | __DIR__.'/../../Resources/views/Form', |
||
| 50 | __DIR__.'/../Fixtures/views/Twig', |
||
| 51 | ))); |
||
| 52 | |||
| 53 | $this->twig->addExtension($formExtension = new FormExtension($this->formRenderer)); |
||
| 54 | $this->twig->addExtension(new FormExtraExtension($this->formRenderer)); |
||
| 55 | |||
| 56 | $formExtension->initRuntime($this->twig); |
||
| 57 | } |
||
| 58 | |||
| 59 | /** |
||
| 60 | * {@inheritdoc} |
||
| 61 | */ |
||
| 62 | protected function tearDown() |
||
| 63 | { |
||
| 64 | unset($this->formRenderer); |
||
| 65 | unset($this->formFactory); |
||
| 66 | unset($this->twig); |
||
| 67 | } |
||
| 68 | |||
| 69 | public function testDefaultJavascriptFragment() |
||
| 70 | { |
||
| 71 | $form = $this->formFactory->createBuilder() |
||
| 72 | ->add('text', $this->getFormType('text')) |
||
| 73 | ->add('submit', $this->getFormType('submit')) |
||
| 74 | ->getForm() |
||
| 75 | ->createView(); |
||
| 76 | |||
| 77 | $template = $this->twig->createTemplate('{{ form_javascript(form) }}'); |
||
| 78 | |||
| 79 | $this->assertEmpty($template->render(array('form' => $form))); |
||
| 80 | } |
||
| 81 | |||
| 82 | public function testCustomJavascriptFragment() |
||
| 83 | { |
||
| 84 | $form = $this->formFactory->createBuilder() |
||
| 85 | ->add('text', $this->getFormType('text')) |
||
| 86 | ->add('submit', $this->getFormType('submit')) |
||
| 87 | ->getForm() |
||
| 88 | ->createView(); |
||
| 89 | |||
| 90 | $this->formRenderer->setTheme($form, 'javascript_custom.html.twig'); |
||
| 91 | |||
| 92 | $template = $this->twig->createTemplate('{{ form_javascript(form) }}'); |
||
| 93 | |||
| 94 | $expected = '<script type="text/javascript">text-javascript</script>'; |
||
| 95 | $expected .= '<script type="text/javascript">submit-javascript</script>'; |
||
| 96 | |||
| 97 | $this->assertSame($expected, $template->render(array('form' => $form))); |
||
| 98 | } |
||
| 99 | |||
| 100 | public function testInheritanceJavascriptFragment() |
||
| 101 | { |
||
| 102 | $form = $this->formFactory->createBuilder() |
||
| 103 | ->add('textarea', $this->getFormType('textarea')) |
||
| 104 | ->add('submit', $this->getFormType('submit')) |
||
| 105 | ->getForm() |
||
| 106 | ->createView(); |
||
| 107 | |||
| 108 | $this->formRenderer->setTheme($form, 'javascript_inheritance.html.twig'); |
||
| 109 | |||
| 110 | $template = $this->twig->createTemplate('{{ form_javascript(form) }}'); |
||
| 111 | |||
| 112 | $expected = '<script type="text/javascript">text-javascript</script>'; |
||
| 113 | $expected .= '<script type="text/javascript">button-javascript</script>'; |
||
| 114 | |||
| 115 | $this->assertSame($expected, $template->render(array('form' => $form))); |
||
| 116 | } |
||
| 117 | |||
| 118 | public function testDefaultStylesheetFragment() |
||
| 119 | { |
||
| 120 | $form = $this->formFactory->createBuilder() |
||
| 121 | ->add('text', $this->getFormType('text')) |
||
| 122 | ->add('submit', $this->getFormType('submit')) |
||
| 123 | ->getForm() |
||
| 124 | ->createView(); |
||
| 125 | |||
| 126 | $template = $this->twig->createTemplate('{{ form_stylesheet(form) }}'); |
||
| 127 | |||
| 128 | $this->assertEmpty($template->render(array('form' => $form))); |
||
| 129 | } |
||
| 130 | |||
| 131 | public function testCustomStylesheetFragment() |
||
| 132 | { |
||
| 133 | $form = $this->formFactory->createBuilder() |
||
| 134 | ->add('text', $this->getFormType('text')) |
||
| 135 | ->add('submit', $this->getFormType('submit')) |
||
| 136 | ->getForm() |
||
| 137 | ->createView(); |
||
| 138 | |||
| 139 | $this->formRenderer->setTheme($form, 'stylesheet_custom.html.twig'); |
||
| 140 | |||
| 141 | $template = $this->twig->createTemplate('{{ form_stylesheet(form) }}'); |
||
| 142 | |||
| 143 | $expected = '<style type="text/css">text-stylesheet</style>'; |
||
| 144 | $expected .= '<style type="text/css">submit-stylesheet</style>'; |
||
| 145 | |||
| 146 | $this->assertSame($expected, $template->render(array('form' => $form))); |
||
| 147 | } |
||
| 148 | |||
| 149 | public function testInheritanceStylesheetFragment() |
||
| 150 | { |
||
| 151 | $form = $this->formFactory->createBuilder() |
||
| 152 | ->add('textarea', $this->getFormType('textarea')) |
||
| 153 | ->add('submit', $this->getFormType('submit')) |
||
| 154 | ->getForm() |
||
| 155 | ->createView(); |
||
| 156 | |||
| 157 | $this->formRenderer->setTheme($form, 'stylesheet_inheritance.html.twig'); |
||
| 158 | |||
| 159 | $template = $this->twig->createTemplate('{{ form_stylesheet(form) }}'); |
||
| 160 | |||
| 161 | $expected = '<style type="text/css">text-stylesheet</style>'; |
||
| 162 | $expected .= '<style type="text/css">button-stylesheet</style>'; |
||
| 163 | |||
| 164 | $this->assertSame($expected, $template->render(array('form' => $form))); |
||
| 165 | } |
||
| 166 | |||
| 167 | /** |
||
| 168 | * Gets the form type according to the Symfony version. |
||
| 169 | * |
||
| 170 | * @param string $type The form type. |
||
| 171 | * |
||
| 172 | * @return string The form type. |
||
| 173 | */ |
||
| 174 | private function getFormType($type) |
||
| 175 | { |
||
| 176 | return method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix') |
||
| 177 | ? 'Symfony\Component\Form\Extension\Core\Type\\'.ucfirst($type).'Type' |
||
| 178 | : $type; |
||
| 179 | } |
||
| 180 | } |
||
| 181 |