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'); |
|
|
|
|
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 |
|
|
|
|
85
|
|
|
->method('render') |
86
|
|
|
->willReturn('html content...') |
87
|
|
|
; |
88
|
|
|
|
89
|
|
|
$this->controller->showAction('foo'); |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
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: