Completed
Push — develop ( a9d829...037456 )
by Jens
04:55 queued 43s
created

HandlebarsEngineTest   A

Complexity

Total Complexity 6

Size/Duplication

Total Lines 87
Duplicated Lines 60.92 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 1
Bugs 0 Features 1
Metric Value
wmc 6
c 1
b 0
f 1
lcom 1
cbo 4
dl 53
loc 87
rs 10

How to fix   Duplicated Code   

Duplicated Code

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
2
/**
3
 * @author @jayS-de <[email protected]>
4
 */
5
6
7
namespace JaySDe\HandlebarsBundle\Tests;
8
9
10
use JaySDe\HandlebarsBundle\HandlebarsEngine;
11
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