Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,8 @@
"tools/phpunit/vendor/bin/phpunit --testsuite=adapter-elasticsearch-integration --log-junit ./var/phpunit/logs/adapter-elasticsearch-integration.junit.xml --coverage-clover=./var/phpunit/coverage/clover/adapter-elasticsearch-integration.coverage.xml --coverage-html=./var/phpunit/coverage/html/adapter-elasticsearch-integration"
],
"test:adapter:google-sheet": [
"tools/phpunit/vendor/bin/phpunit --testsuite=adapter-google-sheet-unit --log-junit ./var/phpunit/logs/adapter-google-sheet-unit.junit.xml --coverage-clover=./var/phpunit/coverage/clover/adapter-google-sheet-unit.coverage.xml --coverage-html=./var/phpunit/coverage/html/adapter-google-sheet-unit"
"tools/phpunit/vendor/bin/phpunit --testsuite=adapter-google-sheet-unit --log-junit ./var/phpunit/logs/adapter-google-sheet-unit.junit.xml --coverage-clover=./var/phpunit/coverage/clover/adapter-google-sheet-unit.coverage.xml --coverage-html=./var/phpunit/coverage/html/adapter-google-sheet-unit",
"tools/phpunit/vendor/bin/phpunit --testsuite=adapter-google-sheet-integration --log-junit ./var/phpunit/coverage/clover/adapter-google-sheet-integration.junit.xml --coverage-clover=./var/phpunit/coverage/clover/adapter-google-sheet-integration.coverage.xml --coverage-html=./var/phpunit/coverage/html/adapter-google-sheet-integration"
],
"test:adapter:http": [
"tools/phpunit/vendor/bin/phpunit --testsuite=adapter-http-unit --log-junit ./var/phpunit/logs/adapter-http-unit.junit.xml --coverage-clover=./var/phpunit/coverage/clover/adapter-http-unit.coverage.xml --coverage-html=./var/phpunit/coverage/html/adapter-http-unit",
Expand Down
3 changes: 3 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@
<testsuite name="adapter-google-sheet-unit">
<directory>src/adapter/etl-adapter-google-sheet/tests/Flow/ETL/Adapter/GoogleSheet/Tests/Unit</directory>
</testsuite>
<testsuite name="adapter-google-sheet-integration">
<directory>src/adapter/etl-adapter-google-sheet/tests/Flow/ETL/Adapter/GoogleSheet/Tests/Integration</directory>
</testsuite>
<testsuite name="adapter-http-unit">
<directory>src/adapter/etl-adapter-http/tests/Flow/ETL/Adapter/HTTP/Tests/Unit</directory>
</testsuite>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
{
use Limitable;

private bool $dropExtraColumns = true;

/**
* @param array{dateTimeRenderOption?: string, majorDimension?: string, valueRenderOption?: string} $options
*/
Expand Down Expand Up @@ -57,24 +59,31 @@
$totalRows = 0;
}

$headersCount = \count($headers);
Comment thread
stloyd marked this conversation as resolved.

$shouldPutInputIntoRows = $context->config->shouldPutInputIntoRows();

while (\count($values)) {
$rows = \array_map(
function (array $rowData) use ($headers, $shouldPutInputIntoRows) {
if (\count($headers) > \count($rowData)) {
function (array $rowData) use ($headers, $headersCount, $shouldPutInputIntoRows) {
$rowDataCount = \count($rowData);

if ($headersCount > $rowDataCount) {
\array_push(
$rowData,
...\array_map(
static fn (int $i) => null,
\range(1, \count($headers) - \count($rowData))
\range(1, $headersCount - $rowDataCount)

Check warning on line 76 in src/adapter/etl-adapter-google-sheet/src/Flow/ETL/Adapter/GoogleSheet/GoogleSheetExtractor.php

View check run for this annotation

Codecov / codecov/patch

src/adapter/etl-adapter-google-sheet/src/Flow/ETL/Adapter/GoogleSheet/GoogleSheetExtractor.php#L76

Added line #L76 was not covered by tests
)
);
}

if (\count($rowData) > \count($headers)) {
/** @phpstan-ignore-next-line */
$rowData = \array_chunk($rowData, \count($headers));
if ($rowDataCount > $headersCount) {
if (!$this->dropExtraColumns) {
throw InvalidArgumentException::because('Row has more columns (%d) than headers (%d)', $rowDataCount, $headersCount);
}

$rowData = \array_slice($rowData, 0, $headersCount);
}

$row = \array_combine($headers, $rowData);
Expand Down Expand Up @@ -105,7 +114,7 @@
}

$cellsRange = $cellsRange->nextRows($this->rowsPerPage);
/** @var Sheets\ValueRange $response */

$response = $this->service->spreadsheets_values->get($this->spreadsheetId, $cellsRange->toString(), $this->options);
/**
* @var array<array> $values
Expand All @@ -114,6 +123,13 @@
}
}

public function withDropExtraColumns(bool $dropExtraColumns) : self
{
$this->dropExtraColumns = $dropExtraColumns;

return $this;
}

public function withHeader(bool $withHeader) : self
{
$this->withHeader = $withHeader;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Flow\ETL\Adapter\GoogleSheet;

use Flow\ETL\Attribute\{DocumentationDSL, Module, Type};
use Flow\ETL\Extractor;
use Google\Client;
use Google\Service\Sheets;

Expand All @@ -25,7 +24,7 @@ function from_google_sheet(
bool $with_header = true,
int $rows_per_page = 1000,
array $options = [],
) : Extractor {
) : GoogleSheetExtractor {
Comment thread
stloyd marked this conversation as resolved.
if ($auth_config instanceof Sheets) {
$sheets = $auth_config;
} else {
Expand Down Expand Up @@ -64,7 +63,7 @@ function from_google_sheet_columns(
bool $with_header = true,
int $rows_per_page = 1000,
array $options = [],
) : Extractor {
) : GoogleSheetExtractor {
if ($auth_config instanceof Sheets) {
$sheets = $auth_config;
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"range": "Sheet!A1:D11",
"majorDimension": "ROWS",
"values": [
["Header 1", "Header 2", "Header 3"],
["A2", "B2", "C2"],
["A3", "B3", "C3"],
["A4", "B4", "C4"],
["A5", "B5", "C5"],
["A6", "B6", "C6"],
["A7", "B7", "C7"],
["A8", "B8", "C8"],
["A9", "B9", "C9"],
["A10", "B10", "C10"],
["A11", "B11", "C11", "D11"]
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

declare(strict_types=1);

namespace Flow\ETL\Adapter\GoogleSheet\Tests;

use Google\Client as GoogleClient;
use Google\Service\Sheets;
use GuzzleHttp\{Client as HttpClient, HandlerStack};
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\Psr7\Response;

final readonly class GoogleSheetsContext
{
private GoogleClient $client;

public function __construct()
{
$this->client = new GoogleClient();
}

public function sheets(string $fixtureFile) : Sheets
{
$this->client->setHttpClient($this->createHttpClient($fixtureFile));

return new Sheets($this->client);
}

private function createHttpClient(string $fixtureFile) : HttpClient
{
return new HttpClient(
[
'handler' => HandlerStack::create(
new MockHandler(
[
new Response(
200,
['Content-Type' => 'application/json'],
file_get_contents($fixtureFile) ?: throw new \RuntimeException('Failed to read file: ' . $fixtureFile)
),
]
)
),
]
);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

declare(strict_types=1);

namespace Flow\ETL\Adapter\GoogleSheet\Tests\Integration;

use function Flow\ETL\DSL\{config, flow_context};
use Flow\ETL\Adapter\GoogleSheet\{Columns, GoogleSheetExtractor, Tests\GoogleSheetsContext};
use Flow\ETL\Exception\InvalidArgumentException;
use Flow\ETL\Tests\FlowTestCase;

final class GoogleSheetExtractorTest extends FlowTestCase
{
private GoogleSheetsContext $context;

protected function setUp() : void
{
$this->context = new GoogleSheetsContext();
}

public function test_extract_with_cut_extra_columns() : void
{
$extractor = new GoogleSheetExtractor(
$this->context->sheets(__DIR__ . '/../Fixtures/extra-columns.json'),
'1234567890',
new Columns('Sheet', 'A', 'Z'),
);

$rows = $extractor->extract(flow_context(config()));

foreach ($rows as $row) {
self::assertNotNull($row);
}
}

public function test_extract_without_cut_extra_columns() : void
{
$extractor = new GoogleSheetExtractor(
$this->context->sheets(__DIR__ . '/../Fixtures/extra-columns.json'),
'1234567890',
new Columns('Sheet', 'A', 'Z'),
);
$extractor->withDropExtraColumns(false);

$rows = $extractor->extract(flow_context(config()));

$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('Row has more columns (4) than headers (3)');

foreach ($rows as $row) {
self::assertNotNull($row);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,8 @@ public function test_its_stop_fetching_data_if_processed_row_count_is_less_then_
$sheetName = 'sheet',
'A',
'B',
true,
2,
);
)->withHeader(true)
->withRowsPerPage(2);
$spreadSheetIdEntry = string_entry('_spread_sheet_id', $spreadSheetId);
$sheetNameEntry = string_entry('_sheet_name', $sheetName);
$firstValueRangeMock = $this->createMock(ValueRange::class);
Expand Down Expand Up @@ -63,9 +62,7 @@ public function test_rows_in_batch_must_be_positive_integer() : void
'sheet',
'A',
'B',
true,
0
);
)->withRowsPerPage(0);
}

public function test_works_for_no_data() : void
Expand All @@ -76,9 +73,8 @@ public function test_works_for_no_data() : void
'sheet',
'A',
'B',
true,
20
);
)->withHeader(true)
->withRowsPerPage(20);
$ValueRangeMock = $this->createMock(ValueRange::class);
$ValueRangeMock->method('getValues')->willReturn(null);

Expand Down
2 changes: 1 addition & 1 deletion web/landing/resources/dsl.json

Large diffs are not rendered by default.