Skip to content

Commit

Permalink
Merge pull request #11 from lloc/add-di
Browse files Browse the repository at this point in the history
Tests finished
  • Loading branch information
lloc authored Dec 7, 2024
2 parents e64a9a9 + 9fb350d commit 0973019
Show file tree
Hide file tree
Showing 12 changed files with 121 additions and 49 deletions.
41 changes: 13 additions & 28 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -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*
2 changes: 1 addition & 1 deletion includes/Integration/ApiStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion includes/Integration/AvailableCurrencies.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion includes/Integration/EstimatedPrice.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion includes/Integration/MinimumPaymentAmount.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion includes/Integration/Payment.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
10 changes: 0 additions & 10 deletions includes/Rest/Endpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) ) );
}
}
8 changes: 3 additions & 5 deletions includes/Services/AvailableCurrenciesService.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ public function __construct(
/**
* @return array<string, string[]>
*/
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 );
}
Expand All @@ -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() );
}
}
38 changes: 38 additions & 0 deletions tests/Services/TestApiStatusService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

namespace lloc\NowpaymentsTests\Services;

use lloc\Nowpayments\Integration\ApiStatus;
use lloc\Nowpayments\Rest\Api;
use lloc\Nowpayments\Rest\Client;
use lloc\Nowpayments\Rest\EndpointGetInterface;
use lloc\Nowpayments\Rest\ResponseInterface;
use lloc\Nowpayments\Services\ApiStatusService;
use lloc\NowpaymentsTests\LlocTestCase;

class TestApiStatusService extends LlocTestCase {

public function test_get_data(): void {
$service = \Mockery::mock( Api::class );
$service->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() );
}
}
41 changes: 41 additions & 0 deletions tests/Services/TestAvailableCurrenciesService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace lloc\NowpaymentsTests\Services;

use lloc\Nowpayments\Integration\AvailableCurrencies;
use lloc\Nowpayments\Rest\ResponseInterface;
use lloc\Nowpayments\Services\AvailableCurrenciesService;
use lloc\NowpaymentsTests\LlocTestCase;
use Brain\Monkey\Functions;

class TestAvailableCurrenciesService extends LlocTestCase {

private AvailableCurrenciesService $test;

const RESULT = array( 'currencies' => 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(): void {
$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' ) );
}
}
20 changes: 20 additions & 0 deletions tests/TestAdminWidget.php
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
2 changes: 1 addition & 1 deletion tests/TestPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 0973019

Please sign in to comment.