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 |
||
| 28 | class FormExtraHelperTest extends \PHPUnit_Framework_TestCase |
||
| 29 | { |
||
| 30 | /** @var \Symfony\Component\Templating\PhpEngine */ |
||
| 31 | private $phpEngine; |
||
| 32 | |||
| 33 | /** @var \Symfony\Component\Form\FormFactoryInterface */ |
||
| 34 | private $formFactory; |
||
| 35 | |||
| 36 | /** @var \Symfony\Bridge\Twig\Form\TwigRenderer */ |
||
| 37 | private $formRenderer; |
||
| 38 | |||
| 39 | /** |
||
| 40 | * {@inheritdoc} |
||
| 41 | */ |
||
| 42 | protected function setUp() |
||
| 43 | { |
||
| 44 | $this->phpEngine = new PhpEngine(new TemplateNameParser(), new FilesystemLoader(array( |
||
| 45 | __DIR__.'/../../Resources/views/Form/%name%', |
||
| 46 | __DIR__.'/../Fixtures/views/Templating/%name%', |
||
| 47 | ))); |
||
| 48 | |||
| 49 | $this->formFactory = Forms::createFormFactory(); |
||
| 50 | $this->formRenderer = new FormRenderer(new TemplatingRendererEngine($this->phpEngine, array(''))); |
||
| 51 | $this->phpEngine->addHelpers(array('ivory_form_extra' => new FormExtraHelper($this->formRenderer))); |
||
| 52 | } |
||
| 53 | |||
| 54 | /** |
||
| 55 | * {@inheritdoc} |
||
| 56 | */ |
||
| 57 | protected function tearDown() |
||
| 58 | { |
||
| 59 | unset($this->formRenderer); |
||
| 60 | unset($this->formFactory); |
||
| 61 | unset($this->phpEngine); |
||
| 62 | } |
||
| 63 | |||
| 64 | public function testDefaultJavascriptFragment() |
||
| 65 | { |
||
| 66 | $form = $this->formFactory->createBuilder() |
||
| 67 | ->add('text', $this->getFormType('text')) |
||
| 68 | ->add('submit', $this->getFormType('submit')) |
||
| 69 | ->getForm() |
||
| 70 | ->createView(); |
||
| 71 | |||
| 72 | $this->assertEmpty($this->normalize($this->phpEngine->render('javascript.html.php', array('form' => $form)))); |
||
| 73 | } |
||
| 74 | |||
| 75 | public function testCustomJavascriptFragment() |
||
| 76 | { |
||
| 77 | $form = $this->formFactory->createBuilder() |
||
| 78 | ->add('text', $this->getFormType('text')) |
||
| 79 | ->add('submit', $this->getFormType('submit')) |
||
| 80 | ->getForm() |
||
| 81 | ->createView(); |
||
| 82 | |||
| 83 | $this->formRenderer->setTheme($form, 'Custom'); |
||
| 84 | |||
| 85 | $expected = '<script type="text/javascript">text-javascript</script>'; |
||
| 86 | $expected .= '<script type="text/javascript">submit-javascript</script>'; |
||
| 87 | |||
| 88 | $this->assertSame( |
||
| 89 | $expected, |
||
| 90 | $this->normalize($this->phpEngine->render('javascript.html.php', array('form' => $form))) |
||
| 91 | ); |
||
| 92 | } |
||
| 93 | |||
| 94 | public function testInheritanceJavascriptFragment() |
||
| 95 | { |
||
| 96 | $form = $this->formFactory->createBuilder() |
||
| 97 | ->add('textarea', $this->getFormType('textarea')) |
||
| 98 | ->add('submit', $this->getFormType('submit')) |
||
| 99 | ->getForm() |
||
| 100 | ->createView(); |
||
| 101 | |||
| 102 | $this->formRenderer->setTheme($form, 'Inheritance'); |
||
| 103 | |||
| 104 | $expected = '<script type="text/javascript">text-javascript</script>'; |
||
| 105 | $expected .= '<script type="text/javascript">button-javascript</script>'; |
||
| 106 | |||
| 107 | $this->assertSame( |
||
| 108 | $expected, |
||
| 109 | $this->normalize($this->phpEngine->render('javascript.html.php', array('form' => $form))) |
||
| 110 | ); |
||
| 111 | } |
||
| 112 | |||
| 113 | public function testDefaultStylesheetFragment() |
||
| 114 | { |
||
| 115 | $form = $this->formFactory->createBuilder() |
||
| 116 | ->add('text', $this->getFormType('text')) |
||
| 117 | ->add('submit', $this->getFormType('submit')) |
||
| 118 | ->getForm() |
||
| 119 | ->createView(); |
||
| 120 | |||
| 121 | $this->assertEmpty($this->normalize($this->phpEngine->render('stylesheet.html.php', array('form' => $form)))); |
||
| 122 | } |
||
| 123 | |||
| 124 | public function testCustomStylesheetFragment() |
||
| 125 | { |
||
| 126 | $form = $this->formFactory->createBuilder() |
||
| 127 | ->add('text', $this->getFormType('text')) |
||
| 128 | ->add('submit', $this->getFormType('submit')) |
||
| 129 | ->getForm() |
||
| 130 | ->createView(); |
||
| 131 | |||
| 132 | $this->formRenderer->setTheme($form, 'Custom'); |
||
| 133 | |||
| 134 | $expected = '<style type="text/css">text-stylesheet</style>'; |
||
| 135 | $expected .= '<style type="text/css">submit-stylesheet</style>'; |
||
| 136 | |||
| 137 | $this->assertSame( |
||
| 138 | $expected, |
||
| 139 | $this->normalize($this->phpEngine->render('stylesheet.html.php', array('form' => $form))) |
||
| 140 | ); |
||
| 141 | } |
||
| 142 | |||
| 143 | public function testInheritanceStylesheetFragment() |
||
| 144 | { |
||
| 145 | $form = $this->formFactory->createBuilder() |
||
| 146 | ->add('textarea', $this->getFormType('textarea')) |
||
| 147 | ->add('submit', $this->getFormType('submit')) |
||
| 148 | ->getForm() |
||
| 149 | ->createView(); |
||
| 150 | |||
| 151 | $this->formRenderer->setTheme($form, 'Inheritance'); |
||
| 152 | |||
| 153 | $expected = '<style type="text/css">text-stylesheet</style>'; |
||
| 154 | $expected .= '<style type="text/css">button-stylesheet</style>'; |
||
| 155 | |||
| 156 | $this->assertSame( |
||
| 157 | $expected, |
||
| 158 | $this->normalize($this->phpEngine->render('stylesheet.html.php', array('form' => $form))) |
||
| 159 | ); |
||
| 160 | } |
||
| 161 | |||
| 162 | /** |
||
| 163 | * Gets the form type according to the Symfony version. |
||
| 164 | * |
||
| 165 | * @param string $type The form type. |
||
| 166 | * |
||
| 167 | * @return string The form type. |
||
| 168 | */ |
||
| 169 | private function getFormType($type) |
||
| 170 | { |
||
| 171 | return method_exists('Symfony\Component\Form\AbstractType', 'getBlockPrefix') |
||
| 172 | ? 'Symfony\Component\Form\Extension\Core\Type\\'.ucfirst($type).'Type' |
||
| 173 | : $type; |
||
| 174 | } |
||
| 175 | |||
| 176 | /** |
||
| 177 | * Normalizes the output as there is no space control in the PHP templating component. |
||
| 178 | * |
||
| 179 | * @param string $output The output. |
||
| 180 | * |
||
| 181 | * @return string The normalized output. |
||
| 182 | */ |
||
| 183 | private function normalize($output) |
||
| 184 | { |
||
| 185 | return trim(preg_replace('/>\s+</', '><', preg_replace('/\s+/', ' ', $output))); |
||
| 186 | } |
||
| 187 | } |
||
| 188 | |||
| 212 |