Skip to content

Commit 1dd3561

Browse files
author
Babichev Maxim
committed
upgrade tests
1 parent 9182059 commit 1dd3561

File tree

6 files changed

+58
-21
lines changed

6 files changed

+58
-21
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@ composer.phar
22
/vendor/
33
composer.lock
44
.idea/
5+
build/

phpunit.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,8 @@
1919
<directory suffix=".php">src/</directory>
2020
</whitelist>
2121
</filter>
22+
<logging>
23+
<log type="coverage-html" target="./build/html/"/>
24+
<log type="coverage-clover" target="./build/logs/clover.xml"/>
25+
</logging>
2226
</phpunit>

src/WalletServiceProvider.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ class WalletServiceProvider extends ServiceProvider
1111
* Bootstrap services.
1212
*
1313
* @return void
14+
* @codeCoverageIgnore
1415
*/
1516
public function boot(): void
1617
{

tests/ProductTest.php

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,55 @@
22

33
namespace Bavix\Wallet\Test;
44

5-
use Bavix\Wallet\Models\Transfer;
65
use Bavix\Wallet\Test\Models\Buyer;
76
use Bavix\Wallet\Test\Models\Item;
87

98
class ProductTest extends TestCase
109
{
1110

1211
/**
13-
* @return Buyer
12+
* @return void
1413
*/
15-
public function getBuyer(): Buyer
14+
public function testPay(): void
1615
{
17-
return factory(Buyer::class)->create();
16+
$buyer = factory(Buyer::class)->create();
17+
$product = factory(Item::class)->create([
18+
'quantity' => 1,
19+
]);
20+
21+
$this->assertEquals($buyer->balance, 0);
22+
$buyer->deposit($product->price);
23+
24+
$this->assertEquals($buyer->balance, $product->price);
25+
$this->assertNotNull($buyer->pay($product));
26+
27+
$this->assertEquals($buyer->balance, 0);
28+
$this->assertNull($buyer->safePay($product));
1829
}
1930

2031
/**
2132
* @return void
2233
*/
23-
public function testPay(): void
34+
public function testRefund(): void
2435
{
25-
$buyer = $this->getBuyer();
36+
$buyer = factory(Buyer::class)->create();
2637
$product = factory(Item::class)->create([
2738
'quantity' => 1,
2839
]);
40+
2941
$this->assertEquals($buyer->balance, 0);
3042
$buyer->deposit($product->price);
43+
3144
$this->assertEquals($buyer->balance, $product->price);
32-
$this->assertTrue($buyer->pay($product)->exists);
45+
$this->assertNotNull($buyer->pay($product));
46+
47+
$this->assertTrue($buyer->refund($product));
48+
$this->assertEquals($buyer->balance, $product->price);
49+
50+
$this->assertFalse($buyer->safeRefund($product));
51+
$this->assertEquals($buyer->balance, $product->price);
52+
53+
$this->assertNotNull($buyer->pay($product));
3354
$this->assertEquals($buyer->balance, 0);
3455
}
3556

tests/TestCase.php

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,14 @@ public function setUp(): void
1616
{
1717
parent::setUp();
1818
$this->withFactories(__DIR__ . '/factories');
19-
$this->loadMigrationsFrom(__DIR__ . '/migrations');
20-
$this->loadMigrationsFrom(\dirname(__DIR__) . '/database/migrations');
19+
$this->loadMigrationsFrom([
20+
'--database' => 'testbench',
21+
'--path' => __DIR__ . '/migrations'
22+
]);
23+
$this->loadMigrationsFrom([
24+
'--database' => 'testbench',
25+
'--path' => \dirname(__DIR__) . '/database/migrations'
26+
]);
2127
}
2228

2329
/**

tests/WalletTest.php

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,12 @@
77
class WalletTest extends TestCase
88
{
99

10-
/**
11-
* @return User
12-
*/
13-
public function getUser(): User
14-
{
15-
return factory(User::class)->create();
16-
}
17-
1810
/**
1911
* @return void
2012
*/
2113
public function testDeposit(): void
2214
{
23-
$user = $this->getUser();
15+
$user = factory(User::class)->create();
2416
$this->assertEquals($user->balance, 0);
2517

2618
$user->deposit(10);
@@ -44,7 +36,7 @@ public function testDeposit(): void
4436
*/
4537
public function testInvalidDeposit(): void
4638
{
47-
$user = $this->getUser();
39+
$user = factory(User::class)->create();
4840
$user->deposit(-1);
4941
}
5042

@@ -54,7 +46,7 @@ public function testInvalidDeposit(): void
5446
*/
5547
public function testWithdraw(): void
5648
{
57-
$user = $this->getUser();
49+
$user = factory(User::class)->create();
5850
$this->assertEquals($user->balance, 0);
5951

6052
$user->deposit(100);
@@ -78,7 +70,7 @@ public function testWithdraw(): void
7870
*/
7971
public function testInvalidWithdraw(): void
8072
{
81-
$user = $this->getUser();
73+
$user = factory(User::class)->create();
8274
$user->withdraw(-1);
8375
}
8476

@@ -116,6 +108,18 @@ public function testTransfer(): void
116108

117109
$first->withdraw($first->balance);
118110
$this->assertEquals($first->balance, 0);
111+
112+
$this->assertNull($first->safeTransfer($second, 100));
113+
$this->assertEquals($first->balance, 0);
114+
$this->assertEquals($second->balance, 0);
115+
116+
$this->assertNotNull($first->forceTransfer($second, 100));
117+
$this->assertEquals($first->balance, -100);
118+
$this->assertEquals($second->balance, 100);
119+
120+
$this->assertNotNull($second->forceTransfer($first, 100));
121+
$this->assertEquals($first->balance, 0);
122+
$this->assertEquals($second->balance, 0);
119123
}
120124

121125
/**

0 commit comments

Comments
 (0)