Skip to content

Commit

Permalink
Merge pull request #46 from cjmellor/44-bug
Browse files Browse the repository at this point in the history
Add Listener for PointsDecreased Event
  • Loading branch information
cjmellor authored Sep 23, 2023
2 parents 431c095 + d1bbb35 commit 5795a79
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 14 deletions.
13 changes: 4 additions & 9 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
name: "Run Tests"
on:
push:
branches:
- 'main'
pull_request:
branches:
- '*'

on: pull_request

jobs:
tests:
Expand All @@ -17,7 +12,7 @@ jobs:
laravel: [ 10 ]
stability: [ prefer-lowest, prefer-stable ]

name: "PHP: v${{ matrix.php }} / Laravel: v${{ matrix.laravel }} - ${{ matrix.stability }}"
name: "PHP: v${{ matrix.php }} [${{ matrix.stability }}]"

steps:
- name: Checkout
Expand All @@ -40,4 +35,4 @@ jobs:
composer update --${{ matrix.stability }} --prefer-dist --no-interaction
- name: Execute tests
run: vendor/bin/pest
run: vendor/bin/pest --parallel
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,8 @@ public Model $user,
```php
public int $pointsDecreasedBy,
public int $totalPoints,
public ?string $reason,
public Model $user,
```

## ⬆️ Levelling
Expand Down Expand Up @@ -511,6 +513,10 @@ $user->addPoints(
);
```

> **Note**
>
> Auditing happens when the `addPoints` and `deductPoints` methods are called. Auditing must be enabled in the config file.
**View a Users’ Audit Experience**

```php
Expand Down
11 changes: 8 additions & 3 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.1/phpunit.xsd" bootstrap="vendor/autoload.php" colors="true" cacheDirectory=".phpunit.cache">
<phpunit
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.1/phpunit.xsd"
bootstrap="vendor/autoload.php"
colors="true"
>
<testsuites>
<testsuite name="Test Suite">
<directory suffix="Test.php">./tests</directory>
<directory>./tests</directory>
</testsuite>
</testsuites>
<coverage/>
<source>
<include>
<directory suffix=".php">./src</directory>
<directory>./src</directory>
</include>
</source>
</phpunit>
13 changes: 11 additions & 2 deletions src/Concerns/GiveExperience.php
Original file line number Diff line number Diff line change
Expand Up @@ -134,11 +134,20 @@ public function experienceHistory(): HasMany
return $this->hasMany(related: ExperienceAudit::class);
}

public function deductPoints(int $amount): Experience
public function deductPoints(int $amount, string $reason = null): Experience
{
if ($this->experience()->doesntExist()) {
return $this->experience;
}

$this->experience->decrement(column: 'experience_points', amount: $amount);

event(new PointsDecreased(pointsDecreasedBy: $amount, totalPoints: $this->experience->experience_points));
event(new PointsDecreased(
pointsDecreasedBy: $amount,
totalPoints: $this->experience->experience_points,
reason: $reason,
user: $this,
));

return $this->experience;
}
Expand Down
3 changes: 3 additions & 0 deletions src/Events/PointsDecreased.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace LevelUp\Experience\Events;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Foundation\Events\Dispatchable;

class PointsDecreased
Expand All @@ -11,6 +12,8 @@ class PointsDecreased
public function __construct(
public int $pointsDecreasedBy,
public int $totalPoints,
public ?string $reason,
public Model $user,
) {
}
}
24 changes: 24 additions & 0 deletions src/Listeners/PointsDecreasedListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php

namespace LevelUp\Experience\Listeners;

use LevelUp\Experience\Enums\AuditType;
use LevelUp\Experience\Events\PointsDecreased;

class PointsDecreasedListener
{
public function __invoke(PointsDecreased $event): void
{
if ($event->pointsDecreasedBy === 0) {
return;
}

if (config(key: 'level-up.audit.enabled')) {
$event->user->experienceHistory()->create(attributes: [
'points' => $event->pointsDecreasedBy,
'type' => AuditType::Remove->value,
'reason' => $event->reason,
]);
}
}
}
5 changes: 5 additions & 0 deletions src/Providers/EventServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,19 @@
namespace LevelUp\Experience\Providers;

use Illuminate\Foundation\Support\Providers\EventServiceProvider as ServiceProvider;
use LevelUp\Experience\Events\PointsDecreased;
use LevelUp\Experience\Events\PointsIncreased;
use LevelUp\Experience\Events\UserLevelledUp;
use LevelUp\Experience\Listeners\PointsDecreasedListener;
use LevelUp\Experience\Listeners\PointsIncreasedListener;
use LevelUp\Experience\Listeners\UserLevelledUpListener;

class EventServiceProvider extends ServiceProvider
{
protected $listen = [
PointsDecreased::class => [
PointsDecreasedListener::class,
],
PointsIncreased::class => [
PointsIncreasedListener::class,
],
Expand Down
60 changes: 60 additions & 0 deletions tests/Listeners/PointsDecreasedListenerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
<?php

use Illuminate\Support\Facades\Event;
use LevelUp\Experience\Events\PointsDecreased;
use LevelUp\Experience\Listeners\PointsDecreasedListener;

uses()->group('experience');

beforeEach(closure: fn () => config()->set(key: 'level-up.multiplier.enabled', value: false));

test(description: 'the Event is dispatched when points are decreased', closure: function () {
Event::fakeFor(function () {
$this->user->addPoints(amount: 100);
$this->user->deductPoints(amount: 50, reason: 'test');

Event::assertDispatched(event: PointsDecreased::class, callback: function ($event): bool {
return $event->pointsDecreasedBy === 50
&& $event->reason === 'test';
});
Event::assertListening(expectedEvent: PointsDecreased::class, expectedListener: PointsDecreasedListener::class);
});
});

test(description: 'the Event Listener runs and logs the audit', closure: function () {
config()->set(key: 'level-up.audit.enabled', value: true);
config()->set(key: 'level-up.multiplier.enabled', value: false);

$this->user->addPoints(amount: 100);
$this->user->deductPoints(amount: 50, reason: 'test');

$this->assertDatabaseHas(table: 'experience_audits', data: [
'points' => 50,
'type' => 'remove',
'reason' => 'test',
]);
});

test(description: 'deducting points does not create an audit record when disabled', closure: function (): void {
config()->set(key: 'level-up.audit.enabled', value: false);

$this->user->addPoints(amount: 20);
$this->user->deductPoints(amount: 10);

expect($this->user)
->experienceHistory()
->count()
->toBe(expected: 0);
});

test(description: 'deducting points does not create an audit record when the amount is 0', closure: function (): void {
config()->set(key: 'level-up.audit.enabled', value: true);

$this->user->addPoints(amount: 20);
$this->user->deductPoints(amount: 0);

expect($this->user)
->experienceHistory()
->count()
->toBe(expected: 1);
});

0 comments on commit 5795a79

Please sign in to comment.