Completed
Push — master ( 1de9b7...830752 )
by Kristof
38:46 queued 24:09
created

unit/EventListener/SessionSecurityListenerTest.php (12 issues)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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()
19
    {
20
        $logger = $this->createMock(LoggerInterface::class);
21
        $request = $this->createMock(Request::class);
22
        $request->server = $this->createMock(ServerBag::class);
0 ignored issues
show
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
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
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
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);
38
        $listener->onKernelRequest($event);
39
40
        $event = $this->createMock(GetResponseEvent::class);
41
        $listener->onKernelRequest($event);
42
    }
43
44 View Code Duplication
    public function testOnKernelResponse()
45
    {
46
        $logger = $this->createMock(LoggerInterface::class);
47
        $request = $this->createMock(Request::class);
48
        $request->server = $this->createMock(ServerBag::class);
0 ignored issues
show
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
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
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
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);
64
        $listener->onKernelResponse($event);
65
66
        $event = $this->createMock(FilterResponseEvent::class);
67
        $listener->onKernelResponse($event);
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
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
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
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
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);
91
        $listener->onKernelResponse($event);
92
    }
93
}
94