|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Bavix\Wallet\Test; |
| 4 | + |
| 5 | +use Bavix\Wallet\Models\Transfer; |
| 6 | +use Bavix\Wallet\Services\WalletService; |
| 7 | +use Bavix\Wallet\Test\Models\Buyer; |
| 8 | +use Bavix\Wallet\Test\Models\ItemDiscountTax; |
| 9 | + |
| 10 | +class GiftDiscountTaxTest extends TestCase |
| 11 | +{ |
| 12 | + |
| 13 | + /** |
| 14 | + * @return void |
| 15 | + */ |
| 16 | + public function testGift(): void |
| 17 | + { |
| 18 | + /** |
| 19 | + * @var Buyer $first |
| 20 | + * @var Buyer $second |
| 21 | + * @var ItemDiscountTax $product |
| 22 | + */ |
| 23 | + [$first, $second] = factory(Buyer::class, 2)->create(); |
| 24 | + $product = factory(ItemDiscountTax::class)->create([ |
| 25 | + 'quantity' => 1, |
| 26 | + ]); |
| 27 | + |
| 28 | + $this->assertEquals($first->balance, 0); |
| 29 | + $this->assertEquals($second->balance, 0); |
| 30 | + |
| 31 | + $fee = app(WalletService::class)->fee( |
| 32 | + $product, |
| 33 | + $product->getAmountProduct($first) - $product->getPersonalDiscount($first) |
| 34 | + ); |
| 35 | + |
| 36 | + $first->deposit($product->getAmountProduct($first) + $fee); |
| 37 | + $this->assertEquals( |
| 38 | + $first->balance, |
| 39 | + $product->getAmountProduct($first) + $fee |
| 40 | + ); |
| 41 | + |
| 42 | + $transfer = $first->wallet->gift($second, $product); |
| 43 | + $this->assertEquals($first->balance, $product->getPersonalDiscount($first)); |
| 44 | + $this->assertEquals($second->balance, 0); |
| 45 | + $this->assertNull($first->paid($product, true)); |
| 46 | + $this->assertNotNull($second->paid($product, true)); |
| 47 | + $this->assertNull($second->wallet->paid($product)); |
| 48 | + $this->assertNotNull($second->wallet->paid($product, true)); |
| 49 | + $this->assertEquals($transfer->status, Transfer::STATUS_GIFT); |
| 50 | + } |
| 51 | + |
| 52 | + /** |
| 53 | + * @return void |
| 54 | + */ |
| 55 | + public function testRefund(): void |
| 56 | + { |
| 57 | + /** |
| 58 | + * @var Buyer $first |
| 59 | + * @var Buyer $second |
| 60 | + * @var ItemDiscountTax $product |
| 61 | + */ |
| 62 | + [$first, $second] = factory(Buyer::class, 2)->create(); |
| 63 | + $product = factory(ItemDiscountTax::class)->create([ |
| 64 | + 'quantity' => 1, |
| 65 | + ]); |
| 66 | + |
| 67 | + $this->assertEquals($first->balance, 0); |
| 68 | + $this->assertEquals($second->balance, 0); |
| 69 | + |
| 70 | + $fee = app(WalletService::class)->fee( |
| 71 | + $product, |
| 72 | + $product->getAmountProduct($first) - $product->getPersonalDiscount($first) |
| 73 | + ); |
| 74 | + |
| 75 | + $first->deposit($product->getAmountProduct($first) + $fee); |
| 76 | + $this->assertEquals($first->balance, $product->getAmountProduct($first) + $fee); |
| 77 | + |
| 78 | + $transfer = $first->wallet->gift($second, $product); |
| 79 | + $this->assertEquals($first->balance, $product->getPersonalDiscount($first)); |
| 80 | + $this->assertEquals($second->balance, 0); |
| 81 | + $this->assertEquals($transfer->status, Transfer::STATUS_GIFT); |
| 82 | + |
| 83 | + $first->withdraw($product->getPersonalDiscount($first)); |
| 84 | + $this->assertEquals($first->balance, 0); |
| 85 | + |
| 86 | + $this->assertFalse($second->wallet->safeRefund($product)); |
| 87 | + $this->assertTrue($second->wallet->refundGift($product)); |
| 88 | + |
| 89 | + $this->assertEquals( |
| 90 | + $first->balance, |
| 91 | + $product->getAmountProduct($first) - $product->getPersonalDiscount($first) |
| 92 | + ); |
| 93 | + |
| 94 | + $first->withdraw($first->balance); |
| 95 | + $this->assertEquals($first->balance, 0); |
| 96 | + $this->assertEquals($second->balance, 0); |
| 97 | + |
| 98 | + $this->assertNull($second->wallet->safeGift($first, $product)); |
| 99 | + |
| 100 | + $secondFee = app(WalletService::class)->fee( |
| 101 | + $product, |
| 102 | + $product->getAmountProduct($second) - $product->getPersonalDiscount($second) |
| 103 | + ); |
| 104 | + |
| 105 | + $transfer = $second->wallet->forceGift($first, $product); |
| 106 | + $this->assertNotNull($transfer); |
| 107 | + $this->assertEquals($transfer->status, Transfer::STATUS_GIFT); |
| 108 | + |
| 109 | + $this->assertEquals( |
| 110 | + $second->balance, |
| 111 | + -(($product->getAmountProduct($second) + $secondFee) - $product->getPersonalDiscount($second)) |
| 112 | + ); |
| 113 | + |
| 114 | + $second->deposit(-$second->balance); |
| 115 | + $this->assertEquals($second->balance, 0); |
| 116 | + $this->assertEquals($first->balance, 0); |
| 117 | + |
| 118 | + $product->withdraw($product->balance); |
| 119 | + $this->assertEquals($product->balance, 0); |
| 120 | + |
| 121 | + $this->assertFalse($first->safeRefundGift($product)); |
| 122 | + $this->assertTrue($first->forceRefundGift($product)); |
| 123 | + |
| 124 | + $this->assertEquals($second->balance, -$product->balance); |
| 125 | + |
| 126 | + $this->assertEquals( |
| 127 | + $product->balance, |
| 128 | + -($product->getAmountProduct($second) - $product->getPersonalDiscount($second)) |
| 129 | + ); |
| 130 | + |
| 131 | + $this->assertEquals( |
| 132 | + $second->balance, |
| 133 | + $product->getAmountProduct($second) - $product->getPersonalDiscount($second) |
| 134 | + ); |
| 135 | + |
| 136 | + $second->withdraw($second->balance); |
| 137 | + $this->assertEquals($second->balance, 0); |
| 138 | + } |
| 139 | + |
| 140 | +} |
0 commit comments