-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add optional raw response processing to query context #24
Merged
mhsdef
merged 16 commits into
trunk
from
hewsut/move-response-processing-to-query-context
Sep 6, 2024
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
82bf4f5
Move response processing to query context
mhsdef 66dcee6
Lint
mhsdef 5089e70
Merge branch 'trunk' into hewsut/move-response-processing-to-query-co…
mhsdef 10de8e4
Adjust approach
mhsdef 9764d9e
Ya ya, was working on it
mhsdef d59a147
Add comment
mhsdef f3c637d
Add tests
mhsdef 2471cb0
Lint
mhsdef f9c9fa7
more lint
mhsdef 9385d43
WS fix, wut.
mhsdef b524fc3
More comments
mhsdef 8367021
Improve type annotations to reflect JsonObject capabilities
chriszarate f6f9771
Add additional test cases asserting flexible response data types
chriszarate 84b05de
Improve standard processing test
chriszarate 318afbf
Adjust verbiage, single call
mhsdef 8dd1472
Lint
mhsdef File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,103 @@ | ||
<?php | ||
|
||
namespace RemoteDataBlocks\Tests\Config; | ||
|
||
use PHPUnit\Framework\TestCase; | ||
use RemoteDataBlocks\Config\QueryContext; | ||
use RemoteDataBlocks\Test\TestDatasource; | ||
use GuzzleHttp\Psr7\Response; | ||
|
||
class QueryContextTest extends TestCase { | ||
|
||
private $datasource; | ||
private $query_context; | ||
|
||
protected function setUp(): void { | ||
$this->datasource = new TestDatasource(); | ||
$this->query_context = new QueryContext( $this->datasource ); | ||
} | ||
|
||
public function testGetEndpoint() { | ||
$result = $this->query_context->get_endpoint( [] ); | ||
$this->assertEquals( 'https://example.com', $result ); | ||
} | ||
|
||
public function testGetImageUrl() { | ||
$result = $this->query_context->get_image_url(); | ||
$this->assertNull( $result ); | ||
} | ||
|
||
public function testGetMetadata() { | ||
$mock_response = new Response( 200, [ 'Age' => '60' ] ); | ||
$results = [ [ 'id' => 1 ], [ 'id' => 2 ] ]; | ||
|
||
$metadata = $this->query_context->get_metadata( $mock_response, $results ); | ||
|
||
$this->assertArrayHasKey( 'last_updated', $metadata ); | ||
$this->assertArrayHasKey( 'total_count', $metadata ); | ||
$this->assertEquals( 'Last updated', $metadata['last_updated']['name'] ); | ||
$this->assertEquals( 'string', $metadata['last_updated']['type'] ); | ||
$this->assertEquals( 'Total count', $metadata['total_count']['name'] ); | ||
$this->assertEquals( 'number', $metadata['total_count']['type'] ); | ||
$this->assertEquals( 2, $metadata['total_count']['value'] ); | ||
} | ||
|
||
public function testGetRequestMethod() { | ||
$this->assertEquals( 'GET', $this->query_context->get_request_method() ); | ||
} | ||
|
||
public function testGetRequestHeaders() { | ||
$result = $this->query_context->get_request_headers( [] ); | ||
$this->assertEquals( [ 'Content-Type' => 'application/json' ], $result ); | ||
} | ||
|
||
public function testGetRequestBody() { | ||
$this->assertNull( $this->query_context->get_request_body( [] ) ); | ||
} | ||
|
||
public function testGetQueryName() { | ||
$this->assertEquals( 'Query', $this->query_context->get_query_name() ); | ||
} | ||
|
||
public function testIsResponseDataCollection() { | ||
$this->assertFalse( $this->query_context->is_response_data_collection() ); | ||
|
||
$this->query_context->output_variables['is_collection'] = true; | ||
$this->assertTrue( $this->query_context->is_response_data_collection() ); | ||
} | ||
|
||
public function testDefaultProcessResponse() { | ||
$raw_data = '{"key": "value"}'; | ||
$this->assertEquals( $raw_data, $this->query_context->process_response( $raw_data, [] ) ); | ||
} | ||
|
||
public function testCustomProcessResponse() { | ||
$custom_query_context = new class($this->datasource) extends QueryContext { | ||
public function process_response( string $raw_response_data, array $input_variables ): string { | ||
// Convert HTML to JSON | ||
$dom = new \DOMDocument(); | ||
$dom->loadHTML( $raw_response_data, LIBXML_NOERROR ); | ||
// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase | ||
$title = $dom->getElementsByTagName( 'title' )->item( 0 )->nodeValue; | ||
$paragraphs = $dom->getElementsByTagName( 'p' ); | ||
$content = []; | ||
foreach ( $paragraphs as $p ) { | ||
// phpcs:ignore WordPress.NamingConventions.ValidVariableName.UsedPropertyNotSnakeCase | ||
$content[] = $p->nodeValue; | ||
} | ||
|
||
$data = [ | ||
'title' => $title, | ||
'content' => $content, | ||
]; | ||
|
||
return wp_json_encode( $data ); | ||
} | ||
}; | ||
|
||
$html_data = '<html><head><title>Test Page</title></head><body><p>Paragraph 1</p><p>Paragraph 2</p></body></html>'; | ||
$expected_json = '{"title":"Test Page","content":["Paragraph 1","Paragraph 2"]}'; | ||
|
||
$this->assertEquals( $expected_json, $custom_query_context->process_response( $html_data, [] ) ); | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I confirmed, as expected, core escaping happens further on in execution--somewhere closer to output.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
escapin late :)