|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * @copyright Copyright (C) Ibexa AS. All rights reserved. |
| 5 | + * @license For full copyright and license information view LICENSE file distributed with this source code. |
| 6 | + */ |
| 7 | +declare(strict_types=1); |
| 8 | + |
| 9 | +namespace Ibexa\Tests\Solr\Search\Query\Location\CriterionVisitor\Location; |
| 10 | + |
| 11 | +use Ibexa\Contracts\Core\Repository\PermissionResolver; |
| 12 | +use Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion; |
| 13 | +use Ibexa\Contracts\Solr\Query\CriterionVisitor; |
| 14 | +use Ibexa\Core\Repository\Values\User\UserReference; |
| 15 | +use Ibexa\Solr\Query\Location\CriterionVisitor\Location\IsBookmarked; |
| 16 | +use PHPUnit\Framework\TestCase; |
| 17 | + |
| 18 | +/** |
| 19 | + * @covers \Ibexa\Solr\Query\Location\CriterionVisitor\Location\IsBookmarked |
| 20 | + */ |
| 21 | +final class IsBookmarkedTest extends TestCase |
| 22 | +{ |
| 23 | + private const USER_ID = 123; |
| 24 | + |
| 25 | + private CriterionVisitor $visitor; |
| 26 | + |
| 27 | + /** @var \Ibexa\Contracts\Core\Repository\PermissionResolver&\PHPUnit\Framework\MockObject\MockObject */ |
| 28 | + private PermissionResolver $permissionResolver; |
| 29 | + |
| 30 | + protected function setUp(): void |
| 31 | + { |
| 32 | + $this->permissionResolver = $this->createMock(PermissionResolver::class); |
| 33 | + $this->visitor = new IsBookmarked($this->permissionResolver); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * @dataProvider provideDataForTestCanVisit |
| 38 | + */ |
| 39 | + public function testCanVisit( |
| 40 | + bool $expected, |
| 41 | + Criterion $criterion |
| 42 | + ): void { |
| 43 | + self::assertSame( |
| 44 | + $expected, |
| 45 | + $this->visitor->canVisit($criterion) |
| 46 | + ); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * @return iterable<array{ |
| 51 | + * bool, |
| 52 | + * \Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion |
| 53 | + * }> |
| 54 | + */ |
| 55 | + public function provideDataForTestCanVisit(): iterable |
| 56 | + { |
| 57 | + yield 'Not supported criterion' => [ |
| 58 | + false, |
| 59 | + new Criterion\ContentId(123), |
| 60 | + ]; |
| 61 | + |
| 62 | + yield 'Supported criterion' => [ |
| 63 | + true, |
| 64 | + new Criterion\Location\IsBookmarked(), |
| 65 | + ]; |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * @dataProvider provideDataForTestVisit |
| 70 | + */ |
| 71 | + public function testVisit( |
| 72 | + string $expected, |
| 73 | + Criterion $criterion |
| 74 | + ): void { |
| 75 | + $this->mockPermissionResolverGetCurrentUserReference(); |
| 76 | + |
| 77 | + self::assertSame( |
| 78 | + $expected, |
| 79 | + $this->visitor->visit($criterion) |
| 80 | + ); |
| 81 | + } |
| 82 | + |
| 83 | + /** |
| 84 | + * @return iterable<array{ |
| 85 | + * string, |
| 86 | + * \Ibexa\Contracts\Core\Repository\Values\Content\Query\Criterion |
| 87 | + * }> |
| 88 | + */ |
| 89 | + public function provideDataForTestVisit(): iterable |
| 90 | + { |
| 91 | + yield 'Query for bookmarked locations' => [ |
| 92 | + 'location_bookmarked_user_ids_mid:"123"', |
| 93 | + new Criterion\Location\IsBookmarked(), |
| 94 | + ]; |
| 95 | + |
| 96 | + yield 'Query for not bookmarked locations' => [ |
| 97 | + 'NOT location_bookmarked_user_ids_mid:"123"', |
| 98 | + new Criterion\Location\IsBookmarked(false), |
| 99 | + ]; |
| 100 | + } |
| 101 | + |
| 102 | + private function mockPermissionResolverGetCurrentUserReference(): void |
| 103 | + { |
| 104 | + $this->permissionResolver |
| 105 | + ->method('getCurrentUserReference') |
| 106 | + ->willReturn(new UserReference(self::USER_ID)); |
| 107 | + } |
| 108 | +} |
0 commit comments