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 |
||
12 | class HandlebarsEngineTest extends \PHPUnit_Framework_TestCase |
||
13 | { |
||
14 | public function testRender() |
||
15 | { |
||
16 | $environment = $this->prophesize('\JaySDe\HandlebarsBundle\HandlebarsEnvironment'); |
||
17 | $environment->render('test', [])->willReturn('test')->shouldBeCalled(); |
||
18 | $parser = $this->prophesize('\Symfony\Component\Templating\TemplateNameParserInterface'); |
||
19 | $engine = new HandlebarsEngine($environment->reveal(), $parser->reveal()); |
||
20 | |||
21 | $result = $engine->render('test', []); |
||
22 | $this->assertSame('test', $result); |
||
23 | } |
||
24 | |||
25 | public function testExists() |
||
26 | { |
||
27 | $loader = $this->prophesize('\JaySDe\HandlebarsBundle\Loader\FilesystemLoader'); |
||
28 | $loader->exists('test')->willReturn(true)->shouldBeCalled(); |
||
29 | |||
30 | $environment = $this->prophesize('\JaySDe\HandlebarsBundle\HandlebarsEnvironment'); |
||
31 | $environment->getLoader()->willReturn($loader->reveal())->shouldBeCalled(); |
||
32 | |||
33 | $parser = $this->prophesize('\Symfony\Component\Templating\TemplateNameParserInterface'); |
||
34 | $engine = new HandlebarsEngine($environment->reveal(), $parser->reveal()); |
||
35 | |||
36 | $result = $engine->exists('test'); |
||
37 | $this->assertTrue($result); |
||
38 | } |
||
39 | |||
40 | public function getEngineType() |
||
41 | { |
||
42 | return [ |
||
43 | ['hbs', true], |
||
44 | ['handlebars', true], |
||
45 | ['twig', false], |
||
46 | ['foo', false], |
||
47 | ]; |
||
48 | } |
||
49 | /** |
||
50 | * @dataProvider getEngineType |
||
51 | * @param $engineType |
||
52 | */ |
||
53 | public function testSupports($engineType, $expectedResult) |
||
54 | { |
||
55 | $template = $this->prophesize('\Symfony\Component\Templating\TemplateReferenceInterface'); |
||
56 | $template->get('engine')->willReturn($engineType)->shouldBeCalled(); |
||
57 | |||
58 | $environment = $this->prophesize('\JaySDe\HandlebarsBundle\HandlebarsEnvironment'); |
||
59 | |||
60 | $parser = $this->prophesize('\Symfony\Component\Templating\TemplateNameParserInterface'); |
||
61 | $parser->parse('test')->willReturn($template->reveal())->shouldBeCalled(); |
||
62 | |||
63 | $engine = new HandlebarsEngine($environment->reveal(), $parser->reveal()); |
||
64 | |||
65 | $result = $engine->supports('test'); |
||
66 | $this->assertSame($expectedResult, $result); |
||
67 | } |
||
68 | |||
69 | public function testRenderResponse() |
||
70 | { |
||
71 | $environment = $this->prophesize('\JaySDe\HandlebarsBundle\HandlebarsEnvironment'); |
||
72 | $environment->render('test.hbs', [])->willReturn('test')->shouldBeCalled(); |
||
73 | |||
74 | $parser = $this->prophesize('\Symfony\Component\Templating\TemplateNameParserInterface'); |
||
75 | |||
76 | $engine = new HandlebarsEngine($environment->reveal(), $parser->reveal()); |
||
77 | |||
78 | $result = $engine->renderResponse('test.hbs'); |
||
79 | |||
80 | $this->assertInstanceOf('\Symfony\Component\HttpFoundation\Response', $result); |
||
81 | $this->assertSame('test', $result->getContent()); |
||
82 | } |
||
83 | |||
84 | public function testRenderGivenResponse() |
||
85 | { |
||
86 | $environment = $this->prophesize('\JaySDe\HandlebarsBundle\HandlebarsEnvironment'); |
||
87 | $environment->render('test.hbs', ['foo' => 'bar'])->willReturn('test')->shouldBeCalled(); |
||
88 | |||
89 | $parser = $this->prophesize('\Symfony\Component\Templating\TemplateNameParserInterface'); |
||
90 | |||
91 | $engine = new HandlebarsEngine($environment->reveal(), $parser->reveal()); |
||
92 | |||
93 | $response = $this->prophesize('\Symfony\Component\HttpFoundation\Response'); |
||
94 | $response->setContent('test')->shouldBeCalled(); |
||
95 | |||
96 | $engine->renderResponse('test.hbs', ['foo' => 'bar'], $response->reveal()); |
||
97 | } |
||
98 | } |
||
99 |