diff --git a/src/Porter.php b/src/Porter.php index 20fca7d..a5acf55 100644 --- a/src/Porter.php +++ b/src/Porter.php @@ -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) )); } diff --git a/test/Integration/Porter/PorterTest.php b/test/Integration/Porter/PorterTest.php index c28006c..0a4d909 100644 --- a/test/Integration/Porter/PorterTest.php +++ b/test/Integration/Porter/PorterTest.php @@ -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; @@ -42,7 +43,7 @@ final class PorterTest extends \PHPUnit_Framework_TestCase private $provider; /** - * @var ProviderResource + * @var ProviderResource|MockInterface */ private $resource; @@ -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); } @@ -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 @@ -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() ; } } diff --git a/test/MockFactory.php b/test/MockFactory.php index f720966..b2d855e 100644 --- a/test/MockFactory.php +++ b/test/MockFactory.php @@ -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; @@ -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 @@ -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);