Skip to content

Commit

Permalink
Options should be checked in options method, not in extract (#6)
Browse files Browse the repository at this point in the history
Options should be checked in options method, not in extract
  • Loading branch information
kdebisschop authored Dec 25, 2020
1 parent cf58516 commit 9fee048
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 6 deletions.
11 changes: 10 additions & 1 deletion src/Extractors/GoogleAnalytics.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use Google\Exception as GoogleException;
use Wizaplace\Etl\Extractors\Extractor;
use Wizaplace\Etl\Row;
use Wizaplace\Etl\Step;

/**
* Extract statistics from multiple properties in GoogleAnalytics.
Expand Down Expand Up @@ -176,12 +177,19 @@ public function __construct(string $config = '')
}
}

public function options(array $options): Step
{
parent::options($options);
$this->validate();

return $this;
}

/**
* Extract data from the input.
*/
public function extract(): \Generator
{
$this->validate();
$this->reportRequestSetup($this->dimensions, $this->metrics, $this->startDate, $this->endDate);

foreach ($this->getProfiles() as $propertyName => $profileSummary) {
Expand Down Expand Up @@ -247,6 +255,7 @@ private function delay(): void
$delay = 1 + (mt_rand() / mt_getrandmax() - 0.5);
usleep((int) (1000000 * $delay));
}

$this->clientReqCount++;
}

Expand Down
92 changes: 87 additions & 5 deletions tests/Extractors/GoogleAnalyticsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ class GoogleAnalyticsTest extends TestCase

private array $dimensionHeaders;

private string $profile = 'All Data';

protected function setUp(): void
{
parent::setUp();
Expand All @@ -48,9 +50,9 @@ protected function setUp(): void
public function defaultOptions(): void
{
$expected = [
GoogleAnalyticsTest::oneRow('2020-11-11', 2, 2.2, 2200),
GoogleAnalyticsTest::oneRow('2020-11-12', 3, 3.3, 3300),
GoogleAnalyticsTest::oneRow('2020-11-13', 5, 5.5, 5500),
$this->oneRow('2020-11-11', 2, 2.2, 2200),
$this->oneRow('2020-11-12', 3, 3.3, 3300),
$this->oneRow('2020-11-13', 5, 5.5, 5500),
];
$extractor = new GoogleAnalytics();
$extractor->input($this->input);
Expand All @@ -65,15 +67,95 @@ public function defaultOptions(): void
}
}

private static function oneRow(string $date, int $pages, float $time, int $duration): array
/** @test */
public function skipView(): void
{
$extractor = new GoogleAnalytics();
$extractor->input($this->input);
$this->options['views'] = ['www.example.info'];
$extractor->options($this->options);
$extractor->setAnalyticsSvc($this->mockAnalyticsService())
->setReportingSvc($this->mockReportingService($this->mockReportResponse()));

$i = 0;
while ($extractor->extract()->valid()) {
$extractor->extract()->next();
$i++;
}
static::assertEquals(0, $i);
}

public function skipProfile(): void
{
$extractor = new GoogleAnalytics();
$extractor->input($this->input);
$this->profile = 'Some Data';
$extractor->options($this->options);
$extractor->setAnalyticsSvc($this->mockAnalyticsService())
->setReportingSvc($this->mockReportingService($this->mockReportResponse()));

$i = 0;
while ($extractor->extract()->valid()) {
$extractor->extract()->next();
$i++;
}
static::assertEquals(0, $i);
}

/** @test */
public function noDimension(): void
{
$extractor = new GoogleAnalytics();
unset($this->options['dimensions']);
static::expectException(\InvalidArgumentException::class);
$extractor->options($this->options);
}

/** @test */
public function tooManyDimensions(): void
{
$extractor = new GoogleAnalytics();
$this->options['dimensions'] = ['1', '2', '3', '4', '5', '6', '7', '8'];
static::expectException(\InvalidArgumentException::class);
$extractor->options($this->options);
}

/** @test */
public function noMetrics(): void
{
$extractor = new GoogleAnalytics();
unset($this->options['metrics']);
static::expectException(\InvalidArgumentException::class);
$extractor->options($this->options);
}

/** @test */
public function tooManyMetrics(): void
{
$extractor = new GoogleAnalytics();
$this->options['metrics'] = range(1, 11);
static::expectException(\InvalidArgumentException::class);
$extractor->options($this->options);
}

/** @test */
public function noStartDate(): void
{
$extractor = new GoogleAnalytics();
unset($this->options['startDate']);
static::expectException(\InvalidArgumentException::class);
$extractor->options($this->options);
}

private function oneRow(string $date, int $pages, float $time, int $duration): array
{
return [
self::GA_DATE => $date,
self::GA_PAGE_VIEWS => $pages,
self::GA_AVG_PAGE_LOAD_TIME => $time,
self::GA_AVG_SESSION_DURATION => $duration,
'property' => 'www.example.com',
'summary' => 'All Data',
'summary' => $this->profile,
];
}

Expand Down

0 comments on commit 9fee048

Please sign in to comment.