From ca8a6c11ccb2221b8edfede24890c13e6679c568 Mon Sep 17 00:00:00 2001 From: Dennis Ploetner Date: Sat, 7 Dec 2024 16:34:58 +0100 Subject: [PATCH 1/2] Tests finished --- .gitignore | 41 ++++++------------- includes/Integration/ApiStatus.php | 2 +- includes/Integration/AvailableCurrencies.php | 2 +- includes/Integration/EstimatedPrice.php | 2 +- includes/Integration/MinimumPaymentAmount.php | 2 +- includes/Integration/Payment.php | 2 +- includes/Rest/Endpoint.php | 10 ----- .../Services/AvailableCurrenciesService.php | 8 ++-- tests/Services/TestApiStatusService.php | 38 +++++++++++++++++ .../TestAvailableCurrenciesService.php | 41 +++++++++++++++++++ tests/TestAdminWidget.php | 20 +++++++++ tests/TestPlugin.php | 2 +- 12 files changed, 121 insertions(+), 49 deletions(-) create mode 100644 tests/Services/TestApiStatusService.php create mode 100644 tests/Services/TestAvailableCurrenciesService.php diff --git a/.gitignore b/.gitignore index 08f777d..e12b62c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,36 +1,21 @@ +*.log +*.tgz +*.zip +.env +.eslintcache .idea/ +.npm .phpunit.result.cache +build/ +js/ composer.lock -package-lock.json - -# Logs logs -*.log +node_modules/ npm-debug.log* -yarn-debug.log* -yarn-error.log* - -# Coverage directory +package-lock.json reports/ - -# Compiled assets -build/ - -# Dependency directories -node_modules/ vendor/ +wp-nowpayments-integration/ -# Optional npm cache directory -.npm - -# Optional eslint cache -.eslintcache - -# Output of `npm pack` -*.tgz - -# Output of `wp-scripts plugin-zip` -*.zip - -# dotenv environment variables file -.env +yarn-debug.log* +yarn-error.log* diff --git a/includes/Integration/ApiStatus.php b/includes/Integration/ApiStatus.php index 43b3fa1..c3b9109 100644 --- a/includes/Integration/ApiStatus.php +++ b/includes/Integration/ApiStatus.php @@ -6,7 +6,7 @@ use lloc\Nowpayments\Rest\EndpointGetInterface; use lloc\Nowpayments\Rest\ResponseInterface; -final class ApiStatus extends Endpoint implements EndpointGetInterface { +class ApiStatus extends Endpoint implements EndpointGetInterface { /** * @return ResponseInterface diff --git a/includes/Integration/AvailableCurrencies.php b/includes/Integration/AvailableCurrencies.php index 913e669..aeb7e5b 100644 --- a/includes/Integration/AvailableCurrencies.php +++ b/includes/Integration/AvailableCurrencies.php @@ -6,7 +6,7 @@ use lloc\Nowpayments\Rest\EndpointGetInterface; use lloc\Nowpayments\Rest\ResponseInterface; -final class AvailableCurrencies extends Endpoint implements EndpointGetInterface { +class AvailableCurrencies extends Endpoint implements EndpointGetInterface { /** * @return ResponseInterface diff --git a/includes/Integration/EstimatedPrice.php b/includes/Integration/EstimatedPrice.php index 89ac299..08bd595 100644 --- a/includes/Integration/EstimatedPrice.php +++ b/includes/Integration/EstimatedPrice.php @@ -6,7 +6,7 @@ use lloc\Nowpayments\Rest\EndpointGetInterface; use lloc\Nowpayments\Rest\ResponseInterface; -final class EstimatedPrice extends Endpoint implements EndpointGetInterface { +class EstimatedPrice extends Endpoint implements EndpointGetInterface { /** * @param float $amount diff --git a/includes/Integration/MinimumPaymentAmount.php b/includes/Integration/MinimumPaymentAmount.php index 8f29554..b15d829 100644 --- a/includes/Integration/MinimumPaymentAmount.php +++ b/includes/Integration/MinimumPaymentAmount.php @@ -6,7 +6,7 @@ use lloc\Nowpayments\Rest\EndpointGetInterface; use lloc\Nowpayments\Rest\ResponseInterface; -final class MinimumPaymentAmount extends Endpoint implements EndpointGetInterface { +class MinimumPaymentAmount extends Endpoint implements EndpointGetInterface { /** * @param string $currency_from diff --git a/includes/Integration/Payment.php b/includes/Integration/Payment.php index b852b1f..35a9515 100644 --- a/includes/Integration/Payment.php +++ b/includes/Integration/Payment.php @@ -6,7 +6,7 @@ use lloc\Nowpayments\Rest\EndpointPostInterface; use lloc\Nowpayments\Rest\ResponseInterface; -final class Payment extends Endpoint implements EndpointPostInterface { +class Payment extends Endpoint implements EndpointPostInterface { public const ADDITIONAL_PARAMS = array( 'pay_amount', diff --git a/includes/Rest/Endpoint.php b/includes/Rest/Endpoint.php index fee3f6f..aaae34d 100644 --- a/includes/Rest/Endpoint.php +++ b/includes/Rest/Endpoint.php @@ -69,14 +69,4 @@ protected function set_body( array $params ): self { return $this; } - - /** - * @param string $name - * @param string[] $arguments - * - * @return mixed - */ - public function __call( string $name, array $arguments ) { - throw new \BadMethodCallException( sprintf( 'Method %s::%s does not exist.', __CLASS__, esc_attr( $name ) ) ); - } } diff --git a/includes/Services/AvailableCurrenciesService.php b/includes/Services/AvailableCurrenciesService.php index a13ddef..d9a571c 100644 --- a/includes/Services/AvailableCurrenciesService.php +++ b/includes/Services/AvailableCurrenciesService.php @@ -13,12 +13,12 @@ public function __construct( /** * @return array */ - protected function get_data(): array { + public function get_data(): array { $response = wp_cache_get( __METHOD__ ); if ( false === $response ) { $result = $this->available_currencies->get(); - $response = $result->get(); + $response = $result->get()['currencies'] ?? array(); wp_cache_set( __METHOD__, $response ); } @@ -32,8 +32,6 @@ protected function get_data(): array { * @return bool */ public function is_available( string $currency ): bool { - $currencies = $this->get_data()['currencies'] ?? array(); - - return in_array( $currency, $currencies ); + return in_array( $currency, $this->get_data() ); } } diff --git a/tests/Services/TestApiStatusService.php b/tests/Services/TestApiStatusService.php new file mode 100644 index 0000000..d9fcffc --- /dev/null +++ b/tests/Services/TestApiStatusService.php @@ -0,0 +1,38 @@ +shouldReceive( 'info' )->once()->andReturn( 'Sandbox' ); + + $client = \Mockery::mock( Client::class ); + $client->shouldReceive( 'get_service' )->once()->andReturn( $service ); + + $response = \Mockery::mock( ResponseInterface::class ); + $response->shouldReceive( 'get' )->once()->andReturn( array( 'status' => 'ok' ) ); + + $api_status = \Mockery::mock( ApiStatus::class ); + $api_status->shouldReceive( 'get' )->once()->andReturn( $response ); + $api_status->shouldReceive( 'get_client' )->once()->andReturn( $client ); + + $expected = array( + 'status' => 'ok', + 'info' => 'Sandbox', + ); + + $test = new ApiStatusService( $api_status ); + + $this->assertEquals( $expected, $test->get_data() ); + } +} diff --git a/tests/Services/TestAvailableCurrenciesService.php b/tests/Services/TestAvailableCurrenciesService.php new file mode 100644 index 0000000..8da0983 --- /dev/null +++ b/tests/Services/TestAvailableCurrenciesService.php @@ -0,0 +1,41 @@ + array( 'BTC', 'ETH' ) ); + + protected function setUp(): void { + parent::setUp(); + + $response = \Mockery::mock( ResponseInterface::class ); + $response->shouldReceive( 'get' )->atLeast()->once()->andReturn( self::RESULT ); + + $available_currencies = \Mockery::mock( AvailableCurrencies::class ); + $available_currencies->shouldReceive( 'get' )->atLeast()->once()->andReturn( $response ); + + Functions\expect( 'wp_cache_get' )->atLeast()->once()->andReturn( false ); + Functions\expect( 'wp_cache_set' )->atLeast()->once(); + + $this->test = new AvailableCurrenciesService( $available_currencies ); + } + + public function test_get_data() { + $this->assertEquals( self::RESULT['currencies'], $this->test->get_data() ); + } + + public function test_is_available(): void { + $this->assertTrue( $this->test->is_available( 'BTC' ) ); + $this->assertTrue( $this->test->is_available( 'ETH' ) ); + $this->assertFalse( $this->test->is_available( 'LTC' ) ); + } +} diff --git a/tests/TestAdminWidget.php b/tests/TestAdminWidget.php index d377ee4..ad73169 100644 --- a/tests/TestAdminWidget.php +++ b/tests/TestAdminWidget.php @@ -8,6 +8,26 @@ class TestAdminWidget extends LlocTestCase { + public function test_init(): void { + $status = \Mockery::mock( ApiStatusService::class ); + + Functions\expect( 'add_action' ) + ->once() + ->with( 'wp_dashboard_setup', \Mockery::type( 'array' ) ); + + $this->assertInstanceOf( AdminWidget::class, AdminWidget::init( $status ) ); + } + + public function test_add_dashboard_widget(): void { + $status = \Mockery::mock( ApiStatusService::class ); + + Functions\expect( 'wp_add_dashboard_widget' )->once(); + + $this->expectNotToPerformAssertions(); + + ( new AdminWidget( $status ) )->add_dashboard_widget(); + } + public function test_render(): void { $data = array( 'info' => 'abc', diff --git a/tests/TestPlugin.php b/tests/TestPlugin.php index 22e6f3f..dc8ca72 100644 --- a/tests/TestPlugin.php +++ b/tests/TestPlugin.php @@ -10,7 +10,7 @@ class TestPlugin extends LlocTestCase { protected Plugin $plugin; - public function setUp(): void { + protected function setUp(): void { parent::setUp(); Actions\expectAdded( 'plugins_loaded' )->once(); From 9fb350dcc053c1ea485fe60b34b859df70b32c19 Mon Sep 17 00:00:00 2001 From: Dennis Ploetner Date: Sat, 7 Dec 2024 16:49:26 +0100 Subject: [PATCH 2/2] Return type was missing --- tests/Services/TestAvailableCurrenciesService.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/Services/TestAvailableCurrenciesService.php b/tests/Services/TestAvailableCurrenciesService.php index 8da0983..034fcad 100644 --- a/tests/Services/TestAvailableCurrenciesService.php +++ b/tests/Services/TestAvailableCurrenciesService.php @@ -29,7 +29,7 @@ protected function setUp(): void { $this->test = new AvailableCurrenciesService( $available_currencies ); } - public function test_get_data() { + public function test_get_data(): void { $this->assertEquals( self::RESULT['currencies'], $this->test->get_data() ); }