testCanWriteActivityCollection()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 3
b 0
f 0
nc 1
nop 0
dl 0
loc 6
rs 10
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\ForecastClient;
13
14
use ForecastAutomation\Activity\Shared\Dto\ActivityDto;
15
use ForecastAutomation\Activity\Shared\Dto\ActivityDtoCollection;
16
use ForecastAutomation\Cache\CacheFacade;
17
use ForecastAutomation\ForecastClient\ForecastClientFacade;
18
use ForecastAutomation\ForecastClient\ForecastClientFactory;
19
use ForecastAutomation\ForecastClient\Shared\Dto\ForecastConfigDto;
20
use ForecastAutomationTests\Log\Shared\LogFactoryHelper;
21
use GuzzleHttp\Client;
22
use GuzzleHttp\Psr7\Response;
23
use PHPUnit\Framework\TestCase;
24
25
/**
26
 * @internal
27
 * @covers
28
 */
29
final class ForecastClientFacadeTest extends TestCase
30
{
31
    private LogFactoryHelper $logFactoryHelper;
32
33
    protected function setUp(): void
34
    {
35
        $this->logFactoryHelper = new LogFactoryHelper();
36
    }
37
38
    public function testCanWriteActivityCollection(): void
39
    {
40
        $writtenActivities = $this->createForecastClientFacade()->writeActivities(
41
            new ActivityDtoCollection(new ActivityDto('test-ticket-number', 'test', new \DateTime(), 1))
42
        );
43
        static::assertSame(1, $writtenActivities);
44
    }
45
46
    private function createCacheFacadeMock(): CacheFacade
47
    {
48
        $cacheFacadeMock = $this->getMockBuilder(CacheFacade::class)
49
            ->onlyMethods(['set', 'has'])
50
            ->getMock()
51
        ;
52
        $cacheFacadeMock->method('has')
53
            ->willReturn(
54
                false
55
            )
56
        ;
57
58
        $cacheFacadeMock->method('set')
59
            ->willReturn(
60
                true
61
            )
62
        ;
63
64
        return $cacheFacadeMock;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $cacheFacadeMock returns the type PHPUnit\Framework\MockObject\MockObject which is incompatible with the type-hinted return ForecastAutomation\Cache\CacheFacade.
Loading history...
65
    }
66
67
    private function createForecastClientFacade(): ForecastClientFacade
68
    {
69
        $clientMock = $this->getMockBuilder(Client::class)
70
            ->onlyMethods(['request'])
71
            ->getMock()
72
        ;
73
        $clientMock
74
            ->method('request')
75
            ->willReturn(
76
                new Response(200, ['X-Foo' => 'Bar'], '[{"title":"test-ticket-number","id":1}]')
77
            )
78
        ;
79
80
        $forecastClientFactoryMock = $this->getMockBuilder(ForecastClientFactory::class)
81
            ->onlyMethods(['createClient', 'createForecastConfigDto', 'getLogFacade', 'getCacheFacade'])
82
            ->getMock()
83
        ;
84
        $forecastClientFactoryMock
85
            ->method('createClient')
86
            ->willReturn($clientMock)
87
        ;
88
        $forecastClientFactoryMock
89
            ->method('getLogFacade')
90
            ->willReturn($this->logFactoryHelper->createLogFacadeMock())
91
        ;
92
        $forecastClientFactoryMock
93
            ->method('getCacheFacade')
94
            ->willReturn($this->createCacheFacadeMock())
95
        ;
96
97
        $forecastClientFactoryMock
98
            ->method('createForecastConfigDto')
99
            ->willReturn(new ForecastConfigDto('', '', '', '', ''))
100
        ;
101
102
        $forecastClientFacadeMock = $this->getMockBuilder(ForecastClientFacade::class)
103
            ->onlyMethods(['getFactory'])
104
            ->getMock()
105
        ;
106
        $forecastClientFacadeMock
107
            ->method('getFactory')
108
            ->willReturn($forecastClientFactoryMock)
109
        ;
110
111
        return $forecastClientFacadeMock;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $forecastClientFacadeMock returns the type PHPUnit\Framework\MockObject\MockObject which is incompatible with the type-hinted return ForecastAutomation\Forec...nt\ForecastClientFacade.
Loading history...
112
    }
113
}
114