1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace MaksimM\CompositePrimaryKeys\Tests; |
4
|
|
|
|
5
|
|
|
use MaksimM\CompositePrimaryKeys\Tests\Stubs\TestBinaryUser; |
6
|
|
|
use MaksimM\CompositePrimaryKeys\Tests\Stubs\TestUser; |
7
|
|
|
|
8
|
|
|
class UpdatesTest extends CompositeKeyBaseUnit |
9
|
|
|
{ |
10
|
|
|
/** @test */ |
11
|
|
|
public function validateEmptyCounter() |
12
|
|
|
{ |
13
|
|
|
/** |
14
|
|
|
* @var TestUser |
15
|
|
|
*/ |
16
|
|
|
$model = TestUser::find([ |
17
|
|
|
'user_id' => 1, |
18
|
|
|
'organization_id' => 100, |
19
|
|
|
]); |
20
|
|
|
$this->assertNotNull($model); |
21
|
|
|
$this->assertInstanceOf(TestUser::class, $model); |
22
|
|
|
$this->assertEquals(0, $model->counter); |
23
|
|
|
|
24
|
|
|
return $model; |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** @test */ |
28
|
|
|
public function validateEmptyCounterBinaryModel() |
29
|
|
|
{ |
30
|
|
|
/** |
31
|
|
|
* @var TestBinaryUser |
32
|
|
|
*/ |
33
|
|
|
$model = TestBinaryUser::find([ |
34
|
|
|
'user_id' => md5(20000, true), |
35
|
|
|
'organization_id' => 100, |
36
|
|
|
]); |
37
|
|
|
$this->assertNotNull($model); |
38
|
|
|
$this->assertInstanceOf(TestBinaryUser::class, $model); |
39
|
|
|
$this->assertEquals(0, $model->counter); |
40
|
|
|
|
41
|
|
|
return $model; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** @test |
45
|
|
|
* @depends validateEmptyCounter |
46
|
|
|
*/ |
47
|
|
|
public function incrementingTest(TestUser $model) |
48
|
|
|
{ |
49
|
|
|
$model->increment('counter'); |
50
|
|
|
$model->refresh(); |
51
|
|
|
$this->assertEquals(1, $model->counter); |
52
|
|
|
|
53
|
|
|
return $model; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** @test |
57
|
|
|
* @depends validateEmptyCounterBinaryModel |
58
|
|
|
*/ |
59
|
|
|
public function incrementingBinaryTest(TestBinaryUser $model) |
60
|
|
|
{ |
61
|
|
|
$model->increment('counter'); |
62
|
|
|
$model->refresh(); |
63
|
|
|
$this->assertEquals(1, $model->counter); |
64
|
|
|
|
65
|
|
|
return $model; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** @test |
69
|
|
|
* @depends validateEmptyCounter |
70
|
|
|
*/ |
71
|
|
|
public function decrementingTest(TestUser $model) |
72
|
|
|
{ |
73
|
|
|
$model->decrement('counter'); |
74
|
|
|
$model->refresh(); |
75
|
|
|
$this->assertEquals(-1, $model->counter); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** @test |
79
|
|
|
* @depends validateEmptyCounter |
80
|
|
|
*/ |
81
|
|
|
public function updateTest(TestUser $model) |
82
|
|
|
{ |
83
|
|
|
$model->update([ |
84
|
|
|
'counter' => 9999, |
85
|
|
|
]); |
86
|
|
|
$model->refresh(); |
87
|
|
|
$this->assertEquals(9999, $model->counter); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** @test |
91
|
|
|
* @depends validateEmptyCounter |
92
|
|
|
*/ |
93
|
|
|
public function saveTest(TestUser $model) |
94
|
|
|
{ |
95
|
|
|
$this->assertTrue($model->exists); |
96
|
|
|
$model->counter = 6666; |
97
|
|
|
$model->save(); |
98
|
|
|
$model->refresh(); |
99
|
|
|
$this->assertEquals(6666, $model->counter); |
100
|
|
|
} |
101
|
|
|
} |
102
|
|
|
|