Skip to content

Commit a1b2f0a

Browse files
committed
Moved provider tag from ImportSpecification to ProviderDataSource.
1 parent 54413a4 commit a1b2f0a

7 files changed

Lines changed: 46 additions & 39 deletions

File tree

src/Porter/Porter.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,7 @@ public function __construct()
4444
*/
4545
public function import(ImportSpecification $specification)
4646
{
47-
$records = $this->fetch(
48-
$specification->getDataSource(),
49-
$specification->getProviderTag(),
50-
$specification->getCacheAdvice()
51-
);
47+
$records = $this->fetch($specification->getDataSource(), $specification->getCacheAdvice());
5248

5349
if (!$records instanceof ProviderRecords) {
5450
// Wrap Iterator in ProviderRecords.
@@ -75,9 +71,9 @@ private function createPorterRecords(RecordCollection $records, ImportSpecificat
7571
return new PorterRecords($records, $specification);
7672
}
7773

78-
private function fetch(ProviderDataSource $dataSource, $providerTag, CacheAdvice $cacheAdvice = null)
74+
private function fetch(ProviderDataSource $dataSource, CacheAdvice $cacheAdvice = null)
7975
{
80-
$provider = $this->getProvider($dataSource->getProviderClassName(), $providerTag);
76+
$provider = $this->getProvider($dataSource->getProviderClassName(), $dataSource->getProviderTag());
8177
$this->applyCacheAdvice($provider, $cacheAdvice ?: $this->defaultCacheAdvice);
8278

8379
return $provider->fetch($dataSource);

src/Porter/Provider/DataSource/ProviderDataSource.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,17 @@ interface ProviderDataSource
1111
/**
1212
* Gets the class name of the provider this data source belongs to.
1313
*
14-
* @return string
14+
* @return string Provider class name.
1515
*/
1616
public function getProviderClassName();
1717

18+
/**
19+
* Gets the provider identifier tag.
20+
*
21+
* @return string|null Provider tag.
22+
*/
23+
public function getProviderTag();
24+
1825
/**
1926
* Fetches data from the provider using the the specified connector and
2027
* presents its data as an enumerable series.

src/Porter/Provider/DataSource/StaticDataSource.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,11 @@ public function getProviderClassName()
1919
return StaticDataProvider::class;
2020
}
2121

22+
public function getProviderTag()
23+
{
24+
return;
25+
}
26+
2227
public function fetch(Connector $connector, EncapsulatedOptions $options = null)
2328
{
2429
return $this->data;

src/Porter/Specification/ImportSpecification.php

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ class ImportSpecification
1010
/** @var ProviderDataSource */
1111
private $dataSource;
1212

13-
/** @var string */
14-
private $providerTag;
15-
1613
/** @var Mapping */
1714
private $mapping;
1815

@@ -45,26 +42,6 @@ final public function getDataSource()
4542
return $this->dataSource;
4643
}
4744

48-
/**
49-
* @return string
50-
*/
51-
final public function getProviderTag()
52-
{
53-
return $this->providerTag;
54-
}
55-
56-
/**
57-
* @param $providerTag
58-
*
59-
* @return $this
60-
*/
61-
final public function setProviderTag($providerTag)
62-
{
63-
$this->providerTag = "$providerTag";
64-
65-
return $this;
66-
}
67-
6845
/**
6946
* @return Mapping
7047
*/

test/Integration/Porter/PorterTest.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,13 @@ public function testRegisterSameProvider()
6363
$this->porter->registerProvider($this->provider);
6464
}
6565

66+
public function testRegisterSameProviderType()
67+
{
68+
$this->setExpectedException(ProviderAlreadyRegisteredException::class);
69+
70+
$this->porter->registerProvider(clone $this->provider);
71+
}
72+
6673
public function testRegisterProviderTag()
6774
{
6875
$this->porter->registerProvider($provider = clone $this->provider, 'foo');
@@ -96,6 +103,26 @@ public function testGetStaticProviderTag()
96103
$this->porter->getProvider(StaticDataProvider::class, 'foo');
97104
}
98105

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

test/MockFactory.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,9 @@ final class MockFactory
1111
{
1212
use StaticClass;
1313

14-
public static function mockImportSpecification()
14+
public static function mockImportSpecification(ProviderDataSource $dataSource = null)
1515
{
16-
return \Mockery::mock(ImportSpecification::class, [\Mockery::mock(ProviderDataSource::class)]);
16+
return \Mockery::mock(ImportSpecification::class, [$dataSource ?: \Mockery::mock(ProviderDataSource::class)]);
1717
}
1818

1919
/**
@@ -23,7 +23,7 @@ public static function mockImportSpecification()
2323
*/
2424
public static function mockDataSource(Provider $provider)
2525
{
26-
return \Mockery::mock(ProviderDataSource::class)
26+
return \Mockery::spy(ProviderDataSource::class)
2727
->shouldReceive('getProviderClassName')
2828
->andReturn(get_class($provider))
2929
->getMock();

test/Unit/Porter/ImportSpecificationTest.php

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,6 @@ public function testProviderData()
3636
self::assertSame($this->dataSource, $this->specification->getDataSource());
3737
}
3838

39-
public function testProviderTag()
40-
{
41-
self::assertSame('foo', $this->specification->setProviderTag('foo')->getProviderTag());
42-
}
43-
4439
public function testMapping()
4540
{
4641
self::assertSame(

0 commit comments

Comments
 (0)