Skip to content

Commit

Permalink
Moved provider tag from ImportSpecification to ProviderDataSource.
Browse files Browse the repository at this point in the history
  • Loading branch information
Bilge committed Aug 11, 2016
1 parent 54413a4 commit a1b2f0a
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 39 deletions.
10 changes: 3 additions & 7 deletions src/Porter/Porter.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,7 @@ public function __construct()
*/
public function import(ImportSpecification $specification)
{
$records = $this->fetch(
$specification->getDataSource(),
$specification->getProviderTag(),
$specification->getCacheAdvice()
);
$records = $this->fetch($specification->getDataSource(), $specification->getCacheAdvice());

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

private function fetch(ProviderDataSource $dataSource, $providerTag, CacheAdvice $cacheAdvice = null)
private function fetch(ProviderDataSource $dataSource, CacheAdvice $cacheAdvice = null)
{
$provider = $this->getProvider($dataSource->getProviderClassName(), $providerTag);
$provider = $this->getProvider($dataSource->getProviderClassName(), $dataSource->getProviderTag());
$this->applyCacheAdvice($provider, $cacheAdvice ?: $this->defaultCacheAdvice);

return $provider->fetch($dataSource);
Expand Down
9 changes: 8 additions & 1 deletion src/Porter/Provider/DataSource/ProviderDataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,17 @@ interface ProviderDataSource
/**
* Gets the class name of the provider this data source belongs to.
*
* @return string
* @return string Provider class name.
*/
public function getProviderClassName();

/**
* Gets the provider identifier tag.
*
* @return string|null Provider tag.
*/
public function getProviderTag();

/**
* Fetches data from the provider using the the specified connector and
* presents its data as an enumerable series.
Expand Down
5 changes: 5 additions & 0 deletions src/Porter/Provider/DataSource/StaticDataSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ public function getProviderClassName()
return StaticDataProvider::class;
}

public function getProviderTag()
{
return;
}

public function fetch(Connector $connector, EncapsulatedOptions $options = null)
{
return $this->data;
Expand Down
23 changes: 0 additions & 23 deletions src/Porter/Specification/ImportSpecification.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ class ImportSpecification
/** @var ProviderDataSource */
private $dataSource;

/** @var string */
private $providerTag;

/** @var Mapping */
private $mapping;

Expand Down Expand Up @@ -45,26 +42,6 @@ final public function getDataSource()
return $this->dataSource;
}

/**
* @return string
*/
final public function getProviderTag()
{
return $this->providerTag;
}

/**
* @param $providerTag
*
* @return $this
*/
final public function setProviderTag($providerTag)
{
$this->providerTag = "$providerTag";

return $this;
}

/**
* @return Mapping
*/
Expand Down
27 changes: 27 additions & 0 deletions test/Integration/Porter/PorterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,13 @@ public function testRegisterSameProvider()
$this->porter->registerProvider($this->provider);
}

public function testRegisterSameProviderType()
{
$this->setExpectedException(ProviderAlreadyRegisteredException::class);

$this->porter->registerProvider(clone $this->provider);
}

public function testRegisterProviderTag()
{
$this->porter->registerProvider($provider = clone $this->provider, 'foo');
Expand Down Expand Up @@ -96,6 +103,26 @@ public function testGetStaticProviderTag()
$this->porter->getProvider(StaticDataProvider::class, 'foo');
}

public function testImportTaggedResource()
{
$this->porter->registerProvider(
$provider = \Mockery::mock(Provider::class)
->shouldReceive('fetch')
->andReturn(new \ArrayIterator([$output = 'bar']))
->getMock(),
$tag = 'foo'
);

$records = $this->porter->import(MockFactory::mockImportSpecification(
MockFactory::mockDataSource($provider)
->shouldReceive('getProviderTag')
->andReturn($tag)
->getMock()
));

self::assertSame($output, $records->current());
}

public function testHasProvider()
{
self::assertTrue($this->porter->hasProvider(get_class($this->provider)));
Expand Down
6 changes: 3 additions & 3 deletions test/MockFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ final class MockFactory
{
use StaticClass;

public static function mockImportSpecification()
public static function mockImportSpecification(ProviderDataSource $dataSource = null)
{
return \Mockery::mock(ImportSpecification::class, [\Mockery::mock(ProviderDataSource::class)]);
return \Mockery::mock(ImportSpecification::class, [$dataSource ?: \Mockery::mock(ProviderDataSource::class)]);
}

/**
Expand All @@ -23,7 +23,7 @@ public static function mockImportSpecification()
*/
public static function mockDataSource(Provider $provider)
{
return \Mockery::mock(ProviderDataSource::class)
return \Mockery::spy(ProviderDataSource::class)
->shouldReceive('getProviderClassName')
->andReturn(get_class($provider))
->getMock();
Expand Down
5 changes: 0 additions & 5 deletions test/Unit/Porter/ImportSpecificationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,6 @@ public function testProviderData()
self::assertSame($this->dataSource, $this->specification->getDataSource());
}

public function testProviderTag()
{
self::assertSame('foo', $this->specification->setProviderTag('foo')->getProviderTag());
}

public function testMapping()
{
self::assertSame(
Expand Down

0 comments on commit a1b2f0a

Please sign in to comment.