-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
IBX-9121: Added support for IsUserEnabled criterion
- Loading branch information
Showing
7 changed files
with
212 additions
and
36 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Ibexa\Solr\Query\Common\CriterionVisitor; | ||
|
||
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion; | ||
use Ibexa\Contracts\Solr\Query\CriterionVisitor; | ||
|
||
final class IsUserEnabled extends CriterionVisitor | ||
{ | ||
private const SEARCH_FIELD = 'user_is_enabled_b'; | ||
|
||
public function canVisit(Criterion $criterion): bool | ||
{ | ||
return $criterion instanceof Criterion\IsUserEnabled; | ||
} | ||
|
||
public function visit(Criterion $criterion, CriterionVisitor $subVisitor = null): string | ||
{ | ||
$value = $criterion->value; | ||
if (!is_array($value) || !is_bool($value[0])) { | ||
throw new \LogicException( | ||
sprintf( | ||
'%s value should be of type array<bool>, received %s.', | ||
Criterion\IsUserEnabled::class, | ||
get_debug_type($value), | ||
) | ||
); | ||
} | ||
|
||
return self::SEARCH_FIELD . ':' . $this->toString($value[0]); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Ibexa\Tests\Solr\Search\Query; | ||
|
||
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion; | ||
use Ibexa\Contracts\Solr\Query\CriterionVisitor; | ||
use Ibexa\Tests\Solr\Search\Query\Utils\Stub\TestCriterion; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
abstract class BaseCriterionVisitorTestCase extends TestCase | ||
{ | ||
abstract protected function getVisitor(): CriterionVisitor; | ||
|
||
abstract protected function getSupportedCriterion(): Criterion; | ||
|
||
/** | ||
* @return iterable<array{ | ||
* string, | ||
* \Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion | ||
* }> | ||
*/ | ||
abstract protected function provideDataForTestVisit(): iterable; | ||
|
||
/** | ||
* @dataProvider provideDataForTestCanVisit | ||
*/ | ||
public function testCanVisit( | ||
bool $expected, | ||
Criterion $criterion | ||
): void { | ||
self::assertSame( | ||
$expected, | ||
$this->getVisitor()->canVisit($criterion) | ||
); | ||
} | ||
|
||
/** | ||
* @return iterable<array{ | ||
* bool, | ||
* \Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion | ||
* }> | ||
*/ | ||
public function provideDataForTestCanVisit(): iterable | ||
{ | ||
yield 'Not supported criterion' => [ | ||
false, | ||
new TestCriterion(), | ||
]; | ||
|
||
yield 'Supported criterion' => [ | ||
true, | ||
$this->getSupportedCriterion(), | ||
]; | ||
} | ||
|
||
/** | ||
* @dataProvider provideDataForTestVisit | ||
*/ | ||
public function testVisit( | ||
string $expectedQuery, | ||
Criterion $criterion | ||
): void { | ||
self::assertSame( | ||
$expectedQuery, | ||
$this->getVisitor()->visit($criterion) | ||
); | ||
} | ||
} |
50 changes: 50 additions & 0 deletions
50
tests/lib/Search/Query/Common/CriterionVisitor/IsUserEnabledTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Ibexa\Tests\Solr\Search\Query\Common\CriterionVisitor; | ||
|
||
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion; | ||
use Ibexa\Contracts\Solr\Query\CriterionVisitor; | ||
use Ibexa\Solr\Query\Common\CriterionVisitor\IsUserEnabled; | ||
use Ibexa\Tests\Solr\Search\Query\BaseCriterionVisitorTestCase; | ||
|
||
/** | ||
* @covers \Ibexa\Solr\Query\Common\CriterionVisitor\IsUserEnabled | ||
*/ | ||
final class IsUserEnabledTest extends BaseCriterionVisitorTestCase | ||
{ | ||
private CriterionVisitor $criterionVisitor; | ||
|
||
protected function setUp(): void | ||
{ | ||
$this->criterionVisitor = new IsUserEnabled(); | ||
} | ||
|
||
protected function getVisitor(): CriterionVisitor | ||
{ | ||
return $this->criterionVisitor; | ||
} | ||
|
||
protected function getSupportedCriterion(): Criterion | ||
{ | ||
return new Criterion\IsUserEnabled(); | ||
} | ||
|
||
protected function provideDataForTestVisit(): iterable | ||
{ | ||
yield 'Query for enabled user' => [ | ||
'user_is_enabled_b:true', | ||
new Criterion\IsUserEnabled(), | ||
]; | ||
|
||
yield 'Query for disabled user' => [ | ||
'user_is_enabled_b:false', | ||
new Criterion\IsUserEnabled(false), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Ibexa\Tests\Solr\Search\Query\Utils\Stub; | ||
|
||
use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion; | ||
|
||
final class TestCriterion extends Criterion | ||
{ | ||
public function __construct() | ||
{ | ||
// No implementation needed. Used for test purposes only. | ||
} | ||
|
||
public function getSpecifications(): array | ||
{ | ||
return []; | ||
} | ||
} |