Completed
Pull Request — 3.1 (#348)
by Piotr
01:23
created

DeleteElementContextSpec   A

Complexity

Total Complexity 8

Size/Duplication

Total Lines 71
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Importance

Changes 0
Metric Value
wmc 8
lcom 0
cbo 1
dl 0
loc 71
rs 10
c 0
b 0
f 0

7 Methods

Rating   Name   Duplication   Size   Complexity  
A let() 0 10 1
A it_is_context() 0 4 1
A it_has_array_data() 0 7 1
A it_does_not_have_template_name() 0 5 1
A it_handles_request_with_request_handlers() 0 12 1
A it_return_response_from_handler() 0 12 1
A getMatchers() 0 12 2
1
<?php
2
3
namespace spec\FSi\Bundle\AdminBundle\Admin\CRUD\Context;
4
5
use FSi\Bundle\AdminBundle\Admin\Context\Request\HandlerInterface;
6
use FSi\Bundle\AdminBundle\Doctrine\Admin\DeleteElement;
7
use PhpSpec\ObjectBehavior;
8
use Prophecy\Argument;
9
use Symfony\Component\Form\FormBuilderInterface;
10
use Symfony\Component\Form\FormInterface;
11
use Symfony\Component\HttpFoundation\ParameterBag;
12
use Symfony\Component\HttpFoundation\Response;
13
use Symfony\Component\HttpFoundation\Request;
14
use FSi\Bundle\AdminBundle\Admin\Context\ContextInterface;
15
use FSi\Bundle\AdminBundle\Event\FormEvent;
16
17
class DeleteElementContextSpec extends ObjectBehavior
18
{
19
    function let(
20
        DeleteElement $element,
21
        FormBuilderInterface $formBuilder,
22
        FormInterface $batchForm,
23
        HandlerInterface $handler
24
    ) {
25
        $this->beConstructedWith([$handler], $formBuilder);
26
        $formBuilder->getForm()->willReturn($batchForm);
27
        $this->setElement($element);
28
    }
29
30
    function it_is_context()
31
    {
32
        $this->shouldBeAnInstanceOf(ContextInterface::class);
33
    }
34
35
    function it_has_array_data()
36
    {
37
        $this->getData()->shouldBeArray();
38
        $this->getData()->shouldHaveKeyInArray('form');
39
        $this->getData()->shouldHaveKeyInArray('element');
40
        $this->getData()->shouldHaveKeyInArray('indexes');
41
    }
42
43
    function it_does_not_have_template_name()
44
    {
45
        $this->hasTemplateName()->shouldReturn(false);
46
        $this->getTemplateName()->shouldReturn(null);
47
    }
48
49
    function it_handles_request_with_request_handlers(
50
        HandlerInterface $handler,
51
        Request $request,
52
        ParameterBag $requestParameterBag
53
    ) {
54
        $handler->handleRequest(Argument::type(FormEvent::class), $request)->willReturn(null);
55
56
        $request->request = $requestParameterBag;
57
        $requestParameterBag->get('indexes', [])->willReturn([]);
58
59
        $this->handleRequest($request)->shouldReturn(null);
60
    }
61
62
    function it_return_response_from_handler(
63
        HandlerInterface $handler,
64
        Request $request,
65
        ParameterBag $requestParameterBag,
66
        Response $response
67
    ) {
68
        $handler->handleRequest(Argument::type(FormEvent::class), $request)->willReturn($response);
69
        $request->request = $requestParameterBag;
70
        $requestParameterBag->get('indexes', [])->willReturn([]);
71
72
        $this->handleRequest($request)->shouldReturnAnInstanceOf(Response::class);
73
    }
74
75
    public function getMatchers(): array
76
    {
77
        return [
78
            'haveKeyInArray' => function($subject, $key) {
79
                if (!is_array($subject)) {
80
                    return false;
81
                }
82
83
                return array_key_exists($key, $subject);
84
            },
85
        ];
86
    }
87
}
88