Skip to content

Commit

Permalink
Merge pull request #10 from michaeljoseph/adds-where-support-to-matched
Browse files Browse the repository at this point in the history
Adds where support to matched sampler
  • Loading branch information
michaeljoseph authored Sep 21, 2018
2 parents b2b49dd + e2d5915 commit 5d8b330
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/Sampler/Matched.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ class Matched extends BaseSampler
* @var array
*/
protected $constraints;
/**
* @var array
*/
private $where;

/**
* Return a unique name for this sampler for informational purposes
Expand All @@ -52,6 +56,7 @@ public function loadConfig($config)
{
parent::loadConfig($config);
$this->constraints = (array)$this->demandParameterValue($config, 'constraints');
$this->where = $config->where ?? [];
}

/**
Expand Down Expand Up @@ -94,6 +99,10 @@ public function getRows()
}
}

foreach ($this->where as $where) {
$queryBuilder->andWhere($where);
}

if ($this->limit) {
$queryBuilder->setMaxResults($this->limit);
}
Expand Down
18 changes: 18 additions & 0 deletions tests/SamplerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use Quidco\DbSampler\ReferenceStore;
use Quidco\DbSampler\Sampler\CopyAll;
use Quidco\DbSampler\Sampler\CopyEmpty;
use Quidco\DbSampler\Sampler\Matched;

class SamplerTest extends SqliteBasedTestCase
{
Expand Down Expand Up @@ -35,4 +36,21 @@ public function testCopyAllWithReferenceStore()

$this->assertCount(4, $referenceStore->getReferencesByName('fruit_ids'));
}

public function testMatchedWithWhereClause()
{
$sampler = new Matched();
$sampler->setTableName('fruit_x_basket');
$sampler->setSourceConnection($this->sourceConnection);
$sampler->setDestConnection($this->destConnection);

$config = [
'constraints' => ['fruit_id' => 1],
'where' => ['basket_id > 1']
];
$sampler->loadConfig((object)$config);
$sampler->execute();

$this->assertCount(2, $sampler->getRows());
}
}

0 comments on commit 5d8b330

Please sign in to comment.