Skip to content

Commit

Permalink
Make factory macroable
Browse files Browse the repository at this point in the history
  • Loading branch information
mateusjunges committed Jan 28, 2024
1 parent 24a5784 commit e3341f5
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/Facades/InviteCodes.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 5 additions & 0 deletions src/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -23,6 +25,9 @@

class Factory implements InviteCodesFactory
{
use Macroable;
use Conditionable;

protected int $max_usages;
protected ?string $to = null;
protected ?CarbonInterface $expires_at;
Expand Down
15 changes: 14 additions & 1 deletion tests/InviteCodesTest.php → tests/FactoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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()
{
Expand All @@ -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'));
}
}

0 comments on commit e3341f5

Please # to comment.