Passed
Push — master ( f780f9...8d5450 )
by Patrick
03:32
created

createMattermostClientFacade()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 29
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
cc 1
eloc 22
nc 1
nop 1
dl 0
loc 29
rs 9.568
c 1
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\JiraClient\JiraClientFacade;
15
use ForecastAutomation\JiraClient\JiraClientFactory;
16
use ForecastAutomation\JiraClient\Shared\Dto\JiraConfigDto;
17
use ForecastAutomation\MattermostClient\Business\MattermostApi;
18
use ForecastAutomation\MattermostClient\MattermostClientFacade;
19
use ForecastAutomation\MattermostClient\MattermostClientFactory;
20
use ForecastAutomation\MattermostClient\Shared\Dto\MattermostChannelFilterQueryDto;
21
use ForecastAutomation\MattermostClient\Shared\Dto\MattermostConfigDto;
22
use ForecastAutomation\MattermostClient\Shared\Dto\MattermostPostsQueryDto;
23
use GuzzleHttp\Client;
24
use GuzzleHttp\Psr7\Response;
25
use JiraRestApi\Issue\Comment;
26
use JiraRestApi\Issue\Issue;
27
use JiraRestApi\Issue\IssueSearchResult;
28
use JiraRestApi\Issue\IssueService;
29
use JiraRestApi\Request\Author;
30
use PHPUnit\Framework\TestCase;
31
32
/**
33
 * @internal
34
 * @covers
35
 */
36
final class MattermostClientFacadeTest extends TestCase
37
{
38
    public const LAST_POST = '2021-01-02 00:00:00';
39
    public const CHANNEL_TYPE = 'D';
40
    public const MSG_COUNT = 1;
41
    public const CHANNEL_FILTER_LAST_POSTS = '2021-01-01 00:00:00';
42
    public const POSTS_FILTER_LAST_POSTS = '2021-01-01 00:00:00';
43
44
    public function testCanGetChannel(): void
45
    {
46
        $channels = $this->createMattermostClientFacade(
47
            $this->createChannelResponse((int)((new \DateTime(self::LAST_POST))->format('U')) * 1000)
48
        )->getChannel(
49
            new MattermostChannelFilterQueryDto(new \DateTime(self::CHANNEL_FILTER_LAST_POSTS))
50
        );
51
        static::assertCount(1, $channels);
52
        static::assertSame(self::MSG_COUNT, $channels[0]->total_msg_count);
53
        static::assertSame(self::CHANNEL_TYPE, $channels[0]->type);
54
        static::assertSame((new \DateTime(self::LAST_POST))->format('U') * 1000, $channels[0]->last_post_at);
55
    }
56
57
    public function testCanGetPosts(): void
58
    {
59
        $posts = $this->createMattermostClientFacade($this->createPostsResponse())->getPosts(
60
            new MattermostPostsQueryDto('test-id', new \DateTime(self::POSTS_FILTER_LAST_POSTS))
61
        );
62
        static::assertCount(1, $posts);
63
        static::assertSame('test-id-1234', $posts[0]['post-id']);
64
    }
65
66
    private function createMattermostClientFacade(string $jsonApiResponse): MattermostClientFacade
67
    {
68
        $clientMock = $this->getMockBuilder(Client::class)
69
            ->onlyMethods(['request'])
70
            ->getMock();
71
        $clientMock
72
            ->method('request')
73
            ->willReturn(
74
                new Response(200, ['X-Foo' => 'Bar'], $jsonApiResponse)
75
            );
76
77
        $mattermostClientFactoryMock = $this->getMockBuilder(MattermostClientFactory::class)
78
            ->onlyMethods(['createMattermostApi'])
79
            ->getMock();
80
81
        $mattermostApi = new MattermostApi($clientMock, new MattermostConfigDto('', '', '', ''));
82
        $mattermostApi::$token = 'test-token';
83
        $mattermostClientFactoryMock
84
            ->method('createMattermostApi')
85
            ->willReturn($mattermostApi);
86
87
        $mattermostClientFacadeMock = $this->getMockBuilder(MattermostClientFacade::class)
88
            ->onlyMethods(['getFactory'])
89
            ->getMock();
90
        $mattermostClientFacadeMock
91
            ->method('getFactory')
92
            ->willReturn($mattermostClientFactoryMock);
93
94
        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...
95
    }
96
97
    private function createPostsResponse(): string
98
    {
99
        return '{"posts":[{"post-id":"test-id-1234","message":"test-msg"}]}';
100
    }
101
102
    private function createChannelResponse(int $lastPostAt): string
103
    {
104
        return '[{"id":"test-id-1234","total_msg_count":' . self::MSG_COUNT . ',"type":"' . self::CHANNEL_TYPE . '","last_post_at":' . $lastPostAt . '}]';
105
    }
106
}
107