Skip to content

Commit ca1ba76

Browse files
committed
Added Porter::importOne and accompanying test.
Fixed old referneces to Resource in docblocks. Optimized PorterTest.
1 parent 9c90f3c commit ca1ba76

File tree

9 files changed

+102
-37
lines changed

9 files changed

+102
-37
lines changed

src/Porter/Collection/CountableProviderRecords.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class CountableProviderRecords extends ProviderRecords implements \Countable
1010
/**
1111
* @param \Iterator $providerRecords
1212
* @param int $count
13-
* @param Resource $resource
13+
* @param ProviderResource $resource
1414
*/
1515
public function __construct(\Iterator $providerRecords, $count, ProviderResource $resource)
1616
{

src/Porter/Collection/ProviderRecords.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ public function __construct(\Iterator $providerRecords, ProviderResource $resour
1515
}
1616

1717
/**
18-
* @return Resource
18+
* @return ProviderResource
1919
*/
2020
public function getResource()
2121
{

src/Porter/ImportException.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
namespace ScriptFUSION\Porter;
3+
4+
/**
5+
* The exception that is thrown when an import fails.
6+
*/
7+
class ImportException extends \RuntimeException
8+
{
9+
// Intentionally empty.
10+
}

src/Porter/Porter.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,32 @@ public function import(ImportSpecification $specification)
6262
return $this->createPorterRecords($records, clone $specification);
6363
}
6464

65+
/**
66+
* Imports one record according to the design of the specified import specification.
67+
*
68+
* @param ImportSpecification $specification Import specification.
69+
*
70+
* @return mixed Data.
71+
*
72+
* @throws ImportException More than one record was imported.
73+
*/
74+
public function importOne(ImportSpecification $specification)
75+
{
76+
$results = $this->import($specification);
77+
78+
if (!$results->valid()) {
79+
return;
80+
}
81+
82+
$one = $results->current();
83+
84+
if ($results->next() || $results->valid()) {
85+
throw new ImportException('Cannot import one: more than one record imported.');
86+
}
87+
88+
return $one;
89+
}
90+
6591
private function createPorterRecords(RecordCollection $records, ImportSpecification $specification)
6692
{
6793
if ($records instanceof \Countable) {

src/Porter/Provider/AbstractProvider.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public function __construct(Connector $connector)
1616
}
1717

1818
/**
19-
* @param Resource $resource
19+
* @param ProviderResource $resource
2020
*
2121
* @return \Iterator
2222
*

src/Porter/Specification/ImportSpecification.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
class ImportSpecification
99
{
10-
/** @var Resource */
10+
/** @var ProviderResource */
1111
private $resource;
1212

1313
/** @var Mapping */
@@ -35,7 +35,7 @@ public function __clone()
3535
}
3636

3737
/**
38-
* @return Resource
38+
* @return ProviderResource
3939
*/
4040
final public function getResource()
4141
{

test/Integration/Porter/PorterTest.php

Lines changed: 59 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,10 @@
1414
use ScriptFUSION\Porter\Collection\MappedRecords;
1515
use ScriptFUSION\Porter\Collection\PorterRecords;
1616
use ScriptFUSION\Porter\Collection\ProviderRecords;
17+
use ScriptFUSION\Porter\ImportException;
1718
use ScriptFUSION\Porter\Porter;
1819
use ScriptFUSION\Porter\Provider\Provider;
20+
use ScriptFUSION\Porter\Provider\Resource\ProviderResource;
1921
use ScriptFUSION\Porter\Provider\StaticDataProvider;
2022
use ScriptFUSION\Porter\ProviderAlreadyRegisteredException;
2123
use ScriptFUSION\Porter\ProviderNotFoundException;
@@ -33,9 +35,12 @@ final class PorterTest extends \PHPUnit_Framework_TestCase
3335
/** @var Provider|MockInterface */
3436
private $provider;
3537

36-
/** @var Resource */
38+
/** @var ProviderResource */
3739
private $resource;
3840

41+
/** @var ImportSpecification */
42+
private $specification;
43+
3944
protected function setUp()
4045
{
4146
$this->porter = (new Porter)->registerProvider(
@@ -48,6 +53,7 @@ protected function setUp()
4853
);
4954

5055
$this->resource = MockFactory::mockResource($this->provider);
56+
$this->specification = new ImportSpecification($this->resource);
5157
}
5258

5359
public function testGetProvider()
@@ -85,14 +91,14 @@ public function testGetInvalidProvider()
8591
{
8692
$this->setExpectedException(ProviderNotFoundException::class);
8793

88-
(new Porter)->getProvider('foo');
94+
$this->porter->getProvider('foo');
8995
}
9096

9197
public function testGetInvalidTag()
9298
{
9399
$this->setExpectedException(ProviderNotFoundException::class);
94100

95-
(new Porter)->getProvider(get_class($this->provider), 'foo');
101+
$this->porter->getProvider(get_class($this->provider), 'foo');
96102
}
97103

98104
public function testGetStaticProviderTag()
@@ -102,26 +108,6 @@ public function testGetStaticProviderTag()
102108
$this->porter->getProvider(StaticDataProvider::class, 'foo');
103109
}
104110

105-
public function testImportTaggedResource()
106-
{
107-
$this->porter->registerProvider(
108-
$provider = \Mockery::mock(Provider::class)
109-
->shouldReceive('fetch')
110-
->andReturn(new \ArrayIterator([$output = 'bar']))
111-
->getMock(),
112-
$tag = 'foo'
113-
);
114-
115-
$records = $this->porter->import(MockFactory::mockImportSpecification(
116-
MockFactory::mockResource($provider)
117-
->shouldReceive('getProviderTag')
118-
->andReturn($tag)
119-
->getMock()
120-
));
121-
122-
self::assertSame($output, $records->current());
123-
}
124-
125111
public function testHasProvider()
126112
{
127113
self::assertTrue($this->porter->hasProvider(get_class($this->provider)));
@@ -131,10 +117,10 @@ public function testHasProvider()
131117

132118
public function testImport()
133119
{
134-
$records = $this->porter->import($specification = new ImportSpecification($this->resource));
120+
$records = $this->porter->import($this->specification);
135121

136122
self::assertInstanceOf(PorterRecords::class, $records);
137-
self::assertNotSame($specification, $records->getSpecification());
123+
self::assertNotSame($this->specification, $records->getSpecification());
138124
self::assertInstanceOf(ProviderRecords::class, $records->getPreviousCollection());
139125
self::assertSame('foo', $records->current());
140126
}
@@ -194,12 +180,57 @@ public function testImportAndFilterCountableRecords()
194180
self::assertNotInstanceOf(\Countable::class, $records);
195181
}
196182

183+
public function testImportOne()
184+
{
185+
$result = $this->porter->importOne($this->specification);
186+
187+
self::assertSame('foo', $result);
188+
}
189+
190+
public function testImportOneOfNone()
191+
{
192+
$this->provider->shouldReceive('fetch')->andReturn(new \EmptyIterator);
193+
194+
$result = $this->porter->importOne($this->specification);
195+
196+
self::assertNull($result);
197+
}
198+
199+
public function testImportOneOfMany()
200+
{
201+
$this->setExpectedException(ImportException::class);
202+
203+
$this->provider->shouldReceive('fetch')->andReturn(new \ArrayIterator(['foo', 'bar']));
204+
205+
$this->porter->importOne($this->specification);
206+
}
207+
208+
public function testImportTaggedResource()
209+
{
210+
$this->porter->registerProvider(
211+
$provider = \Mockery::mock(Provider::class)
212+
->shouldReceive('fetch')
213+
->andReturn(new \ArrayIterator([$output = 'bar']))
214+
->getMock(),
215+
$tag = 'foo'
216+
);
217+
218+
$records = $this->porter->import(MockFactory::mockImportSpecification(
219+
MockFactory::mockResource($provider)
220+
->shouldReceive('getProviderTag')
221+
->andReturn($tag)
222+
->getMock()
223+
));
224+
225+
self::assertSame($output, $records->current());
226+
}
227+
197228
public function testFilter()
198229
{
199230
$this->provider->shouldReceive('fetch')->andReturn(new \ArrayIterator(range(1, 10)));
200231

201232
$records = $this->porter->import(
202-
(new ImportSpecification($this->resource))
233+
$this->specification
203234
->setFilter(function ($record) {
204235
return $record % 2;
205236
})
@@ -219,9 +250,7 @@ public function testMap()
219250
->once()
220251
->andReturn(new \ArrayIterator($result = ['foo' => 'bar']))
221252
->getMock()
222-
)->import(
223-
(new ImportSpecification($this->resource))->setMapping(\Mockery::mock(Mapping::class))
224-
);
253+
)->import($this->specification->setMapping(\Mockery::mock(Mapping::class)));
225254

226255
self::assertInstanceOf(PorterRecords::class, $records);
227256
self::assertInstanceOf(MappedRecords::class, $records->getPreviousCollection());
@@ -246,6 +275,6 @@ public function testCacheUnavailable()
246275
{
247276
$this->setExpectedException(CacheUnavailableException::class);
248277

249-
$this->porter->import((new ImportSpecification($this->resource))->setCacheAdvice(CacheAdvice::MUST_CACHE()));
278+
$this->porter->import($this->specification->setCacheAdvice(CacheAdvice::MUST_CACHE()));
250279
}
251280
}

test/MockFactory.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public static function mockImportSpecification(ProviderResource $resource = null
1919
/**
2020
* @param Provider $provider
2121
*
22-
* @return MockInterface|Resource
22+
* @return MockInterface|ProviderResource
2323
*/
2424
public static function mockResource(Provider $provider)
2525
{

test/Unit/Porter/ImportSpecificationTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ final class ImportSpecificationTest extends \PHPUnit_Framework_TestCase
1111
/** @var ImportSpecification */
1212
private $specification;
1313

14-
/** @var Resource */
14+
/** @var ProviderResource */
1515
private $resource;
1616

1717
protected function setUp()

0 commit comments

Comments
 (0)