Skip to content

Commit 65fa254

Browse files
Access "response" of CoreAdmion queries
1 parent 8bc45a0 commit 65fa254

File tree

5 files changed

+168
-0
lines changed

5 files changed

+168
-0
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88
### Added
99
- Solarium\QueryType\Extract\Query::setStreamType()
1010

11+
### Fixed
12+
- Solarium\QueryType\Server\CoreAdmin\Result\Result::getResponse() always returns a Solarium\Core\Client\Response object, even if the response data contains a field named `"response"`
13+
- Solarium\QueryType\Server\CoreAdmin\Result\Result::$response publicly exposes the `"response"` field from the response data
14+
1115
### Changed
1216
- Added `void` return type to `Solarium\Core\Plugin\PluginInterface::initPlugin()` method signature
1317
- Added `void` return type to `Solarium\Core\Plugin\PluginInterface::deinitPlugin()` method signature

src/QueryType/Server/CoreAdmin/ResponseParser.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,20 @@ public function parse(ResultInterface $result): array
3838
$data = $this->parseInitFailures($data, $result);
3939
$data = $this->parseStatus($data, $result);
4040

41+
if (isset($data['response'])) {
42+
/*
43+
* The protected property Result::$response always holds the
44+
* Response object for the Query and is accessible through
45+
* Result::getResponse().
46+
*
47+
* We store this value as a different property and revert this in
48+
* Result::__get() to make it accessible as if it were the public
49+
* property Result::$response.
50+
*/
51+
$data['_original_response'] = $data['response'];
52+
unset($data['response']);
53+
}
54+
4155
return $data;
4256
}
4357

src/QueryType/Server/CoreAdmin/Result/Result.php

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,4 +132,25 @@ public function getStatusResultByCoreName(string $coreName): ?StatusResult
132132
{
133133
return $this->statusResults[$coreName] ?? null;
134134
}
135+
136+
/**
137+
* Magic method for getting inaccessible or non-existent properties.
138+
*
139+
* @see \Solarium\QueryType\Server\CoreAdmin\ResponseParser
140+
*
141+
* @param string $name
142+
*
143+
* @return mixed|null
144+
*/
145+
public function __get(string $name)
146+
{
147+
/*
148+
* Revert the workaround from ResponseParser::parse() to make
149+
* Result::$response publicly accessible without conflicting with
150+
* the actual protected property of that name.
151+
*/
152+
if ('response' === $name && isset($this->_original_response)) {
153+
return $this->_original_response;
154+
}
155+
}
135156
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
3+
namespace Solarium\Tests\QueryType\Server\CoreAdmin;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Solarium\Core\Client\Response;
7+
use Solarium\QueryType\Server\CoreAdmin\Query\Query;
8+
use Solarium\QueryType\Server\CoreAdmin\ResponseParser;
9+
use Solarium\QueryType\Server\CoreAdmin\Result\Result;
10+
11+
class ResponseParserTest extends TestCase
12+
{
13+
public function testParseWithResponseProperty()
14+
{
15+
$query = new Query();
16+
$action = $query->createSplit();
17+
$query->setAction($action);
18+
19+
$data = [
20+
'response' => [
21+
'timing' => [
22+
'time' => 318,
23+
'doSplit' => [
24+
'time' => 318,
25+
],
26+
'findDocSetsPerLeaf' => [
27+
'time' => 0,
28+
],
29+
'addIndexes' => [
30+
'time' => 21,
31+
],
32+
'subIWCommit' => [
33+
'time' => 294,
34+
],
35+
],
36+
],
37+
];
38+
39+
$response = new Response(json_encode($data), ['HTTP/1.1 200 OK']);
40+
$result = new Result($query, $response);
41+
$parser = new ResponseParser();
42+
$parsed = $parser->parse($result);
43+
44+
$this->assertSame($data['response'], $parsed['_original_response']);
45+
$this->assertArrayNotHasKey('response', $parsed);
46+
}
47+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
<?php
2+
3+
namespace Solarium\Tests\QueryType\Server\CoreAdmin\Result;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use Solarium\QueryType\Server\CoreAdmin\Result\Result;
7+
8+
class ResultTest extends TestCase
9+
{
10+
/**
11+
* @var Result
12+
*/
13+
protected $result;
14+
15+
public function setUp(): void
16+
{
17+
$this->result = new CoreAdminDummy();
18+
}
19+
20+
public function testGetWasSuccessful()
21+
{
22+
$this->assertTrue($this->result->getWasSuccessful());
23+
}
24+
25+
public function testGetStatusMessage()
26+
{
27+
$this->assertSame('OK', $this->result->getStatusMessage());
28+
}
29+
30+
/**
31+
* @see Solarium\QueryType\Server\CoreAdmin\ResponseParser::parse()
32+
* @see Solarium\QueryType\Server\CoreAdmin\Result\Result::__get()
33+
*/
34+
public function testAccessResponseAsProperty()
35+
{
36+
$data = [
37+
'_original_response' => [
38+
'timing' => [
39+
'time' => 318.0,
40+
'doSplit' => [
41+
'time' => 318.0,
42+
],
43+
'findDocSetsPerLeaf' => [
44+
'time' => 0.0,
45+
],
46+
'addIndexes' => [
47+
'time' => 21.0,
48+
],
49+
'subIWCommit' => [
50+
'time' => 294.0,
51+
],
52+
],
53+
],
54+
];
55+
56+
$this->result->mapData($data);
57+
$this->assertSame($data['_original_response'], $this->result->response);
58+
}
59+
60+
public function testAccessOtherProperty()
61+
{
62+
$this->result->mapData(['foo' => 'bar']);
63+
64+
$this->assertSame('bar', $this->result->foo);
65+
}
66+
}
67+
68+
class CoreAdminDummy extends Result
69+
{
70+
protected $parsed = true;
71+
72+
public function __construct()
73+
{
74+
$this->wasSuccessful = true;
75+
$this->statusMessage = 'OK';
76+
}
77+
78+
public function mapData(array $mapData): void
79+
{
80+
parent::mapData($mapData);
81+
}
82+
}

0 commit comments

Comments
 (0)