GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( 1537c6...c1251f )
by
unknown
07:51
created

ReportControllerSpec   A

Complexity

Total Complexity 21

Size/Duplication

Total Lines 200
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 72
dl 0
loc 200
rs 10
c 0
b 0
f 0
wmc 21

10 Methods

Rating   Name   Duplication   Size   Complexity  
A it_throws_a_403_exception_if_user_is_unauthorized_to_render_the_report() 0 16 1
A it_returns_a_response_for_non_html_view_of_single_resource() 0 32 1
A let() 0 24 1
A it_is_initializable() 0 3 1
A nullifyDates() 0 5 1
A it_should_extends_resource_controller_interface() 0 3 1
A unwrapViewData() 0 3 1
A unwrapIfCollaborator() 0 17 5
A it_returns_a_response_for_html_view_of_a_single_resource() 0 42 1
B getViewComparingCallback() 0 20 8
1
<?php
2
3
namespace spec\Odiseo\SyliusReportPlugin\Controller;
4
5
use Doctrine\Common\Persistence\ObjectManager;
6
use FOS\RestBundle\View\View;
7
use Odiseo\SyliusReportPlugin\Controller\ReportController;
8
use PhpSpec\ObjectBehavior;
9
use PhpSpec\Wrapper\Collaborator;
10
use Prophecy\Argument;
11
use Sylius\Bundle\ResourceBundle\Controller\AuthorizationCheckerInterface;
12
use Sylius\Bundle\ResourceBundle\Controller\EventDispatcherInterface;
13
use Sylius\Bundle\ResourceBundle\Controller\FlashHelperInterface;
14
use Sylius\Bundle\ResourceBundle\Controller\NewResourceFactoryInterface;
15
use Sylius\Bundle\ResourceBundle\Controller\RedirectHandlerInterface;
16
use Sylius\Bundle\ResourceBundle\Controller\RequestConfiguration;
17
use Sylius\Bundle\ResourceBundle\Controller\RequestConfigurationFactoryInterface;
18
use Sylius\Bundle\ResourceBundle\Controller\ResourceController;
19
use Sylius\Bundle\ResourceBundle\Controller\ResourceDeleteHandlerInterface;
20
use Sylius\Bundle\ResourceBundle\Controller\ResourceFormFactoryInterface;
21
use Sylius\Bundle\ResourceBundle\Controller\ResourcesCollectionProviderInterface;
22
use Sylius\Bundle\ResourceBundle\Controller\ResourceUpdateHandlerInterface;
23
use Sylius\Bundle\ResourceBundle\Controller\SingleResourceProviderInterface;
24
use Sylius\Bundle\ResourceBundle\Controller\StateMachineInterface;
25
use Sylius\Bundle\ResourceBundle\Controller\ViewHandlerInterface;
26
use Sylius\Component\Resource\Factory\FactoryInterface;
27
use Sylius\Component\Resource\Metadata\MetadataInterface;
28
use Sylius\Component\Resource\Model\ResourceInterface;
29
use Sylius\Component\Resource\Repository\RepositoryInterface;
30
use Sylius\Component\Resource\ResourceActions;
31
use Symfony\Component\HttpFoundation\Request;
32
use Symfony\Component\HttpFoundation\Response;
33
use Symfony\Component\Security\Core\Exception\AccessDeniedException;
34
35
/**
36
 * @author Diego D'amico <[email protected]>
37
 */
38
class ReportControllerSpec extends ObjectBehavior
39
{
40
    function let(
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
41
        MetadataInterface $metadata,
42
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
43
        ViewHandlerInterface $viewHandler,
44
        RepositoryInterface $repository,
45
        FactoryInterface $factory,
46
        NewResourceFactoryInterface $newResourceFactory,
47
        ObjectManager $manager,
48
        SingleResourceProviderInterface $singleResourceProvider,
49
        ResourcesCollectionProviderInterface $resourcesFinder,
50
        ResourceFormFactoryInterface $resourceFormFactory,
51
        RedirectHandlerInterface $redirectHandler,
52
        FlashHelperInterface $flashHelper,
53
        AuthorizationCheckerInterface $authorizationChecker,
54
        EventDispatcherInterface $eventDispatcher,
55
        StateMachineInterface $stateMachine,
56
        ResourceUpdateHandlerInterface $resourceUpdateHandler,
57
        ResourceDeleteHandlerInterface $resourceDeleteHandler
58
    )
59
    {
60
        $this->beConstructedWith(
61
            $metadata, $requestConfigurationFactory, $viewHandler, $repository, $factory, $newResourceFactory,
62
            $manager, $singleResourceProvider, $resourcesFinder, $resourceFormFactory, $redirectHandler, $flashHelper,
63
            $authorizationChecker, $eventDispatcher, $stateMachine, $resourceUpdateHandler, $resourceDeleteHandler
64
        );
65
    }
66
67
    function it_is_initializable()
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
68
    {
69
        $this->shouldHaveType(ReportController::class);
70
    }
71
72
    function it_should_extends_resource_controller_interface()
73
    {
74
        $this->shouldBeAnInstanceOf(ResourceController::class);
75
    }
76
77
    function it_throws_a_403_exception_if_user_is_unauthorized_to_render_the_report(
78
        MetadataInterface $metadata,
79
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
80
        RequestConfiguration $configuration,
81
        Request $request,
82
        AuthorizationCheckerInterface $authorizationChecker
83
    ): void {
84
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
0 ignored issues
show
Bug introduced by
The method willReturn() does not exist on Sylius\Bundle\ResourceBu...er\RequestConfiguration. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

84
        $requestConfigurationFactory->create($metadata, $request)->/** @scrutinizer ignore-call */ willReturn($configuration);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
85
        $configuration->hasPermission()->willReturn(true);
86
        $configuration->getPermission(ResourceActions::SHOW)->willReturn('odiseo_sylius_report.report.show');
87
88
        $authorizationChecker->isGranted($configuration, 'odiseo_sylius_report.report.show')->willReturn(false);
89
90
        $this
91
            ->shouldThrow(new AccessDeniedException())
92
            ->during('renderAction', [$request])
93
        ;
94
    }
95
96
    function it_returns_a_response_for_html_view_of_a_single_resource(
97
        MetadataInterface $metadata,
98
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
99
        RequestConfiguration $configuration,
100
        AuthorizationCheckerInterface $authorizationChecker,
101
        RepositoryInterface $repository,
102
        SingleResourceProviderInterface $singleResourceProvider,
103
        ResourceInterface $resource,
104
        ViewHandlerInterface $viewHandler,
105
        EventDispatcherInterface $eventDispatcher,
106
        Request $request,
107
        Response $response
108
    ): void {
109
        $metadata->getApplicationName()->willReturn('sylius');
110
        $metadata->getName()->willReturn('product');
111
112
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
113
        $configuration->hasPermission()->willReturn(true);
114
        $configuration->getPermission(ResourceActions::SHOW)->willReturn('sylius.product.show');
115
116
        $authorizationChecker->isGranted($configuration, 'sylius.product.show')->willReturn(true);
117
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
0 ignored issues
show
Bug introduced by
The method willReturn() does not exist on Sylius\Component\Resource\Model\ResourceInterface. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

117
        $singleResourceProvider->get($configuration, $repository)->/** @scrutinizer ignore-call */ willReturn($resource);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
118
119
        $configuration->isHtmlRequest()->willReturn(true);
120
        $configuration->getTemplate(ResourceActions::SHOW . '.html')->willReturn('SyliusShopBundle:Product:show.html.twig');
121
122
        $eventDispatcher->dispatch(ResourceActions::SHOW, $configuration, $resource)->shouldBeCalled();
0 ignored issues
show
Bug introduced by
The method shouldBeCalled() does not exist on Sylius\Bundle\ResourceBu...ResourceControllerEvent. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

122
        $eventDispatcher->dispatch(ResourceActions::SHOW, $configuration, $resource)->/** @scrutinizer ignore-call */ shouldBeCalled();

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
123
124
        $expectedView = View::create()
125
            ->setData([
126
                'configuration' => $configuration,
127
                'metadata' => $metadata,
128
                'resource' => $resource,
129
                'product' => $resource,
130
            ])
131
            ->setTemplateVar('product')
132
            ->setTemplate('SyliusShopBundle:Product:show.html.twig')
133
        ;
134
135
        $viewHandler->handle($configuration, Argument::that($this->getViewComparingCallback($expectedView)))->willReturn($response);
0 ignored issues
show
Bug introduced by
The method willReturn() does not exist on Symfony\Component\HttpFoundation\Response. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

135
        $viewHandler->handle($configuration, Argument::that($this->getViewComparingCallback($expectedView)))->/** @scrutinizer ignore-call */ willReturn($response);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
Bug introduced by
Prophecy\Argument::that(...allback($expectedView)) of type Prophecy\Argument\Token\CallbackToken is incompatible with the type FOS\RestBundle\View\View expected by parameter $view of Sylius\Bundle\ResourceBu...dlerInterface::handle(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

135
        $viewHandler->handle($configuration, /** @scrutinizer ignore-type */ Argument::that($this->getViewComparingCallback($expectedView)))->willReturn($response);
Loading history...
136
137
        $this->showAction($request)->shouldReturn($response);
0 ignored issues
show
Bug introduced by
The method showAction() does not exist on spec\Odiseo\SyliusReport...er\ReportControllerSpec. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

137
        $this->/** @scrutinizer ignore-call */ 
138
               showAction($request)->shouldReturn($response);
Loading history...
138
    }
139
140
    function it_returns_a_response_for_non_html_view_of_single_resource(
141
        MetadataInterface $metadata,
142
        RequestConfigurationFactoryInterface $requestConfigurationFactory,
143
        RequestConfiguration $configuration,
144
        AuthorizationCheckerInterface $authorizationChecker,
145
        RepositoryInterface $repository,
146
        SingleResourceProviderInterface $singleResourceProvider,
147
        ResourceInterface $resource,
148
        ViewHandlerInterface $viewHandler,
149
        EventDispatcherInterface $eventDispatcher,
150
        Request $request,
151
        Response $response
152
    ): void {
153
        $metadata->getApplicationName()->willReturn('sylius');
154
        $metadata->getName()->willReturn('product');
155
156
        $requestConfigurationFactory->create($metadata, $request)->willReturn($configuration);
157
        $configuration->hasPermission()->willReturn(true);
158
        $configuration->getPermission(ResourceActions::SHOW)->willReturn('sylius.product.show');
159
160
        $authorizationChecker->isGranted($configuration, 'sylius.product.show')->willReturn(true);
161
        $singleResourceProvider->get($configuration, $repository)->willReturn($resource);
162
163
        $configuration->isHtmlRequest()->willReturn(false);
164
165
        $eventDispatcher->dispatch(ResourceActions::SHOW, $configuration, $resource)->shouldBeCalled();
166
167
        $expectedView = View::create($resource);
168
169
        $viewHandler->handle($configuration, Argument::that($this->getViewComparingCallback($expectedView)))->willReturn($response);
0 ignored issues
show
Bug introduced by
Prophecy\Argument::that(...allback($expectedView)) of type Prophecy\Argument\Token\CallbackToken is incompatible with the type FOS\RestBundle\View\View expected by parameter $view of Sylius\Bundle\ResourceBu...dlerInterface::handle(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

169
        $viewHandler->handle($configuration, /** @scrutinizer ignore-type */ Argument::that($this->getViewComparingCallback($expectedView)))->willReturn($response);
Loading history...
170
171
        $this->showAction($request)->shouldReturn($response);
172
    }
173
174
    private function getViewComparingCallback(View $expectedView)
175
    {
176
        return function ($value) use ($expectedView) {
177
            if (!$value instanceof View) {
178
                return false;
179
            }
180
181
            // Need to unwrap phpspec's Collaborators to ensure proper comparison.
182
            $this->unwrapViewData($expectedView);
183
            $this->nullifyDates($value);
184
            $this->nullifyDates($expectedView);
185
186
            return
187
                $expectedView->getStatusCode() === $value->getStatusCode() &&
188
                $expectedView->getHeaders() === $value->getHeaders() &&
189
                $expectedView->getEngine() === $value->getEngine() &&
190
                $expectedView->getFormat() === $value->getFormat() &&
191
                $expectedView->getData() === $value->getData() &&
192
                $expectedView->getTemplate() === $value->getTemplate() &&
193
                $expectedView->getTemplateVar() === $value->getTemplateVar()
194
                ;
195
        };
196
    }
197
198
    /**
199
     * @param View $view
200
     */
201
    private function unwrapViewData(View $view)
202
    {
203
        $view->setData($this->unwrapIfCollaborator($view->getData()));
204
    }
205
206
    /**
207
     * @param mixed $value
208
     *
209
     * @return mixed
210
     */
211
    private function unwrapIfCollaborator($value)
212
    {
213
        if (null === $value) {
214
            return null;
215
        }
216
217
        if ($value instanceof Collaborator) {
218
            return $value->getWrappedObject();
219
        }
220
221
        if (is_array($value)) {
222
            foreach ($value as $key => $childValue) {
223
                $value[$key] = $this->unwrapIfCollaborator($childValue);
224
            }
225
        }
226
227
        return $value;
228
    }
229
230
    /**
231
     * @param View $view
232
     */
233
    private function nullifyDates(View $view)
234
    {
235
        $headers = $view->getHeaders();
236
        unset($headers['date']);
237
        $view->setHeaders($headers);
238
    }
239
}
240