Skip to content

Commit df8e352

Browse files
committed
Interfaces introduced
1 parent d566aae commit df8e352

16 files changed

+88
-38
lines changed

includes/AdminWidget.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,17 @@
22

33
namespace lloc\Nowpayments;
44

5-
use lloc\Nowpayments\Integration\ApiStatus;
5+
use lloc\Nowpayments\Integration\ApiStatusInterface;
66

77
class AdminWidget {
88

99
public const WIDGET_ID = 'nowpayments_status_widget';
1010

1111
public function __construct(
12-
protected readonly ApiStatus $status
12+
protected readonly ApiStatusInterface $status
1313
) { }
1414

15-
public static function create( ApiStatus $status ): AdminWidget {
15+
public static function create( ApiStatusInterface $status ): AdminWidget {
1616
$obj = new self( $status );
1717

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

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

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

32-
echo wp_kses_post( '<div>' . sprintf( $format, $service, $arr['message'] ) . '</div>' );
32+
echo wp_kses_post( '<div>' . sprintf( $format, $service, $message ) . '</div>' );
3333
}
3434
}

includes/Integration/ApiStatus.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use lloc\Nowpayments\Rest\Endpoint;
66

7-
class ApiStatus extends Endpoint {
7+
final class ApiStatus extends Endpoint implements ApiStatusInterface {
88

99
/**
1010
* @return string[]
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php declare( strict_types=1 );
2+
3+
namespace lloc\Nowpayments\Integration;
4+
5+
use lloc\Nowpayments\Rest\EndpointInterface;
6+
7+
interface ApiStatusInterface extends EndpointInterface {
8+
9+
10+
/**
11+
* @return string[]
12+
*/
13+
public function get(): array;
14+
}

includes/Integration/AvailableCurrencies.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use lloc\Nowpayments\Rest\Endpoint;
66

7-
class AvailableCurrencies extends Endpoint {
7+
final class AvailableCurrencies extends Endpoint {
88

99
/**
1010
* @return array<string, string[]>

includes/Integration/EstimatedPrice.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use lloc\Nowpayments\Rest\Endpoint;
66

7-
class EstimatedPrice extends Endpoint {
7+
final class EstimatedPrice extends Endpoint {
88

99
/**
1010
* @param float $amount

includes/Integration/MinimumPaymentAmount.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use lloc\Nowpayments\Rest\Endpoint;
66

7-
class MinimumPaymentAmount extends Endpoint {
7+
final class MinimumPaymentAmount extends Endpoint {
88

99
/**
1010
* @param string $currency_from

includes/Integration/Payment.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use lloc\Nowpayments\Rest\Endpoint;
66

7-
class Payment extends Endpoint {
7+
final class Payment extends Endpoint {
88

99
public const ADDITIONAL_PARAMS = array(
1010
'pay_amount',

includes/Integration/PaymentStatus.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use lloc\Nowpayments\Rest\Endpoint;
66

7-
class PaymentStatus extends Endpoint {
7+
final class PaymentStatus extends Endpoint {
88

99
public const ENDPOINT = 'v1/payment';
1010

includes/Rest/Client.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@ public function get_service(): Service {
2323
* @param string[] $body
2424
* @param string[] $headers
2525
*
26-
* @return Result
26+
* @return ResultInterface
2727
*/
28-
public function get( string $endpoint, array $body = array(), array $headers = array() ): Result {
28+
public function get( string $endpoint, array $body = array(), array $headers = array() ): ResultInterface {
2929
$url = add_query_arg( $body, $this->service->get( $endpoint ) );
3030

3131
if ( ! empty( $headers ) ) {
@@ -42,9 +42,9 @@ public function get( string $endpoint, array $body = array(), array $headers = a
4242
* @param string[] $body
4343
* @param string[] $headers
4444
*
45-
* @return Result
45+
* @return ResultInterface
4646
*/
47-
public function post( string $endpoint, array $body = array(), array $headers = array() ): Result {
47+
public function post( string $endpoint, array $body = array(), array $headers = array() ): ResultInterface {
4848
$url = $this->service->get( $endpoint );
4949
$args = array();
5050

includes/Rest/EndpointInterface.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php
2+
3+
namespace lloc\Nowpayments\Rest;
4+
5+
interface EndpointInterface {
6+
7+
8+
/**
9+
* @return Client
10+
*/
11+
public function get_client(): Client;
12+
}

includes/Rest/Error.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use WP_Error;
66

7-
class Error implements Result {
7+
class Error implements ResultInterface {
88

99
/**
1010
* @param WP_Error $error

includes/Rest/MethodGetInterface.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
<?php declare( strict_types=1 );
2+
3+
namespace lloc\Nowpayments\Rest;
4+
5+
interface MethodGetInterface {
6+
7+
8+
/**
9+
* @return string[]
10+
*/
11+
public function get(): array;
12+
}

includes/Rest/Response.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace lloc\Nowpayments\Rest;
44

5-
class Response implements Result {
5+
class Response implements ResultInterface {
66

77
/**
88
* @var string[]

includes/Rest/Result.php renamed to includes/Rest/ResultInterface.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace lloc\Nowpayments\Rest;
44

5-
interface Result {
5+
interface ResultInterface {
66

77
/**
88
* @return string[]

tests/Integration/TestAvailableCurrencies.php

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,21 @@ class TestAvailableCurrencies extends LlocTestCase {
1414

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

17+
/**
18+
* Test Setup
19+
*
20+
* @return void
21+
*/
22+
public function setUp(): void {
23+
parent::setUp();
24+
25+
$response = \Mockery::mock( Response::class );
26+
$response->shouldReceive( 'get' )->andReturn( self::EXPECTED );
27+
28+
$this->client = \Mockery::mock( Client::class );
29+
$this->client->shouldReceive( 'get' )->andReturn( $response );
30+
}
31+
1732
/**
1833
* Method demonstrates how AvailableCurrencies works
1934
*
@@ -28,32 +43,27 @@ public function test_get(): void {
2843
}
2944

3045
/**
31-
* The is_available method looks a currency in the cached result-set up
46+
* Data provider for is_available method
3247
*
33-
* @return void
48+
* @return array<int, array<int, string|bool>>
3449
*/
35-
public function test_is_available(): void {
36-
Functions\expect( 'wp_cache_get' )->atLeast()->once()->andReturn( self::EXPECTED );
37-
38-
$this->assertFalse( ( new AvailableCurrencies( $this->client ) )->is_available( 'bch' ) );
39-
$this->assertTrue( ( new AvailableCurrencies( $this->client ) )->is_available( 'ada' ) );
40-
$this->assertTrue( ( new AvailableCurrencies( $this->client ) )->is_available( 'btc' ) );
41-
$this->assertTrue( ( new AvailableCurrencies( $this->client ) )->is_available( 'eur' ) );
50+
public static function provide_data_for_is_available(): array {
51+
return array(
52+
array( 'bch', false ),
53+
array( 'ada', true ),
54+
array( 'btc', true ),
55+
array( 'eur', true ),
56+
);
4257
}
43-
4458
/**
45-
* Test Setup
59+
* The is_available method looks a currency in the cached result-set up
4660
*
47-
* @return void
61+
* @dataProvider provide_data_for_is_available
4862
*/
49-
public function setUp(): void {
50-
parent::setUp();
51-
52-
$response = \Mockery::mock( Response::class );
53-
$response->shouldReceive( 'get' )->andReturn( self::EXPECTED );
63+
public function test_is_available( string $currency, bool $expected ): void {
64+
Functions\expect( 'wp_cache_get' )->atLeast()->once()->andReturn( self::EXPECTED );
5465

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

5969
/**

tests/TestAdminWidget.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,22 @@
44

55
use lloc\Nowpayments\AdminWidget;
66
use lloc\Nowpayments\Integration\ApiStatus;
7+
use lloc\Nowpayments\Integration\ApiStatusInterface;
78
use lloc\Nowpayments\Rest\Client;
89
use lloc\Nowpayments\Rest\Service;
910
use Brain\Monkey\Functions;
1011

1112
class TestAdminWidget extends LlocTestCase {
1213

1314
public function test_render() {
15+
1416
$service = \Mockery::mock( Service::class );
1517
$service->shouldReceive( 'info' )->once()->andReturn( 'abc' );
1618

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

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

0 commit comments

Comments
 (0)