Skip to content

Commit 3a34625

Browse files
authored
Sprinkle some more tests (#61)
* First pass * ok * Ignore example * yaya * mmmk * Lint fixes * ugh
1 parent 206db85 commit 3a34625

File tree

12 files changed

+321
-54
lines changed

12 files changed

+321
-54
lines changed

.github/codecov.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@ coverage:
99
informational: true
1010
github_checks:
1111
annotations: false
12+
ignore:
13+
- 'example'

tests/inc/Config/QueryContextTest.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ protected function setUp(): void {
1919

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

2525
public function testGetImageUrl() {
@@ -35,28 +35,28 @@ public function testGetMetadata() {
3535

3636
$this->assertArrayHasKey( 'last_updated', $metadata );
3737
$this->assertArrayHasKey( 'total_count', $metadata );
38-
$this->assertEquals( 'Last updated', $metadata['last_updated']['name'] );
39-
$this->assertEquals( 'string', $metadata['last_updated']['type'] );
40-
$this->assertEquals( 'Total count', $metadata['total_count']['name'] );
41-
$this->assertEquals( 'number', $metadata['total_count']['type'] );
42-
$this->assertEquals( 2, $metadata['total_count']['value'] );
38+
$this->assertSame( 'Last updated', $metadata['last_updated']['name'] );
39+
$this->assertSame( 'string', $metadata['last_updated']['type'] );
40+
$this->assertSame( 'Total count', $metadata['total_count']['name'] );
41+
$this->assertSame( 'number', $metadata['total_count']['type'] );
42+
$this->assertSame( 2, $metadata['total_count']['value'] );
4343
}
4444

4545
public function testGetRequestMethod() {
46-
$this->assertEquals( 'GET', $this->query_context->get_request_method() );
46+
$this->assertSame( 'GET', $this->query_context->get_request_method() );
4747
}
4848

4949
public function testGetRequestHeaders() {
5050
$result = $this->query_context->get_request_headers( [] );
51-
$this->assertEquals( [ 'Content-Type' => 'application/json' ], $result );
51+
$this->assertSame( [ 'Content-Type' => 'application/json' ], $result );
5252
}
5353

5454
public function testGetRequestBody() {
5555
$this->assertNull( $this->query_context->get_request_body( [] ) );
5656
}
5757

5858
public function testGetQueryName() {
59-
$this->assertEquals( 'Query', $this->query_context->get_query_name() );
59+
$this->assertSame( 'Query', $this->query_context->get_query_name() );
6060
}
6161

6262
public function testIsResponseDataCollection() {
@@ -68,7 +68,7 @@ public function testIsResponseDataCollection() {
6868

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

7474
public function testCustomProcessResponse() {
@@ -98,6 +98,6 @@ public function process_response( string $raw_response_data, array $input_variab
9898
$html_data = '<html><head><title>Test Page</title></head><body><p>Paragraph 1</p><p>Paragraph 2</p></body></html>';
9999
$expected_json = '{"title":"Test Page","content":["Paragraph 1","Paragraph 2"]}';
100100

101-
$this->assertEquals( $expected_json, $custom_query_context->process_response( $html_data, [] ) );
101+
$this->assertSame( $expected_json, $custom_query_context->process_response( $html_data, [] ) );
102102
}
103103
}

tests/inc/Config/QueryRunnerTest.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ public function testExecuteInvalidScheme() {
142142
$result = $query_runner->execute( $input_variables );
143143

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

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

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

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

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

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

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

185185
public function testExecuteSuccessfulResponse() {
@@ -224,7 +224,7 @@ public function testExecuteSuccessfulResponse() {
224224

225225
$this->assertIsArray( $result['results'] );
226226
$this->assertCount( 1, $result['results'] );
227-
$this->assertEquals( $expected_result, $result['results'][0] );
227+
$this->assertSame( $expected_result, $result['results'][0] );
228228
}
229229

230230
public function testExecuteSuccessfulResponseWithJsonStringResponseData() {
@@ -266,7 +266,7 @@ public function testExecuteSuccessfulResponseWithJsonStringResponseData() {
266266

267267
$this->assertIsArray( $result['results'] );
268268
$this->assertCount( 1, $result['results'] );
269-
$this->assertEquals( $expected_result, $result['results'][0] );
269+
$this->assertSame( $expected_result, $result['results'][0] );
270270
}
271271

272272
public function testExecuteSuccessfulResponseWithArrayResponseData() {
@@ -308,7 +308,7 @@ public function testExecuteSuccessfulResponseWithArrayResponseData() {
308308

309309
$this->assertIsArray( $result['results'] );
310310
$this->assertCount( 1, $result['results'] );
311-
$this->assertEquals( $expected_result, $result['results'][0] );
311+
$this->assertSame( $expected_result, $result['results'][0] );
312312
}
313313

314314
public function testExecuteSuccessfulResponseWithObjectResponseData() {
@@ -355,6 +355,6 @@ public function testExecuteSuccessfulResponseWithObjectResponseData() {
355355

356356
$this->assertIsArray( $result['results'] );
357357
$this->assertCount( 1, $result['results'] );
358-
$this->assertEquals( $expected_result, $result['results'][0] );
358+
$this->assertSame( $expected_result, $result['results'][0] );
359359
}
360360
}

tests/inc/Editor/BlockManagement/ConfigurationLoaderTest.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ public function testRegisterBlock() {
3232

3333
$config = ConfigurationLoader::get_configuration( $block_name );
3434
$this->assertIsArray( $config );
35-
$this->assertEquals( $block_name, $config['name'] );
36-
$this->assertEquals( 'Test Block', $config['title'] );
35+
$this->assertSame( $block_name, $config['name'] );
36+
$this->assertSame( 'Test Block', $config['title'] );
3737
$this->assertFalse( $config['loop'] );
3838
}
3939

@@ -82,7 +82,7 @@ public function testRegisterListQuery() {
8282

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

8888
public function testRegisterSearchQuery() {
@@ -96,7 +96,7 @@ public function testRegisterSearchQuery() {
9696

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

102102
public function testGetBlockNames() {

tests/inc/HttpClient/HttpClientTest.php

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,22 @@ public function testConstructor() {
3434
public function testRequest() {
3535
$this->mock_handler->append( new Response( 200, [], 'Success' ) );
3636
$response = $this->http_client->request( 'GET', '/test' );
37-
$this->assertEquals( 200, $response->getStatusCode() );
38-
$this->assertEquals( 'Success', (string) $response->getBody() );
37+
$this->assertSame( 200, $response->getStatusCode() );
38+
$this->assertSame( 'Success', (string) $response->getBody() );
3939
}
4040

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

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

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

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

8484
$response = new Response( 500 );
8585
$delay = HttpClient::retry_delay( 2, $response );
86-
$this->assertEquals( 2000, $delay );
86+
$this->assertSame( 2000, $delay );
8787
}
8888

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

9898
$this->assertCount( 2, $results );
99-
$this->assertEquals( 'fulfilled', $results[0]['state'] );
100-
$this->assertEquals( 200, $results[0]['value']->getStatusCode() );
101-
$this->assertEquals( 'Response 1', (string) $results[0]['value']->getBody() );
102-
$this->assertEquals( 'fulfilled', $results[1]['state'] );
103-
$this->assertEquals( 201, $results[1]['value']->getStatusCode() );
104-
$this->assertEquals( 'Response 2', (string) $results[1]['value']->getBody() );
99+
$this->assertSame( 'fulfilled', $results[0]['state'] );
100+
$this->assertSame( 200, $results[0]['value']->getStatusCode() );
101+
$this->assertSame( 'Response 1', (string) $results[0]['value']->getBody() );
102+
$this->assertSame( 'fulfilled', $results[1]['state'] );
103+
$this->assertSame( 201, $results[1]['value']->getStatusCode() );
104+
$this->assertSame( 'Response 2', (string) $results[1]['value']->getBody() );
105105
}
106106

107107
public function testQueueRequestAndExecuteParallelWithFailures() {
@@ -117,16 +117,16 @@ public function testQueueRequestAndExecuteParallelWithFailures() {
117117

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

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

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

128-
$this->assertEquals( 'rejected', $results[2]['state'] );
128+
$this->assertSame( 'rejected', $results[2]['state'] );
129129
$this->assertInstanceOf( ConnectException::class, $results[2]['reason'] );
130-
$this->assertEquals( 'Connection Error', $results[2]['reason']->getMessage() );
130+
$this->assertSame( 'Connection Error', $results[2]['reason']->getMessage() );
131131
}
132132
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
<?php
2+
3+
namespace RemoteDataBlocks\Tests\WpdbStorage;
4+
5+
use PHPUnit\Framework\TestCase;
6+
use RemoteDataBlocks\WpdbStorage\DataEncryption;
7+
8+
class DataEncryptionTest extends TestCase {
9+
private $data_encryption;
10+
11+
protected function setUp(): void {
12+
parent::setUp();
13+
$this->data_encryption = new DataEncryption();
14+
}
15+
16+
public function testEncryptAndDecrypt(): void {
17+
$original_value = 'sensitive data';
18+
$encrypted_value = $this->data_encryption->encrypt( $original_value );
19+
20+
$this->assertNotEquals( $original_value, $encrypted_value );
21+
$this->assertNotFalse( $encrypted_value );
22+
23+
$decrypted_value = $this->data_encryption->decrypt( $encrypted_value );
24+
25+
$this->assertSame( $original_value, $decrypted_value );
26+
}
27+
28+
public function testEncryptWithEmptyString(): void {
29+
$encrypted_value = $this->data_encryption->encrypt( '' );
30+
31+
$this->assertNotFalse( $encrypted_value );
32+
$this->assertNotEmpty( $encrypted_value );
33+
34+
$decrypted_value = $this->data_encryption->decrypt( $encrypted_value );
35+
36+
$this->assertSame( '', $decrypted_value );
37+
}
38+
39+
public function testDecryptWithInvalidInput(): void {
40+
$invalid_input = 'not_encrypted_data';
41+
$decrypted_value = $this->data_encryption->decrypt( $invalid_input );
42+
43+
$this->assertSame( $invalid_input, $decrypted_value );
44+
}
45+
}

tests/inc/WpdbStorage/DatasourceCrudTest.php

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public function test_validate_airtable_source_with_valid_input() {
3838

3939
$result = DatasourceCrud::validate_airtable_source( $valid_source );
4040
$this->assertIsObject( $result );
41-
$this->assertEquals( $valid_source->uuid, $result->uuid );
41+
$this->assertSame( $valid_source->uuid, $result->uuid );
4242
}
4343

4444
public function test_validate_airtable_source_with_invalid_input() {
@@ -62,7 +62,7 @@ public function test_validate_shopify_source_with_valid_input() {
6262

6363
$result = DatasourceCrud::validate_shopify_source( $valid_source );
6464
$this->assertIsObject( $result );
65-
$this->assertEquals( $valid_source->uuid, $result->uuid );
65+
$this->assertSame( $valid_source->uuid, $result->uuid );
6666
}
6767

6868
public function test_validate_shopify_source_with_invalid_input() {
@@ -107,7 +107,7 @@ public function test_validate_google_sheets_source_with_valid_input() {
107107

108108
$result = DatasourceCrud::validate_google_sheets_source( $valid_source );
109109
$this->assertIsObject( $result );
110-
$this->assertEquals( $valid_source->uuid, $result->uuid );
110+
$this->assertSame( $valid_source->uuid, $result->uuid );
111111
}
112112

113113
public function test_validate_google_sheets_source_with_invalid_input() {
@@ -140,7 +140,7 @@ public function test_validate_source_with_valid_input() {
140140
$result = DatasourceCrud::validate_source( $valid_source );
141141
$this->assertIsObject( $result );
142142
$this->assertObjectHasProperty( 'uuid', $result );
143-
$this->assertEquals( $valid_source->uuid, $result->uuid );
143+
$this->assertSame( $valid_source->uuid, $result->uuid );
144144
}
145145

146146
public function test_validate_source_with_invalid_input() {
@@ -152,7 +152,7 @@ public function test_validate_source_with_invalid_input() {
152152

153153
$result = DatasourceCrud::validate_source( $invalid_source );
154154
$this->assertInstanceOf( WP_Error::class, $result );
155-
$this->assertEquals( 'invalid_uuid', $result->get_error_code() );
155+
$this->assertSame( 'invalid_uuid', $result->get_error_code() );
156156
}
157157

158158
public function test_register_new_data_source_with_valid_input() {
@@ -216,11 +216,11 @@ public function test_get_data_sources() {
216216

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

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

226226
public function test_get_item_by_uuid_with_valid_uuid() {
@@ -239,7 +239,7 @@ public function test_get_item_by_uuid_with_valid_uuid() {
239239
] );
240240

241241
$retrieved_source = DatasourceCrud::get_item_by_uuid( DatasourceCrud::get_data_sources(), $source->uuid );
242-
$this->assertEquals( $source, $retrieved_source );
242+
$this->assertSame( $source, $retrieved_source );
243243
}
244244

245245
public function test_get_item_by_uuid_with_invalid_uuid() {
@@ -268,8 +268,8 @@ public function test_update_item_by_uuid_with_valid_uuid() {
268268
] );
269269

270270
$this->assertIsObject( $updated_source );
271-
$this->assertEquals( 'updated_token', $updated_source->token );
272-
$this->assertEquals( 'updated-slug', $updated_source->slug );
271+
$this->assertSame( 'updated_token', $updated_source->token );
272+
$this->assertSame( 'updated-slug', $updated_source->slug );
273273
}
274274

275275
public function test_update_item_by_uuid_with_invalid_uuid() {

tests/inc/bootstrap.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
define( 'ABSPATH', __FILE__ );
33
define( 'REMOTE_DATA_BLOCKS__PLUGIN_DIRECTORY', __DIR__ . '/../../' );
44
define( 'REMOTE_DATA_BLOCKS__UNIT_TEST', true );
5+
define( 'REMOTE_DATA_BLOCKS_ENCRYPTION_KEY', 'test_encryption_key' );
6+
define( 'REMOTE_DATA_BLOCKS_ENCRYPTION_SALT', 'test_encryption_salt' );
57

68
require_once __DIR__ . '/../../vendor/autoload.php';
79
require_once __DIR__ . '/stubs.php';

0 commit comments

Comments
 (0)