Skip to content

Commit

Permalink
Merge pull request #9 from bavix/dev
Browse files Browse the repository at this point in the history
Version 1.2.0
  • Loading branch information
rez1dent3 authored Nov 9, 2018
2 parents 762883e + 22b1309 commit d275c60
Show file tree
Hide file tree
Showing 25 changed files with 598 additions and 13 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@ composer.phar
/vendor/
composer.lock
.idea/
build/
40 changes: 40 additions & 0 deletions .scrutinizer.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
build:

environment:
php:
version: 7.1

nodes:
analysis:
project_setup:
override: true
tests:
override:
- php-scrutinizer-run
-
command: 'vendor/bin/phpunit --coverage-clover=some-file'
coverage:
file: 'some-file'
format: 'clover'

filter:
excluded_paths: [tests/*, database/*, config/*]

checks:

php:
remove_extra_empty_lines: true
remove_php_closing_tag: true
remove_trailing_whitespace: true

fix_use_statements:
remove_unused: true
preserve_multiple: false
preserve_blanklines: true
order_alphabetically: true

fix_php_opening_tag: true
fix_linefeed: true
fix_line_ending: true
fix_identation_4spaces: true
fix_doc_comments: true
7 changes: 7 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
language: php
php:
- '7.1'
- '7.2'

before_script:
- "composer install"
11 changes: 11 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,22 @@
"php": "^7.1",
"laravel/framework": "~5.5.0|~5.6.0|~5.7.0"
},
"require-dev": {
"doctrine/dbal": "^2.8",
"mockery/mockery": "^1.2",
"orchestra/testbench": "^3.7",
"phpunit/phpunit": "^7.4"
},
"autoload": {
"psr-4": {
"Bavix\\Wallet\\": "src/"
}
},
"autoload-dev": {
"psr-4": {
"Bavix\\Wallet\\Test\\": "tests/"
}
},
"extra": {
"laravel": {
"providers": [
Expand Down
11 changes: 8 additions & 3 deletions database/migrations/2018_11_07_202152_update_transfers_table.php
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
<?php

use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\SQLiteConnection;
use Illuminate\Database\Migrations\Migration;
use Bavix\Wallet\Models\Transfer;

Expand Down Expand Up @@ -38,9 +40,12 @@ public function up(): void
public function down(): void
{
Schema::table($this->table(), function(Blueprint $table) {
$table->dropIndex('from_to_refund_ind');
$table->dropIndex('from_refund_ind');
$table->dropIndex('to_refund_ind');
if (!(DB::connection() instanceof SQLiteConnection)) {
$table->dropIndex('from_to_refund_ind');
$table->dropIndex('from_refund_ind');
$table->dropIndex('to_refund_ind');
}

$table->dropColumn('refund');
});
}
Expand Down
26 changes: 26 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?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"
processIsolation="false"
stopOnFailure="false">
<testsuites>
<testsuite name="Wallet Test Suite">
<directory>tests</directory>
</testsuite>
</testsuites>
<filter>
<whitelist>
<directory suffix=".php">src/</directory>
</whitelist>
</filter>
<logging>
<log type="coverage-html" target="./build/html/"/>
<log type="coverage-clover" target="./build/logs/clover.xml"/>
</logging>
</phpunit>
2 changes: 1 addition & 1 deletion src/Exceptions/AmountInvalid.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@

class AmountInvalid extends \InvalidArgumentException
{

}
2 changes: 1 addition & 1 deletion src/Exceptions/BalanceIsEmpty.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@

class BalanceIsEmpty extends \LogicException
{

}
2 changes: 1 addition & 1 deletion src/Exceptions/ProductEnded.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@

class ProductEnded extends \LogicException
{

}
6 changes: 3 additions & 3 deletions src/Interfaces/Customer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ interface Customer extends Wallet
/**
* @param Product $product
* @return Transfer
* @throws
* @throws
*/
public function pay(Product $product): Transfer;

/**
* @param Product $product
* @return null|Transfer
* @throws
* @throws
*/
public function safePay(Product $product): ?Transfer;

Expand All @@ -30,7 +30,7 @@ public function paid(Product $product): ?Transfer;
/**
* @param Product $product
* @return bool
* @throws
* @throws
*/
public function refund(Product $product): bool;

Expand Down
2 changes: 1 addition & 1 deletion src/Interfaces/Product.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,5 @@ public function getAmountProduct(): int;
* @return array
*/
public function getMetaProduct(): ?array;

}
4 changes: 2 additions & 2 deletions src/Interfaces/Wallet.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
use Bavix\Wallet\Models\Transfer;
use Illuminate\Database\Eloquent\Relations\MorphMany;

interface Wallet
interface Wallet
{
/**
* @param int $amount
Expand Down Expand Up @@ -55,7 +55,7 @@ public function safeTransfer(self $wallet, int $amount, ?array $meta = null): ?T
* @return Transfer
*/
public function forceTransfer(Wallet $wallet, int $amount, ?array $meta = null): Transfer;

/**
* @param int $amount
* @return bool
Expand Down
2 changes: 1 addition & 1 deletion src/Traits/CanBePaid.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ trait CanBePaid
/**
* @param Product $product
* @return Transfer
* @throws
* @throws
*/
public function pay(Product $product): Transfer
{
Expand Down
1 change: 1 addition & 0 deletions src/WalletServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ class WalletServiceProvider extends ServiceProvider
* Bootstrap services.
*
* @return void
* @codeCoverageIgnore
*/
public function boot(): void
{
Expand Down
27 changes: 27 additions & 0 deletions tests/Models/Buyer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Bavix\Wallet\Test\Models;

use Bavix\Wallet\Traits\CanBePaid;
use Bavix\Wallet\Interfaces\Customer;
use Illuminate\Database\Eloquent\Model;

/**
* Class User
*
* @package Bavix\Wallet\Test\Models
* @property string $name
* @property string $email
*/
class Buyer extends Model implements Customer
{
use CanBePaid;

/**
* @return string
*/
public function getTable(): string
{
return 'users';
}
}
54 changes: 54 additions & 0 deletions tests/Models/Item.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
<?php

namespace Bavix\Wallet\Test\Models;

use Bavix\Wallet\Interfaces\Customer;
use Bavix\Wallet\Interfaces\Product;
use Bavix\Wallet\Traits\HasWallet;
use Illuminate\Database\Eloquent\Model;

/**
* Class Item
*
* @package Bavix\Wallet\Test\Models
* @property string $name
* @property int $quantity
* @property int $price
*/
class Item extends Model implements Product
{

use HasWallet;

/**
* @var array
*/
protected $fillable = ['name', 'quantity', 'price'];

/**
* @param Customer $customer
*
* @return bool
*/
public function canBuy(Customer $customer): bool
{
return $this->quantity > 0 && !$customer->paid($this);
}

/**
* @return int
*/
public function getAmountProduct(): int
{
return $this->price;
}

/**
* @return array|null
*/
public function getMetaProduct(): ?array
{
return null;
}

}
24 changes: 24 additions & 0 deletions tests/Models/User.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace Bavix\Wallet\Test\Models;

use Bavix\Wallet\Traits\HasWallet;
use Bavix\Wallet\Interfaces\Wallet;
use Illuminate\Database\Eloquent\Model;

/**
* Class User
*
* @package Bavix\Wallet\Test\Models
* @property string $name
* @property string $email
*/
class User extends Model implements Wallet
{
use HasWallet;

/**
* @var array
*/
protected $fillable = ['name', 'email'];
}
57 changes: 57 additions & 0 deletions tests/ProductTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php

namespace Bavix\Wallet\Test;

use Bavix\Wallet\Test\Models\Buyer;
use Bavix\Wallet\Test\Models\Item;

class ProductTest extends TestCase
{

/**
* @return void
*/
public function testPay(): void
{
$buyer = factory(Buyer::class)->create();
$product = factory(Item::class)->create([
'quantity' => 1,
]);

$this->assertEquals($buyer->balance, 0);
$buyer->deposit($product->price);

$this->assertEquals($buyer->balance, $product->price);
$this->assertNotNull($buyer->pay($product));

$this->assertEquals($buyer->balance, 0);
$this->assertNull($buyer->safePay($product));
}

/**
* @return void
*/
public function testRefund(): void
{
$buyer = factory(Buyer::class)->create();
$product = factory(Item::class)->create([
'quantity' => 1,
]);

$this->assertEquals($buyer->balance, 0);
$buyer->deposit($product->price);

$this->assertEquals($buyer->balance, $product->price);
$this->assertNotNull($buyer->pay($product));

$this->assertTrue($buyer->refund($product));
$this->assertEquals($buyer->balance, $product->price);

$this->assertFalse($buyer->safeRefund($product));
$this->assertEquals($buyer->balance, $product->price);

$this->assertNotNull($buyer->pay($product));
$this->assertEquals($buyer->balance, 0);
}

}
Loading

0 comments on commit d275c60

Please sign in to comment.