Completed
Push — master ( 81c6e7...6c71bd )
by Piotr
02:30
created

ResourceControllerSpec::it_dispatch_event_if_displatcher_present()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 25
Code Lines 19

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 25
rs 8.8571
c 1
b 0
f 0
cc 1
eloc 19
nc 1
nop 7
1
<?php
2
3
/**
4
 * (c) FSi sp. z o.o. <[email protected]>
5
 *
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 */
9
10
namespace spec\FSi\Bundle\AdminBundle\Controller;
11
12
use FSi\Bundle\AdminBundle\Event\AdminEvents;
13
use PhpSpec\ObjectBehavior;
14
use Prophecy\Argument;
15
16
class ResourceControllerSpec extends ObjectBehavior
17
{
18
    /**
19
     * @param \FSi\Bundle\AdminBundle\Admin\Context\ContextManager $manager
20
     * @param \Symfony\Bundle\FrameworkBundle\Templating\DelegatingEngine $templating
21
     */
22
    function let($manager, $templating)
23
    {
24
        $this->beConstructedWith($templating, $manager, 'default_resource');
25
    }
26
27
    /**
28
     * @param \Symfony\Component\EventDispatcher\EventDispatcherInterface $dispatcher
29
     * @param \Symfony\Component\HttpFoundation\Request $request
30
     * @param \Symfony\Component\HttpFoundation\Response $response
31
     * @param \FSi\Bundle\AdminBundle\Admin\ResourceRepository\Element $element
32
     * @param \FSi\Bundle\AdminBundle\Admin\Context\ContextManager $manager
33
     * @param \FSi\Bundle\AdminBundle\Admin\ResourceRepository\Context\ResourceRepositoryContext $context
34
     * @param \Symfony\Bundle\FrameworkBundle\Templating\DelegatingEngine $templating
35
     */
36
    function it_dispatch_event_if_displatcher_present(
37
        $dispatcher,
38
        $request,
39
        $response,
40
        $element,
41
        $manager,
42
        $context,
43
        $templating
44
    ) {
45
        $this->setEventDispatcher($dispatcher);
46
47
        $dispatcher->dispatch(
48
            AdminEvents::CONTEXT_PRE_CREATE,
49
            Argument::type('FSi\Bundle\AdminBundle\Event\AdminEvent')
50
        )->shouldBeCalled();
51
52
        $manager->createContext('fsi_admin_resource', $element)->willReturn($context);
53
        $context->handleRequest($request)->willReturn(null);
54
        $context->hasTemplateName()->willReturn(false);
55
        $context->getData()->willReturn(array());
56
57
        $templating->renderResponse('default_resource', array(), null)->willReturn($response);
58
59
        $this->resourceAction($element, $request)->shouldReturn($response);
60
    }
61
62
    /**
63
     * @param \FSi\Bundle\AdminBundle\Admin\ResourceRepository\Element $element
64
     * @param \FSi\Bundle\AdminBundle\Admin\Context\ContextManager $manager
65
     * @param \Symfony\Component\HttpFoundation\Request $request
66
     */
67
    function it_throw_exception_when_cant_find_context_builder_that_supports_admin_element(
68
        $element,
69
        $manager,
70
        $request
71
    ) {
72
        $element->getId()->willReturn('my_awesome_resource');
73
        $manager->createContext(Argument::type('string'), $element)->willReturn(null);
74
75
        $this->shouldThrow('Symfony\Component\HttpKernel\Exception\NotFoundHttpException')
76
            ->during('resourceAction', array($element, $request));
77
    }
78
79
    /**
80
     * @param \Symfony\Component\HttpFoundation\Request $request
81
     * @param \Symfony\Component\HttpFoundation\Response $response
82
     * @param \FSi\Bundle\AdminBundle\Admin\ResourceRepository\Element $element
83
     * @param \FSi\Bundle\AdminBundle\Admin\Context\ContextManager $manager
84
     * @param \FSi\Bundle\AdminBundle\Admin\ResourceRepository\Context\ResourceRepositoryContext $context
85
     * @param \Symfony\Bundle\FrameworkBundle\Templating\DelegatingEngine $templating
86
     */
87
    function it_render_default_template_in_resource_action(
88
        $request,
89
        $response,
90
        $element,
91
        $manager,
92
        $context,
93
        $templating
94
    ) {
95
        $manager->createContext('fsi_admin_resource', $element)->willReturn($context);
96
        $context->handleRequest($request)->willReturn(null);
97
        $context->hasTemplateName()->willReturn(false);
98
        $context->getData()->willReturn(array());
99
100
        $templating->renderResponse('default_resource', array(), null)->willReturn($response);
101
        $this->resourceAction($element, $request)->shouldReturn($response);
102
    }
103
104
    /**
105
     * @param \FSi\Bundle\AdminBundle\Admin\Context\ContextManager $manager
106
     * @param \FSi\Bundle\AdminBundle\Admin\ResourceRepository\Element $element
107
     * @param \FSi\Bundle\AdminBundle\Admin\ResourceRepository\Context\ResourceRepositoryContext $context
108
     * @param \Symfony\Component\HttpFoundation\Request $request
109
     * @param \Symfony\Bundle\FrameworkBundle\Templating\DelegatingEngine $templating
110
     * @param \Symfony\Component\HttpFoundation\Response $response
111
     */
112
    function it_render_template_from_element_in_resource_action(
113
        $manager,
114
        $element,
115
        $context,
116
        $request,
117
        $templating,
118
        $response
119
    ) {
120
        $manager->createContext('fsi_admin_resource', $element)->willReturn($context);
121
        $context->handleRequest($request)->willReturn(null);
122
        $context->hasTemplateName()->willReturn(true);
123
        $context->getTemplateName()->willReturn('custom_template');
124
        $context->getData()->willReturn(array());
125
126
        $templating->renderResponse('custom_template', array(), null)->willReturn($response);
127
        $this->resourceAction($element, $request)->shouldReturn($response);
128
    }
129
130
    /**
131
     * @param \FSi\Bundle\AdminBundle\Admin\Context\ContextManager $manager
132
     * @param \FSi\Bundle\AdminBundle\Admin\ResourceRepository\Element $element
133
     * @param \FSi\Bundle\AdminBundle\Admin\ResourceRepository\Context\ResourceRepositoryContext $context
134
     * @param \Symfony\Component\HttpFoundation\Request $request
135
     * @param \Symfony\Component\HttpFoundation\Response $response
136
     */
137
    function it_return_response_from_context_in_resource_action(
138
        $manager,
139
        $element,
140
        $context,
141
        $request,
142
        $response
143
    ) {
144
        $manager->createContext('fsi_admin_resource', $element)->willReturn($context);
145
        $context->handleRequest($request)->willReturn($response);
146
147
        $this->resourceAction($element, $request)->shouldReturn($response);
148
    }
149
}
150