Completed
Push — master ( ce34f1...a61f8e )
by
unknown
04:41 queued 10s
created

SessionDownloadStrategyTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 68
rs 10
wmc 4
lcom 1
cbo 2

4 Methods

Rating   Name   Duplication   Size   Complexity  
A testIsGrantedFalseWithGreaterSessionTime() 0 14 1
A testIsGrantedTrue() 0 14 1
A testIsGrantedTrueWithContainer() 0 21 1
A testInvalidArgumentException() 0 6 1
1
<?php
2
3
/*
4
 * This file is part of the Sonata Project package.
5
 *
6
 * (c) Thomas Rabaix <[email protected]>
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Sonata\MediaBundle\Tests\Security;
13
14
use Sonata\MediaBundle\Security\SessionDownloadStrategy;
15
16
/**
17
 * Class SessionDownloadStrategyTest.
18
 *
19
 * @author Ahmet Akbana <[email protected]>
20
 */
21
final class SessionDownloadStrategyTest extends \PHPUnit_Framework_TestCase
22
{
23
    public function testIsGrantedFalseWithGreaterSessionTime()
24
    {
25
        $translator = $this->getMock('Symfony\Component\Translation\TranslatorInterface');
26
        $media = $this->getMock('Sonata\MediaBundle\Model\MediaInterface');
27
        $request = $this->getMock('Symfony\Component\HttpFoundation\Request');
28
        $session = $this->getMock('Symfony\Component\HttpFoundation\Session\Session');
29
30
        $session->expects($this->any())
31
            ->method('get')
32
            ->willReturn(1);
33
34
        $strategy = new SessionDownloadStrategy($translator, $session, 0);
35
        $this->assertFalse($strategy->isGranted($media, $request));
36
    }
37
38
    public function testIsGrantedTrue()
39
    {
40
        $translator = $this->getMock('Symfony\Component\Translation\TranslatorInterface');
41
        $media = $this->getMock('Sonata\MediaBundle\Model\MediaInterface');
42
        $request = $this->getMock('Symfony\Component\HttpFoundation\Request');
43
        $session = $this->getMock('Symfony\Component\HttpFoundation\Session\Session');
44
45
        $session->expects($this->any())
46
            ->method('get')
47
            ->willReturn(0);
48
49
        $strategy = new SessionDownloadStrategy($translator, $session, 1);
50
        $this->assertTrue($strategy->isGranted($media, $request));
51
    }
52
53
    /**
54
     * @group legacy
55
     * NEXT_MAJOR: remove this method
56
     */
57
    public function testIsGrantedTrueWithContainer()
58
    {
59
        $container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface');
60
61
        $translator = $this->getMock('Symfony\Component\Translation\TranslatorInterface');
62
        $media = $this->getMock('Sonata\MediaBundle\Model\MediaInterface');
63
        $request = $this->getMock('Symfony\Component\HttpFoundation\Request');
64
        $session = $this->getMock('Symfony\Component\HttpFoundation\Session\Session');
65
66
        $session->expects($this->any())
67
            ->method('get')
68
            ->willReturn(0);
69
70
        $container->expects($this->once())
71
            ->method('get')
72
            ->willReturn($session);
73
74
        $strategy = new SessionDownloadStrategy($translator, $container, 1);
75
76
        $this->assertTrue($strategy->isGranted($media, $request));
77
    }
78
79
    /**
80
     * @expectedException \InvalidArgumentException
81
     */
82
    public function testInvalidArgumentException()
83
    {
84
        $translator = $this->getMock('Symfony\Component\Translation\TranslatorInterface');
85
86
        new SessionDownloadStrategy($translator, 'foo', 1);
0 ignored issues
show
Documentation introduced by
'foo' is of type string, but the function expects a object<Symfony\Component...ssion\SessionInterface>.

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...
87
    }
88
}
89