Skip to content

Commit

Permalink
Use RulerZ's behat context
Browse files Browse the repository at this point in the history
  • Loading branch information
K-Phoen committed Sep 28, 2018
1 parent afd4730 commit 24f56e3
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 133 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
"require": {
"php": ">=7.1",

"kphoen/rulerz": "~0.21",
"kphoen/rulerz": "dev-master as 1.0.0",

"doctrine/orm": "^2.4"
},
Expand Down
139 changes: 7 additions & 132 deletions features/bootstrap/Context.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,14 @@
declare(strict_types=1);

use Behat\Behat\Context\Context as BehatContext;
use Behat\Gherkin\Node\PyStringNode;
use Behat\Gherkin\Node\TableNode;
use RulerZ\Test\BaseContext;

class Context implements BehatContext
class Context extends BaseContext implements BehatContext
{
/** @var \RulerZ\RulerZ */
private $rulerz;

/** @var mixed */
private $dataset;

/** @var array */
private $parameters = [];

/** @var array */
private $executionContext = [];

/** @var mixed */
private $results;

/** @var \Doctrine\ORM\EntityManager */
private $entityManager;

public function __construct()
public function initialize()
{
$paths = [__DIR__.'/../../examples/entities']; // meh.
$isDevMode = true;
Expand All @@ -43,130 +27,21 @@ public function __construct()
}

/**
* Returns the compilation target to be tested.
* {@inheritdoc}
*/
private function getCompilationTarget(): \RulerZ\Compiler\CompilationTarget
protected function getCompilationTarget(): \RulerZ\Compiler\CompilationTarget
{
return new \RulerZ\DoctrineORM\Target\DoctrineORM();
}

/**
* Returns the default dataset to be filtered.
*
* @return mixed
* {@inheritdoc}
*/
private function getDefaultDataset()
protected function getDefaultDataset()
{
return $this->entityManager
->createQueryBuilder()
->select('p')
->from(Entity\Player::class, 'p');
}

/**
* Create a default execution context that will be given to RulerZ when
* filtering a dataset.
*
* @return mixed
*/
private function getDefaultExecutionContext()
{
return [];
}

/**
* @Given RulerZ is configured
*/
public function rulerzIsConfigured()
{
// compiler
$compiler = new \RulerZ\Compiler\Compiler(new \RulerZ\Compiler\EvalEvaluator());

// RulerZ engine
$this->rulerz = new \RulerZ\RulerZ($compiler, [$this->getCompilationTarget()]);
}

/**
* @When I define the parameters:
*/
public function iDefineTheParameters(TableNode $parameters)
{
// named parameters
if (count($parameters->getRow(0)) !== 1) {
$this->parameters = $parameters->getRowsHash();

return;
}

// positional parameters
$this->parameters = array_map(function ($row) {
return $row[0];
}, $parameters->getRows());
}

/**
* @When I use the default execution context
*/
public function iUseTheDefaultExecutionContext()
{
$this->executionContext = $this->getDefaultExecutionContext();
}

/**
* @When I use the default dataset
*/
public function iUseTheDefaultDataset()
{
$this->dataset = $this->getDefaultDataset();
}

/**
* @When I filter the dataset with the rule:
*/
public function iFilterTheDatasetWithTheRule(PyStringNode $rule)
{
$this->results = $this->rulerz->filter($this->dataset, (string) $rule, $this->parameters, $this->executionContext);

$this->parameters = [];
$this->executionContext = [];
}

/**
* @Then I should have the following results:
*/
public function iShouldHaveTheFollowingResults(TableNode $table)
{
$results = iterator_to_array($this->results);

if (count($table->getHash()) !== count($results)) {
throw new \RuntimeException(sprintf("Expected %d results, got %d. Expected:\n%s\nGot:\n%s", count($table->getHash()), count($results), $table, var_export($results, true)));
}

foreach ($table as $row) {
foreach ($results as $result) {
$value = $this->fieldFromResult($result, 'pseudo');

if ($value === $row['pseudo']) {
return;
}
}

throw new \RuntimeException(sprintf('Player "%s" not found in the results.', $row['pseudo']));
}
}

/**
* Fetches a field from a result.
*
* @param mixed $result
* @param string $field
*
* @return mixed
*/
private function fieldFromResult($result, $field)
{
$objectResult = is_array($result) ? (object) $result : $result;

return $objectResult->$field;
}
}

0 comments on commit 24f56e3

Please sign in to comment.