Commit ae1f599d authored by alexpott's avatar alexpott

Issue #2684567 by gnuget, chx, heddn, vasi, mikeryan: Requiring a migration w/...

Issue #2684567 by gnuget, chx, heddn, vasi, mikeryan: Requiring a migration w/ a source plugin using a generator fatals
parent d70e577a
......@@ -141,10 +141,12 @@ public function __construct(array $configuration, $plugin_id, $plugin_definition
$this->migration = $migration;
// Set up some defaults based on the source configuration.
$this->cacheCounts = !empty($configuration['cache_counts']);
$this->skipCount = !empty($configuration['skip_count']);
foreach (['cacheCounts' => 'cache_counts', 'skipCount' => 'skip_count', 'trackChanges' => 'track_changes'] as $property => $config_key) {
if (isset($configuration[$config_key])) {
$this->$property = (bool) $configuration[$config_key];
}
}
$this->cacheKey = !empty($configuration['cache_key']) ? $configuration['cache_key'] : NULL;
$this->trackChanges = !empty($configuration['track_changes']) ? $configuration['track_changes'] : FALSE;
$this->idMap = $this->migration->getIdMap();
// Pull out the current highwater mark if we have a highwater property.
......@@ -384,7 +386,7 @@ public function count($refresh = FALSE) {
// If a refresh is requested, or we're not caching counts, ask the derived
// class to get the count from the source.
if ($refresh || !$this->cacheCounts) {
$count = $this->getIterator()->count();
$count = $this->doCount();
$this->getCache()->set($this->cacheKey, $count);
}
else {
......@@ -397,7 +399,7 @@ public function count($refresh = FALSE) {
else {
// No cached count, ask the derived class to count 'em up, and cache
// the result.
$count = $this->getIterator()->count();
$count = $this->doCount();
$this->getCache()->set($this->cacheKey, $count);
}
}
......@@ -417,4 +419,15 @@ protected function getCache() {
return $this->cache;
}
/**
* Gets the source count checking if the source is countable or using the
* iterator_count function.
*
* @return int
*/
protected function doCount() {
$iterator = $this->getIterator();
return $iterator instanceof \Countable ? $iterator->count() : iterator_count($this->initializeIterator());
}
}
......@@ -168,6 +168,17 @@ public function testCount() {
// Test the skip argument.
$source = $this->getSource(['skip_count' => TRUE]);
$this->assertEquals(-1, $source->count());
$this->migrationConfiguration['id'] = 'test_migration';
$migration = $this->getMigration();
$source = new StubSourceGeneratorPlugin([], '', [], $migration);
// Test the skipCount property's default value.
$this->assertEquals(-1, $source->count());
// Test the count value using a generator.
$source = new StubSourceGeneratorPlugin(['skip_count' => FALSE], '', [], $migration);
$this->assertEquals(3, $source->count());
}
/**
......@@ -387,6 +398,21 @@ public function testPrepareRowPrepareException() {
$this->assertFalse($source->prepareRow($row));
}
/**
* Test that cacheCounts, skipCount, trackChanges preserve their default
* values.
*/
public function testDefaultPropertiesValues() {
$this->migrationConfiguration['id'] = 'test_migration';
$migration = $this->getMigration();
$source = new StubSourceGeneratorPlugin([], '', [], $migration);
// Test the default value of the skipCount Value;
$this->assertTrue($source->getSkipCount());
$this->assertTrue($source->getCacheCounts());
$this->assertTrue($source->getTrackChanges());
}
/**
* Gets a mock executable for the test.
*
......@@ -450,3 +476,61 @@ protected function initializeIterator() {
}
}
/**
* Stubbed source plugin with a generator as iterator. Also it overwrites the
* $skipCount, $cacheCounts and $trackChanges properties.
*/
class StubSourceGeneratorPlugin extends StubSourcePlugin {
/**
* {@inheritdoc}
*/
protected $skipCount = TRUE;
/**
* {@inheritdoc}
*/
protected $cacheCounts = TRUE;
/**
* {@inheritdoc}
*/
protected $trackChanges = TRUE;
/**
* Return the skipCount value.
*/
public function getSkipCount() {
return $this->skipCount;
}
/**
* Return the cacheCounts value.
*/
public function getCacheCounts() {
return $this->cacheCounts;
}
/**
* Return the trackChanges value.
*/
public function getTrackChanges() {
return $this->trackChanges;
}
/**
* {@inheritdoc}
*/
protected function initializeIterator() {
$data = [
['title' => 'foo'],
['title' => 'bar'],
['title' => 'iggy'],
];
foreach ($data as $row) {
yield $row;
}
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment