DefaultControllerTest   A
last analyzed

Complexity

Total Complexity 3

Size/Duplication

Total Lines 78
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 5

Importance

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

3 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 7 1
A testShowActionPageNotFound() 0 14 1
A testShowAction() 0 32 1
1
<?php
2
3
namespace Beelab\SimplePageBundle\Tests\Controller;
4
5
use Beelab\SimplePageBundle\Controller\DefaultController;
6
use Beelab\SimplePageBundle\Entity\Page;
7
use Doctrine\ORM\EntityRepository;
8
use Doctrine\Persistence\ManagerRegistry;
9
use PHPUnit\Framework\TestCase;
10
use Symfony\Component\DependencyInjection\ContainerInterface;
11
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
12
use Twig\Environment;
13
14
final class DefaultControllerTest extends TestCase
15
{
16
    /**
17
     * @var DefaultController
18
     */
19
    private $controller;
20
21
    /**
22
     * @var ContainerInterface&\PHPUnit\Framework\MockObject\MockObject
23
     */
24
    private $container;
25
26
    /**
27
     * @var ManagerRegistry&\PHPUnit\Framework\MockObject\MockObject
28
     */
29
    private $doctrine;
30
31
    /**
32
     * @var Environment&\PHPUnit\Framework\MockObject\MockObject
33
     */
34
    private $twig;
35
36
    protected function setUp(): void
37
    {
38
        $this->container = $this->createMock(ContainerInterface::class);
39
        $this->doctrine = $this->createMock(ManagerRegistry::class);
40
        $this->twig = $this->createMock(Environment::class);
41
        $this->controller = new DefaultController($this->doctrine, $this->twig, 'foo', 'bar');
0 ignored issues
show
Documentation introduced by
$this->doctrine is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Doctrine\Persistence\ManagerRegistry>.

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...
Documentation introduced by
$this->twig is of type object<PHPUnit\Framework\MockObject\MockObject>, but the function expects a object<Twig\Environment>.

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
    public function testShowActionPageNotFound(): void
45
    {
46
        $this->expectException(NotFoundHttpException::class);
47
        $repo = $this->createMock(EntityRepository::class);
48
49
        $this->doctrine
50
            ->expects(self::once())
51
            ->method('getRepository')
52
            ->with('foo')
53
            ->willReturn($repo)
54
        ;
55
56
        $this->controller->showAction('baz');
57
    }
58
59
    public function testShowAction(): void
60
    {
61
        $page = $this->createMock(Page::class);
62
63
        $repo = $this
64
            ->getMockBuilder(EntityRepository::class)
65
            ->disableOriginalConstructor()
66
            ->addMethods(['findOneByPath'])
67
            ->getMock()
68
        ;
69
70
        $this->doctrine
71
            ->expects(self::once())
72
            ->method('getRepository')
73
            ->with('foo')
74
            ->willReturn($repo)
75
        ;
76
77
        $repo
78
            ->expects(self::once())
79
            ->method('findOneByPath')
80
            ->with('foo')
81
            ->willReturn($page)
82
        ;
83
84
        $this->twig
0 ignored issues
show
Bug introduced by
The method method() does not seem to exist on object<PHPUnit\Framework\MockObject\MockObject>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
85
            ->method('render')
86
            ->willReturn('html content...')
87
        ;
88
89
        $this->controller->showAction('foo');
90
    }
91
}
92