Skip to content

Commit 9319ff7

Browse files
Merge pull request #54 from Flutterwave/development
Improve environment variable file detection on installation via composer
2 parents e8dfd50 + baf1e48 commit 9319ff7

14 files changed

+227
-45
lines changed

.distignore

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
.distignore
2+
docker-compose.yml
3+
.gitignore
4+
.github
5+
.env.example

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -195,3 +195,4 @@ examples/*.log
195195
examples/endpoint/*.log
196196
example.php
197197
.phpunit.result.cache
198+
.env.local

setup.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use Flutterwave\Helper;
44
use Dotenv\Dotenv;
55

6-
$dotenv = Dotenv::createImmutable(__DIR__."/../../");
6+
$dotenv = Dotenv::createImmutable(__DIR__."/../../../");
77
$dotenv->safeLoad();
88

99
//check if the current version of php is compatible

src/Helper/Config.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ private function __construct(string $secretKey, string $publicKey, string $encry
4444

4545
$log = new Logger('Flutterwave/PHP');
4646
$this->logger = $log;
47-
$log->pushHandler(new RotatingFileHandler(self::LOG_FILE_NAME, 90));
47+
$log->pushHandler(new RotatingFileHandler(__DIR__."../../../../../../".self::LOG_FILE_NAME, 90));
4848
}
4949

5050
public static function setUp(string $secretKey, string $publicKey, string $enc, string $env): ConfigInterface

tests/Resources/setup/Config.php

+98
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Flutterwave\Test\Resources\Setup;
6+
7+
use Flutterwave\Contract\ConfigInterface;
8+
use GuzzleHttp\Client;
9+
use GuzzleHttp\ClientInterface;
10+
use function is_null;
11+
use Monolog\Handler\RotatingFileHandler;
12+
use Monolog\Logger;
13+
use Psr\Log\LoggerInterface;
14+
15+
class Config implements ConfigInterface
16+
{
17+
public const PUBLIC_KEY = 'PUBLIC_KEY';
18+
public const SECRET_KEY = 'SECRET_KEY';
19+
public const ENCRYPTION_KEY = 'ENCRYPTION_KEY';
20+
public const ENV = 'ENV';
21+
public const VERSION = 'v3';
22+
public const BASE_URL = 'https://api.flutterwave.com/'.self::VERSION;
23+
public const DEFAULT_PREFIX = 'FW|PHP';
24+
public const LOG_FILE_NAME = 'flutterwave-php.log';
25+
protected Logger $logger;
26+
private string $secret;
27+
private string $public;
28+
29+
private static ?Config $instance = null;
30+
private string $env;
31+
private ClientInterface $http;
32+
private string $enc;
33+
34+
private function __construct(string $secretKey, string $publicKey, string $encryptKey, string $env)
35+
{
36+
$this->secret = $secretKey;
37+
$this->public = $publicKey;
38+
$this->enc = $encryptKey;
39+
$this->env = $env;
40+
41+
$this->http = new Client([
42+
'base_uri' => $this->getBaseUrl(),
43+
'timeout' => 60,
44+
]);
45+
46+
$log = new Logger('Flutterwave/PHP');
47+
$this->logger = $log;
48+
$log->pushHandler(new RotatingFileHandler(self::LOG_FILE_NAME, 90));
49+
}
50+
51+
public static function setUp(string $secretKey, string $publicKey, string $enc, string $env): ConfigInterface
52+
{
53+
if (is_null(self::$instance)) {
54+
return new Config($secretKey, $publicKey, $enc, $env);
55+
}
56+
return self::$instance;
57+
}
58+
59+
public function getHttp(): ClientInterface
60+
{
61+
return $this->http ?? new Client();
62+
}
63+
64+
public function getLoggerInstance(): LoggerInterface
65+
{
66+
return $this->logger;
67+
}
68+
69+
public function getEncryptkey(): string
70+
{
71+
return $this->enc;
72+
}
73+
74+
public function getPublicKey(): string
75+
{
76+
return $this->public;
77+
}
78+
79+
public static function getBaseUrl(): string
80+
{
81+
return self::BASE_URL;
82+
}
83+
84+
public function getSecretKey(): string
85+
{
86+
return $this->secret;
87+
}
88+
89+
public function getEnv(): string
90+
{
91+
return $this->env;
92+
}
93+
94+
public static function getDefaultTransactionPrefix(): string
95+
{
96+
return self::DEFAULT_PREFIX;
97+
}
98+
}

tests/Unit/Service/BankTest.php

+16-4
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,32 @@
44

55
use Flutterwave\Service\Banks;
66
use PHPUnit\Framework\TestCase;
7+
use Flutterwave\Test\Resources\Setup\Config;
78

89
class BankTest extends TestCase
910
{
11+
public Banks $service;
12+
protected function setUp(): void
13+
{
14+
$this->service = new Banks(
15+
Config::setUp(
16+
$_SERVER[Config::SECRET_KEY],
17+
$_SERVER[Config::PUBLIC_KEY],
18+
$_SERVER[Config::ENCRYPTION_KEY],
19+
$_SERVER[Config::ENV]
20+
)
21+
);
22+
}
23+
1024
public function testRetrievingBankByCountry()
1125
{
12-
$service = new Banks();
13-
$response = $service->getByCountry("NG");
26+
$response = $this->service->getByCountry("NG");
1427
$this->assertTrue(property_exists($response,'data') && \is_array($response->data));
1528
}
1629

1730
public function testRetrievingBankBranches()
1831
{
19-
$service = new Banks();
20-
$response = $service->getBranches("280");
32+
$response = $this->service->getBranches("280");
2133
$this->assertTrue(property_exists($response,'data') && \is_array($response->data));
2234
}
2335
}

tests/Unit/Service/BankTransferTest.php

+9-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
namespace Unit\Service;
44

5-
use Flutterwave\Helper\Config;
5+
use Flutterwave\Test\Resources\Setup\Config;
66
use Flutterwave\Flutterwave;
77
use Flutterwave\Util\AuthMode;
88
use PHPUnit\Framework\TestCase;
@@ -13,7 +13,14 @@ class BankTransferTest extends TestCase
1313
{
1414
protected function setUp(): void
1515
{
16-
Flutterwave::bootstrap();
16+
Flutterwave::bootstrap(
17+
Config::setUp(
18+
$_SERVER[Config::SECRET_KEY],
19+
$_SERVER[Config::PUBLIC_KEY],
20+
$_SERVER[Config::ENCRYPTION_KEY],
21+
$_SERVER[Config::ENV]
22+
)
23+
);
1724
}
1825

1926
public function testAuthModeReturnBankTransfer()

tests/Unit/Service/BillTest.php

+15-3
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,25 @@
33
namespace Unit\Service;
44

55
use Flutterwave\Flutterwave;
6-
use Flutterwave\Helper\Config;
76
use Flutterwave\Payload;
7+
use Flutterwave\Test\Resources\Setup\Config;
88
use Flutterwave\Service\Bill;
99

1010
class BillTest extends \PHPUnit\Framework\TestCase
1111
{
12+
13+
public Bill $service;
14+
protected function setUp(): void
15+
{
16+
$this->service = new Bill(
17+
Config::setUp(
18+
$_SERVER[Config::SECRET_KEY],
19+
$_SERVER[Config::PUBLIC_KEY],
20+
$_SERVER[Config::ENCRYPTION_KEY],
21+
$_SERVER[Config::ENV]
22+
)
23+
);
24+
}
1225
// public function testBillCreation()
1326
// {
1427
// $payload = new Payload();
@@ -30,8 +43,7 @@ public function testMissingRequiredParam()
3043
$payload->set("customer", "+2349067985861");
3144
$payload->set("amount", "2000");
3245

33-
$service = new Bill();
3446
$this->expectException(\InvalidArgumentException::class);
35-
$request = $service->createPayment($payload);
47+
$request = $this->service->createPayment($payload);
3648
}
3749
}

tests/Unit/Service/CardTest.php

+10-2
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,24 @@
33
namespace Unit\Service;
44

55
use Flutterwave\Flutterwave;
6+
67
use Flutterwave\Util\AuthMode;
78
use PHPUnit\Framework\TestCase;
89
use Flutterwave\Util\Currency;
9-
use Flutterwave\Helper\Config;
10+
use Flutterwave\Test\Resources\Setup\Config;
1011

1112
class CardTest extends TestCase
1213
{
1314
protected function setUp(): void
1415
{
15-
Flutterwave::bootstrap();
16+
Flutterwave::bootstrap(
17+
Config::setUp(
18+
$_SERVER[Config::SECRET_KEY],
19+
$_SERVER[Config::PUBLIC_KEY],
20+
$_SERVER[Config::ENCRYPTION_KEY],
21+
$_SERVER[Config::ENV]
22+
)
23+
);
1624
}
1725

1826
public function testAuthModeReturnPin()

tests/Unit/Service/MomoTest.php

+9-2
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,20 @@
66
use Flutterwave\Util\AuthMode;
77
use PHPUnit\Framework\TestCase;
88
use Flutterwave\Util\Currency;
9-
use Flutterwave\Helper\Config;
9+
use Flutterwave\Test\Resources\Setup\Config;
1010

1111
class MomoTest extends TestCase
1212
{
1313
protected function setUp(): void
1414
{
15-
Flutterwave::bootstrap();
15+
Flutterwave::bootstrap(
16+
Config::setUp(
17+
$_SERVER[Config::SECRET_KEY],
18+
$_SERVER[Config::PUBLIC_KEY],
19+
$_SERVER[Config::ENCRYPTION_KEY],
20+
$_SERVER[Config::ENV]
21+
)
22+
);
1623
}
1724

1825
public function testAuthModeRwandaRedirect(){

tests/Unit/Service/PaymentPlanTest.php

+20-11
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,27 @@
22

33
namespace Unit\Service;
44

5-
use Flutterwave\Helper\Config;
5+
use Flutterwave\Test\Resources\Setup\Config;
66
use Flutterwave\Payload;
77
use Flutterwave\Service\PaymentPlan;
88
use PHPUnit\Framework\TestCase;
99

1010
class PaymentPlanTest extends TestCase
1111
{
12+
13+
public PaymentPlan $service;
14+
protected function setUp(): void
15+
{
16+
$this->service = new PaymentPlan(
17+
Config::setUp(
18+
$_SERVER[Config::SECRET_KEY],
19+
$_SERVER[Config::PUBLIC_KEY],
20+
$_SERVER[Config::ENCRYPTION_KEY],
21+
$_SERVER[Config::ENV]
22+
)
23+
);
24+
}
25+
1226
public function testPlanCreation()
1327
{
1428
$payload = new Payload();
@@ -17,9 +31,8 @@ public function testPlanCreation()
1731
$payload->set("interval", "monthly");
1832
$payload->set("duration", "1");
1933

20-
$service = new PaymentPlan();
2134

22-
$request = $service->create($payload);
35+
$request = $this->service->create($payload);
2336

2437
$this->assertTrue(property_exists($request,'data') && !empty($request->data->id));
2538

@@ -31,15 +44,13 @@ public function testPlanCreation()
3144
*/
3245
public function testRetrievingPlan($id)
3346
{
34-
$service = new PaymentPlan();
35-
$request = $service->get($id);
47+
$request = $this->service->get($id);
3648
$this->assertTrue(property_exists($request,'data') && !empty($request->data->id));
3749
}
3850

3951
public function testRetrievingPlans()
4052
{
41-
$service = new PaymentPlan();
42-
$request = $service->list();
53+
$request = $this->service->list();
4354
$this->assertTrue(property_exists($request,'data') && \is_array($request->data));
4455
}
4556

@@ -48,11 +59,10 @@ public function testRetrievingPlans()
4859
*/
4960
public function testUpdatingPlan($id)
5061
{
51-
$service = new PaymentPlan();
5262
$payload = new Payload();
5363
$payload->set("amount","600");
5464
$payload->set("status", "active");
55-
$request = $service->update($id, $payload);
65+
$request = $this->service->update($id, $payload);
5666
$this->assertTrue(property_exists($request,'data') && isset($request->data->id));
5767
}
5868

@@ -61,8 +71,7 @@ public function testUpdatingPlan($id)
6171
*/
6272
public function testCancelingPlan($id)
6373
{
64-
$service = new PaymentPlan();
65-
$request = $service->cancel($id);
74+
$request = $this->service->cancel($id);
6675
$this->assertTrue(property_exists($request,'data') && $request->data->status == "cancelled");
6776
}
6877
}

0 commit comments

Comments
 (0)