Skip to content

Commit

Permalink
Fix config dumper options to work with default value.
Browse files Browse the repository at this point in the history
  • Loading branch information
lennerd committed Oct 24, 2017
1 parent 1046018 commit 9a86b91
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 26 deletions.
16 changes: 7 additions & 9 deletions BotDetector.php
Expand Up @@ -45,7 +45,7 @@ public function setOptions(array $options)
'cache_dir' => null,
'debug' => false,
'metadata_cache_file' => 'project_vipx_bot_detect_metadata.php',
'metadata_dumper_class' => 'Vipx\\BotDetect\\Metadata\\Dumper\\MetadataDumper',
'metadata_dumper_class' => 'Vipx\\BotDetect\\Metadata\\Dumper\\PHPMetadataDumper',
);

// check option names and live merge, if errors are encountered Exception will be thrown
Expand Down Expand Up @@ -83,30 +83,28 @@ public function getMetadatas()
}

if (null === $this->options['cache_dir'] || null === $this->options['metadata_cache_file']) {
return $this->metadatas = $this->loader->load($this->resource);
$metadataCollection = $this->loader->load($this->resource);

return $this->metadatas = $metadataCollection->getMetadatas();
}

$cache = new ConfigCache($this->options['cache_dir'] . '/' . $this->options['metadata_cache_file'], $this->options['debug']);

if ($cache->isFresh()) {
if (!method_exists($cache, 'getPath')) {
// ConfigCache Symfony < 2.7 syntax.
return $this->metadatas = require $cache;
}

return $this->metadatas = require $cache->getPath();
}

/** @var $metadataCollection \Vipx\BotDetect\Metadata\MetadataCollection */
$metadataCollection = $this->loader->load($this->resource);
$dumperClass = $this->options['metadata_dumper_class'];
$metadatas = $metadataCollection->getMetadatas();

/** @var $dumper \Vipx\BotDetect\Metadata\Dumper\MetadataDumper */
$dumper = new $dumperClass($metadataCollection->getMetadatas());
$dumper = new $dumperClass($metadatas);

$cache->write($dumper->dump(), $metadataCollection->getResources());

return $this->metadatas = $metadataCollection;
return $this->metadatas = $metadatas;
}

/**
Expand Down
74 changes: 58 additions & 16 deletions Tests/BotDetectorTest.php
Expand Up @@ -15,14 +15,17 @@
use Vipx\BotDetect\BotDetector;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Config\Resource\FileResource;
use Vipx\BotDetect\Metadata\Loader\YamlFileLoader;
use Vipx\BotDetect\Metadata\MetadataInterface;
use Vipx\BotDetect\Metadata\Metadata;
use Vipx\BotDetect\Metadata\MetadataCollection;

class BotDetectorTest extends TestCase
{

private $metadatas;
private $metadataCollection;
private $loader;
private $cacheFile;

/**
* @expectedException \InvalidArgumentException
Expand Down Expand Up @@ -50,21 +53,54 @@ public function testValidOptions()
$this->assertTrue($options['debug']);
}

public function testMetadata()
{
$detector = $this->createDetector();
$metadataCollection = $this->getMetadataCollection();

$this->assertEquals($metadataCollection->getMetadatas(), $detector->getMetadatas());
}

public function testCacheOptions()
{
$cacheFile = tempnam(sys_get_temp_dir(), 'vipx_bot_detect_test_metadata_');

$options = array(
'debug' => true,
'cache_dir' => dirname($cacheFile),
'metadata_cache_file' => basename($cacheFile),
);

$metadataCollection = $this->getMetadataCollection();

$metadataCollection->expects($this->any())
->method('getResources')
->willReturn(array(
new FileResource($cacheFile),
));

$detector = $this->createDetector();
$detector->setOptions($options);

$this->assertEquals(require $cacheFile, $detector->getMetadatas());
try {
unlink($cacheFile);

$this->assertNotNull($detector->detect('Googlebot', '127.0.0.1'));
$this->assertTrue(\file_exists($cacheFile));

$cachedDetector = $this->createDetector();
$cachedDetector->setOptions($options);

unlink($cacheFile);
$this->assertNotNull($cachedDetector->detect('Googlebot', '127.0.0.1'));
} finally {
$files = array($cacheFile, $cacheFile . '.meta');

foreach ($files as $file) {
if (file_exists($file)) {
unlink($file);
}
}
}
}

public function testDetection()
Expand Down Expand Up @@ -94,32 +130,38 @@ private function getLoader()

$loader->expects($this->any())
->method('load')
->will($this->returnValue($this->getMetadatas()));
->will($this->returnValue($this->getMetadataCollection()));

$this->loader = $loader;
}

return $this->loader;
}

private function getMetadatas()
private function getMetadataCollection()
{
if (null === $this->metadatas) {
$googleBot = $this->getMockBuilder(MetadataInterface::class)
if (null === $this->metadataCollection) {
$googleBot = new Metadata('Googlebot', 'Googlebot', '127.0.0.1');

$metadatas = array(
$googleBot,
);

$this->metadataCollection = $this->getMockBuilder(MetadataCollection::class)
->getMock();

$googleBot->expects($this->any())
->method('match')
->will($this->returnCallback(function($agent, $ip) {
return $agent == 'Googlebot' && $ip === '127.0.0.1';
$this->metadataCollection->expects($this->any())
->method('getIterator')
->will($this->returnCallback(function() use ($metadatas) {
return new \ArrayIterator($metadatas);
}));

$this->metadatas = array(
$googleBot,
);
$this->metadataCollection->expects($this->any())
->method('getMetadatas')
->willReturn($metadatas);
}

return $this->metadatas;
return $this->metadataCollection;
}

}
2 changes: 1 addition & 1 deletion composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9a86b91

Please sign in to comment.