diff --git a/src/Facades/InviteCodes.php b/src/Facades/InviteCodes.php index 4ea1338..fe06877 100644 --- a/src/Facades/InviteCodes.php +++ b/src/Facades/InviteCodes.php @@ -19,6 +19,8 @@ * @method static $this expiresIn(int $days) Set the invite code expiration date to $days from now. * @method static Invite save() Save the invite code. * @method static Collection make(int $quantity) Save $quantity invite codes. + * @method static void macro($name, $macro) + * @method static bool hasMacro($name) * @method static $this canBeUsedOnce() */ class InviteCodes extends Facade diff --git a/src/Factory.php b/src/Factory.php index 3dfb6ad..1968213 100644 --- a/src/Factory.php +++ b/src/Factory.php @@ -7,6 +7,8 @@ use Illuminate\Support\Collection; use Illuminate\Support\Facades\Auth; use Illuminate\Support\Str; +use Illuminate\Support\Traits\Conditionable; +use Illuminate\Support\Traits\Macroable; use Junges\InviteCodes\Contracts\InviteCodesFactory; use Junges\InviteCodes\Contracts\InviteContract; use Junges\InviteCodes\Events\InviteRedeemedEvent; @@ -23,6 +25,9 @@ class Factory implements InviteCodesFactory { + use Macroable; + use Conditionable; + protected int $max_usages; protected ?string $to = null; protected ?CarbonInterface $expires_at; diff --git a/tests/InviteCodesTest.php b/tests/FactoryTest.php similarity index 62% rename from tests/InviteCodesTest.php rename to tests/FactoryTest.php index e0999d0..22722f0 100644 --- a/tests/InviteCodesTest.php +++ b/tests/FactoryTest.php @@ -3,10 +3,11 @@ namespace Junges\InviteCodes\Tests; use Illuminate\Support\Facades\Event; +use Junges\InviteCodes\Contracts\InviteContract; use Junges\InviteCodes\Events\InviteRedeemedEvent; use Junges\InviteCodes\Facades\InviteCodes; -class InviteCodesTest extends TestCase +class FactoryTest extends TestCase { public function test_an_event_is_dispatched_when_invite_has_been_redeemed() { @@ -29,4 +30,16 @@ public function test_a_user_can_redeem_invite_codes_without_dispatching_events() Event::assertNotDispatched(InviteRedeemedEvent::class); } + + public function test_macro(): void + { + InviteCodes::macro('restrictedTo', function (string $email) { + return $this->restrictUsageTo($email)->save(); + }); + + $invite = InviteCodes::restrictedTo('test@example.com'); + + $this->assertInstanceOf(InviteContract::class, $invite); + $this->assertTrue($invite->usageRestrictedToEmail('test@example.com')); + } }