View source
<?php
namespace Drupal\Tests\apigee_edge\Unit;
use Apigee\Edge\Entity\EntityInterface;
use Drupal\apigee_edge\Entity\Query\Condition;
use Drupal\Core\Entity\Query\QueryInterface;
use Drupal\Tests\UnitTestCase;
class ConditionTest extends UnitTestCase {
protected $entityData = [];
protected $entities = [];
public function testEmptyCondition() {
$this
->assertFilters($this
->mockCondition(), function () {
return TRUE;
});
}
public function testSimpleEquality() {
$value = $this
->randomData()[0];
$cond = $this
->mockCondition();
$cond
->condition('id', $value);
$this
->assertFilters($cond, function (EntityInterface $item) use ($value) : bool {
return $item
->id() === $value;
});
}
public function testBool() {
$cond = $this
->mockCondition();
$cond
->condition('foo_baz', TRUE);
$this
->assertFilters($cond, function (EntityInterface $item) : bool {
return $item
->isFooBaz();
});
}
public function testIn() {
$values = [];
for ($i = 0; $i < 16; $i++) {
$values[] = $this
->randomData()[1];
}
$cond = $this
->mockCondition();
$cond
->condition('foo_bar', $values, 'IN');
$this
->assertFilters($cond, function (EntityInterface $item) use ($values) : bool {
$item_value = $item
->getFooBar();
foreach ($values as $value) {
if ($item_value === $value) {
return TRUE;
}
}
return FALSE;
});
}
public function testAnd() {
$data = $this
->randomData();
$value0 = $data[0];
$value1 = $data[1];
$cond = $this
->mockCondition();
$cond
->condition('id', $value0);
$cond
->condition('foo_bar', $value1);
$this
->assertFilters($cond, function (EntityInterface $item) use ($value0, $value1) : bool {
return $item
->id() == $value0 && $item
->getFooBar() == $value1;
});
}
public function testOr() {
$value0 = $this
->randomData()[0];
$value1 = $this
->randomData()[1];
$cond = $this
->mockCondition('OR');
$cond
->condition('id', $value0);
$cond
->condition('id', $value1);
$this
->assertFilters($cond, function (EntityInterface $item) use ($value0, $value1) : bool {
return $item
->id() == $value0 || $item
->id() == $value1;
});
}
public function testComplex() {
$data0 = $this
->randomData();
$data1 = $this
->randomData();
$value00 = $data0[0];
$value01 = $data0[1];
$value10 = $data1[0];
$value11 = $data1[1];
$cond0 = $this
->mockCondition();
$cond0
->condition('id', $value00);
$cond0
->condition('foo_bar', $value01);
$cond1 = $this
->mockCondition();
$cond1
->condition('id', $value10);
$cond1
->condition('foo_bar', $value11);
$cond = $this
->mockCondition('OR');
$cond
->condition($cond0);
$cond
->condition($cond1);
$this
->assertFilters($cond, function (EntityInterface $item) use ($value00, $value01, $value10, $value11) : bool {
$id = $item
->id();
$foobar = $item
->getFooBar();
return $id == $value00 && $foobar == $value01 || $id == $value10 && $foobar == $value11;
});
}
protected function randomData() : array {
return $this->entityData[mt_rand(0, count($this->entityData) - 1)];
}
protected function mockCondition($conjunction = 'AND') : Condition {
return new Condition($conjunction, new class implements QueryInterface {
public function addTag($tag) {
}
public function hasTag($tag) {
}
public function hasAllTags() {
}
public function hasAnyTag() {
}
public function addMetaData($key, $object) {
}
public function getMetaData($key) {
}
public function getEntityTypeId() {
}
public function condition($field, $value = NULL, $operator = NULL, $langcode = NULL) {
}
public function exists($field, $langcode = NULL) {
}
public function notExists($field, $langcode = NULL) {
}
public function pager($limit = 10, $element = NULL) {
}
public function range($start = NULL, $length = NULL) {
}
public function sort($field, $direction = 'ASC', $langcode = NULL) {
}
public function count() {
}
public function tableSort(&$headers) {
}
public function accessCheck($access_check = TRUE) {
}
public function execute() {
}
public function andConditionGroup() {
}
public function orConditionGroup() {
}
public function currentRevision() {
}
public function allRevisions() {
}
public function latestRevision() {
}
});
}
protected function assertFilters(Condition $condition, callable $filter) : void {
$expected = array_filter($this->entities, $filter);
$actual = array_filter($this->entities, $condition
->compile(NULL));
$this
->assertEquals($expected, $actual);
}
protected function setUp() {
parent::setUp();
for ($i = 0; $i < 1024; $i++) {
$this->entityData[] = [
$this
->getRandomGenerator()
->name(),
(int) mt_rand(1, 1024 * 1024 * 1024),
(bool) mt_rand(0, 1),
];
}
$this->entities = array_map(function ($data) {
return new class($data[0], $data[1], $data[2]) implements EntityInterface {
protected $id;
protected $fooBar;
protected $fooBaz;
public function __construct(string $id, int $foo_bar, bool $foo_baz) {
$this->id = $id;
$this->fooBar = $foo_bar;
$this->fooBaz = $foo_baz;
}
public static function idProperty() : string {
return 'id';
}
public function id() : ?string {
return $this->id;
}
public function getFooBar() : int {
return $this->fooBar;
}
public function setFooBar(int $foo_bar) : void {
$this->fooBar = $foo_bar;
}
public function isFooBaz() : bool {
return $this->fooBaz;
}
public function setFooBaz(bool $foo_baz) : void {
$this->fooBaz = $foo_baz;
}
};
}, $this->entityData);
}
}