Subiendo proyecto completo sin restricciones de git ignore

This commit is contained in:
Jose Sanchez
2023-08-17 11:44:02 -04:00
parent a0d4f5ba3b
commit 20f1c60600
19921 changed files with 2509159 additions and 45 deletions

View File

@@ -0,0 +1,46 @@
<?php
namespace Tests\Concerns;
use ReflectionClass;
use ReflectionProperty;
trait ExtractProperties {
/**
* Extract "a" property from a class.
*
* @param \stdClass $class
* @param string $name Property name.
* @return array Extracted name and value of property.
* @throws \ReflectionException
*/
function extractProperty($class, $name) {
$reflector = new ReflectionClass($class);
$property = $reflector->getProperty($name);
$property->setAccessible(true);
return [
"name" => $property->getName(),
"value" => $property->getValue($class),
];
}
/**
* Set property of class.
*
* @param \stdClass $class
* @param string $name Property name
* @param mixed $value
* @throws \ReflectionException
*/
function setProperty($class, $name, $value = null) {
$reflector = new ReflectionClass($class);
$property = $reflector->getProperty($name);
$property->setAccessible(true);
$property->setValue($class, $value);
}
}

View File

@@ -0,0 +1,225 @@
<?php
namespace Tests\Feature;
use Tests\TestCase;
use Tests\Stubs\Request;
use KingFlamez\Rave\Rave;
use Tests\Stubs\PaymentEventHandler;
use Tests\Concerns\ExtractProperties;
class FeatureTests extends TestCase {
use ExtractProperties;
/**
* Test if parameters are set on setData.
*
* @test
* @return void
*/
function getParams () {
$request = new Request();
$request->subaccounts = [];
$request->meta = [];
$request->ref = false;
$request->logo = false;
$request->title = false;
$request->paymentplan = false;
$request->phonenumber = '080232382382';
$request->payment_method = 'online';
$request->pay_button_text = 'Pay Now';
$rave = new Rave();
$rave->initialize("http://localhost");
$this->assertTrue($rave instanceof Rave);
return $rave;
}
/**
* Test if hash is created.
*
* @test
* @depends getParams
* @param Rave $rave
* @return void
* @throws \ReflectionException
*/
function creatingCheckSum(Rave $rave) {
#$rave = $rave->createReferenceNumber();
$publicKey = "FLWPUBK-MOCK-1cf610974690c2560cb4c36f4921244a-X";
$rave->initialize("http://localhost");
$rave = $rave->createCheckSum('http://localhost');
$hash = $this->extractProperty($rave, "integrityHash");
$this->assertEquals(64, strlen($hash["value"]));
return $rave;
}
/**
* Testing payment.
*
* @test
* @depends creatingCheckSum
* @param Rave $rave
* @return void
*/
function paymentInitialize(Rave $rave) {
$response = $rave->eventHandler(new PaymentEventHandler)->initialize("http://localhost");
$values = json_decode($response, true);
$class = $this->data["class"];
$this->assertArrayHasKey("meta", $values);
$this->assertArrayHasKey("txref", $values);
$this->assertArrayHasKey("amount", $values);
$this->assertArrayHasKey("country", $values);
$this->assertArrayHasKey("currency", $values);
$this->assertArrayHasKey("PBFPubKey", $values);
$this->assertArrayHasKey("custom_logo", $values);
$this->assertArrayHasKey("redirect_url", $values);
$this->assertArrayHasKey("data-integrity_hash", $values);
$this->assertArrayHasKey("payment_method", $values);
$this->assertArrayHasKey("customer_phone", $values);
$this->assertArrayHasKey("customer_email", $values);
$this->assertArrayHasKey("pay_button_text", $values);
$this->assertArrayHasKey("customer_lastname", $values);
$this->assertArrayHasKey("custom_description", $values);
$this->assertArrayHasKey("customer_firstname", $values);
}
/**
* Test if proper actions are taken when payment is cancelled.
*
* @test
* @return void
*/
function paymentCancelledTest() {
$request = new Request();
$request->cancelled = true;
$rave = new Rave();
$rave = $rave->createReferenceNumber();
$ref = $rave->getReferenceNumber();
// This section tests if json is returned when no handler is set.
$returned = $rave->paymentCanceled($ref);
$this->assertTrue( is_object($returned));
// Tests if json has certain keys when payment is cancelled.
$returned = json_decode(json_encode($returned), true);
$this->assertArrayHasKey("data", $returned);
$this->assertArrayHasKey("txRef", $returned['data']);
$this->assertArrayHasKey("status", $returned['data']);
// This section tests if instance of rave is returned when a handler is set.
$rave->eventHandler(new PaymentEventHandler)->paymentCanceled($ref);
$this->assertEquals(Rave::class, get_class($rave));
return $ref;
}
/**
* Testing requery transactions.
*
* @test
* @depends paymentCancelledTest
* @dataProvider providesResponse
* @runInSeparateProcess
* @preserveGlobalState disabled
* @param string $ref txref
*/
// function requeryTransactionTransactionTest($mResponse, $ref) {
//
// $data = [
// 'txref' => $ref,
// 'SECKEY' => $this->app->config->get("secretKey"),
// 'last_attempt' => '1'
// // 'only_successful' => '1'
// ];
//
// $url = "https://rave-api-v2.herokuapp.com";
// $headers = ['Content-Type' => 'application/json'];
//
// $data = Body::json($data);
// $response = json_encode($mResponse);
//
// $decodedResponse = json_decode($response);
//
// $mRequest = $this->m->mock("alias:Unirest\Request");
// $mRequest->shouldReceive("post")
// ->andReturn($decodedResponse);
//
// $rave = new Rave(new Request(['cancelled' => true]), $mRequest, new Body);
//
// $raveResponse = $rave->verifyTransaction($ref);
//
// // Test if data is returned when no handler.
// // $this->assertEquals($decodedResponse->body->status, $raveResponse->status);
//
// $this->setProperty($rave, "handler", new PaymentEventHandler);
//
// $raveResponse = $rave->verifyTransaction($ref);
//
// // Tests that an instance of rave is returned when a handler is set
// $this->assertTrue(Rave::class, get_class($raveResponse));
// }
/**
* Provides data for all events of requery transaction.
*
* @return array
*/
function providesResponse () {
return [
[
[
"body" => [
"status" => "unknown",
"data" => ["status", "unknown"]
],
],
],
[
[
"body" => [
"status" => "success",
],
]
],
[
[
"body" => [
"status" => "success",
"data" => [
"status" => "failed"
]
],
]
],
[
[
"body" => [
"status" => "success",
"data" => [
"status" => "successful"
]
],
]
]
];
}
}

View File

@@ -0,0 +1,77 @@
<?php
namespace Tests\Stubs;
use KingFlamez\Rave\RaveEventHandlerInterface;
class PaymentEventHandler implements RaveEventHandlerInterface {
/**
* This is called when the Rave class is initialized
* */
function onInit($initializationData){
// Save the transaction to your DB.
return 'Payment started......'.json_encode($initializationData).'<br />'; //Remember to delete this line
}
/**
* This is called only when a transaction is successful
* */
function onSuccessful($transactionData){
// Get the transaction from your DB using the transaction reference (txref)
// Check if you have previously given value for the transaction. If you have, redirect to your successpage else, continue
// Comfirm that the transaction is successful
// Confirm that the chargecode is 00 or 0
// Confirm that the currency on your db transaction is equal to the returned currency
// Confirm that the db transaction amount is equal to the returned amount
// Update the db transaction record (includeing parameters that didn't exist before the transaction is completed. for audit purpose)
// Give value for the transaction
// Update the transaction to note that you have given value for the transaction
// You can also redirect to your success page from here
return 'Payment Successful!'.json_encode($transactionData).'<br />'; //Remember to delete this line
}
/**
* This is called only when a transaction failed
* */
function onFailure($transactionData){
// Get the transaction from your DB using the transaction reference (txref)
// Update the db transaction record (includeing parameters that didn't exist before the transaction is completed. for audit purpose)
// You can also redirect to your failure page from here
return 'Payment Failed!'.json_encode($transactionData).'<br />'; //Remember to delete this line
}
/**
* This is called when a transaction is requeryed from the payment gateway
* */
function onRequery($transactionReference){
// Do something, anything!
return 'Payment requeried......'.$transactionReference.'<br />'; //Remember to delete this line
}
/**
* This is called a transaction requery returns with an error
* */
function onRequeryError($requeryResponse){
// Do something, anything!
return 'An error occured while requeying the transaction...'.json_encode($requeryResponse).'<br />'; //Remember to delete this line
}
/**
* This is called when a transaction is canceled by the user
* */
function onCancel($transactionReference){
// Do something, anything!
// Note: Somethings a payment can be successful, before a user clicks the cancel button so proceed with caution
return 'Payment canceled by user......'.$transactionReference.'<br />'; //Remember to delete this line
}
/**
* This is called when a transaction doesn't return with a success or a failure response. This can be a timedout transaction on the Rave server or an abandoned transaction by the customer.
* */
function onTimeout($transactionReference, $data){
// Get the transaction from your DB using the transaction reference (txref)
// Queue it for requery. Preferably using a queue system. The requery should be about 15 minutes after.
// Ask the customer to contact your support and you should escalate this issue to the flutterwave support team. Send this as an email and as a notification on the page. just incase the page timesout or disconnects
return 'Payment timeout......'.$transactionReference.' - '.json_encode($data).'<br />'; //Remember to delete this line
}
}

View File

@@ -0,0 +1,29 @@
<?php
namespace Tests\Stubs;
use Illuminate\Http\Request as BaseRequest;
class Request extends BaseRequest {
public $email;
public $amount;
public $country;
public $currency;
public $lastname;
public $firstname;
public $phonenumber;
public $description;
public $payment_method;
public $pay_button_text;
function __construct( ) {
$data = (array) include __DIR__ . "/request_data.php";
array_walk($data["form"], function($value, $key) {
$this->{$key} = $value;
});
}
}

View File

@@ -0,0 +1,15 @@
<?php
/**
* Stub required environment variables.
*
* @return array
*/
return [
"prefix" => "rave",
"secretKey" => "FLWSECK-4127f15e63c9098402dcc7891798fb0f-X",
"publicKey" => "FLWPUBK-1cf610974690c2560cb4c36f4921244a-X",
"title" => "Rave Payment Gateway",
"env" => "testing",
"logo" => "https://files.readme.io/ee907a0-small-rave_by_flutterwave.png"
];

View File

@@ -0,0 +1,46 @@
<?php
/**
* Sample request data.
*
* @return array
*/
return [
/**
* Request mocking data
*/
"form" => [
"amount" => 30000,
"title" => "A Title",
"lastname" => "Last",
"currency" => "naira",
"country" => "Nigeria",
"firstname" => "First",
"payment_method" => "card",
"phonenumber" => "08012345678",
"email" => "email@example.org",
"pay_button_text" => "Pay Now",
"metadata" => "[{ flightid:3849 }]",
"description" => "Some random description.",
"logo" => "https://files.readme.io/ee907a0-small-rave_by_flutterwave.png",
],
/**
* Rave original properties representation.
*/
"class" => [
"amount",
"country",
"currency",
"lastname",
"firstname",
["logo" => "customLogo"],
"phonenumber",
["title" => "customTitle"],
"payButtonText",
["email" => "customerEmail"],
"payment_method",
["description" => "customeDescription"],
"metadata",
]
];

View File

@@ -0,0 +1,81 @@
<?php
namespace Tests;
use Mockery;
use Orchestra\Testbench\TestCase as BaseTestCase;
use phpDocumentor\Reflection\Types\Void_;
abstract class TestCase extends BaseTestCase {
public $m;
protected $envVars;
protected $data;
protected $formData;
function setUp () : void {
$this->m = new Mockery;
$this->envVars = (array) include __DIR__ . "/Stubs/env.php";
$this->data = (array) include __DIR__ . "/Stubs/request_data.php";
$this->formData = $this->data["form"];
parent::setUp();
}
/**
* Clear mockery after every test in preparation for a new mock.
*
* @return void
*/
function tearDown() : void {
$this->m->close();
parent::tearDown();
}
/**
* Register package.
*
* @param \Illuminate\Foundation\Application $app
* @return array Packages to register
*/
protected function getPackageProviders($app)
{
return [ "\KingFlamez\Rave\RaveServiceProvider" ];
}
/**
* Get alias packages from app.
*
* @param \illuminate\Foundation\Application $app
* @return array Aliases.
*/
protected function getPackageAliases($app)
{
return [
"Rave" => "\KingFlamez\Rave\Facades\Rave"
];
}
/**
* Configure Environment.
*
* @param \Illuminate\Foundation\Application $app
* @return void
*/
protected function getEnvironmentSetUp($app)
{
array_walk($this->envVars, function ($value, $key) use (&$app) {
$app["config"]->set("flutterwave.{$key}", $value);
});
}
}

View File

@@ -0,0 +1,29 @@
<?php
namespace Tests\Unit;
use Tests\TestCase;
use KingFlamez\Rave\Rave;
class RaveServiceProviderTests extends TestCase
{
/**
* Tests if service provider Binds alias "laravelrave" to \KingFlamez\Rave\Rave
*
* @test
*/
public function isBound()
{
$this->assertTrue($this->app->bound('laravelrave'));
}
/**
* Test if service provider returns \Rave as alias for \KingFlamez\Rave\Rave
*
* @test
*/
public function hasAliased()
{
$this->assertTrue($this->app->isAlias("KingFlamez\Rave\Rave"));
$this->assertEquals('laravelrave', $this->app->getAlias("KingFlamez\Rave\Rave"));
}
}

View File

@@ -0,0 +1,114 @@
<?php
namespace Tests\Unit;
use Carbon\Carbon;
use Tests\TestCase;
use ReflectionClass;
use ReflectionProperty;
use KingFlamez\Rave\Rave;
use Tests\Stubs\PaymentEventHandler;
use Tests\Concerns\ExtractProperties;
class UnitTests extends TestCase
{
use ExtractProperties;
/**
* Tests if app returns \KingFlamez\Rave\Rave if called with ailas.
*
* @test
* @return \KingFlamez\Rave\Rave
*/
function initiateRaveFromApp()
{
$rave = $this->app->make("laravelrave");
$this->assertTrue($rave instanceof Rave);
return $rave;
}
/**
* Test Rave initiallizes with default values;.
*
* @test
*
* @depends initiateRaveFromApp
* @param \KingFlamez\Rave\Rave $rave
* @return void
* @throws \ReflectionException
*/
function initializeWithDefaultValues(Rave $rave)
{
$reflector = new ReflectionClass($rave);
$methods = $reflector->getProperties(ReflectionProperty::IS_PROTECTED);
foreach ($methods as $method) {
if ($method->getName() == 'baseUrl') $baseUrl = $method;
if ($method->getName() == 'secretKey') $secretKey = $method;
if ($method->getName() == 'publicKey') $publicKey = $method;
};
$baseUrl->setAccessible(true);
$publicKey->setAccessible(true);
$secretKey->setAccessible(true);
$this->assertEquals($this->app->config->get("flutterwave.secretKey"), $secretKey->getValue($rave));
$this->assertEquals($this->app->config->get("flutterwave.publicKey"), $publicKey->getValue($rave));
$this->assertEquals(
"https://api.flutterwave.com/v3",
$baseUrl->getValue($rave)
);
}
/**
* Tests if transaction reference is generated.
*
* @test
* @depends initiateRaveFromApp
* @param Rave $rave
* @return void
*/
function generateReference(Rave $rave)
{
$ref = $rave->generateReference();
$prefix = 'flw';
$this->assertRegExp("/^{$prefix}_\w{13}$/", $ref);
}
/**
* Testing if keys are modified using setkeys.
*
* @test
* @depends initiateRaveFromApp
* @param Rave $rave
* @return void
* @throws \ReflectionException
*/
function settingKeys(Rave $rave)
{
$newPublicKey = "public_key";
$newSecretKey = "secret_key";
$rave->setKeys($newPublicKey, $newSecretKey);
$reflector = new ReflectionClass($rave);
$reflector = $reflector->getProperties(ReflectionProperty::IS_PROTECTED);
$keys = array_map(function ($value) use ($rave, $newPublicKey, $newSecretKey) {
$name = $value->getName();
if ($name === "publicKey" || $name === "secretKey") {
$value->setAccessible(true);
$key = $value->getValue($rave);
$this->assertEquals(${"new" . ucfirst($name)}, $key);
}
}, $reflector);
}
}