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 |
||
| 15 | class FrontRenderTest extends \PHPUnit_Framework_TestCase |
||
| 16 | { |
||
| 17 | /** |
||
| 18 | * @var string TEMPLATE_PATH |
||
| 19 | */ |
||
| 20 | const TEMPLATE_PATH = 'Tests/Template'; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * @var string TEMPLATE |
||
| 24 | */ |
||
| 25 | const TEMPLATE = 'index.html.twig'; |
||
| 26 | |||
| 27 | /** |
||
| 28 | * @var string FAILED_TEMPLATE |
||
| 29 | */ |
||
| 30 | const FAILED_TEMPLATE = 'failed.html.twig'; |
||
| 31 | |||
| 32 | /** |
||
| 33 | * @var string FAILED_PATH |
||
| 34 | */ |
||
| 35 | const FAILED_PATH = 'failedPath.html.twig'; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var EventDispatcher |
||
| 39 | */ |
||
| 40 | protected $eventDispatcher; |
||
| 41 | |||
| 42 | /** |
||
| 43 | * @var \Twig_Loader_Filesystem |
||
| 44 | */ |
||
| 45 | protected $twigLoader; |
||
| 46 | |||
| 47 | /** |
||
| 48 | * @var \Twig_Environment |
||
| 49 | */ |
||
| 50 | protected $twigEnvironment; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * @var TwigListener |
||
| 54 | */ |
||
| 55 | protected $twigListener; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * @var TemplateNameParser |
||
| 59 | */ |
||
| 60 | protected $templateNameParser; |
||
| 61 | |||
| 62 | /** |
||
| 63 | * @var FileLocator |
||
| 64 | */ |
||
| 65 | protected $fileLocator; |
||
| 66 | |||
| 67 | /** |
||
| 68 | * @var TwigEngine |
||
| 69 | */ |
||
| 70 | protected $engine; |
||
| 71 | |||
| 72 | /** |
||
| 73 | * @var FrontRender |
||
| 74 | */ |
||
| 75 | protected $frontRender; |
||
| 76 | |||
| 77 | /** |
||
| 78 | * Set up the front render test |
||
| 79 | */ |
||
| 80 | public function setUp() |
||
| 96 | |||
| 97 | /** |
||
| 98 | * Test render of the template with parameter |
||
| 99 | */ |
||
| 100 | View Code Duplication | public function testTemplateRenderWithParameter() |
|
| 115 | |||
| 116 | /** |
||
| 117 | * Test render of the template without parameter |
||
| 118 | */ |
||
| 119 | View Code Duplication | public function testTemplateRenderWithoutParameter() |
|
| 128 | |||
| 129 | /** |
||
| 130 | * Test on the update of lexer |
||
| 131 | * |
||
| 132 | * @dataProvider lexerTags |
||
| 133 | * |
||
| 134 | * @param string $oldTags |
||
| 135 | * @param string $newTags |
||
| 136 | */ |
||
| 137 | public function testSetLexer($oldTags, $newTags) |
||
| 159 | |||
| 160 | /** |
||
| 161 | * Test on the failed template |
||
| 162 | * |
||
| 163 | * @expectedException Twig_Error_Syntax |
||
| 164 | */ |
||
| 165 | public function testExceptionOnSyntax() |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Test when the template doesn't exist |
||
| 174 | * |
||
| 175 | * @expectedException InvalidArgumentException |
||
| 176 | */ |
||
| 177 | public function testExceptionOnFailedPath() |
||
| 183 | |||
| 184 | /** |
||
| 185 | * Test when the template path is empty |
||
| 186 | * |
||
| 187 | * @expectedException Chris\Bundle\FrontRenderBundle\Exception\FrontRenderException |
||
| 188 | */ |
||
| 189 | public function testExceptionOnEmptyPath() |
||
| 195 | |||
| 196 | /** |
||
| 197 | * Provide the lexer tags |
||
| 198 | * |
||
| 199 | * @return array |
||
| 200 | */ |
||
| 201 | public function lexerTags() |
||
| 210 | |||
| 211 | |||
| 212 | } |
||
| 213 |
Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable: