3
3
declare (strict_types=1 );
4
4
5
5
use App \Models \User ;
6
- use Illuminate \Database \Eloquent \Model ;
7
6
use Laravelcm \Gamify \Exceptions \InvalidPayeeModelException ;
8
7
use Laravelcm \Gamify \Exceptions \PointsNotDefinedException ;
9
8
use Laravelcm \Gamify \Exceptions \PointSubjectNotSetException ;
10
- use Laravelcm \Gamify \PointType ;
11
-
12
- final class FakeCreatePostPoint extends PointType
13
- {
14
- public int $ points = 10 ;
15
-
16
- public ?User $ author ;
17
-
18
- public function __construct (mixed $ subject , ?User $ author = null )
19
- {
20
- $ this ->subject = $ subject ;
21
- $ this ->author = $ author ;
22
- }
23
-
24
- public function payee (): ?User
25
- {
26
- return $ this ->author ;
27
- }
28
- }
29
-
30
- final class FakeWelcomeUserWithNamePoint extends PointType
31
- {
32
- public int $ points = 30 ;
33
-
34
- public string $ name = 'FakeName ' ;
35
-
36
- public function __construct (mixed $ subject , public ?User $ author = null ) {}
37
-
38
- public function payee (): ?User
39
- {
40
- return $ this ->author ;
41
- }
42
- }
43
-
44
- final class FakePointTypeWithoutSubject extends PointType
45
- {
46
- protected int $ point = 12 ;
47
-
48
- public function __construct ($ subject = null )
49
- {
50
- $ this ->subject = $ subject ;
51
- }
52
-
53
- public function payee (): User
54
- {
55
- return new User ;
56
- }
57
- }
58
-
59
- final class FakePointTypeWithoutPayee extends PointType
60
- {
61
- protected int $ point = 24 ;
62
-
63
- public function __construct (mixed $ subject = null )
64
- {
65
- $ this ->subject = $ subject ;
66
- }
67
- }
68
-
69
- final class FakePointWithoutPoint extends PointType
70
- {
71
- protected string $ payee = 'user ' ;
72
-
73
- public function __construct ($ subject = null )
74
- {
75
- $ this ->subject = $ subject ;
76
- }
77
- }
78
-
79
- final class FakeWelcomeUserWithFalseQualifier extends PointType
80
- {
81
- protected int $ points = 10 ;
82
-
83
- public function __construct ($ subject )
84
- {
85
- $ this ->subject = $ subject ;
86
- }
87
-
88
- public function qualifier (): bool
89
- {
90
- return false ;
91
- }
92
-
93
- public function payee (): ?User
94
- {
95
- return $ this ->getSubject ()->user ;
96
- }
97
- }
98
-
99
- final class FakePayeeFieldPoint extends PointType
100
- {
101
- protected int $ points = 10 ;
102
-
103
- /** @var string payee model relation on subject */
104
- protected string $ payee = 'user ' ;
105
-
106
- public function __construct (mixed $ subject )
107
- {
108
- $ this ->subject = $ subject ;
109
- }
110
- }
9
+ use Laravelcm \Gamify \Tests \Fixtures ;
111
10
112
11
beforeEach (function (): void {
113
12
$ this ->user = $ this ->createUser ();
114
13
});
115
14
116
- describe (PointType::class , function (): void {
15
+ describe (' PointType ' , function (): void {
117
16
it ('sets point type name from class name ' , function (): void {
118
- $ point = new FakeCreatePostPoint ($ this ->user );
17
+ $ point = new Fixtures \ FakeCreatePostPoint ($ this ->user );
119
18
120
19
expect ($ point ->getName ())->toBe ('FakeCreatePostPoint ' );
121
20
});
122
21
123
22
it ('uses name property for point name if provided ' , function (): void {
124
- $ point = new FakeWelcomeUserWithNamePoint ($ this ->user );
23
+ $ point = new Fixtures \ FakeWelcomeUserWithNamePoint ($ this ->user );
125
24
126
25
expect ($ point ->getName ())->toBe ('FakeName ' );
127
26
});
128
27
129
28
it ('can get points for a point type ' , function (): void {
130
- $ point = new FakeCreatePostPoint ($ this ->user );
29
+ $ point = new Fixtures \ FakeCreatePostPoint ($ this ->user );
131
30
132
31
expect ($ point ->getPoints ())->toBe (10 );
133
32
});
134
33
135
34
it ('gives point to a user ' , function (): void {
136
35
$ post = $ this ->createPost (['user_id ' => $ this ->user ->id ]);
137
36
138
- $ this ->user ->givePoint (new FakeCreatePostPoint ($ post , $ this ->user ));
37
+ $ this ->user ->givePoint (new Fixtures \ FakeCreatePostPoint ($ post , $ this ->user ));
139
38
140
- expect ($ this ->user ->getPoints ())->toBe (10 );
141
- expect ($ this ->user ->reputations )->toHaveCount (1 );
39
+ expect ($ this ->user ->getPoints ())->toBe (10 )
40
+ -> and ($ this ->user ->reputations )->toHaveCount (1 );
142
41
});
143
42
144
43
it ('can access a reputation payee and subject ' , function (): void {
145
44
$ post = $ this ->createPost (['user_id ' => $ this ->user ->id ]);
146
45
147
- $ this ->user ->givePoint (new FakeCreatePostPoint ($ post , $ this ->user ));
46
+ $ this ->user ->givePoint (new Fixtures \ FakeCreatePostPoint ($ post , $ this ->user ));
148
47
149
48
$ point = $ this ->user ->reputations ()->first ();
150
49
151
- expect ($ point ->payee )->toBeInstanceOf (User::class);
152
- expect ($ point ->subject )->toBeInstanceOf ($ post ::class);
153
- expect ($ point ->name )->toBe ('FakeCreatePostPoint ' );
50
+ expect ($ point ->payee )->toBeInstanceOf (User::class)
51
+ -> and ($ point ->subject )->toBeInstanceOf ($ post ::class)
52
+ -> and ($ point ->name )->toBe ('FakeCreatePostPoint ' );
154
53
});
155
54
156
55
it ('do not give point if qualifier returns false ' , function (): void {
157
56
$ post = $ this ->createPost (['user_id ' => $ this ->user ->id ]);
158
57
159
- $ this ->user ->givePoint (new FakeWelcomeUserWithFalseQualifier ($ post ->user ));
58
+ $ this ->user ->givePoint (new Fixtures \ FakeWelcomeUserWithFalseQualifier ($ post ->user ));
160
59
161
60
expect ($ this ->user ->fresh ()->getPoints ())->toBe (0 );
162
61
});
163
62
164
63
it ('throws exception if no payee is returned ' , function (): void {
165
- $ this ->user ->givePoint (new FakePointTypeWithoutPayee );
64
+ $ this ->user ->givePoint (new Fixtures \ FakePointTypeWithoutPayee );
166
65
167
66
expect ($ this ->user ->fresh ()->getPoints ())->toBe (0 );
168
67
})
169
68
->throws (InvalidPayeeModelException::class);
170
69
171
70
it ('throws exception if no subject is set ' , function (): void {
172
- $ this ->user ->givePoint (new FakePointTypeWithoutSubject );
71
+ $ this ->user ->givePoint (new Fixtures \ FakePointTypeWithoutSubject );
173
72
174
- expect ($ this ->user ->getPoints ())->toBe (0 );
175
- expect ($ this ->user ->reputations )->toHaveCount (0 );
73
+ expect ($ this ->user ->getPoints ())->toBe (0 )
74
+ -> and ($ this ->user ->reputations )->toHaveCount (0 );
176
75
})
177
76
->throws (PointSubjectNotSetException::class);
178
77
179
78
it ('throws exception if no points field or method is defined ' , function (): void {
180
79
$ post = $ this ->createPost (['user_id ' => $ this ->user ->id ]);
181
80
182
- $ this ->user ->givePoint (new FakePointWithoutPoint ($ post ));
81
+ $ this ->user ->givePoint (new Fixtures \ FakePointWithoutPoint ($ post ));
183
82
184
83
expect ($ this ->user ->getPoint ())->toBe (0 );
185
84
})
@@ -189,13 +88,13 @@ public function __construct(mixed $subject)
189
88
it ('gives and undo point via helper functions ' , function (): void {
190
89
$ post = $ this ->createPost (['user_id ' => $ this ->user ->id ]);
191
90
192
- givePoint (new FakePayeeFieldPoint ($ post ), $ this ->user );
91
+ givePoint (new Fixtures \ FakePayeeFieldPoint ($ post ), $ this ->user );
193
92
194
93
expect ($ this ->user ->fresh ()->getPoints ())->toBe (10 );
195
94
196
- undoPoint (new FakePayeeFieldPoint ($ post ), $ this ->user );
95
+ undoPoint (new Fixtures \ FakePayeeFieldPoint ($ post ), $ this ->user );
197
96
198
- $ user = $ this ->user ->fresh ();
97
+ $ this ->user ->fresh ();
199
98
expect ($ this ->user ->fresh ()->getPoints ())->toBe (0 );
200
99
});
201
100
});
0 commit comments