Completed
Push — master ( 92e6a8...22c070 )
by Níckolas Daniel
04:56
created

createDefaultAudioEpisodeCollection()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 21
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 16
dl 0
loc 21
rs 9.7333
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
3
namespace PODEntender\Application\Service\Post;
4
5
use PHPUnit\Framework\TestCase;
6
use PODEntender\Domain\Model\Post\AudioEpisode;
7
use PODEntender\Domain\Model\Post\AudioEpisodeCollection;
8
use PODEntender\Domain\Model\Post\PostRepository;
9
10
class FetchLatestEpisodesTest extends TestCase
11
{
12
    /** @var PostRepository */
13
    private $postRepository;
14
15
    /** @var FetchLatestEpisodes */
16
    private $fetchLatestEpisodesService;
17
18
    protected function setUp(): void
19
    {
20
        $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...
21
        $this->fetchLatestEpisodesService = new FetchLatestEpisodes(
22
            $this->postRepository->reveal()
23
        );
24
    }
25
26
    public function testExecuteFetchesExactAmount(): void
27
    {
28
        $audioEpisodeCollection = $this->createDefaultAudioEpisodeCollection()
29
            ->sortByDesc(function (AudioEpisode $episode) {
30
                return $episode->createdAt();
31
            });
32
33
        $this->postRepository->withAudio()->willReturn($audioEpisodeCollection);
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

33
        $this->postRepository->withAudio()->/** @scrutinizer ignore-call */ willReturn($audioEpisodeCollection);
Loading history...
34
35
        $result = $this->fetchLatestEpisodesService->execute(2, null);
36
        $this->assertEquals(2, $result->count());
37
38
        $lastEpisode = $result->first();
39
        $episodeBeforeLast = $result->last();
40
41
        $this->assertEquals($audioEpisodeCollection->take(2)->first()->guid(), $lastEpisode->guid());
42
        $this->assertEquals($audioEpisodeCollection->take(2)->last()->guid(), $episodeBeforeLast->guid());
43
    }
44
45
    public function testExecuteFiltersCategoryName(): void
46
    {
47
        $audioEpisodeCollection = $this->createDefaultAudioEpisodeCollection()
48
            ->sortByDesc(function (AudioEpisode $episode) {
49
                return $episode->createdAt();
50
            });
51
52
        $this->postRepository->withAudio()->willReturn($audioEpisodeCollection);
53
54
        $result = $this->fetchLatestEpisodesService->execute(2, 'Entrevista');
55
        $this->assertEquals(2, $result->count());
56
57
        $lastEpisode = $result->first();
58
        $episodeBeforeLast = $result->last();
59
60
        $this->assertEquals($audioEpisodeCollection->take(2)->last()->guid(), $lastEpisode->guid());
61
        $this->assertEquals($audioEpisodeCollection->last()->guid(), $episodeBeforeLast->guid());
62
    }
63
64
    private function createDefaultAudioEpisodeCollection(): AudioEpisodeCollection
65
    {
66
        $episode01 = $this->prophesize(AudioEpisode::class);
67
        $episode01->guid()->willReturn('ep01');
68
        $episode01->category()->willReturn('Entrevista');
69
        $episode01->createdAt()->willReturn(new \DateTime('2019-01-01'));
70
71
        $episode02 = $this->prophesize(AudioEpisode::class);
72
        $episode02->guid()->willReturn('ep02');
73
        $episode02->category()->willReturn('Entrevista');
74
        $episode02->createdAt()->willReturn(new \DateTime('2019-02-01'));
75
76
        $episode03 = $this->prophesize(AudioEpisode::class);
77
        $episode03->guid()->willReturn('ep03');
78
        $episode03->category()->willReturn('News');
79
        $episode03->createdAt()->willReturn(new \DateTime('2019-03-01'));
80
81
        return new AudioEpisodeCollection([
82
            $episode01->reveal(),
83
            $episode02->reveal(),
84
            $episode03->reveal(),
85
        ]);
86
    }
87
}
88