Skip to content

Commit

Permalink
Update tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
melaniekung committed Aug 9, 2024
1 parent b30e60b commit 5b00b7a
Show file tree
Hide file tree
Showing 9 changed files with 259 additions and 152 deletions.
4 changes: 4 additions & 0 deletions plugins/arSolrPlugin/lib/query/arSolrMatchAllQuery.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ public function getDefaultOperator()

public function setDefaultOperator($operator)
{
if ('AND' !== $operator && 'OR' !== $operator) {
throw new Exception('Invalid operator. AND and OR are the only acceptable operator types.');
}

$this->operator = $operator;
}

Expand Down
2 changes: 1 addition & 1 deletion plugins/arSolrPlugin/lib/query/arSolrQuery.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public function getDefaultOperator()
public function setDefaultOperator($operator)
{
if ('AND' !== $operator && 'OR' !== $operator) {
throw new Exception('Invalid operator. AND and OR are the only acceptable operator types');
throw new Exception('Invalid operator. AND and OR are the only acceptable operator types.');
}

$this->operator = $operator;
Expand Down
19 changes: 19 additions & 0 deletions plugins/arSolrPlugin/lib/query/arSolrRangeQuery.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,27 @@ public function getRange()
return $this->range;
}

public function validateRange($range)
{
$keys = array_keys($range);
$d = DateTime::createFromFormat('Y-m-d', $range[$keys[0]]);
$y = DateTime::createFromFormat('Y', $range[$keys[0]]);
if (false !== $d && $d->format('Y-m-d') === $range[$keys[0]]) {
return true;
}
if (false !== $y && $y->format('Y') === $range[$keys[0]]) {
return true;
}

return false;
}

public function setRange($range)
{
if (!is_array($range) || !$this->validateRange($range)) {
throw new Exception('Invalid range date format. Range date must be formatted as YYYY-MM-DD or YYYY.');
}

$this->range = $range;
$this->setComputedRange($range);
}
Expand Down
32 changes: 16 additions & 16 deletions test/phpunit/arSolrPlugin/lib/query/ArSolrExistsQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ public function createSolrExistsQueryProvider(): array
'result' => null,
],
'New arSolrExistsQuery with string field' => [
'field' => 'testString',
'result' => 'testString',
'field' => 'testField',
'result' => 'testField',
],
];
}
Expand All @@ -35,25 +35,25 @@ public function createSolrExistsQueryProvider(): array
* @dataProvider createSolrExistsQueryProvider
*
* @param mixed $field
* @param mixed $result
* @param mixed $expected
*/
public function testCreateSolrExistsQuery($field, $result)
public function testCreateSolrExistsQuery($field, $expected)
{
$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.');
$this->assertSame($expected, $this->existsQuery->getField(), 'Assert arSolrExistsQuery field is correct.');
}

public function getQueryParamsProvider(): array
{
return [
'Generate exists query with specified type' => [
'field' => 'test_field',
'type' => 'test_type',
'field' => 'testField',
'type' => 'testType',
'result' => [
'query' => [
'lucene' => [
'query' => 'test_type.test_field:*',
'query' => 'testType.testField:*',
],
],
'offset' => 0,
Expand All @@ -66,32 +66,32 @@ public function getQueryParamsProvider(): array
/**
* @dataProvider getQueryParamsProvider
*
* @param mixed $result
* @param mixed $expected
* @param mixed $field
* @param mixed $type
*/
public function testGetQueryParams($field, $type, $result)
public function testGetQueryParams($field, $type, $expected)
{
$this->existsQuery = new arSolrExistsQuery($field);
$this->existsQuery->setType($type);

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

$this->assertSame($params, $result);
$this->assertSame($expected, $actual, 'Params passed does not match expected.');
}

public function getQueryParamsExceptionProvider(): array
{
return [
'Generate exists query with missing type' => [
'field' => 'test_field',
'field' => 'testField',
'type' => '',
'expectedException' => '\Exception',
'expectedExceptionMessage' => 'Field \'type\' is not set.',
],
'Generate exists query with missing field and type' => [
'Generate exists query with missing field' => [
'field' => '',
'type' => 'test_type',
'type' => 'testType',
'expectedException' => '\Exception',
'expectedExceptionMessage' => 'Field is not set.',
],
Expand Down Expand Up @@ -120,6 +120,6 @@ public function testGetQueryParamsException($field, $type, $expectedException, $
$this->expectException($expectedException);
$this->expectExceptionMessage($expectedExceptionMessage);

$params = $this->existsQuery->getQueryParams();
$this->existsQuery->getQueryParams();
}
}
80 changes: 38 additions & 42 deletions test/phpunit/arSolrPlugin/lib/query/ArSolrMatchAllQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,7 @@ public function createSolrMatchAllQueryProvider()
{
return [
'New arSolrMatchAllQuery with default options' => [
'operator' => 'AND',
'searchQuery' => '*:*',
'result' => [
'expected' => [
'query' => [
'lucene' => [
'q.op' => 'AND',
Expand All @@ -34,70 +32,68 @@ public function createSolrMatchAllQueryProvider()

/**
* @dataProvider createSolrMatchAllQueryProvider
*
* @param array $expected
*/
public function testCreateSolrMatchAllQuery()
public function testCreateSolrMatchAllQuery($expected)
{
$this->matchAllQuery = new arSolrMatchAllQuery();
$actual = $this->matchAllQuery->getQueryParams();

$this->assertTrue($this->matchAllQuery instanceof arSolrMatchAllQuery, 'Assert plugin object is arSolrMatchAllQuery.');
$this->assertSame($expected, $actual, 'Params passed do not match expected.');
}

public function testSetDefaultOperator()
{
$this->matchAllQuery = new arSolrMatchAllQuery();

// Test setting the default operator to 'OR'
$this->matchAllQuery->setDefaultOperator('OR');
$this->assertEquals('OR', $this->matchAllQuery->getDefaultOperator());

// Test setting the default operator to 'AND'
$this->matchAllQuery->setDefaultOperator('AND');
$this->assertEquals('AND', $this->matchAllQuery->getDefaultOperator());
}

public function testSetSearchQuery()
public function testSetDefaultOperatorException()
{
$this->matchAllQuery = new arSolrMatchAllQuery();

// Test setting the search query to blank query
$this->matchAllQuery->setSearchQuery('');
$this->assertEquals('', $this->matchAllQuery->getSearchQuery());
$this->expectException('\Exception');
$this->expectExceptionMessage('Invalid operator. AND and OR are the only acceptable operator types.');

// Test setting the search query to default
$this->matchAllQuery->setSearchQuery('*:*');
$this->assertEquals('*:*', $this->matchAllQuery->getSearchQuery());
$this->matchAllQuery->setDefaultOperator('testOperator');
}

public function getQueryParamsProvider(): array
public function setDefaultOperatorProvider()
{
return [
'Test Solr MatchAll query with default options' => [
'result' => [
'query' => [
'lucene' => [
'q.op' => 'AND',
'stopwords' => 'true',
'query' => '*:*',
],
],
'offset' => 0,
'limit' => 10,
],
'Test setting the default operator to \'OR\'' => [
'operator' => 'OR',
'expected' => 'OR',
],
'Test setting the default operator to \'AND\'' => [
'operator' => 'AND',
'expected' => 'AND',
],
];
}

/**
* @dataProvider getQueryParamsProvider
* @dataProvider setDefaultOperatorProvider
*
* @param mixed $result
* @param mixed $expected
* @param string $operator
*/
public function testGetQueryParams($result)
public function testSetDefaultOperator($expected, $operator)
{
$this->matchAllQuery = new arSolrMatchAllQuery();
$this->matchAllQuery->setDefaultOperator($operator);

$actual = $this->matchAllQuery->getDefaultOperator();

$this->assertSame($expected, $actual, 'Params passed do not match expected.');
}

public function testSetSearchQuery()
{
$this->matchAllQuery = new arSolrMatchAllQuery();

$params = $this->matchAllQuery->getQueryParams();
// Test setting the search query to blank query
$this->matchAllQuery->setSearchQuery('');
$this->assertEquals('', $this->matchAllQuery->getSearchQuery());

$this->assertSame($params, $result);
// Test setting the search query to default
$this->matchAllQuery->setSearchQuery('*:*');
$this->assertEquals('*:*', $this->matchAllQuery->getSearchQuery());
}
}
13 changes: 7 additions & 6 deletions test/phpunit/arSolrPlugin/lib/query/ArSolrMatchQueryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
* @internal
*
* @covers \arSolrMatchQuery
* @covers \arSolrTermQuery
*/
class ArSolrMatchQueryTest extends TestCase
{
Expand All @@ -28,7 +29,7 @@ public function getQueryParamsProvider(): array
'field' => 'test_field',
'value' => 'testVal',
'type' => 'test_type',
'result' => [
'expected' => [
'query' => [
'edismax' => [
'query' => 'test_type.test_field:testVal~',
Expand All @@ -47,17 +48,17 @@ public function getQueryParamsProvider(): array
* @param mixed $field
* @param mixed $value
* @param mixed $type
* @param mixed $result
* @param mixed $expected
*/
public function testGetQueryParams($field, $value, $type, $result)
public function testGetQueryParams($field, $value, $type, $expected)
{
$this->matchQuery = new arSolrMatchQuery();
$this->matchQuery->setFieldQuery($field, $value);
$this->matchQuery->setType($type);

$params = $this->matchQuery->getQueryParams();
$actual = $this->matchQuery->getQueryParams();

$this->assertSame($params, $result);
$this->assertSame($expected, $actual, 'Params passed do not match expected.');
}

public function getQueryParamsUsingSetExceptionProvider(): array
Expand Down Expand Up @@ -105,6 +106,6 @@ public function testGetQueryParamsUsingSetException($field, $value, $type, $expe
$this->expectException($expectedException);
$this->expectExceptionMessage($expectedExceptionMessage);

$params = $this->matchQuery->getQueryParams();
$this->matchQuery->getQueryParams();
}
}
Loading

0 comments on commit 5b00b7a

Please sign in to comment.