From 613fbd9b9b48d9079d45c3f87dc1ec7667bd0a7f Mon Sep 17 00:00:00 2001 From: Babichev Maxim Date: Thu, 19 Sep 2019 16:06:30 +0300 Subject: [PATCH] #106 #105 add example --- .phpstorm.meta.php | 10 +- config/config.php | 13 +- src/Objects/Cart.php | 2 +- src/Services/CommonService.php | 10 +- src/Services/LockService.php | 4 +- src/Services/MakeService.php | 42 ------ src/Traits/CanExchange.php | 5 +- src/Traits/CanPay.php | 22 +-- src/Traits/CartPay.php | 3 +- src/Traits/HasGift.php | 5 +- src/WalletServiceProvider.php | 20 ++- tests/BalanceTest.php | 1 + tests/CartTest.php | 18 +-- tests/Common/Models/Transaction.php | 15 ++ tests/EmptyLockTest.php | 6 +- tests/Objects/Operation.php | 18 +++ tests/SingletonTest.php | 135 ++++++++++++++++++ tests/TestCase.php | 6 +- tests/WalletExtensionTest.php | 53 +++++++ ..._191432_alter_column_transaction_table.php | 33 +++++ 20 files changed, 323 insertions(+), 98 deletions(-) delete mode 100644 src/Services/MakeService.php create mode 100644 tests/Objects/Operation.php create mode 100644 tests/SingletonTest.php create mode 100644 tests/WalletExtensionTest.php create mode 100644 tests/migrations/2019_09_19_191432_alter_column_transaction_table.php diff --git a/.phpstorm.meta.php b/.phpstorm.meta.php index bae077fee..1d656a60e 100644 --- a/.phpstorm.meta.php +++ b/.phpstorm.meta.php @@ -6,14 +6,20 @@ use Bavix\Wallet\Models\Transaction; use Bavix\Wallet\Models\Transfer; use Bavix\Wallet\Models\Wallet; + use Bavix\Wallet\Objects\Bring; + use Bavix\Wallet\Objects\Cart; + use Bavix\Wallet\Objects\EmptyLock; + use Bavix\Wallet\Objects\Operation; use Bavix\Wallet\Services\CommonService; use Bavix\Wallet\Services\ExchangeService; use Bavix\Wallet\Services\ProxyService; use Bavix\Wallet\Services\WalletService; - use Bavix\Wallet\Services\MakeService; override(\app(0), map([ - MakeService::class => MakeService::class, + Cart::class => Cart::class, + Bring::class => Bring::class, + Operation::class => Operation::class, + EmptyLock::class => EmptyLock::class, ExchangeService::class => ExchangeService::class, CommonService::class => CommonService::class, ProxyService::class => ProxyService::class, diff --git a/config/config.php b/config/config.php index b67d38815..8e04cf9e9 100644 --- a/config/config.php +++ b/config/config.php @@ -1,8 +1,11 @@ ProxyService::class, 'wallet' => WalletService::class, 'lock' => LockService::class, - 'make' => MakeService::class, + ], + + 'objects' => [ + 'bring' => Bring::class, + 'cart' => Cart::class, + 'emptyLock' => EmptyLock::class, + 'operation' => Operation::class, ], /** diff --git a/src/Objects/Cart.php b/src/Objects/Cart.php index 7950b7847..d71fd7b40 100644 --- a/src/Objects/Cart.php +++ b/src/Objects/Cart.php @@ -25,7 +25,7 @@ class Cart implements Countable /** * @return static - * @deprecated use app(MakeService::class)->makeCart() + * @deprecated use app(Cart::class) */ public static function make(): self { diff --git a/src/Services/CommonService.php b/src/Services/CommonService.php index 13a21e9b8..25669e015 100644 --- a/src/Services/CommonService.php +++ b/src/Services/CommonService.php @@ -9,6 +9,7 @@ use Bavix\Wallet\Models\Transfer; use Bavix\Wallet\Models\Wallet as WalletModel; use Bavix\Wallet\Objects\Bring; +use Bavix\Wallet\Objects\Operation; use Bavix\Wallet\Traits\HasWallet; use Illuminate\Support\Facades\DB; use function app; @@ -52,8 +53,7 @@ public function forceTransfer(Wallet $from, Wallet $to, int $amount, ?array $met ->getWallet($from); $transfers = $this->multiBrings([ - app(MakeService::class) - ->makeBring() + app(Bring::class) ->setStatus($status) ->setDeposit($deposit) ->setWithdraw($withdraw) @@ -84,8 +84,7 @@ public function forceWithdraw(Wallet $wallet, int $amount, ?array $meta, bool $c $wallet = $walletService->getWallet($wallet); $transactions = $this->multiOperation($wallet, [ - app(MakeService::class) - ->makeOperation() + app(Operation::class) ->setType(Transaction::TYPE_WITHDRAW) ->setConfirmed($confirmed) ->setAmount(-$amount) @@ -115,8 +114,7 @@ public function deposit(Wallet $wallet, int $amount, ?array $meta, bool $confirm $wallet = $walletService->getWallet($wallet); $transactions = $this->multiOperation($wallet, [ - app(MakeService::class) - ->makeOperation() + app(Operation::class) ->setType(Transaction::TYPE_DEPOSIT) ->setConfirmed($confirmed) ->setAmount($amount) diff --git a/src/Services/LockService.php b/src/Services/LockService.php index 05319508e..c94104f5f 100644 --- a/src/Services/LockService.php +++ b/src/Services/LockService.php @@ -2,6 +2,7 @@ namespace Bavix\Wallet\Services; +use Bavix\Wallet\Objects\EmptyLock; use Illuminate\Contracts\Cache\Lock; use Illuminate\Contracts\Cache\LockProvider; use Illuminate\Contracts\Cache\Store; @@ -87,8 +88,7 @@ protected function lockProvider($self, string $name, int $seconds): Lock } // @codeCoverageIgnoreEnd - return app(MakeService::class) - ->makeEmptyLock(); + return app(EmptyLock::class); } } diff --git a/src/Services/MakeService.php b/src/Services/MakeService.php deleted file mode 100644 index cda53d90d..000000000 --- a/src/Services/MakeService.php +++ /dev/null @@ -1,42 +0,0 @@ -deposit($to, $amount * $rate, $meta); $transfers = app(CommonService::class)->multiBrings([ - app(MakeService::class) - ->makeBring() + app(Bring::class) ->setStatus(Transfer::STATUS_EXCHANGE) ->setDeposit($deposit) ->setWithdraw($withdraw) diff --git a/src/Traits/CanPay.php b/src/Traits/CanPay.php index 4ec427f90..4336906de 100644 --- a/src/Traits/CanPay.php +++ b/src/Traits/CanPay.php @@ -4,7 +4,7 @@ use Bavix\Wallet\Interfaces\Product; use Bavix\Wallet\Models\Transfer; -use Bavix\Wallet\Services\MakeService; +use Bavix\Wallet\Objects\Cart; use function current; trait CanPay @@ -19,7 +19,7 @@ trait CanPay */ public function payFree(Product $product): Transfer { - return current($this->payFreeCart(app(MakeService::class)->makeCart()->addItem($product))); + return current($this->payFreeCart(app(Cart::class)->addItem($product))); } /** @@ -29,7 +29,7 @@ public function payFree(Product $product): Transfer */ public function safePay(Product $product, bool $force = null): ?Transfer { - return current($this->safePayCart(app(MakeService::class)->makeCart()->addItem($product), $force)) ?: null; + return current($this->safePayCart(app(Cart::class)->addItem($product), $force)) ?: null; } /** @@ -40,7 +40,7 @@ public function safePay(Product $product, bool $force = null): ?Transfer */ public function pay(Product $product, bool $force = null): Transfer { - return current($this->payCart(app(MakeService::class)->makeCart()->addItem($product), $force)); + return current($this->payCart(app(Cart::class)->addItem($product), $force)); } /** @@ -50,7 +50,7 @@ public function pay(Product $product, bool $force = null): Transfer */ public function forcePay(Product $product): Transfer { - return current($this->forcePayCart(app(MakeService::class)->makeCart()->addItem($product))); + return current($this->forcePayCart(app(Cart::class)->addItem($product))); } /** @@ -61,7 +61,7 @@ public function forcePay(Product $product): Transfer */ public function safeRefund(Product $product, bool $force = null, bool $gifts = null): bool { - return $this->safeRefundCart(app(MakeService::class)->makeCart()->addItem($product), $force, $gifts); + return $this->safeRefundCart(app(Cart::class)->addItem($product), $force, $gifts); } /** @@ -73,7 +73,7 @@ public function safeRefund(Product $product, bool $force = null, bool $gifts = n */ public function refund(Product $product, bool $force = null, bool $gifts = null): bool { - return $this->refundCart(app(MakeService::class)->makeCart()->addItem($product), $force, $gifts); + return $this->refundCart(app(Cart::class)->addItem($product), $force, $gifts); } /** @@ -84,7 +84,7 @@ public function refund(Product $product, bool $force = null, bool $gifts = null) */ public function forceRefund(Product $product, bool $gifts = null): bool { - return $this->forceRefundCart(app(MakeService::class)->makeCart()->addItem($product), $gifts); + return $this->forceRefundCart(app(Cart::class)->addItem($product), $gifts); } /** @@ -94,7 +94,7 @@ public function forceRefund(Product $product, bool $gifts = null): bool */ public function safeRefundGift(Product $product, bool $force = null): bool { - return $this->safeRefundGiftCart(app(MakeService::class)->makeCart()->addItem($product), $force); + return $this->safeRefundGiftCart(app(Cart::class)->addItem($product), $force); } /** @@ -105,7 +105,7 @@ public function safeRefundGift(Product $product, bool $force = null): bool */ public function refundGift(Product $product, bool $force = null): bool { - return $this->refundGiftCart(app(MakeService::class)->makeCart()->addItem($product), $force); + return $this->refundGiftCart(app(Cart::class)->addItem($product), $force); } /** @@ -115,7 +115,7 @@ public function refundGift(Product $product, bool $force = null): bool */ public function forceRefundGift(Product $product): bool { - return $this->forceRefundGiftCart(app(MakeService::class)->makeCart()->addItem($product)); + return $this->forceRefundGiftCart(app(Cart::class)->addItem($product)); } } diff --git a/src/Traits/CartPay.php b/src/Traits/CartPay.php index 52edc9cc8..cd9397606 100644 --- a/src/Traits/CartPay.php +++ b/src/Traits/CartPay.php @@ -7,7 +7,6 @@ use Bavix\Wallet\Models\Transfer; use Bavix\Wallet\Objects\Cart; use Bavix\Wallet\Services\CommonService; -use Bavix\Wallet\Services\MakeService; use Illuminate\Database\Eloquent\ModelNotFoundException; use Illuminate\Support\Facades\DB; use Throwable; @@ -230,7 +229,7 @@ public function forceRefundGiftCart(Cart $cart): bool */ public function paid(Product $product, bool $gifts = null): ?Transfer { - return current(app(MakeService::class)->makeCart()->addItem($product)->alreadyBuy($this, $gifts)) ?: null; + return current(app(Cart::class)->addItem($product)->alreadyBuy($this, $gifts)) ?: null; } } diff --git a/src/Traits/HasGift.php b/src/Traits/HasGift.php index 4af6141fa..480198198 100644 --- a/src/Traits/HasGift.php +++ b/src/Traits/HasGift.php @@ -5,9 +5,9 @@ use Bavix\Wallet\Interfaces\Product; use Bavix\Wallet\Interfaces\Wallet; use Bavix\Wallet\Models\Transfer; +use Bavix\Wallet\Objects\Bring; use Bavix\Wallet\Services\CommonService; use Bavix\Wallet\Services\LockService; -use Bavix\Wallet\Services\MakeService; use Bavix\Wallet\Services\WalletService; use Illuminate\Support\Facades\DB; use Throwable; @@ -86,8 +86,7 @@ public function gift(Wallet $to, Product $product, bool $force = null): Transfer ->getWallet($to); $transfers = $commonService->assemble([ - app(MakeService::class) - ->makeBring() + app(Bring::class) ->setStatus(Transfer::STATUS_GIFT) ->setDeposit($deposit) ->setWithdraw($withdraw) diff --git a/src/WalletServiceProvider.php b/src/WalletServiceProvider.php index 68c3baf00..6c1954834 100644 --- a/src/WalletServiceProvider.php +++ b/src/WalletServiceProvider.php @@ -7,10 +7,13 @@ use Bavix\Wallet\Models\Transaction; use Bavix\Wallet\Models\Transfer; use Bavix\Wallet\Models\Wallet; +use Bavix\Wallet\Objects\Bring; +use Bavix\Wallet\Objects\Cart; +use Bavix\Wallet\Objects\EmptyLock; +use Bavix\Wallet\Objects\Operation; use Bavix\Wallet\Services\CommonService; use Bavix\Wallet\Services\ExchangeService; use Bavix\Wallet\Services\LockService; -use Bavix\Wallet\Services\MakeService; use Bavix\Wallet\Services\ProxyService; use Bavix\Wallet\Services\WalletService; use Illuminate\Support\ServiceProvider; @@ -79,15 +82,22 @@ public function register(): void // Bind eloquent models to IoC container $this->app->singleton(Rateable::class, config('wallet.package.rateable')); - $this->app->singleton(Transaction::class, config('wallet.transaction.model')); - $this->app->singleton(Transfer::class, config('wallet.transfer.model')); - $this->app->singleton(Wallet::class, config('wallet.wallet.model')); $this->app->singleton(ExchangeService::class, config('wallet.services.exchange')); $this->app->singleton(CommonService::class, config('wallet.services.common')); $this->app->singleton(ProxyService::class, config('wallet.services.proxy')); $this->app->singleton(WalletService::class, config('wallet.services.wallet')); $this->app->singleton(LockService::class, config('wallet.services.lock')); - $this->app->singleton(MakeService::class, config('wallet.services.make')); + + // models + $this->app->bind(Transaction::class, config('wallet.transaction.model')); + $this->app->bind(Transfer::class, config('wallet.transfer.model')); + $this->app->bind(Wallet::class, config('wallet.wallet.model')); + + // object's + $this->app->bind(Bring::class, config('wallet.objects.bring')); + $this->app->bind(Cart::class, config('wallet.objects.cart')); + $this->app->bind(EmptyLock::class, config('wallet.objects.emptyLock')); + $this->app->bind(Operation::class, config('wallet.objects.operation')); } } diff --git a/tests/BalanceTest.php b/tests/BalanceTest.php index 778fe286b..87561f0f7 100644 --- a/tests/BalanceTest.php +++ b/tests/BalanceTest.php @@ -3,6 +3,7 @@ namespace Bavix\Wallet\Test; use Bavix\Wallet\Models\Wallet; +use Bavix\Wallet\Objects\Cart; use Bavix\Wallet\Services\CommonService; use Bavix\Wallet\Services\ProxyService; use Bavix\Wallet\Test\Models\Buyer; diff --git a/tests/CartTest.php b/tests/CartTest.php index 5c1f18e50..0e93687a8 100644 --- a/tests/CartTest.php +++ b/tests/CartTest.php @@ -3,7 +3,7 @@ namespace Bavix\Wallet\Test; use Bavix\Wallet\Models\Transfer; -use Bavix\Wallet\Services\MakeService; +use Bavix\Wallet\Objects\Cart; use Bavix\Wallet\Test\Models\Buyer; use Bavix\Wallet\Test\Models\Item; use Illuminate\Database\Eloquent\ModelNotFoundException; @@ -26,10 +26,7 @@ public function testPay(): void 'quantity' => 1, ]); - $cart = app(MakeService::class) - ->makeCart() - ->addItems($products); - + $cart = app(Cart::class)->addItems($products); foreach ($cart->getItems() as $product) { $this->assertEquals($product->balance, 0); } @@ -72,9 +69,7 @@ public function testCartQuantity(): void 'quantity' => 10, ]); - $cart = app(MakeService::class) - ->makeCart(); - + $cart = app(Cart::class); $amount = 0; for ($i = 0; $i < count($products) - 1; $i++) { $rnd = random_int(1, 5); @@ -110,9 +105,7 @@ public function testModelNotFoundException(): void 'quantity' => 10, ]); - $cart = app(MakeService::class) - ->makeCart(); - + $cart = app(Cart::class); $total = 0; for ($i = 0; $i < count($products) - 1; $i++) { $rnd = random_int(1, 5); @@ -126,8 +119,7 @@ public function testModelNotFoundException(): void $transfers = $buyer->payCart($cart); $this->assertCount($total, $transfers); - $refundCart = app(MakeService::class) - ->makeCart() + $refundCart = app(Cart::class) ->addItems($products); // all goods $buyer->refundCart($refundCart); diff --git a/tests/Common/Models/Transaction.php b/tests/Common/Models/Transaction.php index 02b596157..8f43321e2 100644 --- a/tests/Common/Models/Transaction.php +++ b/tests/Common/Models/Transaction.php @@ -2,7 +2,22 @@ namespace Bavix\Wallet\Test\Common\Models; +/** + * Class Transaction + * @package Bavix\Wallet\Test\Common\Models + * @property null|string $bank_method + */ class Transaction extends \Bavix\Wallet\Models\Transaction { + /** + * @inheritDoc + */ + public function getFillable(): array + { + return array_merge($this->fillable, [ + 'bank_method', + ]); + } + } diff --git a/tests/EmptyLockTest.php b/tests/EmptyLockTest.php index b785fc62a..e4b9d8033 100644 --- a/tests/EmptyLockTest.php +++ b/tests/EmptyLockTest.php @@ -2,7 +2,7 @@ namespace Bavix\Wallet\Test; -use Bavix\Wallet\Services\MakeService; +use Bavix\Wallet\Objects\EmptyLock; class EmptyLockTest extends TestCase { @@ -12,7 +12,7 @@ class EmptyLockTest extends TestCase */ public function testSimple(): void { - $empty = app(MakeService::class)->makeEmptyLock(); + $empty = app(EmptyLock::class); $this->assertTrue($empty->block(1)); $this->assertTrue($empty->block(1, null)); $this->assertNull($empty->get()); @@ -26,7 +26,7 @@ public function testSimple(): void */ public function testOwner(): void { - $empty = app(MakeService::class)->makeEmptyLock(); + $empty = app(EmptyLock::class); $str = $empty->owner(); $this->assertIsString($str); $this->assertEquals($str, $empty->owner()); diff --git a/tests/Objects/Operation.php b/tests/Objects/Operation.php new file mode 100644 index 000000000..5c7491a65 --- /dev/null +++ b/tests/Objects/Operation.php @@ -0,0 +1,18 @@ + $this->meta['bank_method'] ?? null, + ]); + } + +} diff --git a/tests/SingletonTest.php b/tests/SingletonTest.php new file mode 100644 index 000000000..c234b54ca --- /dev/null +++ b/tests/SingletonTest.php @@ -0,0 +1,135 @@ +assertNotEquals($this->getRefId(Bring::class), $this->getRefId(Bring::class)); + } + + /** + * @return void + */ + public function testCart(): void + { + $this->assertNotEquals($this->getRefId(Cart::class), $this->getRefId(Cart::class)); + } + + /** + * @return void + */ + public function testEmptyLock(): void + { + $this->assertNotEquals($this->getRefId(EmptyLock::class), $this->getRefId(EmptyLock::class)); + } + + /** + * @return void + */ + public function testOperation(): void + { + $this->assertNotEquals($this->getRefId(Operation::class), $this->getRefId(Operation::class)); + } + + /** + * @return void + */ + public function testRateable(): void + { + $this->assertEquals($this->getRefId(Rateable::class), $this->getRefId(Rateable::class)); + } + + /** + * @return void + */ + public function testTransaction(): void + { + $this->assertNotEquals($this->getRefId(Transaction::class), $this->getRefId(Transaction::class)); + } + + /** + * @return void + */ + public function testTransfer(): void + { + $this->assertNotEquals($this->getRefId(Transfer::class), $this->getRefId(Transfer::class)); + } + + /** + * @return void + */ + public function testWallet(): void + { + $this->assertNotEquals($this->getRefId(Wallet::class), $this->getRefId(Wallet::class)); + } + + /** + * @return void + */ + public function testExchangeService(): void + { + $this->assertEquals($this->getRefId(ExchangeService::class), $this->getRefId(ExchangeService::class)); + } + + /** + * @return void + */ + public function testCommonService(): void + { + $this->assertEquals($this->getRefId(CommonService::class), $this->getRefId(CommonService::class)); + } + + /** + * @return void + */ + public function testProxyService(): void + { + $this->assertEquals($this->getRefId(ProxyService::class), $this->getRefId(ProxyService::class)); + } + + /** + * @return void + */ + public function testWalletService(): void + { + $this->assertEquals($this->getRefId(WalletService::class), $this->getRefId(WalletService::class)); + } + + /** + * @return void + */ + public function testLockService(): void + { + $this->assertEquals($this->getRefId(LockService::class), $this->getRefId(LockService::class)); + } + +} diff --git a/tests/TestCase.php b/tests/TestCase.php index 5c6f840f9..5caff5088 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -26,15 +26,15 @@ public function setUp(): void $this->withFactories(__DIR__ . '/factories'); $this->loadMigrationsFrom([ '--database' => 'testbench', - '--path' => __DIR__ . '/migrations' + '--path' => dirname(__DIR__) . '/database/migrations_v1' ]); $this->loadMigrationsFrom([ '--database' => 'testbench', - '--path' => dirname(__DIR__) . '/database/migrations_v1' + '--path' => dirname(__DIR__) . '/database/migrations_v2' ]); $this->loadMigrationsFrom([ '--database' => 'testbench', - '--path' => dirname(__DIR__) . '/database/migrations_v2' + '--path' => __DIR__ . '/migrations' ]); } diff --git a/tests/WalletExtensionTest.php b/tests/WalletExtensionTest.php new file mode 100644 index 000000000..2662d0fbc --- /dev/null +++ b/tests/WalletExtensionTest.php @@ -0,0 +1,53 @@ +app->bind(Operation::class, Objects\Operation::class); + } + + /** + * @return void + */ + public function testCustomAttribute(): void + { + /** + * @var Buyer $buyer + */ + $buyer = factory(Buyer::class)->create(); + $this->assertFalse($buyer->relationLoaded('wallet')); + $transaction = $buyer->deposit(1000, ['bank_method' => 'VietComBank']); + + $this->assertEquals($transaction->amount, $buyer->balance); + $this->assertInstanceOf(Transaction::class, $transaction); + $this->assertEquals('VietComBank', $transaction->bank_method); + } + + public function testNoCustomAttribute(): void + { + /** + * @var Buyer $buyer + */ + $buyer = factory(Buyer::class)->create(); + $this->assertFalse($buyer->relationLoaded('wallet')); + $transaction = $buyer->deposit(1000); + + $this->assertEquals($transaction->amount, $buyer->balance); + $this->assertInstanceOf(Transaction::class, $transaction); + $this->assertNull($transaction->bank_method); + } + +} diff --git a/tests/migrations/2019_09_19_191432_alter_column_transaction_table.php b/tests/migrations/2019_09_19_191432_alter_column_transaction_table.php new file mode 100644 index 000000000..75a0ca0bb --- /dev/null +++ b/tests/migrations/2019_09_19_191432_alter_column_transaction_table.php @@ -0,0 +1,33 @@ +getTable(), function (Blueprint $table) { + $table->string('bank_method')->nullable(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + Schema::table((new Transaction())->getTable(), function (Blueprint $table) { + $table->dropColumn('bank_method'); + }); + } +}