Passed
Push — master ( ee061b...116180 )
by Patrick
03:02
created

MattermostClientFacadeTest::testCanGetPosts()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 0
loc 7
rs 10
c 2
b 0
f 1
1
<?php
2
3
declare(strict_types = 1);
4
5
/*
6
 * This file is part of forecast.it.fill project.
7
 * (c) Patrick Jaja <[email protected]>
8
 * This source file is subject to the MIT license that is bundled
9
 * with this source code in the file LICENSE.
10
 */
11
12
namespace ForecastAutomationTests\MattermostClient;
13
14
use ForecastAutomation\MattermostClient\Business\MattermostApi;
15
use ForecastAutomation\MattermostClient\MattermostClientFacade;
16
use ForecastAutomation\MattermostClient\MattermostClientFactory;
17
use ForecastAutomation\MattermostClient\Shared\Dto\MattermostConfigDto;
18
use ForecastAutomation\MattermostClient\Shared\Dto\MattermostPostsQueryDto;
19
use ForecastAutomation\MattermostClient\Shared\Plugin\Filter\HasMessageChannelFilter;
20
use ForecastAutomation\MattermostClient\Shared\Plugin\Filter\IsDirectChannelFilter;
21
use ForecastAutomationTests\GuzzleClient\Shared\GuzzleFactoryHelper;
22
use GuzzleHttp\Client;
23
use GuzzleHttp\Psr7\Response;
24
use PHPUnit\Framework\TestCase;
25
26
/**
27
 * @internal
28
 * @covers
29
 */
30
final class MattermostClientFacadeTest extends TestCase
31
{
32
    public const LAST_POST = '2021-01-02 00:00:00';
33
    public const CHANNEL_TYPE = 'D';
34
    public const MSG_COUNT = 1;
35
    public const CHANNEL_FILTER_LAST_POSTS = '2021-01-01 00:00:00';
36
    public const POSTS_FILTER_LAST_POSTS = '2021-01-01 00:00:00';
37
38
    private GuzzleFactoryHelper $guzzleFactoryHelper;
39
40
    protected function setUp(): void
41
    {
42
        $this->guzzleFactoryHelper = new GuzzleFactoryHelper();
43
    }
44
45
    public function testCanGetChannel(): void
46
    {
47
        $channels = $this->createMattermostClientFacade(
48
            $this->createChannelResponse((int)((new \DateTime(self::LAST_POST))->format('U')) * 1000)
49
        )->getChannel(
50
            [
51
                new HasMessageChannelFilter(new \DateTime(self::CHANNEL_FILTER_LAST_POSTS)),
52
                new IsDirectChannelFilter(),
53
            ]
54
        )->wait();
55
        static::assertCount(1, $channels);
56
        static::assertSame(self::MSG_COUNT, $channels[0]->total_msg_count);
57
        static::assertSame(self::CHANNEL_TYPE, $channels[0]->type);
58
        static::assertSame((new \DateTime(self::LAST_POST))->format('U') * 1000, $channels[0]->last_post_at);
59
    }
60
61
    public function testCanGetPosts(): void
62
    {
63
        $posts = $this->createMattermostClientFacade($this->createPostsResponse())->getPosts(
64
            new MattermostPostsQueryDto('test-id', new \DateTime(self::POSTS_FILTER_LAST_POSTS))
65
        )->wait();
66
        static::assertCount(1, $posts);
67
        static::assertSame('test-id-1234', $posts[0]['post-id']);
68
    }
69
70
    private function createMattermostClientFacade(string $jsonApiResponse): MattermostClientFacade
71
    {
72
        $clientMock = $this->getMockBuilder(Client::class)
73
            ->onlyMethods(['requestAsync'])
74
            ->getMock();
75
        $clientMock
76
            ->method('requestAsync')
77
            ->willReturn(
78
                $this->guzzleFactoryHelper->createResolvedPromise(
79
                    new Response(200, ['X-Foo' => 'Bar'], $jsonApiResponse)
80
                )
81
            );
82
83
        $mattermostClientFactoryMock = $this->getMockBuilder(MattermostClientFactory::class)
84
            ->onlyMethods(['createMattermostApi'])
85
            ->getMock();
86
87
        $mattermostApi = new MattermostApi($clientMock, new MattermostConfigDto('', '', '', ''));
88
        $mattermostApi::$token = 'test-token';
89
        $mattermostClientFactoryMock
90
            ->method('createMattermostApi')
91
            ->willReturn($mattermostApi);
92
93
        $mattermostClientFacadeMock = $this->getMockBuilder(MattermostClientFacade::class)
94
            ->onlyMethods(['getFactory'])
95
            ->getMock();
96
        $mattermostClientFacadeMock
97
            ->method('getFactory')
98
            ->willReturn($mattermostClientFactoryMock);
99
100
        return $mattermostClientFacadeMock;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $mattermostClientFacadeMock returns the type PHPUnit\Framework\MockObject\MockObject which is incompatible with the type-hinted return ForecastAutomation\Matte...\MattermostClientFacade.
Loading history...
101
    }
102
103
    private function createPostsResponse(): string
104
    {
105
        return '{"posts":[{"post-id":"test-id-1234","message":"test-msg"}]}';
106
    }
107
108
    private function createChannelResponse(int $lastPostAt): string
109
    {
110
        return '[{"id":"test-id-1234","total_msg_count":' . self::MSG_COUNT . ',"type":"' . self::CHANNEL_TYPE . '","last_post_at":' . $lastPostAt . '}]';
111
    }
112
}
113