Completed
Push — symfony3-core ( d5a708...a5fc54 )
by Kamil
85:01 queued 66:56
created

TwigGridRendererSpec::let()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 24
Code Lines 18

Duplication

Lines 0
Ratio 0 %

Importance

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