ApiUpdaterTest   A
last analyzed

Complexity

Total Complexity 8

Size/Duplication

Total Lines 155
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 4

Importance

Changes 7
Bugs 4 Features 0
Metric Value
wmc 8
c 7
b 4
f 0
lcom 1
cbo 4
dl 0
loc 155
rs 10

8 Methods

Rating   Name   Duplication   Size   Complexity  
A testUpdateNoLongerValid() 0 9 1
A testUpdateSuccess() 0 17 1
A testUpdateFailed() 0 16 1
A testUpdateFailedMaxReached() 0 17 1
A mockApiCallLoad() 0 8 1
A mockPrepareApiCall() 0 15 1
A mockFinishApiCall() 0 9 1
A setUp() 0 17 1
1
<?php
2
namespace Tarioch\EveapiFetcherBundle\Tests\Unit\Component\Worker;
3
4
use Mockery as m;
5
use Doctrine\ORM\EntityManager;
6
use Psr\Log\LoggerInterface;
7
use Tarioch\EveapiFetcherBundle\Component\Worker\ApiUpdater;
8
use Tarioch\EveapiFetcherBundle\Entity\Api;
9
use Pheal\Exceptions\APIException;
10
11
class ApiUpdaterTest extends \PHPUnit_Framework_TestCase
12
{
13
    const API_CALL_ID = 'API_CALL_ID';
14
    const EARLIEST_NEXT_CALL = 'EARLIEST_NEXT_CALL';
15
16
    /**
17
     * @var EntityManager
18
     */
19
    private $entityManager;
20
    /**
21
     * @var LoggerInterface
22
     */
23
    private $logger;
24
    /**
25
     * @var ApiTimeCalculator
26
     */
27
    private $apiTimeCalculator;
28
    /**
29
     * @var SectionApiFactory
30
     */
31
    private $sectionApiFactory;
32
    /**
33
     * @var ApiCall
34
     */
35
    private $apiCall;
36
    /**
37
     * @var Api
38
     */
39
    private $api;
40
    /**
41
     * @var SectionApi
42
     */
43
    private $sectionApi;
44
45
    /**
46
     * @var ApiUpdater
47
     */
48
    private $apiUpdater;
49
50
    public function testUpdateNoLongerValid()
51
    {
52
        $this->mockApiCallLoad();
53
        $this->apiTimeCalculator->shouldReceive('isCallStillValid')
54
            ->with($this->apiCall)
55
            ->andReturn(false);
56
57
        $this->apiUpdater->update(self::API_CALL_ID);
58
    }
59
60
    public function testUpdateSuccess()
61
    {
62
        $cachedUntil = '@1234';
63
64
        $this->mockPrepareApiCall();
65
66
        $this->sectionApi->shouldReceive('update')
67
            ->with($this->apiCall)
68
            ->andReturn($cachedUntil);
69
70
        $this->apiCall->shouldReceive('clearErrorCount');
71
        $this->apiCall->shouldReceive('setCachedUntil');
72
73
        $this->mockFinishApiCall();
74
75
        $this->apiUpdater->update(self::API_CALL_ID);
76
    }
77
78
    public function testUpdateFailed()
79
    {
80
        $this->mockPrepareApiCall();
81
82
        $this->sectionApi->shouldReceive('update')
83
            ->with($this->apiCall)
84
            ->andThrow(new APIException(111, '', new \SimpleXMLElement('<foo></foo>')));
85
86
        $this->logger->shouldReceive('error');
87
        $this->apiCall->shouldReceive('increaseErrorCount');
88
        $this->apiCall->shouldReceive('getErrorCount')->andReturn(5);
89
90
        $this->mockFinishApiCall();
91
92
        $this->apiUpdater->update(self::API_CALL_ID);
93
    }
94
95
    public function testUpdateFailedMaxReached()
96
    {
97
        $this->mockPrepareApiCall();
98
99
        $this->sectionApi->shouldReceive('update')
100
            ->with($this->apiCall)
101
            ->andThrow(new APIException(111, '', new \SimpleXMLElement('<foo></foo>')));
102
103
        $this->logger->shouldReceive('error');
104
        $this->apiCall->shouldReceive('increaseErrorCount');
105
        $this->apiCall->shouldReceive('getErrorCount')->andReturn(21);
106
        $this->apiCall->shouldReceive('setActive')->with(false)->once();
107
108
        $this->mockFinishApiCall();
109
110
        $this->apiUpdater->update(self::API_CALL_ID);
111
    }
112
113
    private function mockApiCallLoad()
114
    {
115
        $this->entityManager->shouldReceive('find')
116
            ->with('TariochEveapiFetcherBundle:ApiCall', self::API_CALL_ID)
117
            ->andReturn($this->apiCall);
118
        $this->apiCall->shouldReceive('getOwner')
119
            ->andReturn(null);
120
    }
121
122
    private function mockPrepareApiCall()
123
    {
124
        $this->mockApiCallLoad();
125
        $this->apiTimeCalculator->shouldReceive('isCallStillValid')
126
            ->with($this->apiCall)
127
            ->andReturn(true);
128
129
        $this->apiCall->shouldReceive('getApi')->andReturn($this->api);
130
        $this->api->shouldReceive('getSection')->andReturn('section');
131
        $this->api->shouldReceive('getName')->andReturn('name');
132
133
        $this->sectionApiFactory->shouldReceive('create')
134
            ->with($this->apiCall)
135
            ->andReturn($this->sectionApi);
136
    }
137
138
    private function mockFinishApiCall()
139
    {
140
        $this->apiTimeCalculator->shouldReceive('calculateEarliestNextCall')
141
            ->with($this->apiCall)
142
            ->andReturn(self::EARLIEST_NEXT_CALL);
143
        $this->apiCall->shouldReceive('setEarliestNextCall')->with(self::EARLIEST_NEXT_CALL);
144
145
        $this->logger->shouldReceive('info');
146
    }
147
148
    public function setUp()
149
    {
150
        $this->entityManager = m::mock('Doctrine\ORM\EntityManager');
151
        $this->logger = m::mock('Psr\Log\LoggerInterface');
152
        $this->apiTimeCalculator = m::mock('Tarioch\EveapiFetcherBundle\Component\Worker\ApiTimeCalculator');
153
        $this->sectionApiFactory = m::mock('Tarioch\EveapiFetcherBundle\Component\Section\SectionApiFactory');
154
        $this->apiCall = m::mock('Tarioch\EveapiFetcherBundle\Entity\ApiCall');
155
        $this->api = m::mock('Tarioch\EveapiFetcherBundle\Entity\Api');
156
        $this->sectionApi = m::mock('Tarioch\EveapiFetcherBundle\Component\Section\SectionApi');
157
158
        $this->apiUpdater = new ApiUpdater(
159
            $this->entityManager,
160
            $this->logger,
161
            $this->apiTimeCalculator,
162
            $this->sectionApiFactory
163
        );
164
    }
165
}
166