ViewFactoryTest   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 3
lcom 1
cbo 3
dl 0
loc 65
rs 10
c 0
b 0
f 0

1 Method

Rating   Name   Duplication   Size   Complexity  
B testFactory() 0 62 3
1
<?php
2
3
namespace Pagerfanta\Tests\View;
4
5
use Pagerfanta\View\ViewFactory;
6
use PHPUnit\Framework\TestCase;
7
8
class ViewFactoryTest extends TestCase
9
{
10
    public function testFactory()
11
    {
12
        $view1 = $this->getMockBuilder('Pagerfanta\View\ViewInterface')->getMock();
13
        $view2 = $this->getMockBuilder('Pagerfanta\View\ViewInterface')->getMock();
14
        $view3 = $this->getMockBuilder('Pagerfanta\View\ViewInterface')->getMock();
15
        $view4 = $this->getMockBuilder('Pagerfanta\View\ViewInterface')->getMock();
16
17
        $factory = new ViewFactory();
18
19
        $factory->set('foo', $view1);
0 ignored issues
show
Documentation introduced by
$view1 is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Pagerfanta\View\ViewInterface>.

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...
20
        $factory->set('bar', $view2);
0 ignored issues
show
Documentation introduced by
$view2 is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Pagerfanta\View\ViewInterface>.

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...
21
22
        $this->assertSame(array('foo' => $view1, 'bar' => $view2), $factory->all());
23
24
        $this->assertSame($view1, $factory->get('foo'));
25
        $this->assertSame($view2, $factory->get('bar'));
26
        try {
27
            $factory->get('foobar');
28
            $this->fail();
29
        } catch (\Exception $e) {
30
            $this->assertInstanceOf('Pagerfanta\Exception\InvalidArgumentException', $e);
31
        }
32
33
        $this->assertTrue($factory->has('foo'));
34
        $this->assertTrue($factory->has('bar'));
35
        $this->assertFalse($factory->has('foobar'));
36
37
        $factory->add(array(
38
            'ups' => $view3,
39
            'man' => $view4,
40
        ));
41
        $this->assertSame($view3, $factory->get('ups'));
42
        $this->assertSame($view4, $factory->get('man'));
43
        $this->assertTrue($factory->has('ups'));
44
        $this->assertTrue($factory->has('man'));
45
        $this->assertSame(array(
46
            'foo' => $view1,
47
            'bar' => $view2,
48
            'ups' => $view3,
49
            'man' => $view4,
50
        ), $factory->all());
51
52
        $factory->remove('bar');
53
        $this->assertFalse($factory->has('bar'));
54
        $this->assertTrue($factory->has('foo'));
55
        $this->assertTrue($factory->has('ups'));
56
        $this->assertTrue($factory->has('man'));
57
        $this->assertSame(array(
58
            'foo' => $view1,
59
            'ups' => $view3,
60
            'man' => $view4,
61
        ), $factory->all());
62
        try {
63
            $factory->remove('foobar');
64
            $this->fail();
65
        } catch (\Exception $e) {
66
            $this->assertInstanceOf('Pagerfanta\Exception\InvalidArgumentException', $e);
67
        }
68
69
        $factory->clear();
70
        $this->assertSame(array(), $factory->all());
71
    }
72
}
73