Skip to content

Commit e2f1ab2

Browse files
committed
Prevent fatal error when extracted columns don't match headers amount
1 parent ab09964 commit e2f1ab2

2 files changed

Lines changed: 57 additions & 33 deletions

File tree

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Flow\ETL\Adapter\GoogleSheet\Tests;
6+
7+
use Google\Client as GoogleClient;
8+
use Google\Service\Sheets;
9+
use GuzzleHttp\{Client as HttpClient, HandlerStack};
10+
use GuzzleHttp\Handler\MockHandler;
11+
use GuzzleHttp\Psr7\Response;
12+
13+
final readonly class GoogleSheetsContext
14+
{
15+
private GoogleClient $client;
16+
17+
public function __construct()
18+
{
19+
$this->client = new GoogleClient();
20+
}
21+
22+
public function sheets(string $fixtureFile) : Sheets
23+
{
24+
$this->client->setHttpClient($this->createHttpClient($fixtureFile));
25+
26+
return new Sheets($this->client);
27+
}
28+
29+
private function createHttpClient(string $fixtureFile) : HttpClient
30+
{
31+
return new HttpClient(
32+
[
33+
'handler' => HandlerStack::create(
34+
new MockHandler(
35+
[
36+
new Response(
37+
200,
38+
['Content-Type' => 'application/json'],
39+
file_get_contents($fixtureFile) ?: throw new \RuntimeException('Failed to read file: ' . $fixtureFile)
40+
),
41+
]
42+
)
43+
),
44+
]
45+
);
46+
}
47+
}

src/adapter/etl-adapter-google-sheet/tests/Flow/ETL/Adapter/GoogleSheet/Tests/Integration/GoogleSheetExtractorTest.php

Lines changed: 10 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -5,24 +5,23 @@
55
namespace Flow\ETL\Adapter\GoogleSheet\Tests\Integration;
66

77
use function Flow\ETL\DSL\{config, flow_context};
8-
use Flow\ETL\Adapter\GoogleSheet\{Columns, GoogleSheetExtractor};
8+
use Flow\ETL\Adapter\GoogleSheet\{Columns, GoogleSheetExtractor, Tests\GoogleSheetsContext};
99
use Flow\ETL\Exception\InvalidArgumentException;
1010
use Flow\ETL\Tests\FlowTestCase;
11-
use Google\Client as GoogleClient;
12-
use Google\Service\Sheets;
13-
use GuzzleHttp\{Client, HandlerStack};
14-
use GuzzleHttp\Handler\MockHandler;
15-
use GuzzleHttp\Psr7\{Response};
1611

1712
final class GoogleSheetExtractorTest extends FlowTestCase
1813
{
19-
public function test_extract_with_cut_extra_columns() : void
14+
private GoogleSheetsContext $context;
15+
16+
protected function setUp() : void
2017
{
21-
$client = new GoogleClient();
22-
$client->setHttpClient($this->createHttpClient());
18+
$this->context = new GoogleSheetsContext();
19+
}
2320

21+
public function test_extract_with_cut_extra_columns() : void
22+
{
2423
$extractor = new GoogleSheetExtractor(
25-
new Sheets($client),
24+
$this->context->sheets(__DIR__ . '/../Fixtures/extra-columns.json'),
2625
'1234567890',
2726
new Columns('Sheet', 'A', 'Z'),
2827
);
@@ -36,11 +35,8 @@ public function test_extract_with_cut_extra_columns() : void
3635

3736
public function test_extract_without_cut_extra_columns() : void
3837
{
39-
$client = new GoogleClient();
40-
$client->setHttpClient($this->createHttpClient());
41-
4238
$extractor = new GoogleSheetExtractor(
43-
new Sheets($client),
39+
$this->context->sheet(__DIR__ . '/../Fixtures/extra-columns.json'),
4440
'1234567890',
4541
new Columns('Sheet', 'A', 'Z'),
4642
);
@@ -55,23 +51,4 @@ public function test_extract_without_cut_extra_columns() : void
5551
self::assertNotNull($row);
5652
}
5753
}
58-
59-
private function createHttpClient() : Client
60-
{
61-
return new Client(
62-
[
63-
'handler' => HandlerStack::create(
64-
new MockHandler(
65-
[
66-
new Response(
67-
200,
68-
['Content-Type' => 'application/json'],
69-
file_get_contents(__DIR__ . '/../Fixtures/extra-columns.json') ?: throw new \RuntimeException('Failed to read file')
70-
),
71-
]
72-
)
73-
),
74-
]
75-
);
76-
}
7754
}

0 commit comments

Comments
 (0)