Passed
Pull Request — master (#9)
by Бабичев
03:04
created

WalletTest   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 131
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 54
dl 0
loc 131
c 0
b 0
f 0
rs 10
wmc 7

7 Methods

Rating   Name   Duplication   Size   Complexity  
A testWithdraw() 0 18 1
A testInvalidDeposit() 0 4 1
A getUser() 0 3 1
A testInvalidWithdraw() 0 4 1
A testDeposit() 0 18 1
A testTransferYourself() 0 14 1
A testTransfer() 0 31 1
1
<?php
2
3
namespace Bavix\Wallet\Test;
4
5
use Bavix\Wallet\Test\Models\User;
6
7
class WalletTest extends TestCase
8
{
9
10
    /**
11
     * @return User
12
     */
13
    public function getUser(): User
14
    {
15
        return factory(User::class)->create();
16
    }
17
18
    /**
19
     * @return void
20
     */
21
    public function testDeposit(): void
22
    {
23
        $user = $this->getUser();
24
        $this->assertEquals($user->balance, 0);
25
26
        $user->deposit(10);
27
        $this->assertEquals($user->balance, 10);
28
29
        $user->deposit(10);
30
        $this->assertEquals($user->balance, 20);
31
32
        $user->deposit(980);
33
        $this->assertEquals($user->balance, 1000);
34
35
        $this->assertEquals($user->transactions()->count(), 3);
36
37
        $user->withdraw($user->balance);
38
        $this->assertEquals($user->balance, 0);
39
    }
40
41
    /**
42
     * @return void
43
     * @expectedException \Bavix\Wallet\Exceptions\AmountInvalid
44
     */
45
    public function testInvalidDeposit(): void
46
    {
47
        $user = $this->getUser();
48
        $user->deposit(-1);
49
    }
50
51
    /**
52
     * @return void
53
     * @expectedException \Bavix\Wallet\Exceptions\BalanceIsEmpty
54
     */
55
    public function testWithdraw(): void
56
    {
57
        $user = $this->getUser();
58
        $this->assertEquals($user->balance, 0);
59
60
        $user->deposit(100);
61
        $this->assertEquals($user->balance, 100);
62
63
        $user->withdraw(10);
64
        $this->assertEquals($user->balance, 90);
65
66
        $user->withdraw(81);
67
        $this->assertEquals($user->balance, 9);
68
69
        $user->withdraw(9);
70
        $this->assertEquals($user->balance, 0);
71
72
        $user->withdraw(1);
73
    }
74
75
    /**
76
     * @return void
77
     * @expectedException \Bavix\Wallet\Exceptions\AmountInvalid
78
     */
79
    public function testInvalidWithdraw(): void
80
    {
81
        $user = $this->getUser();
82
        $user->withdraw(-1);
83
    }
84
85
    /**
86
     * @return void
87
     */
88
    public function testTransfer(): void
89
    {
90
        /**
91
         * @var User $first
92
         * @var User $second
93
         */
94
        list($first, $second) = factory(User::class, 2)->create();
95
        $this->assertNotEquals($first->id, $second->id);
96
        $this->assertEquals($first->balance, 0);
97
        $this->assertEquals($second->balance, 0);
98
99
        $first->deposit(100);
100
        $this->assertEquals($first->balance, 100);
101
102
        $second->deposit(100);
103
        $this->assertEquals($second->balance, 100);
104
105
        $first->transfer($second, 100);
106
        $this->assertEquals($first->balance, 0);
107
        $this->assertEquals($second->balance, 200);
108
109
        $second->transfer($first, 100);
110
        $this->assertEquals($second->balance, 100);
111
        $this->assertEquals($first->balance, 100);
112
113
        $second->transfer($first, 100);
114
        $this->assertEquals($second->balance, 0);
115
        $this->assertEquals($first->balance, 200);
116
117
        $first->withdraw($first->balance);
118
        $this->assertEquals($first->balance, 0);
119
    }
120
121
    /**
122
     * @return void
123
     */
124
    public function testTransferYourself(): void
125
    {
126
        /**
127
         * @var User $user
128
         */
129
        $user = factory(User::class)->create();
130
        $this->assertEquals($user->balance, 0);
131
132
        $user->deposit(100);
133
        $user->transfer($user, 100);
134
        $this->assertEquals($user->balance, 100);
135
136
        $user->withdraw($user->balance);
137
        $this->assertEquals($user->balance, 0);
138
    }
139
140
}
141