22
33namespace Bavix \Wallet \Test ;
44
5+ use Bavix \Wallet \Models \Transaction ;
56use Bavix \Wallet \Test \Models \Buyer ;
67use Bavix \Wallet \Test \Models \Item ;
78
@@ -13,6 +14,10 @@ class ProductTest extends TestCase
1314 */
1415 public function testPay (): void
1516 {
17+ /**
18+ * @var Buyer $buyer
19+ * @var Item $product
20+ */
1621 $ buyer = factory (Buyer::class)->create ();
1722 $ product = factory (Item::class)->create ([
1823 'quantity ' => 1 ,
@@ -22,7 +27,30 @@ public function testPay(): void
2227 $ buyer ->deposit ($ product ->price );
2328
2429 $ this ->assertEquals ($ buyer ->balance , $ product ->price );
25- $ this ->assertNotNull ($ buyer ->pay ($ product ));
30+ $ transfer = $ buyer ->pay ($ product );
31+ $ this ->assertNotNull ($ transfer );
32+
33+ /**
34+ * @var Transaction $withdraw
35+ * @var Transaction $deposit
36+ */
37+ $ withdraw = $ transfer ->withdraw ;
38+ $ deposit = $ transfer ->deposit ;
39+
40+ $ this ->assertInstanceOf (Transaction::class, $ withdraw );
41+ $ this ->assertInstanceOf (Transaction::class, $ deposit );
42+
43+ $ this ->assertInstanceOf (Buyer::class, $ withdraw ->payable );
44+ $ this ->assertInstanceOf (Item::class, $ deposit ->payable );
45+
46+ $ this ->assertEquals ($ buyer ->getKey (), $ withdraw ->payable ->getKey ());
47+ $ this ->assertEquals ($ product ->getKey (), $ deposit ->payable ->getKey ());
48+
49+ $ this ->assertInstanceOf (Buyer::class, $ transfer ->from );
50+ $ this ->assertInstanceOf (Item::class, $ transfer ->to );
51+
52+ $ this ->assertEquals ($ buyer ->getKey (), $ transfer ->from ->getKey ());
53+ $ this ->assertEquals ($ product ->getKey (), $ transfer ->to ->getKey ());
2654
2755 $ this ->assertEquals ($ buyer ->balance , 0 );
2856 $ this ->assertNull ($ buyer ->safePay ($ product ));
@@ -33,6 +61,10 @@ public function testPay(): void
3361 */
3462 public function testRefund (): void
3563 {
64+ /**
65+ * @var Buyer $buyer
66+ * @var Item $product
67+ */
3668 $ buyer = factory (Buyer::class)->create ();
3769 $ product = factory (Item::class)->create ([
3870 'quantity ' => 1 ,
@@ -54,4 +86,88 @@ public function testRefund(): void
5486 $ this ->assertEquals ($ buyer ->balance , 0 );
5587 }
5688
89+ /**
90+ * @return void
91+ */
92+ public function testForceRefund (): void
93+ {
94+ /**
95+ * @var Buyer $buyer
96+ * @var Item $product
97+ */
98+ $ buyer = factory (Buyer::class)->create ();
99+ $ product = factory (Item::class)->create ([
100+ 'quantity ' => 1 ,
101+ ]);
102+
103+ $ this ->assertNotEquals ($ product ->balance , 0 );
104+ $ product ->withdraw ($ product ->balance );
105+
106+ $ this ->assertEquals ($ buyer ->balance , 0 );
107+ $ buyer ->deposit ($ product ->price );
108+
109+ $ this ->assertEquals ($ buyer ->balance , $ product ->price );
110+
111+ $ buyer ->pay ($ product );
112+ $ this ->assertEquals ($ buyer ->balance , 0 );
113+ $ this ->assertEquals ($ product ->balance , $ product ->price );
114+
115+ $ product ->withdraw ($ product ->balance );
116+ $ this ->assertEquals ($ product ->balance , 0 );
117+
118+ $ this ->assertFalse ($ buyer ->safeRefund ($ product ));
119+ $ this ->assertTrue ($ buyer ->forceRefund ($ product ));
120+
121+ $ this ->assertEquals ($ product ->balance , -$ product ->price );
122+ $ this ->assertEquals ($ buyer ->balance , $ product ->price );
123+ $ product ->deposit (-$ product ->balance );
124+ $ buyer ->withdraw ($ buyer ->balance );
125+
126+ $ this ->assertEquals ($ product ->balance , 0 );
127+ $ this ->assertEquals ($ buyer ->balance , 0 );
128+ }
129+
130+ /**
131+ * @return void
132+ * @expectedException \Bavix\Wallet\Exceptions\ProductEnded
133+ */
134+ public function testOutOfStock (): void
135+ {
136+ /**
137+ * @var Buyer $buyer
138+ * @var Item $product
139+ */
140+ $ buyer = factory (Buyer::class)->create ();
141+ $ product = factory (Item::class)->create ([
142+ 'quantity ' => 1 ,
143+ ]);
144+
145+ $ buyer ->deposit ($ product ->price );
146+ $ buyer ->pay ($ product );
147+ $ buyer ->pay ($ product );
148+ }
149+
150+ /**
151+ * @return void
152+ */
153+ public function testForcePay (): void
154+ {
155+ /**
156+ * @var Buyer $buyer
157+ * @var Item $product
158+ */
159+ $ buyer = factory (Buyer::class)->create ();
160+ $ product = factory (Item::class)->create ([
161+ 'quantity ' => 1 ,
162+ ]);
163+
164+ $ this ->assertEquals ($ buyer ->balance , 0 );
165+ $ buyer ->forcePay ($ product );
166+
167+ $ this ->assertEquals ($ buyer ->balance , -$ product ->price );
168+
169+ $ buyer ->deposit (-$ buyer ->balance );
170+ $ this ->assertEquals ($ buyer ->balance , 0 );
171+ }
172+
57173}
0 commit comments