Completed
Push — master ( 6d6774...64f3ed )
by Jeroen
11:23 queued 05:13
created

testInvalidateSessionWithNoIpSet()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 23
rs 9.552
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace Kunstmaan\AdminBundle\Tests\EventListener;
4
5
use Kunstmaan\AdminBundle\EventListener\SessionSecurityListener;
6
use PHPUnit\Framework\TestCase;
7
use Psr\Log\LoggerInterface;
8
use Symfony\Component\HttpFoundation\HeaderBag;
9
use Symfony\Component\HttpFoundation\Request;
10
use Symfony\Component\HttpFoundation\ServerBag;
11
use Symfony\Component\HttpFoundation\Session\Session;
12
use Symfony\Component\HttpKernel\Event\FilterResponseEvent;
13
use Symfony\Component\HttpKernel\Event\GetResponseEvent;
14
use Symfony\Component\HttpKernel\HttpKernelInterface;
15
16
class SessionSecurityListenerTest extends TestCase
17
{
18 View Code Duplication
    public function testOnKernelRequest()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
19
    {
20
        $logger = $this->createMock(LoggerInterface::class);
21
        $request = $this->createMock(Request::class);
22
        $request->server = $this->createMock(ServerBag::class);
0 ignored issues
show
Bug introduced by
Accessing server on the interface PHPUnit\Framework\MockObject\MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
23
        $request->headers = $this->createMock(HeaderBag::class);
0 ignored issues
show
Bug introduced by
Accessing headers on the interface PHPUnit\Framework\MockObject\MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
24
25
        $event = $this->createMock(GetResponseEvent::class);
26
        $session = $this->createMock(Session::class);
27
28
        $event->expects($this->any())->method('getRequestType')->willReturn(HttpKernelInterface::MASTER_REQUEST);
29
        $event->expects($this->any())->method('getRequest')->willReturn($request);
30
        $request->expects($this->once())->method('hasSession')->willReturn(true);
31
        $request->expects($this->exactly(2))->method('getSession')->willReturn($session);
32
        $request->server->expects($this->any())->method('get')->will($this->onConsecutiveCalls('Session ip', 'kuma_ua'));
0 ignored issues
show
Bug introduced by
Accessing server on the interface PHPUnit\Framework\MockObject\MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
33
        $request->headers->expects($this->any())->method('get')->willReturn('kuma_ua');
0 ignored issues
show
Bug introduced by
Accessing headers on the interface PHPUnit\Framework\MockObject\MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
34
        $session->expects($this->once())->method('isStarted')->willReturn(true);
35
        $session->expects($this->any())->method('has')->willReturn(true);
36
37
        $listener = new SessionSecurityListener(true, true, $logger);
0 ignored issues
show
Documentation introduced by
$logger is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Psr\Log\LoggerInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
38
        $listener->onKernelRequest($event);
0 ignored issues
show
Documentation introduced by
$event is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Symfony\Component...Event\GetResponseEvent>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
39
40
        $event = $this->createMock(GetResponseEvent::class);
41
        $listener->onKernelRequest($event);
0 ignored issues
show
Documentation introduced by
$event is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Symfony\Component...Event\GetResponseEvent>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
42
    }
43
44 View Code Duplication
    public function testOnKernelResponse()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
45
    {
46
        $logger = $this->createMock(LoggerInterface::class);
47
        $request = $this->createMock(Request::class);
48
        $request->server = $this->createMock(ServerBag::class);
0 ignored issues
show
Bug introduced by
Accessing server on the interface PHPUnit\Framework\MockObject\MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
49
        $request->headers = $this->createMock(HeaderBag::class);
0 ignored issues
show
Bug introduced by
Accessing headers on the interface PHPUnit\Framework\MockObject\MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
50
51
        $event = $this->createMock(FilterResponseEvent::class);
52
        $session = $this->createMock(Session::class);
53
54
        $event->expects($this->any())->method('getRequestType')->willReturn(HttpKernelInterface::MASTER_REQUEST);
55
        $event->expects($this->any())->method('getRequest')->willReturn($request);
56
        $request->expects($this->once())->method('hasSession')->willReturn(true);
57
        $request->expects($this->exactly(2))->method('getSession')->willReturn($session);
58
        $request->server->expects($this->any())->method('get')->will($this->onConsecutiveCalls('Session ip', 'kuma_ua'));
0 ignored issues
show
Bug introduced by
Accessing server on the interface PHPUnit\Framework\MockObject\MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
59
        $request->headers->expects($this->any())->method('get')->willReturn('kuma_ua');
0 ignored issues
show
Bug introduced by
Accessing headers on the interface PHPUnit\Framework\MockObject\MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
60
        $session->expects($this->once())->method('isStarted')->willReturn(true);
61
        $session->expects($this->exactly(2))->method('has')->willReturn(false);
62
63
        $listener = new SessionSecurityListener(true, true, $logger);
0 ignored issues
show
Documentation introduced by
$logger is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Psr\Log\LoggerInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
64
        $listener->onKernelResponse($event);
0 ignored issues
show
Documentation introduced by
$event is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Symfony\Component...nt\FilterResponseEvent>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
65
66
        $event = $this->createMock(FilterResponseEvent::class);
67
        $listener->onKernelResponse($event);
0 ignored issues
show
Documentation introduced by
$event is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Symfony\Component...nt\FilterResponseEvent>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
68
    }
69
70
    public function testInvalidateSessionWithNoIpSet()
71
    {
72
        $logger = $this->createMock(LoggerInterface::class);
73
        $request = $this->createMock(Request::class);
74
        $request->server = $this->createMock(ServerBag::class);
0 ignored issues
show
Bug introduced by
Accessing server on the interface PHPUnit\Framework\MockObject\MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
75
        $request->headers = $this->createMock(HeaderBag::class);
0 ignored issues
show
Bug introduced by
Accessing headers on the interface PHPUnit\Framework\MockObject\MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
76
77
        $event = $this->createMock(FilterResponseEvent::class);
78
        $session = $this->createMock(Session::class);
79
80
        $event->expects($this->any())->method('getRequestType')->willReturn(HttpKernelInterface::MASTER_REQUEST);
81
        $event->expects($this->any())->method('getRequest')->willReturn($request);
82
        $request->expects($this->once())->method('hasSession')->willReturn(true);
83
        $request->expects($this->exactly(2))->method('getSession')->willReturn($session);
84
        $request->expects($this->once())->method('getClientIp')->willReturn('95.154.243.5');
85
        $request->server->expects($this->any())->method('get')->willReturn('');
0 ignored issues
show
Bug introduced by
Accessing server on the interface PHPUnit\Framework\MockObject\MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
86
        $request->headers->expects($this->any())->method('get')->willReturn('kuma_ua');
0 ignored issues
show
Bug introduced by
Accessing headers on the interface PHPUnit\Framework\MockObject\MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?

If you access a property on an interface, you most likely code against a concrete implementation of the interface.

Available Fixes

  1. Adding an additional type check:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeInterface $object) {
        if ($object instanceof SomeClass) {
            $a = $object->a;
        }
    }
    
  2. Changing the type hint:

    interface SomeInterface { }
    class SomeClass implements SomeInterface {
        public $a;
    }
    
    function someFunction(SomeClass $object) {
        $a = $object->a;
    }
    
Loading history...
87
        $session->expects($this->once())->method('isStarted')->willReturn(true);
88
        $session->expects($this->exactly(2))->method('has')->willReturn(false);
89
90
        $listener = new SessionSecurityListener(true, true, $logger);
0 ignored issues
show
Documentation introduced by
$logger is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Psr\Log\LoggerInterface>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
91
        $listener->onKernelResponse($event);
0 ignored issues
show
Documentation introduced by
$event is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Symfony\Component...nt\FilterResponseEvent>.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
92
    }
93
}
94