Skip to content

Commit

Permalink
Added test case for importing foreign resource.
Browse files Browse the repository at this point in the history
  • Loading branch information
Bilge committed Jul 5, 2017
1 parent 557d694 commit c342d69
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/Porter.php
Expand Up @@ -113,7 +113,7 @@ private function fetch(

if ($resource->getProviderClassName() !== get_class($provider)) {
throw new ForeignResourceException(sprintf(
'Cannot fetch data from foreign source: "%s".',
'Cannot fetch data from foreign resource: "%s".',
get_class($resource)
));
}
Expand Down
33 changes: 21 additions & 12 deletions test/Integration/Porter/PorterTest.php
Expand Up @@ -16,6 +16,7 @@
use ScriptFUSION\Porter\ImportException;
use ScriptFUSION\Porter\Porter;
use ScriptFUSION\Porter\PorterAware;
use ScriptFUSION\Porter\Provider\ForeignResourceException;
use ScriptFUSION\Porter\Provider\Provider;
use ScriptFUSION\Porter\Provider\Resource\ProviderResource;
use ScriptFUSION\Porter\ProviderNotFoundException;
Expand All @@ -42,7 +43,7 @@ final class PorterTest extends \PHPUnit_Framework_TestCase
private $provider;

/**
* @var ProviderResource
* @var ProviderResource|MockInterface
*/
private $resource;

Expand All @@ -60,15 +61,7 @@ protected function setUp()
{
$this->porter = new Porter($this->container = $container = \Mockery::spy(ContainerInterface::class));

$this->registerProvider(
$this->provider =
\Mockery::mock(Provider::class)
->shouldReceive('getConnector')
->andReturn(\Mockery::mock(Connector::class))
->byDefault()
->getMock()
);

$this->registerProvider($this->provider = MockFactory::mockProvider());
$this->resource = MockFactory::mockResource($this->provider);
$this->specification = new ImportSpecification($this->resource);
}
Expand Down Expand Up @@ -173,7 +166,19 @@ public function testImportUnregisteredProvider()
{
$this->setExpectedException(ProviderNotFoundException::class);

$this->porter->import((new ImportSpecification($this->resource))->setProviderName('foo'));
$this->porter->import($this->specification->setProviderName('foo'));
}

/**
* Tests that when a resource's provider class name does not match the provider an exception is thrown.
*/
public function testImportForeignResource()
{
// Replace existing provider with a different one.
$this->registerProvider(MockFactory::mockProvider(), get_class($this->provider));

$this->setExpectedException(ForeignResourceException::class);
$this->porter->import($this->specification);
}

#endregion
Expand Down Expand Up @@ -316,13 +321,17 @@ public function testCacheUnavailable()
$this->porter->import($this->specification->setCacheAdvice(CacheAdvice::MUST_CACHE()));
}

/**
* @param Provider $provider
* @param string|null $name
*/
private function registerProvider(Provider $provider, $name = null)
{
$name = $name ?: get_class($provider);

$this->container
->shouldReceive('has')->with($name)->andReturn(true)
->shouldReceive('get')->with($name)->andReturn($provider)
->shouldReceive('get')->with($name)->andReturn($provider)->byDefault()
;
}
}
18 changes: 16 additions & 2 deletions test/MockFactory.php
Expand Up @@ -2,6 +2,7 @@
namespace ScriptFUSIONTest;

use Mockery\MockInterface;
use ScriptFUSION\Porter\Connector\Connector;
use ScriptFUSION\Porter\Provider\Provider;
use ScriptFUSION\Porter\Provider\Resource\ProviderResource;
use ScriptFUSION\StaticClass;
Expand All @@ -10,6 +11,19 @@ final class MockFactory
{
use StaticClass;

/**
* @return Provider|MockInterface
*/
public static function mockProvider()
{
return \Mockery::namedMock(uniqid(Provider::class, false), Provider::class)
->shouldReceive('getConnector')
->andReturn(\Mockery::mock(Connector::class))
->byDefault()
->getMock()
;
}

/**
* @param Provider $provider
* @param \Iterator $return
Expand All @@ -21,13 +35,13 @@ public static function mockResource(Provider $provider, \Iterator $return = null
$resource = \Mockery::mock(ProviderResource::class)
->shouldReceive('getProviderClassName')
->andReturn(get_class($provider))
->byDefault()
->shouldReceive('fetch')
->andReturnUsing(function () {
yield 'foo';
})
->byDefault()
->getMock();
->getMock()
;

if ($return !== null) {
$resource->shouldReceive('fetch')->andReturn($return);
Expand Down

0 comments on commit c342d69

Please sign in to comment.