Skip to content

Commit

Permalink
Add PaymobMethodFeature to tests
Browse files Browse the repository at this point in the history
  • Loading branch information
shabayekdes committed Dec 16, 2021
1 parent dd50fb6 commit c85a664
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 2 deletions.
2 changes: 1 addition & 1 deletion phpunit.xml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>
<testsuite name="Feature">
<directory suffix="Test.php">./tests/Feature</directory>
<directory suffix="Feature.php">./tests/Feature</directory>
</testsuite>
</testsuites>
<php>
Expand Down
113 changes: 113 additions & 0 deletions tests/Feature/Paymob/PaymobMethodFeature.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
<?php

namespace Shabayek\Payment\Tests\Feature\Paymob;

use Illuminate\Support\Str;
use Illuminate\Support\Facades\Http;
use Shabayek\Payment\Facade\Payment;
use Shabayek\Payment\Tests\TestCase;
use Illuminate\Support\Facades\Config;

/**
* Class PaymobMethodFeature
* @test
*/
class PaymobMethodFeature extends TestCase
{
/** @test*/
public function test_payment_token_in_return_purchase_url_with_paymob()
{
$order_id = rand(1, 100);
$payment_key = Str::random(512);
Http::fake([
// Stub a JSON response for paymob endpoints...
'https://accept.paymobsolutions.com/api/auth/tokens' => Http::response(['token' => Str::random(512)], 200),
'https://accept.paymobsolutions.com/api/ecommerce/orders' => Http::response(['id' => $order_id], 200),
'https://accept.paymobsolutions.com/api/acceptance/payment_keys' => Http::response(['token' => $payment_key], 200),
]);

$method_id = 2;
$payment = Payment::store($method_id);

$payment->customer($this->customer());
$payment->address($this->address());
$payment->items($this->items());

$payUrl = $payment->purchase();
$query = [];
$parts = parse_url($payUrl, PHP_URL_QUERY);
parse_str($parts, $query);


$this->assertEquals($query['payment_token'], $payment_key);
}
/** @test*/
public function test_iframe_in_return_purchase_url_with_paymob()
{
$iframe = rand(1000, 9999);
Config::set('payment.stores.2.credentials.iframe_id', $iframe);
$order_id = rand(1, 100);
$payment_key = Str::random(512);
Http::fake([
// Stub a JSON response for paymob endpoints...
'https://accept.paymobsolutions.com/api/auth/tokens' => Http::response(['token' => Str::random(512)], 200),
'https://accept.paymobsolutions.com/api/ecommerce/orders' => Http::response(['id' => $order_id], 200),
'https://accept.paymobsolutions.com/api/acceptance/payment_keys' => Http::response(['token' => $payment_key], 200),
]);

$method_id = 2;
$payment = Payment::store($method_id);

$payment->customer($this->customer());
$payment->address($this->address());
$payment->items($this->items());

$payUrl = $payment->purchase();

$parts = parse_url($payUrl, PHP_URL_PATH);
$arr = explode('/', $parts);

$this->assertEquals($arr[4], $iframe);
}

/**
* Get customer fake data
*
* @return array
*/
private function customer(): array
{
return [
'first_name' => 'John',
'last_name' => 'Doe',
'phone' => '+989120000000',
'email' => '[email protected]',
];
}
/**
* Get items fake data
*
* @return array
*/
private function items(): array
{
return [
"name" => "Product name",
"description" => "Product description",
"amount_cents" => 15000,
"quantity" => 1
];
}

private function address(): array
{
return [
'floor' => '1',
'street' => 'Test street',
'city' => 'Test city',
'state' => 'Test state',
'apartment' => 'Test apartment',
'building' => 'Test building'
];
}
}
2 changes: 1 addition & 1 deletion tests/Unit/Paymob/PaymobRequestTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ public function test_payment_keys_success()
}
/**
* Get customer fake data
*
*
* @return array
*/
private function customer(): array
Expand Down

0 comments on commit c85a664

Please sign in to comment.