Skip to content

Commit 11daa41

Browse files
committed
add tests for paginator
1 parent 0493ef8 commit 11daa41

1 file changed

Lines changed: 285 additions & 0 deletions

File tree

Lines changed: 285 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,285 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Keboola\GoogleAnalyticsExtractor\Extractor\Paginator;
6+
7+
use Keboola\Csv\CsvFile;
8+
use Keboola\GoogleAnalyticsExtractor\Extractor\Output;
9+
use Keboola\GoogleAnalyticsExtractor\GoogleAnalytics\Client;
10+
use Keboola\GoogleAnalyticsExtractor\GoogleAnalytics\Result;
11+
use PHPUnit\Framework\MockObject\MockObject;
12+
use PHPUnit\Framework\TestCase;
13+
use Psr\Log\LoggerInterface;
14+
15+
class PropertiesPaginatorTest extends TestCase
16+
{
17+
private PropertiesPaginator $paginator;
18+
private MockObject $output;
19+
private MockObject $client;
20+
private MockObject $logger;
21+
22+
public function setUp(): void
23+
{
24+
$this->output = $this->createMock(Output::class);
25+
$this->client = $this->createMock(Client::class);
26+
$this->logger = $this->createMock(LoggerInterface::class);
27+
28+
$this->paginator = new PropertiesPaginator(
29+
$this->output,
30+
$this->client,
31+
$this->logger,
32+
);
33+
}
34+
35+
public function testPaginateSinglePage(): void
36+
{
37+
$property = [
38+
'propertyKey' => 'properties/123456789',
39+
'propertyName' => 'Test Property',
40+
];
41+
42+
$query = [
43+
'query' => [
44+
'dimensions' => [['name' => 'ga:date']],
45+
'metrics' => [['name' => 'ga:sessions']],
46+
'dateRanges' => [
47+
['startDate' => '2023-01-01', 'endDate' => '2023-01-31'],
48+
],
49+
],
50+
];
51+
52+
$report = [
53+
'data' => [
54+
new Result(['ga:sessions' => '100'], ['ga:date' => '2023-01-01']),
55+
new Result(['ga:sessions' => '150'], ['ga:date' => '2023-01-02']),
56+
],
57+
'totals' => 2,
58+
'rowCount' => 2,
59+
];
60+
61+
/** @var CsvFile $csvFile */
62+
$csvFile = $this->createMock(CsvFile::class);
63+
64+
$this->paginator->setProperty($property);
65+
66+
// Expect writeReport to be called once with the correct property ID
67+
$this->output->expects($this->once())
68+
->method('writeReport')
69+
->with($csvFile, $report, '123456789');
70+
71+
// Expect logger to be called with progress info
72+
$this->logger->expects($this->once())
73+
->method('info')
74+
->with('Downloaded 2/2 records.');
75+
76+
$result = $this->paginator->paginate($query, $report, $csvFile);
77+
78+
$this->assertEquals(2, $result);
79+
}
80+
81+
public function testPaginateMultiplePages(): void
82+
{
83+
$property = [
84+
'propertyKey' => 'properties/123456789',
85+
'propertyName' => 'Test Property',
86+
];
87+
88+
$query = [
89+
'query' => [
90+
'dimensions' => [['name' => 'ga:date']],
91+
'metrics' => [['name' => 'ga:sessions']],
92+
'dateRanges' => [
93+
['startDate' => '2023-01-01', 'endDate' => '2023-01-31'],
94+
],
95+
],
96+
];
97+
98+
$firstReport = [
99+
'data' => [
100+
new Result(['ga:sessions' => '100'], ['ga:date' => '2023-01-01']),
101+
new Result(['ga:sessions' => '150'], ['ga:date' => '2023-01-02']),
102+
],
103+
'totals' => 4,
104+
'rowCount' => 2,
105+
];
106+
107+
$secondReport = [
108+
'data' => [
109+
new Result(['ga:sessions' => '200'], ['ga:date' => '2023-01-03']),
110+
new Result(['ga:sessions' => '250'], ['ga:date' => '2023-01-04']),
111+
],
112+
'totals' => 4,
113+
'rowCount' => 2,
114+
];
115+
116+
/** @var CsvFile $csvFile */
117+
$csvFile = $this->createMock(CsvFile::class);
118+
119+
$this->paginator->setProperty($property);
120+
121+
// Expect writeReport to be called twice
122+
$this->output->expects($this->exactly(2))
123+
->method('writeReport')
124+
->with($csvFile, $this->isType('array'), '123456789');
125+
126+
// Expect logger to be called twice with progress info
127+
$this->logger->expects($this->exactly(2))
128+
->method('info')
129+
->willReturnOnConsecutiveCalls(
130+
$this->returnValue(null),
131+
$this->returnValue(null),
132+
);
133+
134+
// Expect client to be called once for the second page
135+
$this->client->expects($this->once())
136+
->method('getPropertyReport')
137+
->with(
138+
[
139+
'query' => [
140+
'dimensions' => [['name' => 'ga:date']],
141+
'metrics' => [['name' => 'ga:sessions']],
142+
'dateRanges' => [
143+
['startDate' => '2023-01-01', 'endDate' => '2023-01-31'],
144+
],
145+
'offset' => 2,
146+
],
147+
],
148+
$property,
149+
)
150+
->willReturn($secondReport);
151+
152+
$result = $this->paginator->paginate($query, $firstReport, $csvFile);
153+
154+
$this->assertEquals(4, $result);
155+
}
156+
157+
public function testPaginateEmptyData(): void
158+
{
159+
$property = [
160+
'propertyKey' => 'properties/123456789',
161+
'propertyName' => 'Test Property',
162+
];
163+
164+
$query = [
165+
'query' => [
166+
'dimensions' => [['name' => 'ga:date']],
167+
'metrics' => [['name' => 'ga:sessions']],
168+
'dateRanges' => [
169+
['startDate' => '2023-01-01', 'endDate' => '2023-01-31'],
170+
],
171+
],
172+
];
173+
174+
$report = [
175+
'data' => [],
176+
'totals' => 0,
177+
'rowCount' => 0,
178+
];
179+
180+
/** @var CsvFile $csvFile */
181+
$csvFile = $this->createMock(CsvFile::class);
182+
183+
$this->paginator->setProperty($property);
184+
185+
// Expect writeReport to be called once even with empty data
186+
$this->output->expects($this->once())
187+
->method('writeReport')
188+
->with($csvFile, $report, '123456789');
189+
190+
// Expect logger to be called with progress info
191+
$this->logger->expects($this->once())
192+
->method('info')
193+
->with('Downloaded 0/0 records.');
194+
195+
$result = $this->paginator->paginate($query, $report, $csvFile);
196+
197+
$this->assertEquals(0, $result);
198+
}
199+
200+
public function testPaginateWithOffsetInQuery(): void
201+
{
202+
$property = [
203+
'propertyKey' => 'properties/987654321',
204+
'propertyName' => 'Test Property 2',
205+
];
206+
207+
$query = [
208+
'query' => [
209+
'dimensions' => [['name' => 'ga:date']],
210+
'metrics' => [['name' => 'ga:sessions']],
211+
'dateRanges' => [
212+
['startDate' => '2023-01-01', 'endDate' => '2023-01-31'],
213+
],
214+
'offset' => 10, // Existing offset should be preserved
215+
],
216+
];
217+
218+
$firstReport = [
219+
'data' => [
220+
new Result(['ga:sessions' => '100'], ['ga:date' => '2023-01-01']),
221+
],
222+
'totals' => 3,
223+
'rowCount' => 1,
224+
];
225+
226+
$secondReport = [
227+
'data' => [
228+
new Result(['ga:sessions' => '200'], ['ga:date' => '2023-01-02']),
229+
],
230+
'totals' => 3,
231+
'rowCount' => 1,
232+
];
233+
234+
$thirdReport = [
235+
'data' => [
236+
new Result(['ga:sessions' => '300'], ['ga:date' => '2023-01-03']),
237+
],
238+
'totals' => 3,
239+
'rowCount' => 1,
240+
];
241+
242+
/** @var CsvFile $csvFile */
243+
$csvFile = $this->createMock(CsvFile::class);
244+
245+
$this->paginator->setProperty($property);
246+
247+
// Expect writeReport to be called three times
248+
$this->output->expects($this->exactly(3))
249+
->method('writeReport')
250+
->with($csvFile, $this->isType('array'), '987654321');
251+
252+
// Expect logger to be called three times
253+
$this->logger->expects($this->exactly(3))
254+
->method('info')
255+
->willReturnOnConsecutiveCalls(
256+
$this->returnValue(null),
257+
$this->returnValue(null),
258+
$this->returnValue(null),
259+
);
260+
261+
// Expect client to be called twice for subsequent pages
262+
$callCount = 0;
263+
$this->client->expects($this->exactly(2))
264+
->method('getPropertyReport')
265+
->willReturnCallback(function ($query, $property) use (&$callCount, $secondReport, $thirdReport) {
266+
$callCount++;
267+
if ($callCount === 1) {
268+
$this->assertEquals(1, $query['query']['offset']);
269+
return $secondReport;
270+
} else {
271+
$this->assertEquals(2, $query['query']['offset']);
272+
return $thirdReport;
273+
}
274+
});
275+
276+
$result = $this->paginator->paginate($query, $firstReport, $csvFile);
277+
278+
$this->assertEquals(3, $result);
279+
}
280+
281+
public function testGetOutput(): void
282+
{
283+
$this->assertSame($this->output, $this->paginator->getOutput());
284+
}
285+
}

0 commit comments

Comments
 (0)