Skip to content

Commit e754328

Browse files
committed
Fixed tests, and in particular, CachingConnectorTest.
Removed dependency on eloquent/enumeration.
1 parent b98eae5 commit e754328

File tree

7 files changed

+97
-93
lines changed

7 files changed

+97
-93
lines changed

composer.json

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
"scriptfusion/static-class": "^1",
1414
"scriptfusion/retry": "^1.1",
1515
"scriptfusion/retry-exception-handlers": "^1",
16-
"eloquent/enumeration": "^5",
1716
"psr/container": "^1",
1817
"psr/cache": "^1"
1918
},

src/Connector/CachingConnector.php

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ public function __construct(
3535
) {
3636
$this->connector = $connector;
3737
$this->cache = $cache ?: new MemoryCache;
38-
$this->cacheKeyGenerator ?: new JsonCacheKeyGenerator;
38+
$this->cacheKeyGenerator = $cacheKeyGenerator ?: new JsonCacheKeyGenerator;
3939
}
4040

4141
/**
@@ -53,7 +53,7 @@ public function fetch(ConnectionContext $context, $source, EncapsulatedOptions $
5353

5454
ksort($optionsCopy);
5555

56-
$key = $this->validateCacheKey($this->cacheKeyGenerator->generateCacheKey($source, $optionsCopy));
56+
$this->validateCacheKey($key = $this->cacheKeyGenerator->generateCacheKey($source, $optionsCopy));
5757

5858
if ($this->cache->hasItem($key)) {
5959
return $this->cache->getItem($key)->get();
@@ -70,12 +70,13 @@ public function fetch(ConnectionContext $context, $source, EncapsulatedOptions $
7070
/**
7171
* @param mixed $key
7272
*
73-
* @return string
73+
* @return void
7474
*
7575
* @throws InvalidCacheKeyException Cache key contains invalid data.
7676
*/
7777
private function validateCacheKey($key)
7878
{
79+
// TODO: Remove when PHP 5 support dropped and replace with string hint.
7980
if (!is_string($key)) {
8081
throw new InvalidCacheKeyException('Cache key must be a string.');
8182
}
@@ -87,7 +88,5 @@ private function validateCacheKey($key)
8788
CacheKeyGenerator::RESERVED_CHARACTERS
8889
));
8990
}
90-
91-
return $key;
9291
}
9392
}

test/FixtureFactory.php

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?php
22
namespace ScriptFUSIONTest;
33

4-
use ScriptFUSION\Porter\Cache\CacheAdvice;
54
use ScriptFUSION\Porter\Connector\ConnectionContext;
65
use ScriptFUSION\Porter\Specification\ImportSpecification;
76
use ScriptFUSION\StaticClass;
@@ -13,19 +12,19 @@ final class FixtureFactory
1312
/**
1413
* Builds ConnectionContexts with sane defaults for testing.
1514
*
16-
* @param CacheAdvice $cacheAdvice
15+
* @param bool $cacheAdvice
1716
* @param callable|null $fetchExceptionHandler
1817
* @param int $maxFetchAttempts
1918
*
2019
* @return ConnectionContext
2120
*/
2221
public static function buildConnectionContext(
23-
CacheAdvice $cacheAdvice = null,
22+
$cacheAdvice = false,
2423
callable $fetchExceptionHandler = null,
2524
$maxFetchAttempts = ImportSpecification::DEFAULT_FETCH_ATTEMPTS
2625
) {
2726
return new ConnectionContext(
28-
$cacheAdvice ?: CacheAdvice::SHOULD_NOT_CACHE(),
27+
$cacheAdvice,
2928
$fetchExceptionHandler ?: function () {
3029
// Intentionally empty.
3130
},

test/Integration/Porter/Connector/CachingConnectorTest.php

Lines changed: 80 additions & 73 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,11 @@
55
use Mockery\MockInterface;
66
use Psr\Cache\CacheItemInterface;
77
use Psr\Cache\CacheItemPoolInterface;
8-
use ScriptFUSION\Porter\Cache\CacheAdvice;
98
use ScriptFUSION\Porter\Cache\CacheKeyGenerator;
109
use ScriptFUSION\Porter\Cache\InvalidCacheKeyException;
11-
use ScriptFUSION\Porter\Cache\JsonCacheKeyGenerator;
12-
use ScriptFUSION\Porter\Cache\MemoryCache;
1310
use ScriptFUSION\Porter\Connector\CachingConnector;
1411
use ScriptFUSION\Porter\Connector\ConnectionContext;
12+
use ScriptFUSION\Porter\Connector\Connector;
1513
use ScriptFUSION\Porter\Options\EncapsulatedOptions;
1614
use ScriptFUSIONTest\FixtureFactory;
1715
use ScriptFUSIONTest\Stubs\TestOptions;
@@ -21,10 +19,15 @@ final class CachingConnectorTest extends \PHPUnit_Framework_TestCase
2119
use MockeryPHPUnitIntegration;
2220

2321
/**
24-
* @var CachingConnector|MockInterface $connector
22+
* @var CachingConnector $connector
2523
*/
2624
private $connector;
2725

26+
/**
27+
* @var Connector|MockInterface
28+
*/
29+
private $wrappedConnector;
30+
2831
/**
2932
* @var ConnectionContext
3033
*/
@@ -37,143 +40,147 @@ final class CachingConnectorTest extends \PHPUnit_Framework_TestCase
3740

3841
protected function setUp()
3942
{
40-
$this->connector = \Mockery::mock(CachingConnector::class, [])->makePartial()
41-
->shouldReceive('fetchFreshData')
42-
->andReturn('foo', 'bar')
43-
->getMock();
43+
$this->connector = new CachingConnector(
44+
$this->wrappedConnector = \Mockery::mock(Connector::class)
45+
->shouldReceive('fetch')
46+
->andReturn('foo', 'bar')
47+
->getMock()
48+
);
4449

45-
$this->context = FixtureFactory::buildConnectionContext(CacheAdvice::SHOULD_CACHE());
50+
$this->context = FixtureFactory::buildConnectionContext(true);
4651

4752
$this->options = new TestOptions;
4853
}
4954

55+
/**
56+
* Tests that when cache is enabled, the same result is returned because the wrapped connector is bypassed.
57+
*/
5058
public function testCacheEnabled()
5159
{
5260
self::assertSame('foo', $this->connector->fetch($this->context, 'baz', $this->options));
5361
self::assertSame('foo', $this->connector->fetch($this->context, 'baz', $this->options));
5462
}
5563

64+
/**
65+
* Tests that when cache is disabled, different results are returned from the wrapped connector.
66+
*/
5667
public function testCacheDisabled()
5768
{
58-
$context = FixtureFactory::buildConnectionContext(CacheAdvice::SHOULD_NOT_CACHE());
69+
// The default connection context has caching disabled.
70+
$context = FixtureFactory::buildConnectionContext();
5971

6072
self::assertSame('foo', $this->connector->fetch($context, 'baz', $this->options));
6173
self::assertSame('bar', $this->connector->fetch($context, 'baz', $this->options));
6274
}
6375

64-
public function testCacheAvailable()
76+
/**
77+
* Tests that when sources are the same but options are different, the cache is not reused.
78+
*/
79+
public function testCacheBypassedForDifferentOptions()
6580
{
66-
self::assertTrue($this->connector->isCacheAvailable());
81+
self::assertSame('foo', $this->connector->fetch($this->context, 'baz', $this->options));
82+
83+
$this->options->setFoo('bar');
84+
self::assertSame('bar', $this->connector->fetch($this->context, 'baz', $this->options));
6785
}
6886

69-
public function testGetSetCache()
87+
/**
88+
* Tests that when the same options are specified by two different object instances, the cache is reused.
89+
*/
90+
public function testCacheUsedForDifferentOptionsInstance()
7091
{
71-
self::assertInstanceOf(CacheItemPoolInterface::class, $this->connector->getCache());
72-
self::assertNotSame($cache = new MemoryCache, $this->connector->getCache());
73-
74-
$this->connector->setCache($cache);
75-
self::assertSame($cache, $this->connector->getCache());
92+
self::assertSame('foo', $this->connector->fetch($this->context, 'baz', $this->options));
93+
self::assertSame('foo', $this->connector->fetch($this->context, 'baz', clone $this->options));
7694
}
7795

78-
public function testGetSetCacheKeyGenerator()
96+
public function testNullAndEmptyOptionsAreEquivalent()
7997
{
80-
self::assertInstanceOf(CacheKeyGenerator::class, $this->connector->getCacheKeyGenerator());
81-
self::assertNotSame($cacheKeyGenerator = new JsonCacheKeyGenerator, $this->connector->getCacheKeyGenerator());
98+
/** @var EncapsulatedOptions $options */
99+
$options = \Mockery::mock(EncapsulatedOptions::class)->shouldReceive('copy')->andReturn([])->getMock();
82100

83-
$this->connector->setCacheKeyGenerator($cacheKeyGenerator);
84-
self::assertSame($cacheKeyGenerator, $this->connector->getCacheKeyGenerator());
101+
self::assertEmpty($options->copy());
102+
self::assertSame('foo', $this->connector->fetch($this->context, 'baz', $options));
103+
self::assertSame('foo', $this->connector->fetch($this->context, 'baz'));
85104
}
86105

87-
public function testCacheBypassedForDifferentOptions()
106+
/**
107+
* Tests that the default cache key generator does not output reserved characters even when comprised of options
108+
* containing them.
109+
*/
110+
public function testCacheKeyExcludesReservedCharacters()
88111
{
89-
self::assertSame('foo', $this->connector->fetch($this->context, 'baz', $this->options));
112+
$reservedCharacters = CacheKeyGenerator::RESERVED_CHARACTERS;
90113

91-
$this->options->setFoo('bar');
92-
self::assertSame('bar', $this->connector->fetch($this->context, 'baz', $this->options));
93-
}
114+
$connector = $this->createConnector($cache = \Mockery::spy(CacheItemPoolInterface::class));
94115

95-
public function testCacheUsedForDifferentOptionsInstance()
96-
{
97-
self::assertSame('foo', $this->connector->fetch($this->context, 'baz', $this->options));
98-
self::assertSame('foo', $this->connector->fetch($this->context, 'baz', clone $this->options));
116+
$cache->shouldReceive('hasItem')
117+
->andReturnUsing(
118+
function ($key) use ($reservedCharacters) {
119+
foreach (str_split($reservedCharacters) as $reservedCharacter) {
120+
self::assertNotContains($reservedCharacter, $key);
121+
}
122+
}
123+
)->once()
124+
->shouldReceive('getItem')->andReturnSelf()
125+
->shouldReceive('set')->andReturn(\Mockery::mock(CacheItemInterface::class))
126+
;
127+
128+
$connector->fetch($this->context, $reservedCharacters, (new TestOptions)->setFoo($reservedCharacters));
99129
}
100130

101131
/**
102-
* Tests that when the cache key generator returns the same hash the same data is fetched, and when it does not,
132+
* Tests that when the cache key generator returns the same key the same data is fetched, and when it does not,
103133
* fresh data is fetched.
104134
*/
105135
public function testCacheKeyGenerator()
106136
{
107-
$this->connector->setCacheKeyGenerator(
137+
$connector = $this->createConnector(
138+
null,
108139
\Mockery::mock(CacheKeyGenerator::class)
109140
->shouldReceive('generateCacheKey')
110141
->with($source = 'baz', $this->options->copy())
111142
->andReturn('qux', 'qux', 'quux')
112143
->getMock()
113144
);
114145

115-
self::assertSame('foo', $this->connector->fetch($this->context, $source, $this->options));
116-
self::assertSame('foo', $this->connector->fetch($this->context, $source, $this->options));
117-
self::assertSame('bar', $this->connector->fetch($this->context, $source, $this->options));
146+
self::assertSame('foo', $connector->fetch($this->context, $source, $this->options));
147+
self::assertSame('foo', $connector->fetch($this->context, $source, $this->options));
148+
self::assertSame('bar', $connector->fetch($this->context, $source, $this->options));
118149
}
119150

151+
/**
152+
* TODO: Remove when PHP 5 support dropped.
153+
*/
120154
public function testFetchThrowsInvalidCacheKeyExceptionOnNonStringCacheKey()
121155
{
122-
$this->connector->setCacheKeyGenerator(
156+
$connector = $this->createConnector(
157+
null,
123158
\Mockery::mock(CacheKeyGenerator::class)
124159
->shouldReceive('generateCacheKey')
125160
->andReturn(1)
126161
->getMock()
127162
);
128163

129164
$this->setExpectedException(InvalidCacheKeyException::class, 'Cache key must be a string.');
130-
$this->connector->fetch($this->context, 'baz', $this->options);
165+
$connector->fetch($this->context, 'baz', $this->options);
131166
}
132167

133168
public function testFetchThrowsInvalidCacheKeyExceptionOnNonPSR6CompliantCacheKey()
134169
{
135-
$this->connector->setCacheKeyGenerator(
170+
$connector = $this->createConnector(
171+
null,
136172
\Mockery::mock(CacheKeyGenerator::class)
137173
->shouldReceive('generateCacheKey')
138-
->andReturn(CachingConnector::RESERVED_CHARACTERS)
174+
->andReturn(CacheKeyGenerator::RESERVED_CHARACTERS)
139175
->getMock()
140176
);
141177

142178
$this->setExpectedException(InvalidCacheKeyException::class, 'contains one or more reserved characters');
143-
$this->connector->fetch($this->context, 'baz', $this->options);
144-
}
145-
146-
public function testNullAndEmptyOptionsAreEquivalent()
147-
{
148-
/** @var EncapsulatedOptions $options */
149-
$options = \Mockery::mock(EncapsulatedOptions::class)->shouldReceive('copy')->andReturn([])->getMock();
150-
151-
self::assertEmpty($options->copy());
152-
self::assertSame('foo', $this->connector->fetch($this->context, 'baz', $options));
153-
self::assertSame('foo', $this->connector->fetch($this->context, 'baz'));
179+
$connector->fetch($this->context, 'baz', $this->options);
154180
}
155181

156-
/**
157-
* Tests that the default cache key generator does not output reserved characters even when comprised of options
158-
* containing them.
159-
*/
160-
public function testCacheKeyExcludesReservedCharacters()
182+
private function createConnector(MockInterface $cache = null, MockInterface $cacheKeyGenerator = null)
161183
{
162-
$reservedCharacters = CachingConnector::RESERVED_CHARACTERS;
163-
164-
$this->connector->setCache($cache = \Mockery::spy(CacheItemPoolInterface::class));
165-
166-
$cache->shouldReceive('hasItem')
167-
->andReturnUsing(
168-
function ($key) use ($reservedCharacters) {
169-
foreach (str_split($reservedCharacters) as $reservedCharacter) {
170-
self::assertNotContains($reservedCharacter, $key);
171-
}
172-
}
173-
)->once()
174-
->shouldReceive('getItem')->andReturnSelf()
175-
->shouldReceive('set')->andReturn(\Mockery::mock(CacheItemInterface::class));
176-
177-
$this->connector->fetch($this->context, $reservedCharacters, (new TestOptions)->setFoo($reservedCharacters));
184+
return new CachingConnector($this->wrappedConnector, $cache, $cacheKeyGenerator);
178185
}
179186
}

test/Integration/Porter/PorterTest.php

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,15 @@
44
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration;
55
use Mockery\MockInterface;
66
use Psr\Container\ContainerInterface;
7-
use ScriptFUSION\Porter\Cache\CacheAdvice;
87
use ScriptFUSION\Porter\Cache\CacheUnavailableException;
98
use ScriptFUSION\Porter\Collection\FilteredRecords;
109
use ScriptFUSION\Porter\Collection\PorterRecords;
1110
use ScriptFUSION\Porter\Collection\ProviderRecords;
1211
use ScriptFUSION\Porter\Collection\RecordCollection;
1312
use ScriptFUSION\Porter\Connector\ConnectionContext;
1413
use ScriptFUSION\Porter\Connector\Connector;
15-
use ScriptFUSION\Porter\Connector\RecoverableConnectorException;
1614
use ScriptFUSION\Porter\Connector\ImportConnector;
15+
use ScriptFUSION\Porter\Connector\RecoverableConnectorException;
1716
use ScriptFUSION\Porter\ImportException;
1817
use ScriptFUSION\Porter\Options\EncapsulatedOptions;
1918
use ScriptFUSION\Porter\Porter;
@@ -344,11 +343,14 @@ public function testFilter()
344343
self::assertNotSame($previous->getFilter(), $filter, 'Filter was not cloned.');
345344
}
346345

346+
/**
347+
* Tests that when caching is required but a caching facility is unavailable, an exception is thrown.
348+
*/
347349
public function testCacheUnavailable()
348350
{
349351
$this->setExpectedException(CacheUnavailableException::class);
350352

351-
$this->porter->import($this->specification->setCache(CacheAdvice::MUST_CACHE()));
353+
$this->porter->import($this->specification->enableCache());
352354
}
353355

354356
/**

test/Unit/Porter/Connector/SuperConnectorTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
use Mockery\MockInterface;
55
use ScriptFUSION\Porter\Cache\Cache;
66
use ScriptFUSION\Porter\Cache\CacheAdvice;
7-
use ScriptFUSION\Porter\Cache\CacheUnavailableException;
87
use ScriptFUSION\Porter\Connector\Connector;
98
use ScriptFUSION\Porter\Connector\ImportConnector;
109
use ScriptFUSIONTest\FixtureFactory;

test/Unit/Porter/ImportSpecificationTest.php

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
<?php
22
namespace ScriptFUSIONTest\Unit\Porter;
33

4-
use ScriptFUSION\Porter\Cache\CacheAdvice;
54
use ScriptFUSION\Porter\Provider\Resource\ProviderResource;
65
use ScriptFUSION\Porter\Specification\DuplicateTransformerException;
76
use ScriptFUSION\Porter\Specification\ImportSpecification;
@@ -100,12 +99,12 @@ public function testContext()
10099
self::assertSame($context = 'foo', $this->specification->setContext($context)->getContext());
101100
}
102101

103-
public function testCacheAdvice()
102+
public function testCache()
104103
{
105-
self::assertSame(
106-
$advice = CacheAdvice::MUST_CACHE(),
107-
$this->specification->setCacheAdvice($advice)->getCacheAdvice()
108-
);
104+
self::assertFalse($this->specification->mustCache());
105+
106+
$this->specification->enableCache();
107+
self::assertTrue($this->specification->mustCache());
109108
}
110109

111110
/**

0 commit comments

Comments
 (0)