Skip to content

Commit 71075f2

Browse files
committed
feat: add test
1 parent 5a9413a commit 71075f2

File tree

11 files changed

+2877
-50
lines changed

11 files changed

+2877
-50
lines changed

app-modules/gamify/composer.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,15 @@
22
"name": "laravelcm/gamify",
33
"description": "Module laravelcm/gamify for Laravel.cm",
44
"type": "library",
5-
"version": "1.0",
5+
"version": "1.1.0",
66
"license": "proprietary",
77
"require": {
88
"filament/filament": "^3.2"
99
},
10+
"require-dev": {
11+
"pestphp/pest": "^2.32",
12+
"pestphp/pest-plugin-laravel": "^2.1"
13+
},
1014
"autoload": {
1115
"psr-4": {
1216
"Laravelcm\\Gamify\\": "src/",

app-modules/gamify/src/Console/stubs/point.stub

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ declare(strict_types=1);
55
namespace DummyNamespace;
66

77
use App\Models\User;
8-
use Illuminate\Database\Eloquent\Model;
98
use Laravelcm\Gamify\PointType;
109
use Laravelcm\Gamify\Exceptions\PointSubjectNotSetException;
1110

@@ -34,7 +33,7 @@ final class DummyClass extends PointType
3433
* @return mixed
3534
* @throws PointSubjectNotSetException
3635
*/
37-
public function payee(): Model
36+
public function payee(): ?User
3837
{
3938
return $this->getSubject()->user;
4039
}

app-modules/gamify/src/PointType.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,13 @@
44

55
namespace Laravelcm\Gamify;
66

7+
use App\Models\User;
78
use Illuminate\Database\Eloquent\Model;
9+
use Laravelcm\Gamify\Models\Reputation;
810
use Illuminate\Database\Eloquent\Relations\HasMany;
9-
use Laravelcm\Gamify\Exceptions\InvalidPayeeModelException;
1011
use Laravelcm\Gamify\Exceptions\PointsNotDefinedException;
12+
use Laravelcm\Gamify\Exceptions\InvalidPayeeModelException;
1113
use Laravelcm\Gamify\Exceptions\PointSubjectNotSetException;
12-
use Laravelcm\Gamify\Models\Reputation;
1314

1415
abstract class PointType
1516
{
@@ -33,7 +34,7 @@ public function qualifier(): bool
3334
* @throws PointSubjectNotSetException
3435
* @throws InvalidPayeeModelException
3536
*/
36-
public function payee(): Model
37+
public function payee(): User
3738
{
3839
if (property_exists($this, 'payee')) {
3940
return $this->getSubject()->{$this->payee};
@@ -108,7 +109,7 @@ public function reputationExists(): bool
108109
* @throws InvalidPayeeModelException
109110
* @throws PointSubjectNotSetException
110111
*/
111-
public function firstReputation(): Model
112+
public function firstReputation(): User
112113
{
113114
return $this->reputationQuery()->first();
114115
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
use App\Models\User;
6+
use Laravelcm\Gamify\Models\Reputation;
7+
use Illuminate\Database\Eloquent\Relations\MorphTo;
8+
9+
describe(Reputation::class, function () {
10+
11+
it('gets user points', function () {
12+
$user = createUser(['reputation' => 10]);
13+
14+
expect($user->getPoints())->toBe(10);
15+
});
16+
17+
it('gives reputation point to a user', function () {
18+
expect($user->getPoints())->toBe(0);
19+
20+
$user->addPoint(10);
21+
22+
expect($user->fresh()->getPoints())->toBe(10);
23+
});
24+
25+
it('reduces reputation point for a user', function () {
26+
$user = createUser(['reputation' => 20]);
27+
expect($user->reputation)->toBe(20);
28+
29+
$user->reducePoint(5);
30+
31+
expect($user->fresh()->getPoints())->toBe(15);
32+
});
33+
34+
it('zeros reputation point of a user', function () {
35+
$user = createUser(['reputation' => 50]);
36+
expect($user->getPoints())->toBe(50);
37+
38+
$user->resetPoint();
39+
40+
expect($user->fresh()->getPoints())->toBe(0);
41+
});
42+
43+
});

app-modules/gamify/tests/Pest.php

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+
use App\Models\User;
6+
use Illuminate\Foundation\Testing\RefreshDatabase;
7+
use Tests\TestCase;
8+
9+
uses(TestCase::class, RefreshDatabase::class)>in('Feature');
10+
11+
function createUser(array $attributes = []): User
12+
{
13+
$user = new User();
14+
15+
$user->forceFill(array_merge($attributes, [
16+
'name' => 'Demo',
17+
'email' => '[email protected]',
18+
'password' => 'password',
19+
]))->save();
20+
21+
return $user->fresh();
22+
}

app-modules/gamify/tests/TestCase.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Tests;
6+
7+
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;
8+
use Illuminate\Support\Facades\App;
9+
10+
abstract class TestCase extends BaseTestCase
11+
{
12+
protected function setUp(): void
13+
{
14+
parent::setUp();
15+
}
16+
}

app/Gamify/Points/ReplyCreated.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ public function __construct(mixed $subject, ?User $author = null)
1919
$this->author = $author;
2020
}
2121

22-
public function payee(): \Illuminate\Database\Eloquent\Model
22+
public function payee(): ?User
2323
{
2424
return $this->author;
2525
}

app/Gamify/Points/Test.php

Lines changed: 0 additions & 38 deletions
This file was deleted.

composer.lock

Lines changed: 3 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

phpunit.xml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
<server name="APP_TIMEZONE" value="UTC"/>
1313
<server name="BCRYPT_ROUNDS" value="4"/>
1414
<server name="CACHE_DRIVER" value="array"/>
15-
<server name="DB_CONNECTION" value="sqlite"/>
1615
<server name="DB_DATABASE" value=":memory:"/>
1716
<server name="MAIL_MAILER" value="array"/>
1817
<server name="QUEUE_CONNECTION" value="sync"/>

0 commit comments

Comments
 (0)