Skip to content

Commit 0973019

Browse files
authored
Merge pull request #11 from lloc/add-di
Tests finished
2 parents e64a9a9 + 9fb350d commit 0973019

12 files changed

+121
-49
lines changed

.gitignore

Lines changed: 13 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,21 @@
1+
*.log
2+
*.tgz
3+
*.zip
4+
.env
5+
.eslintcache
16
.idea/
7+
.npm
28
.phpunit.result.cache
9+
build/
10+
js/
311
composer.lock
4-
package-lock.json
5-
6-
# Logs
712
logs
8-
*.log
13+
node_modules/
914
npm-debug.log*
10-
yarn-debug.log*
11-
yarn-error.log*
12-
13-
# Coverage directory
15+
package-lock.json
1416
reports/
15-
16-
# Compiled assets
17-
build/
18-
19-
# Dependency directories
20-
node_modules/
2117
vendor/
18+
wp-nowpayments-integration/
2219

23-
# Optional npm cache directory
24-
.npm
25-
26-
# Optional eslint cache
27-
.eslintcache
28-
29-
# Output of `npm pack`
30-
*.tgz
31-
32-
# Output of `wp-scripts plugin-zip`
33-
*.zip
34-
35-
# dotenv environment variables file
36-
.env
20+
yarn-debug.log*
21+
yarn-error.log*

includes/Integration/ApiStatus.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use lloc\Nowpayments\Rest\EndpointGetInterface;
77
use lloc\Nowpayments\Rest\ResponseInterface;
88

9-
final class ApiStatus extends Endpoint implements EndpointGetInterface {
9+
class ApiStatus extends Endpoint implements EndpointGetInterface {
1010

1111
/**
1212
* @return ResponseInterface

includes/Integration/AvailableCurrencies.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use lloc\Nowpayments\Rest\EndpointGetInterface;
77
use lloc\Nowpayments\Rest\ResponseInterface;
88

9-
final class AvailableCurrencies extends Endpoint implements EndpointGetInterface {
9+
class AvailableCurrencies extends Endpoint implements EndpointGetInterface {
1010

1111
/**
1212
* @return ResponseInterface

includes/Integration/EstimatedPrice.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use lloc\Nowpayments\Rest\EndpointGetInterface;
77
use lloc\Nowpayments\Rest\ResponseInterface;
88

9-
final class EstimatedPrice extends Endpoint implements EndpointGetInterface {
9+
class EstimatedPrice extends Endpoint implements EndpointGetInterface {
1010

1111
/**
1212
* @param float $amount

includes/Integration/MinimumPaymentAmount.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use lloc\Nowpayments\Rest\EndpointGetInterface;
77
use lloc\Nowpayments\Rest\ResponseInterface;
88

9-
final class MinimumPaymentAmount extends Endpoint implements EndpointGetInterface {
9+
class MinimumPaymentAmount extends Endpoint implements EndpointGetInterface {
1010

1111
/**
1212
* @param string $currency_from

includes/Integration/Payment.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use lloc\Nowpayments\Rest\EndpointPostInterface;
77
use lloc\Nowpayments\Rest\ResponseInterface;
88

9-
final class Payment extends Endpoint implements EndpointPostInterface {
9+
class Payment extends Endpoint implements EndpointPostInterface {
1010

1111
public const ADDITIONAL_PARAMS = array(
1212
'pay_amount',

includes/Rest/Endpoint.php

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,14 +69,4 @@ protected function set_body( array $params ): self {
6969

7070
return $this;
7171
}
72-
73-
/**
74-
* @param string $name
75-
* @param string[] $arguments
76-
*
77-
* @return mixed
78-
*/
79-
public function __call( string $name, array $arguments ) {
80-
throw new \BadMethodCallException( sprintf( 'Method %s::%s does not exist.', __CLASS__, esc_attr( $name ) ) );
81-
}
8272
}

includes/Services/AvailableCurrenciesService.php

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ public function __construct(
1313
/**
1414
* @return array<string, string[]>
1515
*/
16-
protected function get_data(): array {
16+
public function get_data(): array {
1717
$response = wp_cache_get( __METHOD__ );
1818

1919
if ( false === $response ) {
2020
$result = $this->available_currencies->get();
21-
$response = $result->get();
21+
$response = $result->get()['currencies'] ?? array();
2222

2323
wp_cache_set( __METHOD__, $response );
2424
}
@@ -32,8 +32,6 @@ protected function get_data(): array {
3232
* @return bool
3333
*/
3434
public function is_available( string $currency ): bool {
35-
$currencies = $this->get_data()['currencies'] ?? array();
36-
37-
return in_array( $currency, $currencies );
35+
return in_array( $currency, $this->get_data() );
3836
}
3937
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
namespace lloc\NowpaymentsTests\Services;
4+
5+
use lloc\Nowpayments\Integration\ApiStatus;
6+
use lloc\Nowpayments\Rest\Api;
7+
use lloc\Nowpayments\Rest\Client;
8+
use lloc\Nowpayments\Rest\EndpointGetInterface;
9+
use lloc\Nowpayments\Rest\ResponseInterface;
10+
use lloc\Nowpayments\Services\ApiStatusService;
11+
use lloc\NowpaymentsTests\LlocTestCase;
12+
13+
class TestApiStatusService extends LlocTestCase {
14+
15+
public function test_get_data(): void {
16+
$service = \Mockery::mock( Api::class );
17+
$service->shouldReceive( 'info' )->once()->andReturn( 'Sandbox' );
18+
19+
$client = \Mockery::mock( Client::class );
20+
$client->shouldReceive( 'get_service' )->once()->andReturn( $service );
21+
22+
$response = \Mockery::mock( ResponseInterface::class );
23+
$response->shouldReceive( 'get' )->once()->andReturn( array( 'status' => 'ok' ) );
24+
25+
$api_status = \Mockery::mock( ApiStatus::class );
26+
$api_status->shouldReceive( 'get' )->once()->andReturn( $response );
27+
$api_status->shouldReceive( 'get_client' )->once()->andReturn( $client );
28+
29+
$expected = array(
30+
'status' => 'ok',
31+
'info' => 'Sandbox',
32+
);
33+
34+
$test = new ApiStatusService( $api_status );
35+
36+
$this->assertEquals( $expected, $test->get_data() );
37+
}
38+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?php
2+
3+
namespace lloc\NowpaymentsTests\Services;
4+
5+
use lloc\Nowpayments\Integration\AvailableCurrencies;
6+
use lloc\Nowpayments\Rest\ResponseInterface;
7+
use lloc\Nowpayments\Services\AvailableCurrenciesService;
8+
use lloc\NowpaymentsTests\LlocTestCase;
9+
use Brain\Monkey\Functions;
10+
11+
class TestAvailableCurrenciesService extends LlocTestCase {
12+
13+
private AvailableCurrenciesService $test;
14+
15+
const RESULT = array( 'currencies' => array( 'BTC', 'ETH' ) );
16+
17+
protected function setUp(): void {
18+
parent::setUp();
19+
20+
$response = \Mockery::mock( ResponseInterface::class );
21+
$response->shouldReceive( 'get' )->atLeast()->once()->andReturn( self::RESULT );
22+
23+
$available_currencies = \Mockery::mock( AvailableCurrencies::class );
24+
$available_currencies->shouldReceive( 'get' )->atLeast()->once()->andReturn( $response );
25+
26+
Functions\expect( 'wp_cache_get' )->atLeast()->once()->andReturn( false );
27+
Functions\expect( 'wp_cache_set' )->atLeast()->once();
28+
29+
$this->test = new AvailableCurrenciesService( $available_currencies );
30+
}
31+
32+
public function test_get_data(): void {
33+
$this->assertEquals( self::RESULT['currencies'], $this->test->get_data() );
34+
}
35+
36+
public function test_is_available(): void {
37+
$this->assertTrue( $this->test->is_available( 'BTC' ) );
38+
$this->assertTrue( $this->test->is_available( 'ETH' ) );
39+
$this->assertFalse( $this->test->is_available( 'LTC' ) );
40+
}
41+
}

0 commit comments

Comments
 (0)