Skip to content

Commit dda11a7

Browse files
committed
wip
1 parent 55e11bc commit dda11a7

8 files changed

+174
-122
lines changed

app-modules/gamify/tests/Feature/PointTest.php

Lines changed: 21 additions & 122 deletions
Original file line numberDiff line numberDiff line change
@@ -3,183 +3,82 @@
33
declare(strict_types=1);
44

55
use App\Models\User;
6-
use Illuminate\Database\Eloquent\Model;
76
use Laravelcm\Gamify\Exceptions\InvalidPayeeModelException;
87
use Laravelcm\Gamify\Exceptions\PointsNotDefinedException;
98
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;
11110

11211
beforeEach(function (): void {
11312
$this->user = $this->createUser();
11413
});
11514

116-
describe(PointType::class, function (): void {
15+
describe('PointType', function (): void {
11716
it('sets point type name from class name', function (): void {
118-
$point = new FakeCreatePostPoint($this->user);
17+
$point = new Fixtures\FakeCreatePostPoint($this->user);
11918

12019
expect($point->getName())->toBe('FakeCreatePostPoint');
12120
});
12221

12322
it('uses name property for point name if provided', function (): void {
124-
$point = new FakeWelcomeUserWithNamePoint($this->user);
23+
$point = new Fixtures\FakeWelcomeUserWithNamePoint($this->user);
12524

12625
expect($point->getName())->toBe('FakeName');
12726
});
12827

12928
it('can get points for a point type', function (): void {
130-
$point = new FakeCreatePostPoint($this->user);
29+
$point = new Fixtures\FakeCreatePostPoint($this->user);
13130

13231
expect($point->getPoints())->toBe(10);
13332
});
13433

13534
it('gives point to a user', function (): void {
13635
$post = $this->createPost(['user_id' => $this->user->id]);
13736

138-
$this->user->givePoint(new FakeCreatePostPoint($post, $this->user));
37+
$this->user->givePoint(new Fixtures\FakeCreatePostPoint($post, $this->user));
13938

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);
14241
});
14342

14443
it('can access a reputation payee and subject', function (): void {
14544
$post = $this->createPost(['user_id' => $this->user->id]);
14645

147-
$this->user->givePoint(new FakeCreatePostPoint($post, $this->user));
46+
$this->user->givePoint(new Fixtures\FakeCreatePostPoint($post, $this->user));
14847

14948
$point = $this->user->reputations()->first();
15049

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');
15453
});
15554

15655
it('do not give point if qualifier returns false', function (): void {
15756
$post = $this->createPost(['user_id' => $this->user->id]);
15857

159-
$this->user->givePoint(new FakeWelcomeUserWithFalseQualifier($post->user));
58+
$this->user->givePoint(new Fixtures\FakeWelcomeUserWithFalseQualifier($post->user));
16059

16160
expect($this->user->fresh()->getPoints())->toBe(0);
16261
});
16362

16463
it('throws exception if no payee is returned', function (): void {
165-
$this->user->givePoint(new FakePointTypeWithoutPayee);
64+
$this->user->givePoint(new Fixtures\FakePointTypeWithoutPayee);
16665

16766
expect($this->user->fresh()->getPoints())->toBe(0);
16867
})
16968
->throws(InvalidPayeeModelException::class);
17069

17170
it('throws exception if no subject is set', function (): void {
172-
$this->user->givePoint(new FakePointTypeWithoutSubject);
71+
$this->user->givePoint(new Fixtures\FakePointTypeWithoutSubject);
17372

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);
17675
})
17776
->throws(PointSubjectNotSetException::class);
17877

17978
it('throws exception if no points field or method is defined', function (): void {
18079
$post = $this->createPost(['user_id' => $this->user->id]);
18180

182-
$this->user->givePoint(new FakePointWithoutPoint($post));
81+
$this->user->givePoint(new Fixtures\FakePointWithoutPoint($post));
18382

18483
expect($this->user->getPoint())->toBe(0);
18584
})
@@ -189,13 +88,13 @@ public function __construct(mixed $subject)
18988
it('gives and undo point via helper functions', function (): void {
19089
$post = $this->createPost(['user_id' => $this->user->id]);
19190

192-
givePoint(new FakePayeeFieldPoint($post), $this->user);
91+
givePoint(new Fixtures\FakePayeeFieldPoint($post), $this->user);
19392

19493
expect($this->user->fresh()->getPoints())->toBe(10);
19594

196-
undoPoint(new FakePayeeFieldPoint($post), $this->user);
95+
undoPoint(new Fixtures\FakePayeeFieldPoint($post), $this->user);
19796

198-
$user = $this->user->fresh();
97+
$this->user->fresh();
19998
expect($this->user->fresh()->getPoints())->toBe(0);
20099
});
201100
});
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Laravelcm\Gamify\Tests\Fixtures;
6+
7+
use App\Models\User;
8+
use Laravelcm\Gamify\PointType;
9+
10+
final class FakeCreatePostPoint extends PointType
11+
{
12+
public int $points = 10;
13+
14+
public ?User $author;
15+
16+
public function __construct(mixed $subject, ?User $author = null)
17+
{
18+
$this->subject = $subject;
19+
$this->author = $author;
20+
}
21+
22+
public function payee(): ?User
23+
{
24+
return $this->author;
25+
}
26+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Laravelcm\Gamify\Tests\Fixtures;
6+
7+
use Laravelcm\Gamify\PointType;
8+
9+
final class FakePayeeFieldPoint extends PointType
10+
{
11+
protected int $points = 10;
12+
13+
/** @var string payee model relation with subject */
14+
protected string $payee = 'user';
15+
16+
public function __construct(mixed $subject)
17+
{
18+
$this->subject = $subject;
19+
}
20+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Laravelcm\Gamify\Tests\Fixtures;
6+
7+
use Laravelcm\Gamify\PointType;
8+
9+
final class FakePointTypeWithoutPayee extends PointType
10+
{
11+
protected int $point = 24;
12+
13+
public function __construct(mixed $subject = null)
14+
{
15+
$this->subject = $subject;
16+
}
17+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Laravelcm\Gamify\Tests\Fixtures;
6+
7+
use App\Models\User;
8+
use Laravelcm\Gamify\PointType;
9+
10+
final class FakePointTypeWithoutSubject extends PointType
11+
{
12+
protected int $point = 12;
13+
14+
public function __construct($subject = null)
15+
{
16+
$this->subject = $subject;
17+
}
18+
19+
public function payee(): User
20+
{
21+
return new User;
22+
}
23+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Laravelcm\Gamify\Tests\Fixtures;
6+
7+
use Laravelcm\Gamify\PointType;
8+
9+
final class FakePointWithoutPoint extends PointType
10+
{
11+
protected string $payee = 'user';
12+
13+
public function __construct($subject = null)
14+
{
15+
$this->subject = $subject;
16+
}
17+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Laravelcm\Gamify\Tests\Fixtures;
6+
7+
use App\Models\User;
8+
use Laravelcm\Gamify\PointType;
9+
10+
final class FakeWelcomeUserWithFalseQualifier extends PointType
11+
{
12+
protected int $points = 10;
13+
14+
public function __construct($subject)
15+
{
16+
$this->subject = $subject;
17+
}
18+
19+
public function qualifier(): bool
20+
{
21+
return false;
22+
}
23+
24+
public function payee(): ?User
25+
{
26+
return $this->getSubject()->user;
27+
}
28+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Laravelcm\Gamify\Tests\Fixtures;
6+
7+
use App\Models\User;
8+
use Laravelcm\Gamify\PointType;
9+
10+
final class FakeWelcomeUserWithNamePoint extends PointType
11+
{
12+
public int $points = 30;
13+
14+
public string $name = 'FakeName';
15+
16+
public function __construct(mixed $subject, public ?User $author = null) {}
17+
18+
public function payee(): ?User
19+
{
20+
return $this->author;
21+
}
22+
}

0 commit comments

Comments
 (0)