Skip to content

Commit 5aa3e11

Browse files
committed
add tests
1 parent 3aff7a9 commit 5aa3e11

15 files changed

+587
-40
lines changed

.github/workflows/tests.yml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
name: tests
2+
3+
on:
4+
push:
5+
pull_request:
6+
schedule:
7+
- cron: "0 0 * * *"
8+
9+
jobs:
10+
tests:
11+
runs-on: ubuntu-20.04
12+
13+
strategy:
14+
fail-fast: true
15+
matrix:
16+
php: [7.3, 7.4]
17+
laravel: [^7.0]
18+
19+
name: PHP ${{ matrix.php }} - Laravel ${{ matrix.laravel }}
20+
21+
steps:
22+
- name: Checkout code
23+
uses: actions/checkout@v2
24+
25+
- name: Setup PHP
26+
uses: shivammathur/setup-php@v2
27+
with:
28+
php-version: ${{ matrix.php }}
29+
extensions: dom, curl, libxml, mbstring, zip
30+
tools: composer:v1
31+
coverage: none
32+
33+
- name: Install dependencies
34+
run: |
35+
composer update --prefer-dist --no-interaction --no-progress
36+
37+
- name: Execute tests
38+
run: vendor/bin/phpunit --verbose
39+
env:
40+
STRIPE_SECRET: ${{ secrets.STRIPE_SECRET }}

.phpunit.result.cache

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"version":1,"defects":{"Devinweb\\LaravelHyperpay\\Tests\\UserTest::true_is_true":3,"Devinweb\\LaravelHyperpay\\Tests\\UserTest::test_user_can_get_pending_transactions":3,"Devinweb\\LaravelHyperpay\\Tests\\PrepareCheckoutTest::test_user_can_prepare_checkout":4,"Warning":6,"Devinweb\\LaravelHyperpay\\Tests\\PrepareCheckoutTest::user_can_prepare_checkout":3,"Devinweb\\LaravelHyperpay\\Tests\\PrepareCheckoutTest::user_can_prepare_checkout_for_mada_brand":3,"Devinweb\\LaravelHyperpay\\Tests\\PaymentStatusTest::payment_status_with_fail_response":4},"times":{"Devinweb\\LaravelHyperpay\\Tests\\ExampleTest::true_is_true":0.089,"Devinweb\\LaravelHyperpay\\Tests\\UserTest::true_is_true":0.007,"Devinweb\\LaravelHyperpay\\Tests\\UserTest::test_user_can_get_pending_transactions":0.008,"Devinweb\\LaravelHyperpay\\Tests\\PrepareCheckoutTest::test_user_can_prepare_checkout":0.096,"Warning":0,"Devinweb\\LaravelHyperpay\\Tests\\PrepareCheckoutTest::user_can_prepare_checkout":0.277,"Devinweb\\LaravelHyperpay\\Tests\\PrepareCheckoutTest::user_can_prepare_checkout_for_default_brands_visa_master":0.011,"Devinweb\\LaravelHyperpay\\Tests\\PrepareCheckoutTest::user_can_prepare_checkout_for_mada_brand":0.01,"Devinweb\\LaravelHyperpay\\Tests\\PaymentStatusTest::payment_status_with_fail_response":0.111}}

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
# Laravel HyperPay
44

5+
[![tests](https://github.com/devinweb/laravel-hyperpay/actions/workflows/tests.yml/badge.svg)](https://github.com/devinweb/laravel-hyperpay/actions/workflows/tests.yml)
56
<a href="https://github.styleci.io/repos/347104704"><img src="https://github.styleci.io/repos/347104704/shield?branch=master" alt="StyleCI Shield"></a>
67
[![Latest Version on Packagist](https://img.shields.io/packagist/v/devinweb/laravel-hyperpay.svg?style=flat-square)](https://packagist.org/packages/devinweb/laravel-hyperpay)
78
[![Total Downloads](https://img.shields.io/packagist/dt/devinweb/laravel-hyperpay.svg?style=flat-square)](https://packagist.org/packages/devinweb/laravel-hyperpay)

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
],
1818
"require": {
1919
"php": "^7.3",
20-
"guzzlehttp/guzzle": "^6.3|^7.0"
20+
"guzzlehttp/guzzle": "^7.0"
2121
},
2222
"require-dev": {
2323
"orchestra/testbench": "^4.0",
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
use Illuminate\Database\Migrations\Migration;
4+
use Illuminate\Database\Schema\Blueprint;
5+
use Illuminate\Support\Facades\Schema;
6+
7+
class CreateTransactionsTable extends Migration
8+
{
9+
/**
10+
* Run the migrations.
11+
*
12+
* @return void
13+
*/
14+
public function up()
15+
{
16+
Schema::create('transactions', function (Blueprint $table) {
17+
$table->string('id')->primary();
18+
$table->string('user_id');
19+
$table->string('checkout_id')->unique();
20+
$table->string('status');
21+
$table->float('amount');
22+
$table->string('currency');
23+
$table->json('data')->nullable();
24+
$table->json('trackable_data');
25+
$table->string('brand');
26+
27+
$table->foreign('user_id')->references('id')->on('users')
28+
->onUpdate('cascade')->onDelete('cascade');
29+
30+
$table->timestamps();
31+
});
32+
}
33+
34+
/**
35+
* Reverse the migrations.
36+
*
37+
* @return void
38+
*/
39+
public function down()
40+
{
41+
Schema::dropIfExists('transactions');
42+
}
43+
}

phpunit.xml.dist

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,25 @@
11
<?xml version="1.0" encoding="UTF-8"?>
2-
<phpunit bootstrap="vendor/autoload.php"
3-
backupGlobals="false"
2+
<phpunit backupGlobals="false"
43
backupStaticAttributes="false"
4+
bootstrap="vendor/autoload.php"
55
colors="true"
6-
verbose="true"
76
convertErrorsToExceptions="true"
87
convertNoticesToExceptions="true"
98
convertWarningsToExceptions="true"
109
processIsolation="false"
11-
stopOnFailure="false">
10+
stopOnFailure="false"
11+
>
1212
<testsuites>
13-
<testsuite name="Test Suite">
14-
<directory>tests</directory>
13+
<testsuite name="Unit">
14+
<directory suffix="Test.php">./tests/Unit</directory>
15+
</testsuite>
16+
<testsuite name="Feature">
17+
<directory suffix="Test.php">./tests/Feature</directory>
1518
</testsuite>
1619
</testsuites>
17-
<filter>
18-
<whitelist>
19-
<directory suffix=".php">src/</directory>
20-
</whitelist>
21-
</filter>
22-
<logging>
23-
<log type="tap" target="build/report.tap"/>
24-
<log type="junit" target="build/report.junit.xml"/>
25-
<log type="coverage-html" target="build/coverage" charset="UTF-8" yui="true" highlight="true"/>
26-
<log type="coverage-text" target="build/coverage.txt"/>
27-
<log type="coverage-clover" target="build/logs/clover.xml"/>
28-
</logging>
29-
</phpunit>
20+
<php>
21+
<env name="APP_ENV" value="testing"/>
22+
<env name="DB_CONNECTION" value="testing"/>
23+
<env name="DB_DATABASE" value=":memory:"/>
24+
</php>
25+
</phpunit>

src/LaravelHyperpayServiceProvider.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ public function boot()
1717
$this->registerRoutes();
1818
$this->registerResources();
1919
$this->registerPublishing();
20+
$this->registerMigrations();
2021
}
2122

2223
/**
@@ -69,6 +70,18 @@ protected function registerPublishing()
6970
}
7071
}
7172

73+
/**
74+
* Register the package migrations.
75+
*
76+
* @return void
77+
*/
78+
protected function registerMigrations()
79+
{
80+
if ($this->app->runningInConsole()) {
81+
$this->loadMigrationsFrom(__DIR__.'/../database/migrations');
82+
}
83+
}
84+
7285
/**
7386
* Register the application services.
7487
*/

tests/ExampleTest.php

Lines changed: 0 additions & 20 deletions
This file was deleted.
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
<?php
2+
3+
namespace Devinweb\LaravelHyperpay\Tests;
4+
5+
use Illuminate\Support\Facades\Artisan;
6+
use Illuminate\Support\Facades\File;
7+
8+
class BillingCommandTest extends TestCase
9+
{
10+
public function setUp(): void
11+
{
12+
parent::setUp();
13+
}
14+
15+
/** @test */
16+
public function its_create_a_new_billing_class()
17+
{
18+
19+
// destination path of the Foo class
20+
$billingClass = app_path('Billing/MyBillingClass.php');
21+
22+
// make sure we're starting from a clean state
23+
if (File::exists($billingClass)) {
24+
unlink($billingClass);
25+
}
26+
$this->assertFalse(File::exists($billingClass));
27+
28+
Artisan::call('make:billing MyBillingClass');
29+
30+
$this->assertTrue(File::exists($billingClass));
31+
32+
$expectedContents = <<<CLASS
33+
<?php
34+
35+
namespace App\Billing;
36+
37+
use Devinweb\LaravelHyperpay\Contracts\BillingInterface;
38+
39+
class MyBillingClass implements BillingInterface
40+
{
41+
/**
42+
* Get the billing data.
43+
*
44+
* @return array
45+
*/
46+
public function getBillingData(): array
47+
{
48+
return [
49+
//
50+
];
51+
}
52+
}
53+
CLASS;
54+
$this->assertEquals($expectedContents, file_get_contents($billingClass));
55+
}
56+
}
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<?php
2+
3+
namespace Devinweb\LaravelHyperpay\Tests;
4+
5+
use Devinweb\LaravelHyperpay\Events\FailTransaction;
6+
use Devinweb\LaravelHyperpay\Events\SuccessTransaction;
7+
use Devinweb\LaravelHyperpay\LaravelHyperpay;
8+
use Devinweb\LaravelHyperpay\Models\Transaction;
9+
use Devinweb\LaravelHyperpay\Tests\Fixtures\HyperPayResponses;
10+
use GuzzleHttp\Client;
11+
use GuzzleHttp\Handler\MockHandler;
12+
use GuzzleHttp\HandlerStack;
13+
use Illuminate\Support\Facades\Event;
14+
15+
class PaymentStatusTest extends TestCase
16+
{
17+
public function setUp(): void
18+
{
19+
parent::setUp();
20+
21+
$this->faker = \Faker\Factory::create();
22+
$this->checkoutId = $this->faker->uuid;
23+
$this->mock = new MockHandler([]);
24+
25+
$handlerStack = HandlerStack::create($this->mock);
26+
$this->client = new Client(['handler' => $handlerStack]);
27+
$this->hyperpay = new LaravelHyperpay($this->client);
28+
29+
config()->set('hyperpay.sandboxMode', true);
30+
config()->set('hyperpay.entityIdMada', '28df736b-5e0b-4d99-a4a9-e701e550a9c1');
31+
config()->set('hyperpay.entityId', '44b3c638-f5dc-4b44-99c0-bdad68919f8b');
32+
config()->set('hyperpay.access_token', '467f7720-7fda-4bd6-b4c7-6ac790792487');
33+
$this->user = $this->createCustomer();
34+
}
35+
36+
/** @test */
37+
public function payment_status_for_fail_transaction()
38+
{
39+
Event::fake();
40+
41+
$transaction = Transaction::create([
42+
'id' => $this->faker->uuid,
43+
'user_id' => $this->user->id,
44+
'checkout_id' => $this->checkoutId,
45+
'brand' => 'default',
46+
'status' => 'pending',
47+
'amount' => '10.0',
48+
'currency' => 'SAR',
49+
'trackable_data' => [
50+
'product_id'=>'bc842310-371f-49d1-b479-ad4b387f6630', 'product_type'=>'t-shirt', 'amount'=>10,
51+
],
52+
'data' => [
53+
'code'=>'000.200.100',
54+
'description'=>'successfully created checkout',
55+
],
56+
]);
57+
58+
// (new HyperPayResponses())->paymentStatusFailResponse($this->checkoutId),
59+
// (new HyperPayResponses())->paymentStatusSuccessResponse(),
60+
61+
$this->mock->reset();
62+
$this->mock->append((new HyperPayResponses())->paymentStatusFailResponse($this->checkoutId));
63+
64+
$resourcePath = "/v1/checkouts/{$this->checkoutId}/payment";
65+
$response = $this->hyperpay->paymentStatus($resourcePath, $this->checkoutId);
66+
67+
$transaction = Transaction::whereCheckoutId($this->checkoutId)->first();
68+
69+
$this->assertArrayHasKey('message', (array) $response->getData());
70+
$this->assertEquals($response->status(), 422);
71+
$this->assertEquals($transaction->status, 'cancel');
72+
Event::assertDispatched(FailTransaction::class);
73+
}
74+
75+
/** @test */
76+
public function payment_status_for_success_transaction()
77+
{
78+
Event::fake();
79+
80+
$transaction = Transaction::create([
81+
'id' => $this->faker->uuid,
82+
'user_id' => $this->user->id,
83+
'checkout_id' => $this->checkoutId,
84+
'brand' => 'default',
85+
'status' => 'pending',
86+
'amount' => '10.0',
87+
'currency' => 'SAR',
88+
'trackable_data' => [
89+
'product_id'=>'bc842310-371f-49d1-b479-ad4b387f6630',
90+
'product_type'=>'t-shirt',
91+
'amount'=>10,
92+
],
93+
'data' => [
94+
'code'=>'000.200.100',
95+
'description'=>'successfully created checkout',
96+
],
97+
]);
98+
99+
$this->mock->reset();
100+
101+
$this->mock->append(
102+
(new HyperPayResponses())->paymentStatusSuccessResponse($this->checkoutId),
103+
);
104+
105+
$resourcePath = "/v1/checkouts/{$this->checkoutId}/payment";
106+
$response = $this->hyperpay->paymentStatus($resourcePath, $this->checkoutId);
107+
108+
$transaction = Transaction::whereCheckoutId($this->checkoutId)->first();
109+
110+
$this->assertArrayHasKey('message', (array) $response->getData());
111+
$this->assertEquals($response->status(), 200);
112+
$this->assertEquals($transaction->status, 'success');
113+
Event::assertDispatched(SuccessTransaction::class);
114+
}
115+
}

0 commit comments

Comments
 (0)