Skip to content

Commit

Permalink
Interfaces introduced
Browse files Browse the repository at this point in the history
  • Loading branch information
lloc committed Dec 4, 2024
1 parent d566aae commit df8e352
Show file tree
Hide file tree
Showing 16 changed files with 88 additions and 38 deletions.
10 changes: 5 additions & 5 deletions includes/AdminWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,17 @@

namespace lloc\Nowpayments;

use lloc\Nowpayments\Integration\ApiStatus;
use lloc\Nowpayments\Integration\ApiStatusInterface;

class AdminWidget {

public const WIDGET_ID = 'nowpayments_status_widget';

public function __construct(
protected readonly ApiStatus $status
protected readonly ApiStatusInterface $status
) { }

public static function create( ApiStatus $status ): AdminWidget {
public static function create( ApiStatusInterface $status ): AdminWidget {
$obj = new self( $status );

$widget_name = __( 'Nowpayments Status', 'wp-nowpayments-integration' );
Expand All @@ -23,12 +23,12 @@ public static function create( ApiStatus $status ): AdminWidget {
}

public function render(): void {
$arr = $this->status->get();
$message = $this->status->get()['message'] ?? '';
$service = sprintf( '<strong>%s</strong>', $this->status->get_client()->get_service()->info() );

/* translators: 1: service name, 2: message */
$format = __( '%1$s responds with "%2$s".', 'wp-nowpayments-integration' );

echo wp_kses_post( '<div>' . sprintf( $format, $service, $arr['message'] ) . '</div>' );
echo wp_kses_post( '<div>' . sprintf( $format, $service, $message ) . '</div>' );
}
}
2 changes: 1 addition & 1 deletion includes/Integration/ApiStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use lloc\Nowpayments\Rest\Endpoint;

class ApiStatus extends Endpoint {
final class ApiStatus extends Endpoint implements ApiStatusInterface {

/**
* @return string[]
Expand Down
14 changes: 14 additions & 0 deletions includes/Integration/ApiStatusInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php declare( strict_types=1 );

namespace lloc\Nowpayments\Integration;

use lloc\Nowpayments\Rest\EndpointInterface;

interface ApiStatusInterface extends EndpointInterface {


/**
* @return string[]
*/
public function get(): array;
}
2 changes: 1 addition & 1 deletion includes/Integration/AvailableCurrencies.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use lloc\Nowpayments\Rest\Endpoint;

class AvailableCurrencies extends Endpoint {
final class AvailableCurrencies extends Endpoint {

/**
* @return array<string, string[]>
Expand Down
2 changes: 1 addition & 1 deletion includes/Integration/EstimatedPrice.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use lloc\Nowpayments\Rest\Endpoint;

class EstimatedPrice extends Endpoint {
final class EstimatedPrice extends Endpoint {

/**
* @param float $amount
Expand Down
2 changes: 1 addition & 1 deletion includes/Integration/MinimumPaymentAmount.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use lloc\Nowpayments\Rest\Endpoint;

class MinimumPaymentAmount extends Endpoint {
final class MinimumPaymentAmount extends Endpoint {

/**
* @param string $currency_from
Expand Down
2 changes: 1 addition & 1 deletion includes/Integration/Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use lloc\Nowpayments\Rest\Endpoint;

class Payment extends Endpoint {
final class Payment extends Endpoint {

public const ADDITIONAL_PARAMS = array(
'pay_amount',
Expand Down
2 changes: 1 addition & 1 deletion includes/Integration/PaymentStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use lloc\Nowpayments\Rest\Endpoint;

class PaymentStatus extends Endpoint {
final class PaymentStatus extends Endpoint {

public const ENDPOINT = 'v1/payment';

Expand Down
8 changes: 4 additions & 4 deletions includes/Rest/Client.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,9 @@ public function get_service(): Service {
* @param string[] $body
* @param string[] $headers
*
* @return Result
* @return ResultInterface
*/
public function get( string $endpoint, array $body = array(), array $headers = array() ): Result {
public function get( string $endpoint, array $body = array(), array $headers = array() ): ResultInterface {
$url = add_query_arg( $body, $this->service->get( $endpoint ) );

if ( ! empty( $headers ) ) {
Expand All @@ -42,9 +42,9 @@ public function get( string $endpoint, array $body = array(), array $headers = a
* @param string[] $body
* @param string[] $headers
*
* @return Result
* @return ResultInterface
*/
public function post( string $endpoint, array $body = array(), array $headers = array() ): Result {
public function post( string $endpoint, array $body = array(), array $headers = array() ): ResultInterface {
$url = $this->service->get( $endpoint );
$args = array();

Expand Down
12 changes: 12 additions & 0 deletions includes/Rest/EndpointInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

namespace lloc\Nowpayments\Rest;

interface EndpointInterface {


/**
* @return Client
*/
public function get_client(): Client;
}
2 changes: 1 addition & 1 deletion includes/Rest/Error.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use WP_Error;

class Error implements Result {
class Error implements ResultInterface {

/**
* @param WP_Error $error
Expand Down
12 changes: 12 additions & 0 deletions includes/Rest/MethodGetInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php declare( strict_types=1 );

namespace lloc\Nowpayments\Rest;

interface MethodGetInterface {


/**
* @return string[]
*/
public function get(): array;
}
2 changes: 1 addition & 1 deletion includes/Rest/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace lloc\Nowpayments\Rest;

class Response implements Result {
class Response implements ResultInterface {

/**
* @var string[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace lloc\Nowpayments\Rest;

interface Result {
interface ResultInterface {

/**
* @return string[]
Expand Down
48 changes: 29 additions & 19 deletions tests/Integration/TestAvailableCurrencies.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,21 @@ class TestAvailableCurrencies extends LlocTestCase {

public const EXPECTED = array( 'currencies' => array( 'ada', 'btc', 'eur' ) );

/**
* Test Setup
*
* @return void
*/
public function setUp(): void {
parent::setUp();

$response = \Mockery::mock( Response::class );
$response->shouldReceive( 'get' )->andReturn( self::EXPECTED );

$this->client = \Mockery::mock( Client::class );
$this->client->shouldReceive( 'get' )->andReturn( $response );
}

/**
* Method demonstrates how AvailableCurrencies works
*
Expand All @@ -28,32 +43,27 @@ public function test_get(): void {
}

/**
* The is_available method looks a currency in the cached result-set up
* Data provider for is_available method
*
* @return void
* @return array<int, array<int, string|bool>>
*/
public function test_is_available(): void {
Functions\expect( 'wp_cache_get' )->atLeast()->once()->andReturn( self::EXPECTED );

$this->assertFalse( ( new AvailableCurrencies( $this->client ) )->is_available( 'bch' ) );
$this->assertTrue( ( new AvailableCurrencies( $this->client ) )->is_available( 'ada' ) );
$this->assertTrue( ( new AvailableCurrencies( $this->client ) )->is_available( 'btc' ) );
$this->assertTrue( ( new AvailableCurrencies( $this->client ) )->is_available( 'eur' ) );
public static function provide_data_for_is_available(): array {
return array(
array( 'bch', false ),
array( 'ada', true ),
array( 'btc', true ),
array( 'eur', true ),
);
}

/**
* Test Setup
* The is_available method looks a currency in the cached result-set up
*
* @return void
* @dataProvider provide_data_for_is_available
*/
public function setUp(): void {
parent::setUp();

$response = \Mockery::mock( Response::class );
$response->shouldReceive( 'get' )->andReturn( self::EXPECTED );
public function test_is_available( string $currency, bool $expected ): void {
Functions\expect( 'wp_cache_get' )->atLeast()->once()->andReturn( self::EXPECTED );

$this->client = \Mockery::mock( Client::class );
$this->client->shouldReceive( 'get' )->andReturn( $response );
$this->assertEquals( $expected, ( new AvailableCurrencies( $this->client ) )->is_available( $currency ) );
}

/**
Expand Down
4 changes: 3 additions & 1 deletion tests/TestAdminWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,22 @@

use lloc\Nowpayments\AdminWidget;
use lloc\Nowpayments\Integration\ApiStatus;
use lloc\Nowpayments\Integration\ApiStatusInterface;
use lloc\Nowpayments\Rest\Client;
use lloc\Nowpayments\Rest\Service;
use Brain\Monkey\Functions;

class TestAdminWidget extends LlocTestCase {

public function test_render() {

$service = \Mockery::mock( Service::class );
$service->shouldReceive( 'info' )->once()->andReturn( 'abc' );

$client = \Mockery::mock( Client::class );
$client->shouldReceive( 'get_service' )->once()->andReturn( $service );

$status = \Mockery::mock( ApiStatus::class );
$status = \Mockery::mock( ApiStatusInterface::class );
$status->shouldReceive( 'get' )->once()->andReturn( array( 'message' => 'def' ) );
$status->shouldReceive( 'get_client' )->once()->andReturn( $client );

Expand Down

0 comments on commit df8e352

Please sign in to comment.