Issues (3627)

Tests/EventListener/ApiSubscriberTest.php (1 issue)

1
<?php
2
3
/*
4
 * @copyright   2017 Mautic Contributors. All rights reserved
5
 * @author      Mautic, Inc.
6
 *
7
 * @link        https://mautic.org
8
 *
9
 * @license     GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html
10
 */
11
12
namespace Mautic\ApiBundle\Tests\EventListener;
13
14
use Mautic\ApiBundle\EventListener\ApiSubscriber;
15
use Mautic\CoreBundle\Helper\CoreParametersHelper;
16
use Mautic\CoreBundle\Tests\CommonMocks;
17
use PHPUnit\Framework\MockObject\MockObject;
18
use Symfony\Component\HttpFoundation\JsonResponse;
19
use Symfony\Component\HttpFoundation\ParameterBag;
20
use Symfony\Component\HttpFoundation\Request;
21
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
22
use Symfony\Component\Translation\TranslatorInterface;
23
24
class ApiSubscriberTest extends CommonMocks
25
{
26
    /**
27
     * @var CoreParametersHelper|MockObject
28
     */
29
    private $coreParametersHelper;
30
31
    /**
32
     * @var TranslatorInterface|MockObject
33
     */
34
    private $translator;
35
36
    /**
37
     * @var Request|MockObject
38
     */
39
    private $request;
40
41
    /**
42
     * @var GetResponseEvent|MockObject
43
     */
44
    private $event;
45
46
    /**
47
     * @var ApiSubscriber
48
     */
49
    private $subscriber;
50
51
    protected function setUp(): void
52
    {
53
        parent::setUp();
54
55
        $this->coreParametersHelper = $this->createMock(CoreParametersHelper::class);
56
        $this->translator           = $this->createMock(TranslatorInterface::class);
57
        $this->request              = $this->createMock(Request::class);
58
        $this->request->headers     = new ParameterBag();
59
        $this->event                = $this->createMock(GetResponseEvent::class);
60
        $this->subscriber           = new ApiSubscriber(
61
            $this->coreParametersHelper,
62
            $this->translator
63
        );
64
    }
65
66
    public function testOnKernelRequestWhenNotMasterRequest()
67
    {
68
        $this->event->expects($this->once())
69
            ->method('isMasterRequest')
70
            ->willReturn(false);
71
72
        $this->coreParametersHelper->expects($this->never())
73
            ->method('get');
74
75
        $this->assertNull($this->subscriber->onKernelRequest($this->event));
0 ignored issues
show
Are you sure the usage of $this->subscriber->onKernelRequest($this->event) targeting Mautic\ApiBundle\EventLi...iber::onKernelRequest() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
76
    }
77
78
    public function testOnKernelRequestOnApiRequestWhenApiDisabled()
79
    {
80
        $this->event->expects($this->once())
81
            ->method('isMasterRequest')
82
            ->willReturn(true);
83
84
        $this->event->expects($this->once())
85
            ->method('getRequest')
86
            ->willReturn($this->request);
87
88
        $this->request->expects($this->once())
89
            ->method('getRequestUri')
90
            ->willReturn('/api/endpoint');
91
92
        $this->coreParametersHelper->expects($this->once())
93
            ->method('get')
94
            ->with('api_enabled')
95
            ->willReturn(false);
96
97
        $this->event->expects($this->once())
98
            ->method('setResponse')
99
            ->with($this->isInstanceOf(JsonResponse::class))
100
            ->willReturnCallback(
101
                function (JsonResponse $response) {
102
                    $this->assertEquals(403, $response->getStatusCode());
103
                }
104
            );
105
106
        $this->subscriber->onKernelRequest($this->event);
107
    }
108
109
    public function testOnKernelRequestOnApiRequestWhenApiEnabled()
110
    {
111
        $this->event->expects($this->once())
112
            ->method('isMasterRequest')
113
            ->willReturn(true);
114
115
        $this->event->expects($this->once())
116
            ->method('getRequest')
117
            ->willReturn($this->request);
118
119
        $this->request->expects($this->once())
120
            ->method('getRequestUri')
121
            ->willReturn('/api/endpoint');
122
123
        $this->coreParametersHelper->expects($this->exactly(2))
124
            ->method('get')
125
            ->withConsecutive(['api_enabled'], ['api_enable_basic_auth'])
126
            ->willReturnOnConsecutiveCalls(true, true);
127
128
        $this->subscriber->onKernelRequest($this->event);
129
    }
130
}
131