-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDependencies.php
65 lines (55 loc) · 2.1 KB
/
Dependencies.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
namespace adamcameron\symfonythefasttrack\tests\Functional\Controller\ConferenceController\Show;
use adamcameron\symfonythefasttrack\Entity\Comment;
use adamcameron\symfonythefasttrack\Entity\Conference;
use adamcameron\symfonythefasttrack\Repository\CommentRepository;
use ArrayIterator;
use Doctrine\ORM\Tools\Pagination\Paginator;
use Twig\Environment as TwigEnvironment;
trait Dependencies
{
private function getTestDependencies(string $conferenceTitle): array
{
$conference = $this->getMockedConference($conferenceTitle);
$commentRepository = $this->getMockedCommentRepository();
return array($conference, $commentRepository);
}
private function getMockedConference(string $conferenceTitle): Conference
{
$conference = $this->getMockBuilder(Conference::class)
->disableOriginalConstructor()
->getMock();
$conference
->expects($this->any())
->method("__toString")
->willReturn($conferenceTitle);
return $conference;
}
private function getMockedCommentRepository(): CommentRepository
{
$commentCount = 2;
$commentsToTestFor = range(1, $commentCount);
$testComments = array_map(function ($i) {
return (new Comment())
->setAuthor("COMMENTER" . $i)
->setText("COMMENT" . $i)
->setCreatedAt(new \DateTimeImmutable("2020-01-0{$i}"));
}, $commentsToTestFor);
$mockedPaginator = $this->getMockBuilder(Paginator::class)
->disableOriginalConstructor()
->getMock();
$mockedPaginator
->method("count")
->willReturn($commentCount);
$mockedPaginator
->method("getIterator")
->willReturn(new ArrayIterator($testComments));
$commentRepository = $this->getMockBuilder(CommentRepository::class)
->disableOriginalConstructor()
->getMock();
$commentRepository
->method("getPaginator")
->willReturn($mockedPaginator);
return $commentRepository;
}
}