Completed
Push — master ( 954c4b...e8a8b5 )
by Níckolas Daniel
04:55
created

testRecommendationsAreSortedByCreationDateDescending()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 7
dl 0
loc 12
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace PODEntender\Domain\Service\Post;
4
5
use DateTime;
6
use PHPUnit\Framework\TestCase;
7
use PODEntender\Domain\Model\Post\AudioEpisode;
8
use PODEntender\Domain\Model\Post\AudioEpisodeCollection;
9
use PODEntender\Domain\Model\Post\PostRepository;
10
11
class RecommendationTest extends TestCase
12
{
13
    /** @var PostRepository */
14
    private $postRepository;
15
16
    /** @var Recommendation */
17
    private $recommendationService;
18
19
    protected function setUp(): void
20
    {
21
        $this->postRepository = $this->prophesize(PostRepository::class);
1 ignored issue
show
Documentation Bug introduced by
It seems like $this->prophesize(PODEnt...\PostRepository::class) of type Prophecy\Prophecy\ObjectProphecy is incompatible with the declared type PODEntender\Domain\Model\Post\PostRepository of property $postRepository.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
22
23
        $this->recommendationService = new Recommendation(
24
            $this->postRepository->reveal()
25
        );
26
    }
27
28
    public function testRecommendEpisodesForPostDoesntIncludeReferencePost(): void
29
    {
30
        $this->postRepository->withAudio()->willReturn($this->createDefaultAudioEpisodeCollection());
1 ignored issue
show
Bug introduced by
The method willReturn() does not exist on PODEntender\Domain\Model...\AudioEpisodeCollection. Since you implemented __call, consider adding a @method annotation. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

30
        $this->postRepository->withAudio()->/** @scrutinizer ignore-call */ willReturn($this->createDefaultAudioEpisodeCollection());
Loading history...
31
32
        $referenceEpisodeProphecy = $this->prophesize(AudioEpisode::class);
33
        $referenceEpisodeProphecy->guid()->willReturn('ep03');
34
        $referenceEpisodeProphecy->tags()->willReturn([]);
35
        $referenceEpisode = $referenceEpisodeProphecy->reveal();
36
37
        $recommended = $this->recommendationService
38
            ->recommendEpisodesForPost(
39
                $referenceEpisode,
40
                999
41
            )
42
            ->filter(function (AudioEpisode $episode) use ($referenceEpisode) {
43
                return $episode->guid() === $referenceEpisode->guid();
44
            });
45
46
        $this->assertEmpty($recommended);
47
    }
48
49
    public function testRecommendationsAreSortedByCreationDateDescending(): void
50
    {
51
        $this->postRepository->withAudio()->willReturn($this->createDefaultAudioEpisodeCollection());
52
53
        $referenceEpisodeProphecy = $this->prophesize(AudioEpisode::class);
54
        $referenceEpisodeProphecy->guid()->willReturn('ep03');
55
        $referenceEpisodeProphecy->tags()->willReturn([]);
56
        $referenceEpisode = $referenceEpisodeProphecy->reveal();
57
58
        $recommended = $this->recommendationService->recommendEpisodesForPost($referenceEpisode,999);
59
60
        $this->assertGreaterThan($recommended->last()->createdAt(), $recommended->first()->createdAt());
61
    }
62
63
    public function testRecommendationsAreLimitedByAmountParameter(): void
64
    {
65
        $this->postRepository->withAudio()->willReturn($this->createDefaultAudioEpisodeCollection());
66
67
        $referenceEpisodeProphecy = $this->prophesize(AudioEpisode::class);
68
        $referenceEpisodeProphecy->guid()->willReturn('ep03');
69
        $referenceEpisodeProphecy->tags()->willReturn([]);
70
        $referenceEpisode = $referenceEpisodeProphecy->reveal();
71
72
        $recommended = $this->recommendationService->recommendEpisodesForPost($referenceEpisode,2);
73
        $this->assertEquals(2, $recommended->count());
74
75
        $recommended = $this->recommendationService->recommendEpisodesForPost($referenceEpisode,1);
76
        $this->assertEquals(1, $recommended->count());
77
    }
78
79
    private function createDefaultAudioEpisodeCollection(): AudioEpisodeCollection
80
    {
81
        $episode01 = $this->prophesize(AudioEpisode::class);
82
        $episode01->guid()->willReturn('ep01');
83
        $episode01->tags()->willReturn([]);
84
        $episode01->createdAt()->willReturn(new DateTime('2019-01-01'));
85
86
        $episode02 = $this->prophesize(AudioEpisode::class);
87
        $episode02->guid()->willReturn('ep02');
88
        $episode02->tags()->willReturn([]);
89
        $episode02->createdAt()->willReturn(new DateTime('2019-02-01'));
90
91
        $episode03 = $this->prophesize(AudioEpisode::class);
92
        $episode03->guid()->willReturn('ep03');
93
        $episode03->tags()->willReturn([]);
94
        $episode03->createdAt()->willReturn(new DateTime('2019-03-01'));
95
96
        return new AudioEpisodeCollection([
97
            $episode01->reveal(),
98
            $episode02->reveal(),
99
            $episode03->reveal(),
100
        ]);
101
    }
102
}
103