Skip to content

Commit a38cc2b

Browse files
committed
Added AsyncDataSource and specified it as the parameter for AsyncConnector::fetch().
1 parent 37b143d commit a38cc2b

File tree

6 files changed

+36
-11
lines changed

6 files changed

+36
-11
lines changed

src/Connector/AsyncConnector.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@ interface AsyncConnector
1313
/**
1414
* Fetches data asynchronously from the specified data source.
1515
*
16-
* @param DataSource $source Data source.
16+
* @param AsyncDataSource $source Data source.
1717
*
1818
* @return Promise<mixed> Data.
1919
*/
20-
public function fetchAsync(DataSource $source): Promise;
20+
public function fetchAsync(AsyncDataSource $source): Promise;
2121
}

src/Connector/AsyncDataSource.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
declare(strict_types=1);
3+
4+
namespace ScriptFUSION\Porter\Connector;
5+
6+
use Amp\Promise;
7+
8+
/**
9+
* Specifies a data source and the necessary parameters required to fetch it asynchronously.
10+
*/
11+
interface AsyncDataSource
12+
{
13+
/**
14+
* Computes the hash code for this object's state. The computed hash must be the same when the object state
15+
* is unchanged or is still considered equivalent for cache key purposes, otherwise the hash code must always
16+
* be different for different states.
17+
*
18+
* @return Promise<string> Hash code.
19+
*/
20+
public function computeHash(): Promise;
21+
}

src/Connector/ImportConnector.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,11 +87,11 @@ function () use ($source) {
8787
/**
8888
* Fetches data asynchronously from the specified data source.
8989
*
90-
* @param DataSource $source Data source.
90+
* @param AsyncDataSource $source Data source.
9191
*
9292
* @return Promise<mixed> Data.
9393
*/
94-
public function fetchAsync(DataSource $source): Promise
94+
public function fetchAsync(AsyncDataSource $source): Promise
9595
{
9696
return retryAsync(
9797
$this->maxFetchAttempts,

src/Specification/Specification.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,7 @@ abstract class Specification
2323

2424
private $maxFetchAttempts = self::DEFAULT_FETCH_ATTEMPTS;
2525

26-
/**
27-
* @var RecoverableExceptionHandler
28-
*/
26+
/** @var RecoverableExceptionHandler */
2927
private $recoverableExceptionHandler;
3028

3129
public function __construct()

test/Integration/Connector/ImportConnectorTest.php

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
77
use Mockery\MockInterface;
88
use PHPUnit\Framework\TestCase;
9+
use ScriptFUSION\Porter\Connector\AsyncDataSource;
910
use ScriptFUSION\Porter\Connector\Connector;
1011
use ScriptFUSION\Porter\Connector\DataSource;
1112
use ScriptFUSION\Porter\Connector\ImportConnector;
@@ -28,11 +29,15 @@ final class ImportConnectorTest extends TestCase
2829
/** @var DataSource|MockInterface */
2930
private $source;
3031

32+
/** @var AsyncDataSource|MockInterface */
33+
private $asyncSource;
34+
3135
protected function setUp()
3236
{
3337
parent::setUp();
3438

3539
$this->source = \Mockery::mock(DataSource::class);
40+
$this->asyncSource = \Mockery::mock(AsyncDataSource::class);
3641
}
3742

3843
/**
@@ -135,7 +140,7 @@ public function testAsyncUserRecoverableExceptionHandler(): void
135140
);
136141

137142
try {
138-
wait($connector->fetchAsync($this->source));
143+
wait($connector->fetchAsync($this->asyncSource));
139144
} catch (FailingTooHardException $exception) {
140145
// This is fine.
141146
}
@@ -158,7 +163,7 @@ public function testAsyncResourceRecoverableExceptionHandler(): void
158163
$connector->setRecoverableExceptionHandler(self::createAsyncRecoverableExceptionHandler());
159164

160165
try {
161-
wait($connector->fetchAsync($this->source));
166+
wait($connector->fetchAsync($this->asyncSource));
162167
} catch (FailingTooHardException $exception) {
163168
// This is fine.
164169
}
@@ -183,7 +188,7 @@ public function testAsyncUserAndResourceRecoverableExceptionHandlers(): void
183188
$connector->setRecoverableExceptionHandler(self::createAsyncRecoverableExceptionHandler());
184189

185190
try {
186-
wait($connector->fetchAsync($this->source));
191+
wait($connector->fetchAsync($this->asyncSource));
187192
} catch (FailingTooHardException $exception) {
188193
// This is fine.
189194
}

test/MockFactory.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
use Amp\Promise;
1010
use Mockery\MockInterface;
1111
use ScriptFUSION\Porter\Connector\AsyncConnector;
12+
use ScriptFUSION\Porter\Connector\AsyncDataSource;
1213
use ScriptFUSION\Porter\Connector\Connector;
1314
use ScriptFUSION\Porter\Connector\DataSource;
1415
use ScriptFUSION\Porter\Connector\ImportConnector;
@@ -72,7 +73,7 @@ public static function mockResource(Provider $provider, \Iterator $return = null
7273
->shouldReceive('fetchAsync')
7374
->andReturnUsing(static function (ImportConnector $connector): Iterator {
7475
return new Producer(static function (\Closure $emit) use ($connector): \Generator {
75-
yield $emit([yield $connector->fetchAsync(\Mockery::mock(DataSource::class))]);
76+
yield $emit([yield $connector->fetchAsync(\Mockery::mock(AsyncDataSource::class))]);
7677
});
7778
})
7879
->byDefault()

0 commit comments

Comments
 (0)