MattermostClientFacadeTest   A
last analyzed

Complexity

Total Complexity 6

Size/Duplication

Total Lines 87
Duplicated Lines 0 %

Importance

Changes 6
Bugs 0 Features 1
Metric Value
eloc 48
dl 0
loc 87
rs 10
c 6
b 0
f 1
wmc 6

6 Methods

Rating   Name   Duplication   Size   Complexity  
A testCanGetPosts() 0 7 1
A setUp() 0 3 1
A testCanGetChannel() 0 14 1
A createPostsResponse() 0 3 1
A createMattermostClientFacade() 0 37 1
A createChannelResponse() 0 3 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
        ;
76
        $clientMock
77
            ->method('requestAsync')
78
            ->willReturn(
79
                $this->guzzleFactoryHelper->createResolvedPromise(
80
                    new Response(200, ['X-Foo' => 'Bar'], $jsonApiResponse)
81
                )
82
            )
83
        ;
84
85
        $mattermostClientFactoryMock = $this->getMockBuilder(MattermostClientFactory::class)
86
            ->onlyMethods(['createMattermostApi'])
87
            ->getMock()
88
        ;
89
90
        $mattermostApi = new MattermostApi($clientMock, new MattermostConfigDto('', '', '', ''));
91
        $mattermostApi::$token = 'test-token';
92
        $mattermostClientFactoryMock
93
            ->method('createMattermostApi')
94
            ->willReturn($mattermostApi)
95
        ;
96
97
        $mattermostClientFacadeMock = $this->getMockBuilder(MattermostClientFacade::class)
98
            ->onlyMethods(['getFactory'])
99
            ->getMock()
100
        ;
101
        $mattermostClientFacadeMock
102
            ->method('getFactory')
103
            ->willReturn($mattermostClientFactoryMock)
104
        ;
105
106
        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...
107
    }
108
109
    private function createPostsResponse(): string
110
    {
111
        return '{"posts":[{"post-id":"test-id-1234","message":"test-msg"}]}';
112
    }
113
114
    private function createChannelResponse(int $lastPostAt): string
115
    {
116
        return '[{"id":"test-id-1234","total_msg_count":'.self::MSG_COUNT.',"type":"'.self::CHANNEL_TYPE.'","last_post_at":'.$lastPostAt.'}]';
117
    }
118
}
119