View source
<?php
namespace Drupal\Tests\apigee_edge\Functional;
use Drupal\apigee_edge\Entity\Developer;
use Drupal\apigee_edge\Entity\DeveloperApp;
class QueryTest extends ApigeeEdgeFunctionalTestBase {
protected $developerStorage;
protected $developerAppStorage;
protected $prefix;
protected $developerData = [
[
'email' => 'test00@example.com',
'userName' => 'test00',
'firstName' => 'Test00',
'lastName' => 'User',
],
[
'email' => 'test01@example.com',
'userName' => 'test01',
'firstName' => 'Test01',
'lastName' => 'User',
],
[
'email' => 'test02@example.com',
'userName' => 'test02',
'firstName' => 'Test02',
'lastName' => 'User',
],
];
protected $edgeDevelopers = [];
protected function setUp() {
parent::setUp();
$this->developerStorage = $this->container
->get('entity_type.manager')
->getStorage('developer');
$this->developerAppStorage = $this->container
->get('entity_type.manager')
->getStorage('developer_app');
$this->prefix = $this
->randomMachineName();
foreach ($this->developerData as $data) {
$data['email'] = "{$this->prefix}.{$data['email']}";
$developer = Developer::create($data);
$developer
->save();
$this->edgeDevelopers[$data['email']] = $developer;
}
}
protected function tearDown() {
foreach ($this->edgeDevelopers as $developer) {
try {
if ($developer !== NULL) {
$developer
->delete();
}
} catch (\Exception $exception) {
$this
->logException($exception);
}
}
parent::tearDown();
}
public function testQueries() {
$this
->developerQueryTest();
$this
->smartQueryTest();
}
protected function developerQueryTest() {
$result = $this->developerStorage
->getQuery()
->condition('email', "{$this->prefix}.test", 'STARTS_WITH')
->condition('email', '@example.com', 'ENDS_WITH')
->sort('lastName')
->sort('email', 'DESC')
->execute();
$this
->assertEquals(array_values([
"{$this->prefix}.test02@example.com",
"{$this->prefix}.test01@example.com",
"{$this->prefix}.test00@example.com",
]), array_values($result));
$result = $this->developerStorage
->getQuery()
->condition('email', "{$this->prefix}.test", 'STARTS_WITH')
->condition('email', '@example.com', 'ENDS_WITH')
->sort('email')
->range(1, 1)
->execute();
$this
->assertEquals(array_values([
"{$this->prefix}.test01@example.com",
]), array_values($result));
$result = $this->developerStorage
->getQuery()
->condition('email', "{$this->prefix}.test", 'STARTS_WITH')
->condition('email', '@example.com', 'ENDS_WITH')
->count()
->execute();
$this
->assertEquals(3, $result);
}
protected function smartQueryTest() {
foreach ($this->edgeDevelopers as $developer) {
$app = DeveloperApp::create([
'name' => $this
->randomMachineName(),
'status' => DeveloperApp::STATUS_APPROVED,
'developerId' => $developer
->getDeveloperId(),
]);
$app
->save();
}
$result = $this->developerStorage
->getQuery()
->condition('email', NULL)
->count()
->execute();
$this
->assertEquals(0, $result);
$result = $this->developerStorage
->getQuery()
->condition('developerId', NULL)
->count()
->execute();
$this
->assertEquals(0, $result);
$developer = reset($this->edgeDevelopers);
$result = $this->developerAppStorage
->getQuery()
->condition('developerId', $developer
->getDeveloperId())
->count()
->execute();
$this
->assertEquals(1, $result);
$result = $this->developerAppStorage
->getQuery()
->condition('developerId', NULL)
->count()
->execute();
$this
->assertEquals(0, $result);
$result = $this->developerAppStorage
->getQuery()
->condition('email', $developer
->getEmail())
->count()
->execute();
$this
->assertEquals(1, $result);
$result = $this->developerAppStorage
->getQuery()
->condition('email', NULL)
->count()
->execute();
$this
->assertEquals(0, $result);
$result = $this->developerAppStorage
->getQuery()
->condition('email', $developer
->getEmail())
->condition('name', NULL)
->count()
->execute();
$this
->assertEquals(0, $result);
}
}