Skip to content

Commit

Permalink
Add test for API (#18)
Browse files Browse the repository at this point in the history
* Add test for API

* Add gitignore to data folder

* Fix formatting on PHPCS xml
  • Loading branch information
danielpost authored Jan 19, 2025
1 parent 83b43e8 commit 0c089da
Show file tree
Hide file tree
Showing 4 changed files with 186 additions and 1 deletion.
2 changes: 1 addition & 1 deletion .phpcs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<ruleset xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" name="StarterPlugin" xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/PHPCSStandards/PHP_CodeSniffer/master/phpcs.xsd">

<file>.</file>
<exclude-pattern>/tests/*</exclude-pattern>
<exclude-pattern>/tests/(?!wpunit/).*</exclude-pattern>
<exclude-pattern>/vendor/*</exclude-pattern>
<exclude-pattern>/node_modules/*</exclude-pattern>
<exclude-pattern>/build/*</exclude-pattern>
Expand Down
2 changes: 2 additions & 0 deletions tests/_data/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
*
!.gitignore
Binary file removed tests/_data/db.sqlite
Binary file not shown.
183 changes: 183 additions & 0 deletions tests/wpunit/Bluesky/APITest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,183 @@
<?php

namespace Tests\Bluesky;

use lucatume\WPBrowser\TestCase\WPTestCase;
use Autoblue\Bluesky\API;
use WP_Error;

class APITest extends WPTestCase {
/** @var API */
private $api;

public function setUp(): void {
parent::setUp();
$this->api = new API();
}

public function test_get_did_for_handle_with_valid_handle() {
$handle = 'test.bsky.social';

add_filter(
'pre_http_request',
fn () => [
'response' => [ 'code' => 200 ],
'body' => wp_json_encode( [ 'did' => 'did:plc:testuser123' ] ),
],
);

$result = $this->api->get_did_for_handle( $handle );

$this->assertEquals( 'did:plc:testuser123', $result );
}

public function test_get_did_for_handle_with_empty_handle() {
$result = $this->api->get_did_for_handle( '' );
$this->assertNull( $result );
}

public function test_get_profiles_with_valid_dids() {
$dids = [ 'did:plc:user1', 'did:plc:user2' ];

add_filter(
'pre_http_request',
fn () => [
'response' => [ 'code' => 200 ],
'body' => wp_json_encode(
[
'profiles' => [
[
'did' => 'did:plc:user1',
'handle' => 'user1.bsky.social',
],
[
'did' => 'did:plc:user2',
'handle' => 'user2.bsky.social',
],
],
]
),
],
);

$result = $this->api->get_profiles( $dids );

$this->assertCount( 2, $result );
$this->assertEquals( 'user1.bsky.social', $result[0]['handle'] );
}

public function test_create_session_with_invalid_credentials() {
$result = $this->api->create_session( '', '' );
$this->assertInstanceOf( WP_Error::class, $result );
}

public function test_create_session_with_valid_credentials() {
add_filter(
'pre_http_request',
fn () => [
'response' => [ 'code' => 200 ],
'body' => wp_json_encode(
[
'accessJwt' => 'test-jwt-token',
'refreshJwt' => 'test-refresh-token',
]
),
],
);

$result = $this->api->create_session( 'test-did', 'test-password' );

$this->assertIsArray( $result );
$this->assertArrayHasKey( 'accessJwt', $result );
$this->assertArrayHasKey( 'refreshJwt', $result );
}

public function test_upload_blob_with_invalid_inputs() {
$result = $this->api->upload_blob( '', '', '' );
$this->assertInstanceOf( WP_Error::class, $result );
}

public function test_upload_blob_with_valid_inputs() {
add_filter(
'pre_http_request',
fn () => [
'response' => [ 'code' => 200 ],
'body' => wp_json_encode(
[
'blob' => [
'ref' => [ '$link' => 'test-blob-ref' ],
'mimeType' => 'image/jpeg',
'size' => 1024,
],
]
),
],
);

$result = $this->api->upload_blob(
'test-blob-data',
'image/jpeg',
'test-access-token'
);

$this->assertIsArray( $result );
$this->assertArrayHasKey( 'ref', $result );
}

public function test_search_actors_typeahead() {
add_filter(
'pre_http_request',
fn () => [
'response' => [ 'code' => 200 ],
'body' => wp_json_encode(
[
'actors' => [
[
'did' => 'did:plc:user1',
'handle' => 'user1.bsky.social',
],
[
'did' => 'did:plc:user2',
'handle' => 'user2.bsky.social',
],
],
]
),
],
);

$result = $this->api->search_actors_typeahead( 'test' );

$this->assertIsArray( $result );
$this->assertArrayHasKey( 'actors', $result );
}

public function test_get_post_thread() {
add_filter(
'pre_http_request',
fn () => [
'response' => [ 'code' => 200 ],
'body' => wp_json_encode(
[
'thread' => [
'post' => [
'uri' => 'test-uri',
'text' => 'Test post',
],
],
]
),
],
);

$result = $this->api->get_post_thread( 'test-uri' );

$this->assertIsArray( $result );
$this->assertArrayHasKey( 'thread', $result );
}

protected function tearDown(): void {
parent::tearDown();
remove_all_filters( 'pre_http_request' );
}
}

0 comments on commit 0c089da

Please sign in to comment.