Skip to content

Commit

Permalink
Merge pull request #6 from OgunsakinDamilola/feat/tests-and-upgrades
Browse files Browse the repository at this point in the history
wip: PHP 8, Laravel 8&9 support, test environment setup
  • Loading branch information
ogunsakin01 committed Jul 29, 2022
2 parents e3f92f0 + 0c6d215 commit 6b1c4ba
Show file tree
Hide file tree
Showing 19 changed files with 215 additions and 13 deletions.
2 changes: 2 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/laravel-interswitch.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions .phpunit.result.cache
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"version":1,"defects":{"OgunsakinDamilola\\Interswitch\\Tests\\Unit\\InterswitchPaymentModelTest::model_can_be_initiated_with_factory":4},"times":{"OgunsakinDamilola\\Interswitch\\Tests\\Unit\\InterswitchPaymentModelTest::model_can_be_initiated_with_factory":0.25}}
File renamed without changes.
13 changes: 12 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,21 @@
"email": "ogunsakin191@gmail.com"
}
],
"require": {
"php": "^7.4|^8.0",
"illuminate/support": "^8.0",
"doctrine/dbal": "^2.10",
"laravel/legacy-factories": "^1.1"
},
"require-dev": {
"phpunit/phpunit": "^9.0",
"orchestra/testbench": "^6.21"
},
"minimum-stability": "stable",
"autoload": {
"psr-4": {
"OgunsakinDamilola\\Interswitch\\": "src/"
"OgunsakinDamilola\\Interswitch\\": "src/",
"OgunsakinDamilola\\Interswitch\\Tests\\": "tests/"
}
},
"extra": {
Expand Down
28 changes: 28 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit bootstrap="vendor/autoload.php"
backupGlobals="false"
backupStaticAttributes="false"
colors="true"
verbose="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
testdox="true"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">src/</directory>
</whitelist>
</filter>
<php>
<env name="DB_CONNECTION" value="testing" force="true"/>
<env name="DB_DATABASE" value=":memory:" force="true"/>
<env name="APP_ENV" value="testing" force="true"/>
</php>
</phpunit>
6 changes: 2 additions & 4 deletions src/Interswitch.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
<?php


namespace OgunsakinDamilola\Interswitch;

use Illuminate\Support\Arr;
use OgunsakinDamilola\Interswitch\Models\InterswitchPayment;

class Interswitch extends InterswitchTransactionsHelper
{
public function generatePayment($paymentData): array
public function generatePayment($paymentData)
{
$amount = $paymentData['amount'] * 100;
$reference = $this->transactionReferenceHandler(Arr::get($paymentData, 'reference', ''));
Expand All @@ -27,7 +25,7 @@ public function generatePayment($paymentData): array
'response_full' => ''
];
InterswitchMailHandler::newPaymentNotification($paymentData['customer_email'], $payment);
InterswitchPayment::create($payment);
InterswitchPayment::query()->create($payment);
return [
'customerId' => $paymentData['customer_id'],
'customerName' => $paymentData['customer_name'],
Expand Down
10 changes: 4 additions & 6 deletions src/InterswitchMailHandler.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
<?php


namespace OgunsakinDamilola\Interswitch;


Expand All @@ -11,16 +9,16 @@

class InterswitchMailHandler
{
public static function newPaymentNotification(string $email, array $paymentData): void
public static function newPaymentNotification($email, $paymentData)
{
try {
Mail::to($email)->send(new PrePaymentNotification($paymentData));
} catch (\Exception $e) {
// dd($e->getMessage());
abort(500, $e->getMessage());
}
}

public static function paymentNotification(string $email, array $paymentData): void
public static function paymentNotification($email, $paymentData)
{
try {
if (! in_array($paymentData['response_code'], ['00', '01', '11', '10'])) {
Expand All @@ -29,7 +27,7 @@ public static function paymentNotification(string $email, array $paymentData): v
Mail::to($email)->send(new PaymentSuccessful($paymentData));
}
} catch (\Exception $e){
// dd($e->getMessage());
abort(500, $e->getMessage());
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/InterswitchTransactionsHelper.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
namespace OgunsakinDamilola\interswitch;
namespace OgunsakinDamilola\Interswitch;

class interswitchTransactionsHelper
{
Expand Down
2 changes: 1 addition & 1 deletion src/Models/InterswitchPayment.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class InterswitchPayment extends Model
{
protected $fillable = [ 'id', 'customer_id',
protected $fillable = ['id', 'customer_id',
'customer_name', 'customer_email',
'environment', 'gateway', 'reference',
'amount', 'response_code', 'response_description'];
Expand Down
34 changes: 34 additions & 0 deletions src/database/database.sqlite
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
PRAGMA journal_mode = MEMORY;
PRAGMA synchronous = OFF;
PRAGMA foreign_keys = OFF;
PRAGMA ignore_check_constraints = OFF;
PRAGMA auto_vacuum = NONE;
PRAGMA secure_delete = OFF;
BEGIN TRANSACTION;

DROP TABLE IF EXISTS `interswitch_payment`;

CREATE TABLE `interswitch_payments` (
`id` INTEGER NOT NULL ,
`created_at` timestamp NULL DEFAULT NULL,
`updated_at` timestamp NULL DEFAULT NULL,
`customer_id` INTEGER NOT NULL,
`customer_name` TEXT NOT NULL,
`customer_email` TEXT NOT NULL,
`environment` TEXT NOT NULL,
`gateway` TEXT NOT NULL,
`reference` TEXT NOT NULL,
`amount` TEXT NOT NULL,
`response_code` TEXT NOT NULL,
`response_description` TEXT NULL DEFAULT NULL,
`response_full` TEXT NULL DEFAULT NULL,
`payment_status` TEXT NULL DEFAULT NULL,
PRIMARY KEY (`id`)
);


COMMIT;
PRAGMA ignore_check_constraints = ON;
PRAGMA foreign_keys = ON;
PRAGMA journal_mode = WAL;
PRAGMA synchronous = NORMAL;
20 changes: 20 additions & 0 deletions src/database/factories/InterswitchPaymentFactory.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

use Faker\Generator as Faker;
use OgunsakinDamilola\Interswitch\Models\InterswitchPayment;


$factory->define(InterswitchPayment::class, function (Faker $faker) {
return [
'customer_id' => $faker->randomNumber(1),
'customer_name' => $faker->name,
'customer_email' => $faker->email,
'environment' => 'TEST',
'gateway' => 'WEBPAY',
'reference' => uniqid(),
'amount' => 20000,
'response_code' => '00',
'response_description' => 'Success'
];
});

Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ public function up()
$table->bigInteger('amount'); // always store in kobo
$table->string('response_code')->nullable();
$table->string('response_description')->nullable();
$table->longText('response_full')->nullable();
$table->integer('payment_status')->nullable();
$table->timestamps();
});
}
Expand Down
21 changes: 21 additions & 0 deletions src/database/setup_default_tables_2022.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;

class SetupDefaultTables2022 extends Migration
{

public function up()
{
if (!Schema::hasTable('interswitch_payments')) {
DB::unprepared(file_get_contents(__DIR__.DIRECTORY_SEPARATOR.'database.sqlite'));
}
}

public function down()
{
//
}
}
35 changes: 35 additions & 0 deletions tests/TestCase.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?php
namespace OgunsakinDamilola\Interswitch\Tests;

use Illuminate\Database\Eloquent\Factory as EloquentFactory;
use OgunsakinDamilola\Interswitch\InterswitchServiceProvider;
use Orchestra\Testbench\TestCase as BaseTestCase;

class TestCase extends BaseTestCase
{
public function setUp(): void
{
parent::setUp();

$this->app->make(EloquentFactory::class)->load($this->baseDir().DIRECTORY_SEPARATOR.'database'.DIRECTORY_SEPARATOR.'factories');

}

protected function getPackageProviders($app)
{
return [
InterswitchServiceProvider::class,
];
}

protected function getEnvironmentSetUp($app)
{
include_once $this->baseDir().DIRECTORY_SEPARATOR.'database'.DIRECTORY_SEPARATOR.'setup_default_tables_2022.php';

(new \SetupDefaultTables2022)->up();
}

private function baseDir(){
return str_replace('tests','src',__DIR__);
}
}
24 changes: 24 additions & 0 deletions tests/Unit/InterswitchPaymentModelTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
namespace OgunsakinDamilola\Interswitch\Tests\Unit;

use Illuminate\Foundation\Testing\RefreshDatabase;
use OgunsakinDamilola\Interswitch\Models\InterswitchPayment;
use OgunsakinDamilola\Interswitch\Tests\TestCase;

class InterswitchPaymentModelTest extends TestCase
{
use RefreshDatabase;

public function setUp(): void
{
parent::setUp();
}

/** @test */
public function model_can_be_initiated_with_factory()
{
$model = factory(InterswitchPayment::class)->create();

$this->assertTrue($model instanceof InterswitchPayment);
}
}

0 comments on commit 6b1c4ba

Please sign in to comment.