1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace spec\Slick\Mvc\Http\Renderer\TemplateExtension; |
4
|
|
|
|
5
|
|
|
use PhpSpec\Exception\Example\FailureException; |
6
|
|
|
use Slick\Mvc\Http\Renderer\TemplateExtension\HtmlExtension; |
7
|
|
|
use PhpSpec\ObjectBehavior; |
8
|
|
|
use Prophecy\Argument; |
9
|
|
|
use Slick\Mvc\Service\UriGeneratorInterface; |
10
|
|
|
use Slick\Template\EngineExtensionInterface; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Html Extension Spec |
14
|
|
|
* |
15
|
|
|
* @package spec\Slick\Mvc\Http\Renderer\TemplateExtension |
16
|
|
|
* @author Filipe Silva <[email protected]> |
17
|
|
|
*/ |
18
|
|
|
class HtmlExtensionSpec extends ObjectBehavior |
19
|
|
|
{ |
20
|
|
|
function let(UriGeneratorInterface $generator) |
21
|
|
|
{ |
22
|
|
|
$this->beConstructedWith($generator); |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
function it_is_initializable() |
26
|
|
|
{ |
27
|
|
|
$this->shouldHaveType(HtmlExtension::class); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
function its_a_template_engine_extension() |
31
|
|
|
{ |
32
|
|
|
$this->shouldBeAnInstanceOf(EngineExtensionInterface::class); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
function it_defines_a_template_function_for_url_output() |
36
|
|
|
{ |
37
|
|
|
$this->getFunctions()->shouldHaveAFunctionNamed('url'); |
38
|
|
|
} |
39
|
|
|
|
40
|
|
View Code Duplication |
function it_uses_an_uri_generator_to_output_the_url( |
|
|
|
|
41
|
|
|
UriGeneratorInterface $generator |
42
|
|
|
) |
43
|
|
|
{ |
44
|
|
|
$generator->generate('test', []) |
45
|
|
|
->shouldBeCalled() |
46
|
|
|
->willReturn('/test'); |
47
|
|
|
|
48
|
|
|
$this->beConstructedWith($generator); |
49
|
|
|
|
50
|
|
|
/** @var \Twig_SimpleFunction $func */ |
51
|
|
|
$func = $this->getFunctions()['url']; |
52
|
|
|
call_user_func_array($func->getCallable(), ['test']); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
function it_defines_a_template_function_for_css_include_tag() |
56
|
|
|
{ |
57
|
|
|
$this->getFunctions()->shouldHaveAFunctionNamed('addCss'); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
View Code Duplication |
function it_uses_an_uri_generator_to_output_the_css_url( |
|
|
|
|
61
|
|
|
UriGeneratorInterface $generator |
62
|
|
|
) |
63
|
|
|
{ |
64
|
|
|
$generator->generate('css/test.css', []) |
65
|
|
|
->shouldBeCalled() |
66
|
|
|
->willReturn('/css/test.css'); |
67
|
|
|
|
68
|
|
|
$this->beConstructedWith($generator); |
69
|
|
|
|
70
|
|
|
/** @var \Twig_SimpleFunction $func */ |
71
|
|
|
$func = $this->getFunctions()['addCss']; |
72
|
|
|
call_user_func_array($func->getCallable(), ['test.css']); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
function it_defines_a_template_function_for_add_javascript_tag() |
76
|
|
|
{ |
77
|
|
|
$this->getFunctions()->shouldHaveAFunctionNamed('addJs'); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
View Code Duplication |
function it_uses_an_uri_generator_to_output_the_js_url( |
|
|
|
|
81
|
|
|
UriGeneratorInterface $generator |
82
|
|
|
) |
83
|
|
|
{ |
84
|
|
|
$generator->generate('js/test.js', []) |
85
|
|
|
->shouldBeCalled() |
86
|
|
|
->willReturn('/js/test.js'); |
87
|
|
|
|
88
|
|
|
$this->beConstructedWith($generator); |
89
|
|
|
|
90
|
|
|
/** @var \Twig_SimpleFunction $func */ |
91
|
|
|
$func = $this->getFunctions()['addJs']; |
92
|
|
|
call_user_func_array($func->getCallable(), ['test.js']); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
public function getMatchers() |
96
|
|
|
{ |
97
|
|
|
return [ |
98
|
|
|
'haveAFunctionNamed' => function ($subject, $name) { |
99
|
|
|
$type = gettype($subject); |
100
|
|
|
if (!is_array($subject)) { |
101
|
|
|
throw new FailureException( |
102
|
|
|
"Expected an array of functions, but got {$type}." |
103
|
|
|
); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
$found = null; |
|
|
|
|
107
|
|
|
foreach ($subject as $function) { |
108
|
|
|
if (!$function instanceof \Twig_SimpleFunction) { |
109
|
|
|
continue; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
if ($function->getName() == $name) { |
113
|
|
|
return true; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
throw new FailureException( |
118
|
|
|
"Returned function list does not have a function ". |
119
|
|
|
"named '{$name}'." |
120
|
|
|
); |
121
|
|
|
} |
122
|
|
|
]; |
123
|
|
|
} |
124
|
|
|
} |
125
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.