Skip to content

Commit

Permalink
Added EncapsulatedOptions to ProviderResource::fetch() and updated te…
Browse files Browse the repository at this point in the history
…sts.
  • Loading branch information
Bilge committed Oct 16, 2016
1 parent 85613f0 commit 88bb54f
Show file tree
Hide file tree
Showing 6 changed files with 103 additions and 13 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ php:
- 5.5
- 5.6
- 7.0
- 7.1

install:
- alias composer=composer\ -n && composer selfupdate
Expand Down
4 changes: 2 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@
],
"license": "LGPL-3.0",
"require": {
"php": ">=5.5",
"scriptfusion/mapper": "^1|^0",
"php": "^5.5|^7",
"scriptfusion/mapper": "^1",
"scriptfusion/static-class": "^1",
"scriptfusion/retry-error-handlers": "^1",
"psr/cache": "^1",
Expand Down
31 changes: 30 additions & 1 deletion src/Provider/AbstractProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,21 @@
use ScriptFUSION\Porter\Cache\CacheToggle;
use ScriptFUSION\Porter\Cache\CacheUnavailableException;
use ScriptFUSION\Porter\Connector\Connector;
use ScriptFUSION\Porter\Options\EncapsulatedOptions;
use ScriptFUSION\Porter\Provider\Resource\ProviderResource;

abstract class AbstractProvider implements Provider, CacheToggle
{
/**
* @var Connector
*/
private $connector;

/**
* @var EncapsulatedOptions|null
*/
private $options;

public function __construct(Connector $connector)
{
$this->connector = $connector;
Expand All @@ -31,7 +40,7 @@ public function fetch(ProviderResource $resource)
));
}

return $resource->fetch($this->connector);
return $resource->fetch($this->connector, $this->options ? clone $this->options : null);
}

/**
Expand Down Expand Up @@ -74,4 +83,24 @@ public function isCacheEnabled()

return $connector->isCacheEnabled();
}

/**
* Gets the provider options.
*
* @return EncapsulatedOptions|null
*/
protected function getOptions()
{
return $this->options;
}

/**
* Sets the provider options to the specified options.
*
* @param EncapsulatedOptions $options Options.
*/
protected function setOptions(EncapsulatedOptions $options)
{
$this->options = $options;
}
}
4 changes: 3 additions & 1 deletion src/Provider/Resource/ProviderResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
namespace ScriptFUSION\Porter\Provider\Resource;

use ScriptFUSION\Porter\Connector\Connector;
use ScriptFUSION\Porter\Options\EncapsulatedOptions;

/**
* Defines methods for fetching data from a specific provider resource.
Expand All @@ -27,8 +28,9 @@ public function getProviderTag();
* presents its data as an enumerable series.
*
* @param Connector $connector Connector.
* @param EncapsulatedOptions $options Optional. Options.
*
* @return \Iterator Enumerable data series.
*/
public function fetch(Connector $connector);
public function fetch(Connector $connector, EncapsulatedOptions $options = null);
}
5 changes: 4 additions & 1 deletion test/MockFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@ public static function mockImportSpecification(ProviderResource $resource = null
*/
public static function mockResource(Provider $provider)
{
return \Mockery::spy(ProviderResource::class)
return \Mockery::mock(ProviderResource::class)
->shouldReceive('getProviderClassName')
->andReturn(get_class($provider))
->shouldReceive('getProviderTag')
->andReturn(null)
->byDefault()
->getMock();
}
}
71 changes: 63 additions & 8 deletions test/Unit/Porter/Provider/AbstractProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,24 @@
use ScriptFUSION\Porter\Cache\CacheUnavailableException;
use ScriptFUSION\Porter\Connector\CachingConnector;
use ScriptFUSION\Porter\Connector\Connector;
use ScriptFUSION\Porter\Options\EncapsulatedOptions;
use ScriptFUSION\Porter\Provider\AbstractProvider;
use ScriptFUSION\Porter\Provider\ForeignResourceException;
use ScriptFUSION\Porter\Provider\Resource\ProviderResource;
use ScriptFUSIONTest\MockFactory;

final class AbstractProviderTest extends \PHPUnit_Framework_TestCase
{
use MockeryPHPUnitIntegration;

/** @var AbstractProvider */
/**
* @var AbstractProvider
*/
private $provider;

/** @var MockInterface */
/**
* @var MockInterface|Connector
*/
private $connector;

protected function setUp()
Expand All @@ -40,23 +46,41 @@ private function setupCachingConnector()
return $connector;
}

public function testFetch()
public function testFetchWithoutOptions()
{
self::assertSame(
'foo',
$this->provider->fetch(
\Mockery::mock(ProviderResource::class)
MockFactory::mockResource($this->provider)
->shouldReceive('fetch')
->with($this->connector)
->with($this->connector, null)
->andReturn('foo')
->getMock()
->shouldReceive('getProviderClassName')
->andReturn(get_class($this->provider))
->getMock()
)
);
}

/**
* Tests that a clone of the provider's options are passed to ProviderResource::fetch().
*/
public function testFetchWithOptions()
{
$this->setOptions($options = \Mockery::mock(EncapsulatedOptions::class));

$this->provider->fetch(
MockFactory::mockResource($this->provider)
->shouldReceive('fetch')
->with($this->connector, \Mockery::on(
function (EncapsulatedOptions $argument) use ($options) {
self::assertNotSame($options, $argument);

return get_class($options) === get_class($argument);
}
))
->getMock()
);
}

public function testFetchForeignProvider()
{
$this->setExpectedException(ForeignResourceException::class);
Expand Down Expand Up @@ -114,4 +138,35 @@ public function testCacheEnabledMirrorsCachingConnector()
self::assertTrue($this->provider->isCacheEnabled());
self::assertFalse($this->provider->isCacheEnabled());
}

public function testOptions()
{
$this->setOptions($options = \Mockery::mock(EncapsulatedOptions::class));

self::assertSame($options, $this->getOptions());
}

private function getOptions()
{
return call_user_func(
\Closure::bind(
function () {
return $this->getOptions();
},
$this->provider
)
);
}

private function setOptions(EncapsulatedOptions $options)
{
call_user_func(
\Closure::bind(
function () use ($options) {
$this->setOptions($options);
},
$this->provider
)
);
}
}

0 comments on commit 88bb54f

Please sign in to comment.