Skip to content

Commit d1cf584

Browse files
committed
Slightly different interface, made checks static
1 parent 3230577 commit d1cf584

3 files changed

Lines changed: 95 additions & 57 deletions

File tree

Atomx/ReportStreamer.php

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,10 @@
66

77
class ReportStreamer {
88
private $stream;
9-
109
private $overflow = '';
11-
private $columns = [];
12-
private $count = 0;
1310

1411
public function __construct(Stream $stream, $report = null)
1512
{
16-
if (!is_null($report)) {
17-
$sumsOrMetrics = (isset($report['report']['sums']) ? $report['report']['sums'] : $report['report']['metrics']);
18-
$this->columns = array_merge($report['report']['groups'], $sumsOrMetrics);
19-
$this->count = $report['report']['count'];
20-
}
21-
2213
$this->stream = $stream;
2314
}
2415

Atomx/Resources/Report.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,16 @@ public function status($reportId)
2020
return $this->getUrl('report/' . $reportId, ['status' => true]);
2121
}
2222

23-
public function isReady($report)
23+
public static function getReportId($report)
24+
{
25+
if (isset($report['report']['id'])) {
26+
return $report['report']['id'];
27+
}
28+
29+
return false;
30+
}
31+
32+
public static function isReady($report)
2433
{
2534
if (isset($report['report']['is_ready'])) {
2635
return $report['report']['is_ready'];
@@ -29,6 +38,25 @@ public function isReady($report)
2938
return false;
3039
}
3140

41+
public static function numberOfRows($report)
42+
{
43+
if (isset($report['report']['lines']))
44+
return $report['report']['lines'];
45+
46+
return false;
47+
}
48+
49+
public static function getColumns($report)
50+
{
51+
if (!is_null($report) && isset($report['query'])) {
52+
$sumsOrMetrics = (isset($report['query']['sums']) ? $report['query']['sums'] : $report['query']['metrics']);
53+
54+
return array_merge($report['query']['groups'], $sumsOrMetrics);
55+
}
56+
57+
return false;
58+
}
59+
3260
public function download($report)
3361
{
3462
$reportId = $report['report']['id'];

tests/ReportTest.php

Lines changed: 66 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,7 @@
11
<?php namespace tests;
22

3-
4-
use Atomx\AccountStore;
53
use Atomx\Resources\Report;
6-
7-
class AtomxAccountStore implements AccountStore {
8-
private $token = null;
9-
10-
public function getToken()
11-
{
12-
return $this->token;
13-
}
14-
15-
public function storeToken($token)
16-
{
17-
$this->token = $token;
18-
}
19-
20-
public function getUsername()
21-
{
22-
return '';
23-
}
24-
25-
public function getPassword()
26-
{
27-
return '';
28-
}
29-
30-
public function getApiBase()
31-
{
32-
return '';
33-
}
34-
}
4+
require_once('AtomxAccountStore.php');
355

366
class ReportTest extends \PHPUnit_Framework_TestCase {
377
public function testCreateAndDownloadReport()
@@ -44,23 +14,25 @@ public function testCreateAndDownloadReport()
4414
'scope' => 'advertiser',
4515
'groups' => ['campaign_id', 'domain_id', 'day_timestamp'],
4616
'sums' => ['impressions', 'clicks', 'conversions', 'campaign_cost'],
47-
'where' => [['network_id', '==', '1']],
17+
'where' => [['advertiser_network_id', '==', '1']],
4818
'from' => $from,
4919
'to' => $to,
5020
'timezone' => 'UTC'
5121
);
5222

53-
// $report = $report->run($options);
23+
//$report = $report->run($options);
5424

55-
//var_dump($report);
25+
//var_export($report);
5626
}
5727

5828
public function testReportIsDone()
5929
{
6030
$report = new Report(new AtomxAccountStore());
61-
$rData = $this->getReportData();
31+
$rData = $report->status(Report::getReportId($this->getReportData()));
6232

63-
$this->assertEquals(true, $report->isReady($rData));
33+
var_dump($rData);
34+
35+
$this->assertEquals(true, Report::isReady($rData));
6436
}
6537

6638
public function testDownloadReport()
@@ -69,7 +41,21 @@ public function testDownloadReport()
6941
$rData = $this->getReportData();
7042

7143
$streamer = $report->download($rData);
44+
45+
var_dump(Report::numberOfRows($rData));
46+
var_dump(Report::getColumns($rData));
47+
7248
var_dump($streamer->readLine());
49+
50+
$imps = 0;
51+
$revenue = 0;
52+
53+
while (($row = $streamer->readLine()) !== false) {
54+
$imps += $row[3];
55+
$revenue += $row[6];
56+
}
57+
58+
var_dump($imps, $revenue);
7359
}
7460

7561
/**
@@ -79,17 +65,50 @@ private function getReportData()
7965
{
8066
// TODO: Mock the response
8167
$rData = [
82-
'success' => true,
83-
'timestamp' => "2015-08-18T09:13:15.330011",
84-
'report' => [
85-
'error' => NULL,
86-
'is_ready' => true,
87-
'link' => "/v1/report/7aae3048e3f54c327163c5ef25826abc",
88-
'lines' => 30003,
89-
'id' => "7aae3048e3f54c327163c5ef25826abc",
90-
'started' => 1439889079
91-
]
92-
];
68+
'success' => true,
69+
'timestamp' => '2015-09-23T15:30:38.563796',
70+
'report' =>
71+
[
72+
'link' => '/v1/report/89130c7cd76c7637195224529bbae9c7',
73+
'duration' => NULL,
74+
'started' => 1443022238,
75+
'lines' => 0,
76+
'error' => NULL,
77+
'finished' => NULL,
78+
'fast' => true,
79+
'is_ready' => true,
80+
'id' => '89130c7cd76c7637195224529bbae9c7',
81+
],
82+
'query' =>
83+
[
84+
'from' => '2015-09-22 00:00:00',
85+
'timezone' => 'UTC',
86+
'sums' =>
87+
array (
88+
0 => 'impressions',
89+
1 => 'clicks',
90+
2 => 'conversions',
91+
3 => 'campaign_cost',
92+
),
93+
'groups' =>
94+
array (
95+
0 => 'campaign_id',
96+
1 => 'domain_id',
97+
2 => 'day_timestamp',
98+
),
99+
'to' => '2015-09-23 00:00:00',
100+
'where' =>
101+
array (
102+
0 =>
103+
array (
104+
0 => 'advertiser_network_id',
105+
1 => '==',
106+
2 => '1',
107+
),
108+
),
109+
'scope' => 'advertiser'
110+
]
111+
];
93112
return $rData;
94113
}
95114
}

0 commit comments

Comments
 (0)