Skip to content

Commit

Permalink
Sprinkle some more tests (#61)
Browse files Browse the repository at this point in the history
* First pass

* ok

* Ignore example

* yaya

* mmmk

* Lint fixes

* ugh
  • Loading branch information
mhsdef authored Sep 16, 2024
1 parent 206db85 commit 3a34625
Show file tree
Hide file tree
Showing 12 changed files with 321 additions and 54 deletions.
2 changes: 2 additions & 0 deletions .github/codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,5 @@ coverage:
informational: true
github_checks:
annotations: false
ignore:
- 'example'
22 changes: 11 additions & 11 deletions tests/inc/Config/QueryContextTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ protected function setUp(): void {

public function testGetEndpoint() {
$result = $this->query_context->get_endpoint( [] );
$this->assertEquals( 'https://example.com/api', $result );
$this->assertSame( 'https://example.com/api', $result );
}

public function testGetImageUrl() {
Expand All @@ -35,28 +35,28 @@ public function testGetMetadata() {

$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'] );
$this->assertSame( 'Last updated', $metadata['last_updated']['name'] );
$this->assertSame( 'string', $metadata['last_updated']['type'] );
$this->assertSame( 'Total count', $metadata['total_count']['name'] );
$this->assertSame( 'number', $metadata['total_count']['type'] );
$this->assertSame( 2, $metadata['total_count']['value'] );
}

public function testGetRequestMethod() {
$this->assertEquals( 'GET', $this->query_context->get_request_method() );
$this->assertSame( 'GET', $this->query_context->get_request_method() );
}

public function testGetRequestHeaders() {
$result = $this->query_context->get_request_headers( [] );
$this->assertEquals( [ 'Content-Type' => 'application/json' ], $result );
$this->assertSame( [ '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() );
$this->assertSame( 'Query', $this->query_context->get_query_name() );
}

public function testIsResponseDataCollection() {
Expand All @@ -68,7 +68,7 @@ public function testIsResponseDataCollection() {

public function testDefaultProcessResponse() {
$raw_data = '{"key": "value"}';
$this->assertEquals( $raw_data, $this->query_context->process_response( $raw_data, [] ) );
$this->assertSame( $raw_data, $this->query_context->process_response( $raw_data, [] ) );
}

public function testCustomProcessResponse() {
Expand Down Expand Up @@ -98,6 +98,6 @@ public function process_response( string $raw_response_data, array $input_variab
$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, [] ) );
$this->assertSame( $expected_json, $custom_query_context->process_response( $html_data, [] ) );
}
}
16 changes: 8 additions & 8 deletions tests/inc/Config/QueryRunnerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ public function testExecuteInvalidScheme() {
$result = $query_runner->execute( $input_variables );

$this->assertInstanceOf( WP_Error::class, $result );
$this->assertEquals( 'Invalid endpoint URL scheme', $result->get_error_code() );
$this->assertSame( 'Invalid endpoint URL scheme', $result->get_error_code() );
}

public function testExecuteInvalidHost() {
Expand All @@ -154,7 +154,7 @@ public function testExecuteInvalidHost() {
$result = $query_runner->execute( $input_variables );

$this->assertInstanceOf( WP_Error::class, $result );
$this->assertEquals( 'Invalid endpoint URL parse', $result->get_error_code() );
$this->assertSame( 'Invalid endpoint URL parse', $result->get_error_code() );
}

public function testExecuteHttpClientException() {
Expand All @@ -166,7 +166,7 @@ public function testExecuteHttpClientException() {
$result = $query_runner->execute( $input_variables );

$this->assertInstanceOf( WP_Error::class, $result );
$this->assertEquals( 'remote-data-blocks-unexpected-exception', $result->get_error_code() );
$this->assertSame( 'remote-data-blocks-unexpected-exception', $result->get_error_code() );
}

public function testExecuteBadStatusCode() {
Expand All @@ -179,7 +179,7 @@ public function testExecuteBadStatusCode() {
$result = $query_runner->execute( $input_variables );

$this->assertInstanceOf( WP_Error::class, $result );
$this->assertEquals( 'remote-data-blocks-bad-status-code', $result->get_error_code() );
$this->assertSame( 'remote-data-blocks-bad-status-code', $result->get_error_code() );
}

public function testExecuteSuccessfulResponse() {
Expand Down Expand Up @@ -224,7 +224,7 @@ public function testExecuteSuccessfulResponse() {

$this->assertIsArray( $result['results'] );
$this->assertCount( 1, $result['results'] );
$this->assertEquals( $expected_result, $result['results'][0] );
$this->assertSame( $expected_result, $result['results'][0] );
}

public function testExecuteSuccessfulResponseWithJsonStringResponseData() {
Expand Down Expand Up @@ -266,7 +266,7 @@ public function testExecuteSuccessfulResponseWithJsonStringResponseData() {

$this->assertIsArray( $result['results'] );
$this->assertCount( 1, $result['results'] );
$this->assertEquals( $expected_result, $result['results'][0] );
$this->assertSame( $expected_result, $result['results'][0] );
}

public function testExecuteSuccessfulResponseWithArrayResponseData() {
Expand Down Expand Up @@ -308,7 +308,7 @@ public function testExecuteSuccessfulResponseWithArrayResponseData() {

$this->assertIsArray( $result['results'] );
$this->assertCount( 1, $result['results'] );
$this->assertEquals( $expected_result, $result['results'][0] );
$this->assertSame( $expected_result, $result['results'][0] );
}

public function testExecuteSuccessfulResponseWithObjectResponseData() {
Expand Down Expand Up @@ -355,6 +355,6 @@ public function testExecuteSuccessfulResponseWithObjectResponseData() {

$this->assertIsArray( $result['results'] );
$this->assertCount( 1, $result['results'] );
$this->assertEquals( $expected_result, $result['results'][0] );
$this->assertSame( $expected_result, $result['results'][0] );
}
}
8 changes: 4 additions & 4 deletions tests/inc/Editor/BlockManagement/ConfigurationLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ public function testRegisterBlock() {

$config = ConfigurationLoader::get_configuration( $block_name );
$this->assertIsArray( $config );
$this->assertEquals( $block_name, $config['name'] );
$this->assertEquals( 'Test Block', $config['title'] );
$this->assertSame( $block_name, $config['name'] );
$this->assertSame( 'Test Block', $config['title'] );
$this->assertFalse( $config['loop'] );
}

Expand Down Expand Up @@ -82,7 +82,7 @@ public function testRegisterListQuery() {

$block_name = 'remote-data-blocks/list-block';
$config = ConfigurationLoader::get_configuration( $block_name );
$this->assertEquals( 'list', $config['selectors'][0]['type'] );
$this->assertSame( 'list', $config['selectors'][0]['type'] );
}

public function testRegisterSearchQuery() {
Expand All @@ -96,7 +96,7 @@ public function testRegisterSearchQuery() {

$block_name = 'remote-data-blocks/search-block';
$config = ConfigurationLoader::get_configuration( $block_name );
$this->assertEquals( 'search', $config['selectors'][0]['type'] );
$this->assertSame( 'search', $config['selectors'][0]['type'] );
}

public function testGetBlockNames() {
Expand Down
42 changes: 21 additions & 21 deletions tests/inc/HttpClient/HttpClientTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,22 +34,22 @@ public function testConstructor() {
public function testRequest() {
$this->mock_handler->append( new Response( 200, [], 'Success' ) );
$response = $this->http_client->request( 'GET', '/test' );
$this->assertEquals( 200, $response->getStatusCode() );
$this->assertEquals( 'Success', (string) $response->getBody() );
$this->assertSame( 200, $response->getStatusCode() );
$this->assertSame( 'Success', (string) $response->getBody() );
}

public function testGet() {
$this->mock_handler->append( new Response( 200, [], 'GET Success' ) );
$response = $this->http_client->get( '/test' );
$this->assertEquals( 200, $response->getStatusCode() );
$this->assertEquals( 'GET Success', (string) $response->getBody() );
$this->assertSame( 200, $response->getStatusCode() );
$this->assertSame( 'GET Success', (string) $response->getBody() );
}

public function testPost() {
$this->mock_handler->append( new Response( 201, [], 'POST Success' ) );
$response = $this->http_client->post( '/test' );
$this->assertEquals( 201, $response->getStatusCode() );
$this->assertEquals( 'POST Success', (string) $response->getBody() );
$this->assertSame( 201, $response->getStatusCode() );
$this->assertSame( 'POST Success', (string) $response->getBody() );
}

public function testRetryDecider() {
Expand All @@ -74,7 +74,7 @@ public function testRetryDecider() {
public function testRetryDelay() {
$response = new Response( 429, [ 'Retry-After' => '120' ] );
$delay = HttpClient::retry_delay( 1, $response );
$this->assertEquals( 120000, $delay );
$this->assertSame( 120000, $delay );

$response = new Response( 429, [ 'Retry-After' => ( new \DateTime( '+2 minutes' ) )->format( \DateTime::RFC7231 ) ] );
$delay = HttpClient::retry_delay( 1, $response );
Expand All @@ -83,7 +83,7 @@ public function testRetryDelay() {

$response = new Response( 500 );
$delay = HttpClient::retry_delay( 2, $response );
$this->assertEquals( 2000, $delay );
$this->assertSame( 2000, $delay );
}

public function testQueueRequestAndExecuteParallel() {
Expand All @@ -96,12 +96,12 @@ public function testQueueRequestAndExecuteParallel() {
$results = $this->http_client->execute_parallel();

$this->assertCount( 2, $results );
$this->assertEquals( 'fulfilled', $results[0]['state'] );
$this->assertEquals( 200, $results[0]['value']->getStatusCode() );
$this->assertEquals( 'Response 1', (string) $results[0]['value']->getBody() );
$this->assertEquals( 'fulfilled', $results[1]['state'] );
$this->assertEquals( 201, $results[1]['value']->getStatusCode() );
$this->assertEquals( 'Response 2', (string) $results[1]['value']->getBody() );
$this->assertSame( 'fulfilled', $results[0]['state'] );
$this->assertSame( 200, $results[0]['value']->getStatusCode() );
$this->assertSame( 'Response 1', (string) $results[0]['value']->getBody() );
$this->assertSame( 'fulfilled', $results[1]['state'] );
$this->assertSame( 201, $results[1]['value']->getStatusCode() );
$this->assertSame( 'Response 2', (string) $results[1]['value']->getBody() );
}

public function testQueueRequestAndExecuteParallelWithFailures() {
Expand All @@ -117,16 +117,16 @@ public function testQueueRequestAndExecuteParallelWithFailures() {

$this->assertCount( 3, $results );

$this->assertEquals( 'fulfilled', $results[0]['state'] );
$this->assertEquals( 200, $results[0]['value']->getStatusCode() );
$this->assertEquals( 'Success Response', (string) $results[0]['value']->getBody() );
$this->assertSame( 'fulfilled', $results[0]['state'] );
$this->assertSame( 200, $results[0]['value']->getStatusCode() );
$this->assertSame( 'Success Response', (string) $results[0]['value']->getBody() );

$this->assertEquals( 'rejected', $results[1]['state'] );
$this->assertSame( 'rejected', $results[1]['state'] );
$this->assertInstanceOf( RequestException::class, $results[1]['reason'] );
$this->assertEquals( 'Error', $results[1]['reason']->getMessage() );
$this->assertSame( 'Error', $results[1]['reason']->getMessage() );

$this->assertEquals( 'rejected', $results[2]['state'] );
$this->assertSame( 'rejected', $results[2]['state'] );
$this->assertInstanceOf( ConnectException::class, $results[2]['reason'] );
$this->assertEquals( 'Connection Error', $results[2]['reason']->getMessage() );
$this->assertSame( 'Connection Error', $results[2]['reason']->getMessage() );
}
}
45 changes: 45 additions & 0 deletions tests/inc/WpdbStorage/DataEncryptionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

namespace RemoteDataBlocks\Tests\WpdbStorage;

use PHPUnit\Framework\TestCase;
use RemoteDataBlocks\WpdbStorage\DataEncryption;

class DataEncryptionTest extends TestCase {
private $data_encryption;

protected function setUp(): void {
parent::setUp();
$this->data_encryption = new DataEncryption();
}

public function testEncryptAndDecrypt(): void {
$original_value = 'sensitive data';
$encrypted_value = $this->data_encryption->encrypt( $original_value );

$this->assertNotEquals( $original_value, $encrypted_value );
$this->assertNotFalse( $encrypted_value );

$decrypted_value = $this->data_encryption->decrypt( $encrypted_value );

$this->assertSame( $original_value, $decrypted_value );
}

public function testEncryptWithEmptyString(): void {
$encrypted_value = $this->data_encryption->encrypt( '' );

$this->assertNotFalse( $encrypted_value );
$this->assertNotEmpty( $encrypted_value );

$decrypted_value = $this->data_encryption->decrypt( $encrypted_value );

$this->assertSame( '', $decrypted_value );
}

public function testDecryptWithInvalidInput(): void {
$invalid_input = 'not_encrypted_data';
$decrypted_value = $this->data_encryption->decrypt( $invalid_input );

$this->assertSame( $invalid_input, $decrypted_value );
}
}
20 changes: 10 additions & 10 deletions tests/inc/WpdbStorage/DatasourceCrudTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public function test_validate_airtable_source_with_valid_input() {

$result = DatasourceCrud::validate_airtable_source( $valid_source );
$this->assertIsObject( $result );
$this->assertEquals( $valid_source->uuid, $result->uuid );
$this->assertSame( $valid_source->uuid, $result->uuid );
}

public function test_validate_airtable_source_with_invalid_input() {
Expand All @@ -62,7 +62,7 @@ public function test_validate_shopify_source_with_valid_input() {

$result = DatasourceCrud::validate_shopify_source( $valid_source );
$this->assertIsObject( $result );
$this->assertEquals( $valid_source->uuid, $result->uuid );
$this->assertSame( $valid_source->uuid, $result->uuid );
}

public function test_validate_shopify_source_with_invalid_input() {
Expand Down Expand Up @@ -107,7 +107,7 @@ public function test_validate_google_sheets_source_with_valid_input() {

$result = DatasourceCrud::validate_google_sheets_source( $valid_source );
$this->assertIsObject( $result );
$this->assertEquals( $valid_source->uuid, $result->uuid );
$this->assertSame( $valid_source->uuid, $result->uuid );
}

public function test_validate_google_sheets_source_with_invalid_input() {
Expand Down Expand Up @@ -140,7 +140,7 @@ public function test_validate_source_with_valid_input() {
$result = DatasourceCrud::validate_source( $valid_source );
$this->assertIsObject( $result );
$this->assertObjectHasProperty( 'uuid', $result );
$this->assertEquals( $valid_source->uuid, $result->uuid );
$this->assertSame( $valid_source->uuid, $result->uuid );
}

public function test_validate_source_with_invalid_input() {
Expand All @@ -152,7 +152,7 @@ public function test_validate_source_with_invalid_input() {

$result = DatasourceCrud::validate_source( $invalid_source );
$this->assertInstanceOf( WP_Error::class, $result );
$this->assertEquals( 'invalid_uuid', $result->get_error_code() );
$this->assertSame( 'invalid_uuid', $result->get_error_code() );
}

public function test_register_new_data_source_with_valid_input() {
Expand Down Expand Up @@ -216,11 +216,11 @@ public function test_get_data_sources() {

$airtable_sources = DatasourceCrud::get_data_sources( 'airtable' );
$this->assertCount( 1, $airtable_sources );
$this->assertEquals( 'source-1', $airtable_sources[0]->slug );
$this->assertSame( 'source-1', $airtable_sources[0]->slug );

$shopify_sources = DatasourceCrud::get_data_sources( 'shopify' );
$this->assertCount( 1, $shopify_sources );
$this->assertEquals( 'source-2', $shopify_sources[0]->slug );
$this->assertSame( 'source-2', $shopify_sources[0]->slug );
}

public function test_get_item_by_uuid_with_valid_uuid() {
Expand All @@ -239,7 +239,7 @@ public function test_get_item_by_uuid_with_valid_uuid() {
] );

$retrieved_source = DatasourceCrud::get_item_by_uuid( DatasourceCrud::get_data_sources(), $source->uuid );
$this->assertEquals( $source, $retrieved_source );
$this->assertSame( $source, $retrieved_source );
}

public function test_get_item_by_uuid_with_invalid_uuid() {
Expand Down Expand Up @@ -268,8 +268,8 @@ public function test_update_item_by_uuid_with_valid_uuid() {
] );

$this->assertIsObject( $updated_source );
$this->assertEquals( 'updated_token', $updated_source->token );
$this->assertEquals( 'updated-slug', $updated_source->slug );
$this->assertSame( 'updated_token', $updated_source->token );
$this->assertSame( 'updated-slug', $updated_source->slug );
}

public function test_update_item_by_uuid_with_invalid_uuid() {
Expand Down
2 changes: 2 additions & 0 deletions tests/inc/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
define( 'ABSPATH', __FILE__ );
define( 'REMOTE_DATA_BLOCKS__PLUGIN_DIRECTORY', __DIR__ . '/../../' );
define( 'REMOTE_DATA_BLOCKS__UNIT_TEST', true );
define( 'REMOTE_DATA_BLOCKS_ENCRYPTION_KEY', 'test_encryption_key' );
define( 'REMOTE_DATA_BLOCKS_ENCRYPTION_SALT', 'test_encryption_salt' );

require_once __DIR__ . '/../../vendor/autoload.php';
require_once __DIR__ . '/stubs.php';
Loading

0 comments on commit 3a34625

Please sign in to comment.