diff --git a/includes/AdminWidget.php b/includes/AdminWidget.php index 0c166f9..dd4df8f 100644 --- a/includes/AdminWidget.php +++ b/includes/AdminWidget.php @@ -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' ); @@ -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( '%s', $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( '
' . sprintf( $format, $service, $arr['message'] ) . '
' ); + echo wp_kses_post( '
' . sprintf( $format, $service, $message ) . '
' ); } } diff --git a/includes/Integration/ApiStatus.php b/includes/Integration/ApiStatus.php index 6618480..d956314 100644 --- a/includes/Integration/ApiStatus.php +++ b/includes/Integration/ApiStatus.php @@ -4,7 +4,7 @@ use lloc\Nowpayments\Rest\Endpoint; -class ApiStatus extends Endpoint { +final class ApiStatus extends Endpoint implements ApiStatusInterface { /** * @return string[] diff --git a/includes/Integration/ApiStatusInterface.php b/includes/Integration/ApiStatusInterface.php new file mode 100644 index 0000000..9d61e6d --- /dev/null +++ b/includes/Integration/ApiStatusInterface.php @@ -0,0 +1,14 @@ + diff --git a/includes/Integration/EstimatedPrice.php b/includes/Integration/EstimatedPrice.php index b4810b3..8b60646 100644 --- a/includes/Integration/EstimatedPrice.php +++ b/includes/Integration/EstimatedPrice.php @@ -4,7 +4,7 @@ use lloc\Nowpayments\Rest\Endpoint; -class EstimatedPrice extends Endpoint { +final class EstimatedPrice extends Endpoint { /** * @param float $amount diff --git a/includes/Integration/MinimumPaymentAmount.php b/includes/Integration/MinimumPaymentAmount.php index 270df0f..1aaf6de 100644 --- a/includes/Integration/MinimumPaymentAmount.php +++ b/includes/Integration/MinimumPaymentAmount.php @@ -4,7 +4,7 @@ use lloc\Nowpayments\Rest\Endpoint; -class MinimumPaymentAmount extends Endpoint { +final class MinimumPaymentAmount extends Endpoint { /** * @param string $currency_from diff --git a/includes/Integration/Payment.php b/includes/Integration/Payment.php index a489903..cae4d32 100644 --- a/includes/Integration/Payment.php +++ b/includes/Integration/Payment.php @@ -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', diff --git a/includes/Integration/PaymentStatus.php b/includes/Integration/PaymentStatus.php index 65af3d2..719804e 100644 --- a/includes/Integration/PaymentStatus.php +++ b/includes/Integration/PaymentStatus.php @@ -4,7 +4,7 @@ use lloc\Nowpayments\Rest\Endpoint; -class PaymentStatus extends Endpoint { +final class PaymentStatus extends Endpoint { public const ENDPOINT = 'v1/payment'; diff --git a/includes/Rest/Client.php b/includes/Rest/Client.php index 6b2fff1..2c18820 100644 --- a/includes/Rest/Client.php +++ b/includes/Rest/Client.php @@ -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 ) ) { @@ -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(); diff --git a/includes/Rest/EndpointInterface.php b/includes/Rest/EndpointInterface.php new file mode 100644 index 0000000..9698949 --- /dev/null +++ b/includes/Rest/EndpointInterface.php @@ -0,0 +1,12 @@ + 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 * @@ -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> */ - 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 ) ); } /** diff --git a/tests/TestAdminWidget.php b/tests/TestAdminWidget.php index ac61f8f..43a3441 100644 --- a/tests/TestAdminWidget.php +++ b/tests/TestAdminWidget.php @@ -4,6 +4,7 @@ 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; @@ -11,13 +12,14 @@ 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 );