|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace spec\Sylius\Component\Metadata\Renderer\Custom; |
|
4
|
|
|
|
|
5
|
|
|
use PhpSpec\ObjectBehavior; |
|
6
|
|
|
use Prophecy\Argument; |
|
7
|
|
|
use Sylius\Component\Metadata\Model\Custom\PageMetadataInterface; |
|
8
|
|
|
use Sylius\Component\Metadata\Model\MetadataInterface; |
|
9
|
|
|
use Sylius\Component\Metadata\Model\Twitter\CardInterface; |
|
10
|
|
|
use Sylius\Component\Metadata\Renderer\MetadataRendererInterface; |
|
11
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
|
12
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccessorInterface; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @mixin \Sylius\Component\Metadata\Renderer\Custom\PageMetadataRenderer |
|
16
|
|
|
* |
|
17
|
|
|
* @author Kamil Kokot <[email protected]> |
|
18
|
|
|
*/ |
|
19
|
|
|
class PageMetadataRendererSpec extends ObjectBehavior |
|
20
|
|
|
{ |
|
21
|
|
|
private $defaultOptions = ['group' => 'head', 'defaults' => []]; |
|
22
|
|
|
|
|
23
|
|
|
function let(MetadataRendererInterface $universalRenderer, OptionsResolver $optionsResolver, PropertyAccessorInterface $propertyAccessor) |
|
24
|
|
|
{ |
|
25
|
|
|
$this->beConstructedWith($universalRenderer, $optionsResolver, $propertyAccessor); |
|
26
|
|
|
|
|
27
|
|
|
$optionsResolver->setDefaults(Argument::type('array'))->willReturn($optionsResolver); |
|
28
|
|
|
$optionsResolver->setAllowedValues(Argument::any(), Argument::type('array'))->willReturn($optionsResolver); |
|
29
|
|
|
$optionsResolver->setAllowedTypes(Argument::any(), Argument::any())->willReturn($optionsResolver); |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
function it_is_initializable() |
|
33
|
|
|
{ |
|
34
|
|
|
$this->shouldHaveType('Sylius\Component\Metadata\Renderer\Custom\PageMetadataRenderer'); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
function it_implements_Metadata_Renderer_interface() |
|
38
|
|
|
{ |
|
39
|
|
|
$this->shouldImplement('Sylius\Component\Metadata\Renderer\MetadataRendererInterface'); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
function it_checks_if_metadata_is_supported_or_not( |
|
43
|
|
|
PageMetadataInterface $pageMetadata, |
|
44
|
|
|
MetadataInterface $randomMetadata |
|
45
|
|
|
) { |
|
46
|
|
|
$this->supports($pageMetadata)->shouldReturn(true); |
|
47
|
|
|
$this->supports($randomMetadata)->shouldReturn(false); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
function it_throws_an_exception_if_metadata_has_unsupported_properties( |
|
51
|
|
|
OptionsResolver $optionsResolver, |
|
52
|
|
|
PageMetadataInterface $pageMetadata, |
|
53
|
|
|
PropertyAccessorInterface $propertyAccessor |
|
54
|
|
|
) { |
|
55
|
|
|
$optionsResolver->resolve([])->shouldBeCalled()->willReturn($this->defaultOptions); |
|
56
|
|
|
|
|
57
|
|
|
$data = [ |
|
58
|
|
|
'title' => 'Lorem ipsum', |
|
59
|
|
|
'notexisting' => '42', |
|
60
|
|
|
]; |
|
61
|
|
|
|
|
62
|
|
|
$this->setupPropertyAccessor($propertyAccessor, $pageMetadata, $data, $this->defaultOptions); |
|
63
|
|
|
|
|
64
|
|
|
$pageMetadata->toArray()->shouldBeCalled()->willReturn($data); |
|
65
|
|
|
|
|
66
|
|
|
$this->shouldThrow('\InvalidArgumentException')->duringRender($pageMetadata); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
function it_renders_valid_metadata( |
|
70
|
|
|
OptionsResolver $optionsResolver, |
|
71
|
|
|
PageMetadataInterface $pageMetadata, |
|
72
|
|
|
PropertyAccessorInterface $propertyAccessor |
|
73
|
|
|
) { |
|
74
|
|
|
$optionsResolver->resolve([])->shouldBeCalled()->willReturn($this->defaultOptions); |
|
75
|
|
|
|
|
76
|
|
|
$data = [ |
|
77
|
|
|
'title' => 'Lorem ipsum', |
|
78
|
|
|
'keywords' => ['foo', 'bar'], |
|
79
|
|
|
'author' => 'Krzysztof Krawczyk', |
|
80
|
|
|
]; |
|
81
|
|
|
|
|
82
|
|
|
$this->setupPropertyAccessor($propertyAccessor, $pageMetadata, $data, $this->defaultOptions); |
|
83
|
|
|
|
|
84
|
|
|
$pageMetadata->toArray()->shouldBeCalled()->willReturn($data); |
|
85
|
|
|
|
|
86
|
|
|
$renderedMetadata = $this->render($pageMetadata); |
|
87
|
|
|
$renderedMetadata->shouldContainText('<title>Lorem ipsum</title>'); |
|
88
|
|
|
$renderedMetadata->shouldContainText('<meta name="keywords" content="foo, bar" />'); |
|
89
|
|
|
$renderedMetadata->shouldContainText('<meta name="author" content="Krzysztof Krawczyk" />'); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
function it_does_not_render_null_values( |
|
93
|
|
|
OptionsResolver $optionsResolver, |
|
94
|
|
|
PropertyAccessorInterface $propertyAccessor, |
|
95
|
|
|
PageMetadataInterface $pageMetadata |
|
96
|
|
|
) { |
|
97
|
|
|
$optionsResolver->resolve([])->shouldBeCalled()->willReturn($this->defaultOptions); |
|
98
|
|
|
|
|
99
|
|
|
$data = [ |
|
100
|
|
|
'title' => 'Lorem ipsum', |
|
101
|
|
|
'description' => null, |
|
102
|
|
|
]; |
|
103
|
|
|
|
|
104
|
|
|
$this->setupPropertyAccessor($propertyAccessor, $pageMetadata, $data, $this->defaultOptions); |
|
105
|
|
|
|
|
106
|
|
|
$pageMetadata->toArray()->shouldBeCalled()->willReturn($data); |
|
107
|
|
|
|
|
108
|
|
|
$renderedMetadata = $this->render($pageMetadata); |
|
109
|
|
|
$renderedMetadata->shouldContainText('<title>Lorem ipsum</title>'); |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
function it_delegates_twitter_metadata_rendering_to_another_renderer( |
|
113
|
|
|
MetadataRendererInterface $universalRenderer, |
|
114
|
|
|
OptionsResolver $optionsResolver, |
|
115
|
|
|
PropertyAccessorInterface $propertyAccessor, |
|
116
|
|
|
PageMetadataInterface $pageMetadata, |
|
117
|
|
|
CardInterface $twitterMetadata |
|
118
|
|
|
) { |
|
119
|
|
|
$optionsResolver->resolve([])->shouldBeCalled()->willReturn($this->defaultOptions); |
|
120
|
|
|
|
|
121
|
|
|
$data = ['twitter' => $twitterMetadata]; |
|
122
|
|
|
|
|
123
|
|
|
$this->setupPropertyAccessor($propertyAccessor, $pageMetadata, $data, $this->defaultOptions); |
|
124
|
|
|
|
|
125
|
|
|
$pageMetadata->toArray()->shouldBeCalled()->willReturn($data); |
|
126
|
|
|
|
|
127
|
|
|
$universalRenderer->render($twitterMetadata)->shouldBeCalled()->willReturn('Rendered Twitter metadata'); |
|
128
|
|
|
|
|
129
|
|
|
$this->render($pageMetadata)->shouldReturn('Rendered Twitter metadata'); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
function it_uses_defaults_option_to_set_default_values_while_rendering( |
|
133
|
|
|
OptionsResolver $optionsResolver, |
|
134
|
|
|
PageMetadataInterface $pageMetadata, |
|
135
|
|
|
PropertyAccessorInterface $propertyAccessor |
|
136
|
|
|
) { |
|
137
|
|
|
$options = [ |
|
138
|
|
|
'defaults' => [ |
|
139
|
|
|
'author' => 'Krzysztof Krawczyk', |
|
140
|
|
|
], |
|
141
|
|
|
]; |
|
142
|
|
|
|
|
143
|
|
|
$resolvedOptions = array_merge($this->defaultOptions, $options); |
|
144
|
|
|
|
|
145
|
|
|
$optionsResolver->resolve($options)->shouldBeCalled()->willReturn($resolvedOptions); |
|
146
|
|
|
|
|
147
|
|
|
$data = [ |
|
148
|
|
|
'title' => 'Lorem ipsum', |
|
149
|
|
|
'author' => null, |
|
150
|
|
|
]; |
|
151
|
|
|
|
|
152
|
|
|
$this->setupPropertyAccessor($propertyAccessor, $pageMetadata, $data, $resolvedOptions); |
|
153
|
|
|
|
|
154
|
|
|
$pageMetadata->toArray()->shouldBeCalled()->willReturn($data); |
|
155
|
|
|
|
|
156
|
|
|
$renderedMetadata = $this->render($pageMetadata, $options); |
|
157
|
|
|
$renderedMetadata->shouldContainText('<title>Lorem ipsum</title>'); |
|
158
|
|
|
$renderedMetadata->shouldContainText('<meta name="author" content="Krzysztof Krawczyk" />'); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* {@inheritdoc} |
|
163
|
|
|
*/ |
|
164
|
|
|
public function getMatchers() |
|
165
|
|
|
{ |
|
166
|
|
|
return [ |
|
167
|
|
|
'containText' => function ($subject, $text) { |
|
168
|
|
|
return false !== strpos($subject, $text); |
|
169
|
|
|
}, |
|
170
|
|
|
]; |
|
171
|
|
|
} |
|
172
|
|
|
|
|
173
|
|
|
/** |
|
174
|
|
|
* @param PropertyAccessorInterface $propertyAccessor |
|
175
|
|
|
* @param MetadataInterface $metadata |
|
176
|
|
|
* @param array $data |
|
177
|
|
|
* @param array $options |
|
178
|
|
|
*/ |
|
179
|
|
|
private function setupPropertyAccessor( |
|
180
|
|
|
PropertyAccessorInterface $propertyAccessor, |
|
181
|
|
|
MetadataInterface $metadata, |
|
182
|
|
|
array $data, |
|
183
|
|
|
array $options |
|
184
|
|
|
) { |
|
185
|
|
|
foreach ($options['defaults'] as $key => $value) { |
|
186
|
|
|
if (isset($data[$key]) && null !== $data[$key]) { |
|
187
|
|
|
continue; |
|
188
|
|
|
} |
|
189
|
|
|
|
|
190
|
|
|
$propertyAccessor->setValue($metadata, $key, $value)->shouldBeCalled(); |
|
191
|
|
|
$propertyAccessor->getValue($metadata, $key)->shouldBeCalled()->willReturn(null, $value); |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
foreach ($data as $key => $value) { |
|
195
|
|
|
if (isset($options['defaults'][$key]) && null === $value) { |
|
196
|
|
|
continue; |
|
197
|
|
|
} |
|
198
|
|
|
|
|
199
|
|
|
$propertyAccessor->getValue($metadata, $key)->shouldBeCalled()->willReturn($value); |
|
200
|
|
|
} |
|
201
|
|
|
} |
|
202
|
|
|
} |
|
203
|
|
|
|