JiraClientFacadeTest::createGitlabClientFacade()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 60
Code Lines 39

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
cc 1
eloc 39
c 2
b 0
f 1
nc 1
nop 0
dl 0
loc 60
rs 9.296

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

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\JiraClient;
13
14
use ForecastAutomation\JiraClient\JiraClientFacade;
15
use ForecastAutomation\JiraClient\JiraClientFactory;
16
use ForecastAutomation\JiraClient\Shared\Dto\JiraConfigDto;
17
use JiraRestApi\Issue\Comment;
18
use JiraRestApi\Issue\Issue;
19
use JiraRestApi\Issue\IssueSearchResult;
20
use JiraRestApi\Issue\IssueService;
21
use JiraRestApi\Request\Author;
22
use PHPUnit\Framework\TestCase;
23
24
/**
25
 * @internal
26
 * @covers
27
 */
28
final class JiraClientFacadeTest extends TestCase
29
{
30
    public const TESTUSER_EXAMPLE_COM = '[email protected]';
31
    public const TICKET_COMMENT_UPDATED = '2021-01-02 11:00:00';
32
    public const TICKET_COMMENT_BODY = 'test';
33
34
    public function testCanReadEvents(): void
35
    {
36
        $comments = $this->createGitlabClientFacade()->getComments(
37
            '2021-01-02 10:00:00'
38
        );
39
        static::assertArrayHasKey('test-key', $comments);
40
        static::assertSame(self::TICKET_COMMENT_BODY, $comments['test-key'][0]->body);
41
        static::assertSame('[email protected]', $comments['test-key'][0]->author->emailAddress);
42
        static::assertSame(self::TICKET_COMMENT_UPDATED, $comments['test-key'][0]->updated);
43
    }
44
45
    private function createGitlabClientFacade(): JiraClientFacade
46
    {
47
        $issueServiceMock = $this->getMockBuilder(IssueService::class)->disableOriginalConstructor()
48
            ->onlyMethods(['search', 'getComments'])
49
            ->getMock()
50
        ;
51
52
        $issueSearchResult = new IssueSearchResult();
53
54
        $testIssue = new Issue();
55
        $testIssue->key = 'test-key';
56
        $issueSearchResult->setIssues([$testIssue]);
57
        static::returnValue($issueSearchResult);
58
59
        $issueServiceMock
60
            ->method('search')
61
            ->willReturn(
62
                $issueSearchResult
63
            )
64
        ;
65
66
        $testComment = new Comment();
67
        $testComment->body = self::TICKET_COMMENT_BODY;
68
        $testComment->updated = self::TICKET_COMMENT_UPDATED;
0 ignored issues
show
Documentation Bug introduced by
It seems like self::TICKET_COMMENT_UPDATED of type string is incompatible with the declared type DateTimeInterface of property $updated.

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...
69
70
        $testComments = new \stdClass();
71
        $testComments->comments = [$testComment];
72
        $testAuthor = new Author();
73
        $testAuthor->emailAddress = self::TESTUSER_EXAMPLE_COM;
74
        $testComment->author = $testAuthor;
0 ignored issues
show
Documentation Bug introduced by
It seems like $testAuthor of type JiraRestApi\Request\Author is incompatible with the declared type JiraRestApi\Issue\Reporter of property $author.

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...
75
        $issueServiceMock
76
            ->method('getComments')
77
            ->willReturn(
78
                $testComments
79
            )
80
        ;
81
82
        $jiraClientFactoryMock = $this->getMockBuilder(JiraClientFactory::class)
83
            ->onlyMethods(['createJiraClient', 'createJiraConfigDto'])
84
            ->getMock()
85
        ;
86
        $jiraClientFactoryMock
87
            ->method('createJiraClient')
88
            ->willReturn($issueServiceMock)
89
        ;
90
91
        $jiraClientFactoryMock->method('createJiraConfigDto')
92
            ->willReturn(new JiraConfigDto('', '', self::TESTUSER_EXAMPLE_COM, 0, ''))
0 ignored issues
show
Bug introduced by
The call to ForecastAutomation\JiraC...onfigDto::__construct() has too few arguments starting with jiraQuery. ( Ignorable by Annotation )

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

92
            ->willReturn(/** @scrutinizer ignore-call */ new JiraConfigDto('', '', self::TESTUSER_EXAMPLE_COM, 0, ''))

This check compares calls to functions or methods with their respective definitions. If the call has less arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress. Please note the @ignore annotation hint above.

Loading history...
Bug introduced by
'' of type string is incompatible with the type integer expected by parameter $jiraMaxResults of ForecastAutomation\JiraC...onfigDto::__construct(). ( Ignorable by Annotation )

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

92
            ->willReturn(new JiraConfigDto('', '', self::TESTUSER_EXAMPLE_COM, 0, /** @scrutinizer ignore-type */ ''))
Loading history...
Bug introduced by
self::TESTUSER_EXAMPLE_COM of type string is incompatible with the type boolean expected by parameter $useTokenBasedAuth of ForecastAutomation\JiraC...onfigDto::__construct(). ( Ignorable by Annotation )

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

92
            ->willReturn(new JiraConfigDto('', '', /** @scrutinizer ignore-type */ self::TESTUSER_EXAMPLE_COM, 0, ''))
Loading history...
93
        ;
94
95
        $jiraClientFacadeMock = $this->getMockBuilder(JiraClientFacade::class)
96
            ->onlyMethods(['getFactory'])
97
            ->getMock()
98
        ;
99
        $jiraClientFacadeMock
100
            ->method('getFactory')
101
            ->willReturn($jiraClientFactoryMock)
102
        ;
103
104
        return $jiraClientFacadeMock;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $jiraClientFacadeMock returns the type PHPUnit\Framework\MockObject\MockObject which is incompatible with the type-hinted return ForecastAutomation\JiraClient\JiraClientFacade.
Loading history...
105
    }
106
}
107