File tree Expand file tree Collapse file tree 6 files changed +70
-1
lines changed Expand file tree Collapse file tree 6 files changed +70
-1
lines changed Original file line number Diff line number Diff line change 1+ <?php
2+ declare (strict_types=1 );
3+
4+ namespace ScriptFUSION \Porter ;
5+
6+ use ScriptFUSION \Porter \Provider \Resource \SingleRecordResource ;
7+
8+ /**
9+ * The exception that is throw when a resource is incompatible with an importOne operation because it is not marked
10+ * with the single record interface.
11+ */
12+ final class IncompatibleResourceException extends \LogicException
13+ {
14+ public function __construct ()
15+ {
16+ parent ::__construct ('Cannot import one: resource does not implement ' . SingleRecordResource::class . '. ' );
17+ }
18+ }
Original file line number Diff line number Diff line change 2121use ScriptFUSION \Porter \Provider \Provider ;
2222use ScriptFUSION \Porter \Provider \ProviderFactory ;
2323use ScriptFUSION \Porter \Provider \Resource \ProviderResource ;
24+ use ScriptFUSION \Porter \Provider \Resource \SingleRecordResource ;
2425use ScriptFUSION \Porter \Specification \AsyncImportSpecification ;
2526use ScriptFUSION \Porter \Specification \ImportSpecification ;
2627use ScriptFUSION \Porter \Transform \AsyncTransformer ;
@@ -86,6 +87,10 @@ public function import(ImportSpecification $specification): PorterRecords
8687 */
8788 public function importOne (ImportSpecification $ specification ): ?array
8889 {
90+ if (!$ specification ->getResource () instanceof SingleRecordResource) {
91+ throw new IncompatibleResourceException ;
92+ }
93+
8994 $ results = $ this ->import ($ specification );
9095
9196 if (!$ results ->valid ()) {
@@ -162,6 +167,10 @@ public function importAsync(AsyncImportSpecification $specification): AsyncRecor
162167 public function importOneAsync (AsyncImportSpecification $ specification ): Promise
163168 {
164169 return call (function () use ($ specification ) {
170+ if (!$ specification ->getAsyncResource () instanceof SingleRecordResource) {
171+ throw new IncompatibleResourceException ;
172+ }
173+
165174 $ results = $ this ->importAsync ($ specification );
166175
167176 yield $ results ->advance ();
Original file line number Diff line number Diff line change 1+ <?php
2+ declare (strict_types=1 );
3+
4+ namespace ScriptFUSION \Porter \Provider \Resource ;
5+
6+ /**
7+ * Marker interface that specifies a resource that only fetches a single record and whose iterator is thus only valid
8+ * for one iteration. Such resources are intended to be used with Porter::importOne() rather than import().
9+ */
10+ interface SingleRecordResource
11+ {
12+ // Marker interface.
13+ }
Original file line number Diff line number Diff line change 1212use ScriptFUSION \Porter \ForeignResourceException ;
1313use ScriptFUSION \Porter \ImportException ;
1414use ScriptFUSION \Porter \IncompatibleProviderException ;
15+ use ScriptFUSION \Porter \IncompatibleResourceException ;
1516use ScriptFUSION \Porter \Porter ;
1617use ScriptFUSION \Porter \PorterAware ;
1718use ScriptFUSION \Porter \Provider \Provider ;
19+ use ScriptFUSION \Porter \Provider \Resource \AsyncResource ;
20+ use ScriptFUSION \Porter \Provider \Resource \SingleRecordResource ;
1821use ScriptFUSION \Porter \Specification \AsyncImportSpecification ;
1922use ScriptFUSION \Porter \Transform \AsyncTransformer ;
2023use ScriptFUSION \Porter \Transform \FilterTransformer ;
@@ -51,6 +54,17 @@ public function testImportOneAsync(): \Generator
5154 self ::assertSame (['foo ' ], yield $ this ->porter ->importOneAsync ($ this ->specification ));
5255 }
5356
57+ /**
58+ * Tests that when importing one from a resource not marked with SingleRecordResource, an exception is thrown.
59+ */
60+ public function testImportOneNonSingleAsync (): \Generator
61+ {
62+ $ this ->expectException (IncompatibleResourceException::class);
63+ $ this ->expectExceptionMessage (SingleRecordResource::class);
64+
65+ yield $ this ->porter ->importOneAsync (new AsyncImportSpecification (\Mockery::mock (AsyncResource::class)));
66+ }
67+
5468 /**
5569 * Tests that when the resource is countable, the count is propagated to the outermost collection and the records
5670 * are intact.
Original file line number Diff line number Diff line change 1616use ScriptFUSION \Porter \ForeignResourceException ;
1717use ScriptFUSION \Porter \ImportException ;
1818use ScriptFUSION \Porter \IncompatibleProviderException ;
19+ use ScriptFUSION \Porter \IncompatibleResourceException ;
1920use ScriptFUSION \Porter \PorterAware ;
2021use ScriptFUSION \Porter \Provider \AsyncProvider ;
22+ use ScriptFUSION \Porter \Provider \Resource \ProviderResource ;
23+ use ScriptFUSION \Porter \Provider \Resource \SingleRecordResource ;
2124use ScriptFUSION \Porter \ProviderNotFoundException ;
2225use ScriptFUSION \Porter \Specification \ImportSpecification ;
2326use ScriptFUSION \Porter \Specification \StaticDataImportSpecification ;
@@ -209,6 +212,17 @@ public function testImportOneOfMany(): void
209212 $ this ->porter ->importOne ($ this ->specification );
210213 }
211214
215+ /**
216+ * Tests that when importing one from a resource not marked with SingleRecordResource, an exception is thrown.
217+ */
218+ public function testImportOneNonSingleAsync (): \Generator
219+ {
220+ $ this ->expectException (IncompatibleResourceException::class);
221+ $ this ->expectExceptionMessage (SingleRecordResource::class);
222+
223+ yield $ this ->porter ->importOne (new ImportSpecification (\Mockery::mock (ProviderResource::class)));
224+ }
225+
212226 #endregion
213227
214228 #region Durability
Original file line number Diff line number Diff line change 1616use ScriptFUSION \Porter \Provider \Provider ;
1717use ScriptFUSION \Porter \Provider \Resource \AsyncResource ;
1818use ScriptFUSION \Porter \Provider \Resource \ProviderResource ;
19+ use ScriptFUSION \Porter \Provider \Resource \SingleRecordResource ;
1920use ScriptFUSION \StaticClass ;
2021
2122final class MockFactory
@@ -54,7 +55,7 @@ public static function mockProvider()
5455 */
5556 public static function mockResource (Provider $ provider , \Iterator $ return = null )
5657 {
57- $ resource = \Mockery::mock (ProviderResource::class, AsyncResource::class)
58+ $ resource = \Mockery::mock (ProviderResource::class, AsyncResource::class, SingleRecordResource::class )
5859 ->shouldReceive ('getProviderClassName ' )
5960 ->andReturn (\get_class ($ provider ))
6061 ->shouldReceive ('fetch ' )
You can’t perform that action at this time.
0 commit comments