|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Sylius package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Paweł Jędrzejewski |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace spec\Sylius\Bundle\GridBundle\Renderer; |
|
13
|
|
|
|
|
14
|
|
|
use PhpSpec\ObjectBehavior; |
|
15
|
|
|
use Prophecy\Argument; |
|
16
|
|
|
use Sylius\Bundle\GridBundle\Renderer\TwigGridRenderer; |
|
17
|
|
|
use Sylius\Component\Grid\Definition\Action; |
|
18
|
|
|
use Sylius\Component\Grid\Definition\Field; |
|
19
|
|
|
use Sylius\Component\Grid\FieldTypes\FieldTypeInterface; |
|
20
|
|
|
use Sylius\Component\Grid\Filter\StringFilter; |
|
21
|
|
|
use Sylius\Component\Grid\Renderer\GridRendererInterface; |
|
22
|
|
|
use Sylius\Component\Grid\View\GridView; |
|
23
|
|
|
use Sylius\Component\Grid\View\GridViewInterface; |
|
24
|
|
|
use Sylius\Component\Registry\ServiceRegistryInterface; |
|
25
|
|
|
use Symfony\Component\Form\FormFactoryInterface; |
|
26
|
|
|
use Symfony\Component\OptionsResolver\OptionsResolver; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @author Paweł Jędrzejewski <[email protected]> |
|
30
|
|
|
*/ |
|
31
|
|
|
final class TwigGridRendererSpec extends ObjectBehavior |
|
32
|
|
|
{ |
|
33
|
|
|
function let( |
|
34
|
|
|
\Twig_Environment $twig, |
|
35
|
|
|
ServiceRegistryInterface $fieldsRegistry, |
|
36
|
|
|
FormFactoryInterface $formFactory |
|
37
|
|
|
) { |
|
38
|
|
|
$actionTemplates = [ |
|
39
|
|
|
'link' => 'SyliusGridBundle:Action:_link.html.twig', |
|
40
|
|
|
'form' => 'SyliusGridBundle:Action:_form.html.twig', |
|
41
|
|
|
]; |
|
42
|
|
|
$filterTemplates = [ |
|
43
|
|
|
StringFilter::NAME => 'SyliusGridBundle:Filter:_string.html.twig', |
|
44
|
|
|
]; |
|
45
|
|
|
|
|
46
|
|
|
$this->beConstructedWith( |
|
47
|
|
|
$twig, |
|
48
|
|
|
$fieldsRegistry, |
|
49
|
|
|
$formFactory, |
|
50
|
|
|
'SyliusGridBundle:default.html.twig', |
|
51
|
|
|
$actionTemplates, |
|
52
|
|
|
$filterTemplates |
|
53
|
|
|
); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
function it_is_initializable() |
|
57
|
|
|
{ |
|
58
|
|
|
$this->shouldHaveType(TwigGridRenderer::class); |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
function it_is_a_grid_renderer() |
|
62
|
|
|
{ |
|
63
|
|
|
$this->shouldImplement(GridRendererInterface::class); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
function it_uses_twig_to_render_the_grid_view(\Twig_Environment $twig, GridViewInterface $gridView) |
|
67
|
|
|
{ |
|
68
|
|
|
$twig->render('SyliusGridBundle:default.html.twig', ['grid' => $gridView])->willReturn('<html>Grid!</html>'); |
|
69
|
|
|
$this->render($gridView)->shouldReturn('<html>Grid!</html>'); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
function it_uses_custom_template_if_specified(\Twig_Environment $twig, GridView $gridView) |
|
73
|
|
|
{ |
|
74
|
|
|
$twig->render('SyliusGridBundle:custom.html.twig', ['grid' => $gridView])->willReturn('<html>Grid!</html>'); |
|
75
|
|
|
$this->render($gridView, 'SyliusGridBundle:custom.html.twig')->shouldReturn('<html>Grid!</html>'); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
function it_uses_twig_to_render_the_action(\Twig_Environment $twig, GridViewInterface $gridView, Action $action) |
|
79
|
|
|
{ |
|
80
|
|
|
$action->getType()->willReturn('link'); |
|
81
|
|
|
$action->getOptions()->willReturn([]); |
|
82
|
|
|
|
|
83
|
|
|
$twig |
|
84
|
|
|
->render('SyliusGridBundle:Action:_link.html.twig', [ |
|
85
|
|
|
'grid' => $gridView, |
|
86
|
|
|
'action' => $action, |
|
87
|
|
|
'data' => null |
|
88
|
|
|
]) |
|
89
|
|
|
->willReturn('<a href="#">Action!</a>') |
|
90
|
|
|
; |
|
91
|
|
|
|
|
92
|
|
|
$this->renderAction($gridView, $action)->shouldReturn('<a href="#">Action!</a>'); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
function it_renders_a_field_with_data_via_appropriate_field_type( |
|
96
|
|
|
GridViewInterface $gridView, |
|
97
|
|
|
Field $field, |
|
98
|
|
|
ServiceRegistryInterface $fieldsRegistry, |
|
99
|
|
|
FieldTypeInterface $fieldType |
|
100
|
|
|
) { |
|
101
|
|
|
$field->getType()->willReturn('string'); |
|
102
|
|
|
$fieldsRegistry->get('string')->willReturn($fieldType); |
|
103
|
|
|
$fieldType->configureOptions(Argument::type(OptionsResolver::class)) |
|
104
|
|
|
->will(function ($args) { |
|
105
|
|
|
$args[0]->setRequired('foo'); |
|
106
|
|
|
}) |
|
107
|
|
|
; |
|
108
|
|
|
|
|
109
|
|
|
$field->getOptions()->willReturn([ |
|
110
|
|
|
'foo' => 'bar', |
|
111
|
|
|
]); |
|
112
|
|
|
$fieldType->render($field, 'Value', ['foo' => 'bar'])->willReturn('<strong>Value</strong>'); |
|
113
|
|
|
|
|
114
|
|
|
|
|
115
|
|
|
$this->renderField($gridView, $field, 'Value')->shouldReturn('<strong>Value</strong>'); |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
function it_throws_an_exception_if_template_is_not_configured_for_given_action_type( |
|
119
|
|
|
GridViewInterface $gridView, |
|
120
|
|
|
Action $action |
|
121
|
|
|
) { |
|
122
|
|
|
$action->getType()->willReturn('foo'); |
|
123
|
|
|
|
|
124
|
|
|
$this |
|
125
|
|
|
->shouldThrow(new \InvalidArgumentException('Missing template for action type "foo".')) |
|
126
|
|
|
->during('renderAction', [$gridView, $action]) |
|
127
|
|
|
; |
|
128
|
|
|
} |
|
129
|
|
|
} |
|
130
|
|
|
|