Skip to content

Commit

Permalink
Add test for arSolrExistsQuery
Browse files Browse the repository at this point in the history
Adds PHPUnit tests for arSolrExistsQuery class. Refactor
arSolrExistsQuery to return exceptions when query generation
preconditions are not met before query statement is generated.
  • Loading branch information
sbreker committed Aug 2, 2024
1 parent 28b10ea commit 1c1b07c
Show file tree
Hide file tree
Showing 2 changed files with 162 additions and 14 deletions.
51 changes: 37 additions & 14 deletions plugins/arSolrPlugin/lib/query/arSolrExistsQuery.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,18 @@ class arSolrExistsQuery extends arSolrAbstractQuery
{
/**
* Query Params.
*
* @var mixed
*/
protected $query;
protected array $query = [];

/**
* Field to be queried.
*
* @var string
*/
protected $field;
protected ?string $field = null;

/**
* Type of query.
*/
protected ?string $type = null;

/**
* Constructor.
Expand All @@ -41,11 +42,14 @@ class arSolrExistsQuery extends arSolrAbstractQuery
public function __construct($field)
{
$this->setField($field);
$this->generateQueryParams();
}

public function setField($field)
{
if (empty($field)) {
return;
}

$this->field = $field;
}

Expand All @@ -54,28 +58,47 @@ public function getField()
return $this->field;
}

public function setType($type)
{
if (empty($type)) {
return;
}

$this->type = $type;
}

public function getType()
{
return $this->type;
}

public function getQueryParams()
{
$this->generateQueryParams();

return $this->query;
}

public function generateQueryParams()
protected function generateQueryParams()
{
$field = $this->getField();
if (!isset($field)) {
throw new Exception('Field is not set.');
}

$type = $this->getType();
if (!isset($type)) {
throw new Exception("Field 'type' is not set.");
}

$this->query = [
'query' => [
'lucene' => [
'query' => "{$this->field}:*",
'query' => "{$type}.{$field}:*",
],
],
'offset' => $this->offset,
'limit' => $this->size,
];
}

public function setType($type)
{
$this->setField("{$type}.{$this->field}");
}
}
125 changes: 125 additions & 0 deletions test/phpunit/arSolrPlugin/lib/query/ArSolrExistsQueryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?php

use PHPUnit\Framework\TestCase;

require_once 'plugins/arSolrPlugin/lib/query/arSolrAbstractQuery.class.php';

require_once 'plugins/arSolrPlugin/lib/query/arSolrExistsQuery.class.php';

/**
* @internal
*
* @covers \arSolrExistsQuery
*/
class ArSolrExistsQueryTest extends TestCase
{
public function createSolrExistsQueryProvider(): array
{
return [
'New arSolrExistsQuery with blank field' => [
'field' => '',
'result' => null,
],
'New arSolrExistsQuery with null field' => [
'field' => null,
'result' => null,
],
'New arSolrExistsQuery with string field' => [
'field' => 'testString',
'result' => 'testString',
],
];
}

/**
* @dataProvider createSolrExistsQueryProvider
*
* @param mixed $field
* @param mixed $result
*/
public function testCreateSolrExistsQuery($field, $result)
{
$this->existsQuery = new arSolrExistsQuery($field);
$this->assertTrue($this->existsQuery instanceof arSolrExistsQuery, 'Assert plugin object is arSolrExistsQuery.');
$this->assertSame($this->existsQuery->getField(), $result, 'Assert arSolrExistsQuery field is correct.');
}

public function getQueryParamsProvider(): array
{
return [
'Generate exists query with specified type' => [
'field' => 'test_field',
'type' => 'test_type',
'result' => [
'query' => [
'lucene' => [
'query' => 'test_type.test_field:*',
],
],
'offset' => 0,
'limit' => 10,
],
],
];
}

/**
* @dataProvider getQueryParamsProvider
*
* @param mixed $result
* @param mixed $field
* @param mixed $type
*/
public function testGetQueryParams($field, $type, $result)
{
$this->existsQuery = new arSolrExistsQuery($field);
$this->existsQuery->setType($type);

$params = $this->existsQuery->getQueryParams();

$this->assertSame($params, $result);
}

public function getQueryParamsExceptionProvider(): array
{
return [
'Generate exists query with missing type' => [
'field' => 'test_field',
'type' => '',
'expectedException' => '\Exception',
'expectedExceptionMessage' => 'Field \'type\' is not set.',
],
'Generate exists query with missing field and type' => [
'field' => '',
'type' => 'test_type',
'expectedException' => '\Exception',
'expectedExceptionMessage' => 'Field is not set.',
],
'Generate exists query with missing field and type' => [
'field' => '',
'type' => '',
'expectedException' => '\Exception',
'expectedExceptionMessage' => 'Field is not set.',
],
];
}

/**
* @dataProvider getQueryParamsExceptionProvider
*
* @param mixed $field
* @param mixed $type
* @param mixed $expectedException
* @param mixed $expectedExceptionMessage
*/
public function testGetQueryParamsException($field, $type, $expectedException, $expectedExceptionMessage)
{
$this->existsQuery = new arSolrExistsQuery($field);
$this->existsQuery->setType($type);

$this->expectException($expectedException);
$this->expectExceptionMessage($expectedExceptionMessage);

$params = $this->existsQuery->getQueryParams();
}
}

0 comments on commit 1c1b07c

Please sign in to comment.