Skip to content

Commit d4fb6c5

Browse files
committed
Refactor ClassBuilder Tests
Changed: - Improved routine in `CacheBuilder::resolveOneDriver()` - Replaced `expectException()` methods with `@expectedException` comment tags - Improved unit tests Fixed: - Comments for `CacheBuilder`
1 parent 3908f78 commit d4fb6c5

File tree

8 files changed

+361
-155
lines changed

8 files changed

+361
-155
lines changed

src/Charcoal/Cache/CacheBuilder.php

Lines changed: 39 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,9 @@
1313
use Stash\Pool;
1414

1515
/**
16-
* Model Loader Builder.
16+
* Cache Pool Builder
1717
*
18-
* Build custom ModelLoader objects with a certain obj type / optional obj key.
18+
* Build custom PSR-6 cache pools using Stash drivers.
1919
*/
2020
final class CacheBuilder
2121
{
@@ -27,7 +27,7 @@ final class CacheBuilder
2727
private $drivers;
2828

2929
/**
30-
* Defaultlogger instance.
30+
* Default logger instance.
3131
*
3232
* @var \Psr\Log\LoggerInterface|null
3333
*/
@@ -207,7 +207,7 @@ private function resolveDriver($driver)
207207
*
208208
* @param mixed $driver The name of a registered cache driver,
209209
* the class name or instance of a {@see DriverInterface cache driver}.
210-
* @throws InvalidArgumentException When passed invalid or nonexistant driver name, class name, or object.
210+
* @throws InvalidArgumentException When passed an invalid or nonexistant driver name, class name, or object.
211211
* @return DriverInterface
212212
*/
213213
private function resolveOneDriver($driver)
@@ -218,34 +218,49 @@ private function resolveOneDriver($driver)
218218
);
219219
}
220220

221-
$isObj = is_object($driver);
221+
if (is_object($driver)) {
222+
if ($driver instanceof DriverInterface) {
223+
return $driver;
224+
} else {
225+
throw new InvalidArgumentException(sprintf(
226+
'Driver class %s must implement %s',
227+
get_class($driver),
228+
DriverInterface::class
229+
));
230+
}
231+
}
232+
233+
$name = $driver;
234+
if (isset($this->drivers[$name])) {
235+
$driver = $this->drivers[$name];
236+
237+
if (empty($driver)) {
238+
throw new InvalidArgumentException(
239+
sprintf('Driver "%s" does not exist', $name)
240+
);
241+
}
222242

223-
if (!$isObj) {
224-
$name = $driver;
225-
if (isset($this->drivers[$name])) {
226-
$driver = $this->drivers[$name];
227-
if (empty($driver)) {
243+
if (is_object($driver)) {
244+
if ($driver instanceof DriverInterface) {
245+
return $driver;
246+
} else {
228247
throw new InvalidArgumentException(sprintf(
229-
'Driver "%s" does not exist',
230-
$name
248+
'Driver "%s": Class %s must implement %s',
249+
$name,
250+
get_class($driver),
251+
DriverInterface::class
231252
));
232253
}
233-
$isObj = is_object($driver);
234254
}
235255
}
236256

237257
if (is_a($driver, DriverInterface::class, true)) {
238-
return $isObj ? $driver : new $driver();
258+
return new $driver();
239259
}
240260

241-
throw new InvalidArgumentException(sprintf(
242-
'Unsupported driver "%s"',
243-
(is_object($driver)
244-
? get_class($driver)
245-
: (is_string($driver)
246-
? $driver
247-
: gettype($driver)))
248-
));
261+
throw new InvalidArgumentException(
262+
sprintf('Driver "%s" cannot be resolved', $name)
263+
);
249264
}
250265

251266
/**
@@ -311,7 +326,7 @@ private function setNamespace($namespace)
311326
* Using this function developers can have the builder generate custom Pool objects.
312327
*
313328
* @param string $class The pool class name.
314-
* @throws InvalidArgumentException When passed invalid or nonexistant class.
329+
* @throws InvalidArgumentException When passed an invalid or nonexistant class.
315330
* @return void
316331
*/
317332
private function setPoolClass($class)
@@ -341,7 +356,7 @@ private function setPoolClass($class)
341356
* Using this function developers can have the pool class generate custom Item objects.
342357
*
343358
* @param string $class The item class name.
344-
* @throws InvalidArgumentException When passed invalid or nonexistant class.
359+
* @throws InvalidArgumentException When passed an invalid or nonexistant class.
345360
* @return void
346361
*/
347362
private function setItemClass($class)

src/Charcoal/Cache/CacheConfig.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ public function setDefaultTtl($ttl)
180180
{
181181
if (!is_numeric($ttl)) {
182182
throw new InvalidArgumentException(
183-
'TTL must be an integer (seconds).'
183+
'TTL must be an integer (seconds)'
184184
);
185185
}
186186

@@ -209,14 +209,14 @@ public function setPrefix($prefix)
209209
{
210210
if (!is_string($prefix)) {
211211
throw new InvalidArgumentException(
212-
'Prefix must be a string.'
212+
'Prefix must be a string'
213213
);
214214
}
215215

216216
/** @see \Stash\Pool\::setNamespace */
217217
if (!ctype_alnum($prefix)) {
218218
throw new InvalidArgumentException(
219-
'Prefix must be alphanumeric.'
219+
'Prefix must be alphanumeric'
220220
);
221221
}
222222

tests/Charcoal/Cache/CacheConfigTest.php

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ public function testActive()
8686
* @covers ::setTypes
8787
* @covers ::types
8888
*/
89-
public function testReplaceTypes()
89+
public function testReplaceDrivers()
9090
{
9191
// Chainable
9292
$that = $this->cfg->setTypes([ 'memcache', 'noop' ]);
@@ -104,7 +104,7 @@ public function testReplaceTypes()
104104
* @covers ::addType
105105
* @covers ::types
106106
*/
107-
public function testAddTypes()
107+
public function testAddDrivers()
108108
{
109109
// Chainable
110110
$that = $this->cfg->addTypes([ 'memcache', 'noop' ]);
@@ -118,13 +118,14 @@ public function testAddTypes()
118118
}
119119

120120
/**
121+
* @expectedException InvalidArgumentException
122+
* @expectedExceptionMessage Invalid cache type: "foobar"
123+
*
121124
* @covers ::validTypes
122125
* @covers ::addType
123126
*/
124-
public function testUnsupportedType()
127+
public function testAddDriverOnInvalidType()
125128
{
126-
// Bad Value
127-
$this->expectException(InvalidArgumentException::class);
128129
$this->cfg->addType('foobar');
129130
}
130131

@@ -140,37 +141,52 @@ public function testDefaultTtl()
140141

141142
// Mutated State
142143
$this->assertEquals(42, $this->cfg->defaultTtl());
144+
}
143145

144-
// Bad Value
145-
$this->expectException(InvalidArgumentException::class);
146+
/**
147+
* @expectedException InvalidArgumentException
148+
* @expectedExceptionMessage TTL must be an integer (seconds)
149+
*
150+
* @covers ::setDefaultTtl
151+
*/
152+
public function testSetDefaultTtlOnInvalidType()
153+
{
146154
$this->cfg->setDefaultTtl('foo');
147155
}
148156

149157
/**
150158
* @covers ::setPrefix
151159
* @covers ::prefix
152160
*/
153-
public function testPrefix1()
161+
public function testPrefix()
154162
{
155163
// Chainable
156164
$that = $this->cfg->setPrefix('foo');
157165
$this->assertSame($this->cfg, $that);
158166

159167
// Mutated State
160168
$this->assertEquals('foo', $this->cfg->prefix());
169+
}
161170

162-
// Bad Value
163-
$this->expectException(InvalidArgumentException::class);
171+
/**
172+
* @expectedException InvalidArgumentException
173+
* @expectedExceptionMessage Prefix must be a string
174+
*
175+
* @covers ::setPrefix
176+
*/
177+
public function testSetPrefixOnInvalidType()
178+
{
164179
$this->cfg->setPrefix(false);
165180
}
166181

167182
/**
183+
* @expectedException InvalidArgumentException
184+
* @expectedExceptionMessage Prefix must be alphanumeric
185+
*
168186
* @covers ::setPrefix
169187
*/
170-
public function testPrefix2()
188+
public function testSetPrefixOnInvalidValue()
171189
{
172-
// Bad Value
173-
$this->expectException(InvalidArgumentException::class);
174190
$this->cfg->setPrefix('foo!#$bar');
175191
}
176192
}

tests/Charcoal/Cache/CachePoolAwareTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,13 @@ public function testCachePool()
3030
}
3131

3232
/**
33+
* @expectedException RuntimeException
34+
* @expectedExceptionMessage Cache Pool is not defined for "Charcoal\Tests\Mocks\CachePoolAware"
35+
*
3336
* @covers ::cachePool
3437
*/
3538
public function testMissingPool()
3639
{
37-
$this->expectException(\RuntimeException::class);
38-
3940
$obj = new CachePoolAware();
4041
$obj->cachePool();
4142
}

tests/Charcoal/Cache/Factory/AbstractCacheBuilderTest.php

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,19 @@ public function createBuilder(array $args = [])
3232
return new CacheBuilder($args);
3333
}
3434

35+
/**
36+
* Create a new cache driver for a specific driver name.
37+
*
38+
* @param string $name Cache driver name.
39+
* @param array $options Cache driver options.
40+
* @return DriverInterface
41+
*/
42+
public function createDriver($name, array $options = [])
43+
{
44+
$class = DriverList::getDriverClass($name);
45+
return new $class($options);
46+
}
47+
3548
/**
3649
* Returns a list of cache drivers that are also supported by this system.
3750
*
@@ -57,4 +70,77 @@ public function getDriverClassNames()
5770
unset($drivers['Composite']);
5871
return $drivers;
5972
}
73+
74+
/**
75+
* Create a new cache driver for a specific driver name.
76+
*
77+
* @param string $name Cache driver name.
78+
* @return string
79+
*/
80+
public function getDriverClass($name)
81+
{
82+
return DriverList::getDriverClass($name);
83+
}
84+
85+
/**
86+
* Returns the default attributes of the CacheBuilder.
87+
*
88+
* @return array
89+
*/
90+
public function getDefaultBuilderAttributes()
91+
{
92+
return [
93+
'pool_class' => Pool::class,
94+
'item_class' => null,
95+
'namespace' => null,
96+
'logger' => null,
97+
];
98+
}
99+
100+
/**
101+
* Returns the default attributes of the Stash Pool.
102+
*
103+
* @return array
104+
*/
105+
public function getDefaultPoolAttributes()
106+
{
107+
return [
108+
'item_class' => '\Stash\Item',
109+
'namespace' => null,
110+
'logger' => null,
111+
];
112+
}
113+
114+
/**
115+
* Reports an error if $builder does not use the default options.
116+
*
117+
* @param CacheBuilder $builder The cache builder to test.
118+
* @return void
119+
*/
120+
public function assertCacheBuilderHasDefaultAttributes(CacheBuilder $builder)
121+
{
122+
$builderDefaults = $this->getDefaultBuilderAttributes();
123+
124+
$this->assertAttributeEquals($builderDefaults['pool_class'], 'poolClass', $builder);
125+
$this->assertAttributeEquals($builderDefaults['item_class'], 'itemClass', $builder);
126+
$this->assertAttributeEquals($builderDefaults['namespace'], 'namespace', $builder);
127+
$this->assertAttributeEquals($builderDefaults['logger'], 'logger', $builder);
128+
}
129+
130+
/**
131+
* Reports an error if $pool does not use the default options.
132+
*
133+
* @param PoolInterface $pool The cache pool to test.
134+
* @return void
135+
*/
136+
public function assertCachePoolHasDefaultAttributes(PoolInterface $pool)
137+
{
138+
$builderDefaults = $this->getDefaultBuilderAttributes();
139+
$poolDefaults = $this->getDefaultPoolAttributes();
140+
141+
$this->assertInstanceOf($builderDefaults['pool_class'], $pool);
142+
$this->assertAttributeEquals($poolDefaults['item_class'], 'itemClass', $pool);
143+
$this->assertAttributeEquals($poolDefaults['namespace'], 'namespace', $pool);
144+
$this->assertAttributeEquals($poolDefaults['logger'], 'logger', $pool);
145+
}
60146
}

0 commit comments

Comments
 (0)