diff --git a/CHANGELOG.md b/CHANGELOG.md index d55d109..c92abf1 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ ## Unreleased +- Profile used endpoint details for a query ## 1.0.0 - 2021-05-13 - Initial implementation diff --git a/src/Query/AbstractSolrQuery.php b/src/Query/AbstractSolrQuery.php index e65effd..78c9f9e 100644 --- a/src/Query/AbstractSolrQuery.php +++ b/src/Query/AbstractSolrQuery.php @@ -49,7 +49,9 @@ public function getProfilerId(): string public function getProfilerData(): ?array { - return null; + return [ + 'Endpoint' => $this->getEndpoint() ?? 'Default', + ]; } public function getEndpoint(): ?string diff --git a/src/Query/AbstractSolrSelectQuery.php b/src/Query/AbstractSolrSelectQuery.php index 430b1c2..5f3e349 100644 --- a/src/Query/AbstractSolrSelectQuery.php +++ b/src/Query/AbstractSolrSelectQuery.php @@ -54,4 +54,11 @@ private function prepareQuery(Query $select): Query } abstract public function prepareSelect(Query $select): Query; + + public function getProfilerData(): ?array + { + return parent::getProfilerData() + [ + 'Endpoint.details' => clone $this->client->getEndpoint($this->getEndpoint()), + ]; + } } diff --git a/tests/QueryBuilder/Applicator/GroupingApplicatorTest.php b/tests/QueryBuilder/Applicator/GroupingApplicatorTest.php index c8b0afe..b490ac0 100644 --- a/tests/QueryBuilder/Applicator/GroupingApplicatorTest.php +++ b/tests/QueryBuilder/Applicator/GroupingApplicatorTest.php @@ -30,11 +30,11 @@ public function shouldApplyGroupingOnQuery(): void $this->assertStringContainsString('rows=' . $entity->getNumberOfRows(), $queryUri); $this->assertStringContainsString('group=true', $queryUri); - $this->assertStringContainsString('group.main=' . $entity->getMainResult() ? 'true' : 'false', $queryUri); + $this->assertStringContainsString('group.main=' . ($entity->getMainResult() ? 'true' : 'false'), $queryUri); $this->assertStringContainsString('group.field=' . $entity->getGroupingField(), $queryUri); $this->assertStringContainsString('group.limit=' . $entity->getGroupingLimit(), $queryUri); $this->assertStringContainsString( - 'group.ngroups=' . $entity->getNumberOfGroups() ? 'true' : 'false', + 'group.ngroups=' . ($entity->getNumberOfGroups() ? 'true' : 'false'), $queryUri ); } diff --git a/tests/QueryBuilder/QueryBuilderTest.php b/tests/QueryBuilder/QueryBuilderTest.php index c0db438..d8778ad 100644 --- a/tests/QueryBuilder/QueryBuilderTest.php +++ b/tests/QueryBuilder/QueryBuilderTest.php @@ -33,6 +33,7 @@ use Lmc\Cqrs\Solr\QueryBuilder\Query\BuilderPrototypeQuery; use PHPUnit\Framework\TestCase; use Solarium\Core\Client\Client; +use Solarium\Core\Client\Endpoint; class QueryBuilderTest extends TestCase { @@ -324,4 +325,32 @@ public function provideQueryOptions(): array ], ]; } + + /** + * @test + */ + public function shouldProfileUsedValues(): void + { + $client = new Client(); + $entity = new BaseDummyEntity('query'); + + $query = $this->queryBuilderWithApplicators->buildQuery($entity); + $query->setSolrClient($client); + + $profilerData = $query->getProfilerData(); + + $this->assertIsArray($profilerData); + + $this->assertArrayHasKey('Endpoint', $profilerData); + $this->assertSame('Default', $profilerData['Endpoint']); + + $this->assertArrayHasKey('Endpoint.details', $profilerData); + $this->assertInstanceOf(Endpoint::class, $profilerData['Endpoint.details']); + + $this->assertNotSame( + $client->getEndpoint($query->getEndpoint()), + $profilerData['Endpoint.details'], + 'Profiled endpoint should not be a real instance of endpoint, but it should be cloned, so it remains unchanged in time of profiling.' + ); + } }