2026-03-26 09:56:11 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace App\Tests\Command;
|
|
|
|
|
|
|
|
|
|
use App\Command\StripeSyncCommand;
|
|
|
|
|
use App\Entity\User;
|
|
|
|
|
use App\Service\StripeService;
|
|
|
|
|
use Doctrine\ORM\EntityManagerInterface;
|
|
|
|
|
use Doctrine\ORM\EntityRepository;
|
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
|
use Symfony\Component\Console\Application;
|
|
|
|
|
use Symfony\Component\Console\Tester\CommandTester;
|
|
|
|
|
|
|
|
|
|
class StripeSyncCommandTest extends TestCase
|
|
|
|
|
{
|
2026-04-01 14:14:29 +02:00
|
|
|
private StripeService $stripeService;
|
|
|
|
|
private EntityManagerInterface $em;
|
|
|
|
|
private EntityRepository $userRepo;
|
|
|
|
|
|
|
|
|
|
protected function setUp(): void
|
|
|
|
|
{
|
|
|
|
|
$this->stripeService = $this->createMock(StripeService::class);
|
|
|
|
|
|
|
|
|
|
$this->userRepo = $this->createMock(EntityRepository::class);
|
|
|
|
|
|
|
|
|
|
$this->em = $this->createMock(EntityManagerInterface::class);
|
|
|
|
|
$this->em->method('getRepository')->willReturnCallback(function (string $class) {
|
|
|
|
|
return match ($class) {
|
|
|
|
|
User::class => $this->userRepo,
|
|
|
|
|
default => $this->createMock(EntityRepository::class),
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-26 09:56:11 +01:00
|
|
|
private function createOrganizer(string $stripeId, bool $charges = false, bool $payouts = false): User
|
|
|
|
|
{
|
|
|
|
|
$user = new User();
|
|
|
|
|
$user->setEmail('orga-'.uniqid().'@test.fr');
|
|
|
|
|
$user->setFirstName('Test');
|
|
|
|
|
$user->setLastName('Orga');
|
|
|
|
|
$user->setPassword('hashed');
|
|
|
|
|
$user->setRoles(['ROLE_ORGANIZER']);
|
|
|
|
|
$user->setStripeAccountId($stripeId);
|
|
|
|
|
$user->setStripeChargesEnabled($charges);
|
|
|
|
|
$user->setStripePayoutsEnabled($payouts);
|
|
|
|
|
$user->setCompanyName('Asso Test');
|
|
|
|
|
|
|
|
|
|
return $user;
|
|
|
|
|
}
|
|
|
|
|
|
2026-04-01 14:14:29 +02:00
|
|
|
private function createCommandTester(): CommandTester
|
|
|
|
|
{
|
|
|
|
|
$command = new StripeSyncCommand(
|
|
|
|
|
$this->em,
|
|
|
|
|
$this->stripeService,
|
|
|
|
|
);
|
2026-03-26 09:56:11 +01:00
|
|
|
|
|
|
|
|
$app = new Application();
|
|
|
|
|
$app->addCommand($command);
|
|
|
|
|
|
|
|
|
|
return new CommandTester($app->find('app:stripe:sync'));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testSyncUpdatesStripeStatus(): void
|
|
|
|
|
{
|
|
|
|
|
$user = $this->createOrganizer('acct_123');
|
2026-04-01 14:14:29 +02:00
|
|
|
$this->userRepo->method('findAll')->willReturn([$user]);
|
2026-03-26 09:56:11 +01:00
|
|
|
|
2026-04-01 14:14:29 +02:00
|
|
|
$this->stripeService->method('retrieveAccountStatus')
|
2026-03-26 09:56:11 +01:00
|
|
|
->with('acct_123')
|
|
|
|
|
->willReturn(['charges_enabled' => true, 'payouts_enabled' => true]);
|
|
|
|
|
|
2026-04-01 14:14:29 +02:00
|
|
|
$tester = $this->createCommandTester();
|
2026-03-26 09:56:11 +01:00
|
|
|
$tester->execute([]);
|
|
|
|
|
|
|
|
|
|
self::assertTrue($user->isStripeChargesEnabled());
|
|
|
|
|
self::assertTrue($user->isStripePayoutsEnabled());
|
|
|
|
|
self::assertStringContainsString('1 synced', $tester->getDisplay());
|
|
|
|
|
self::assertSame(0, $tester->getStatusCode());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testSyncDetectsChanges(): void
|
|
|
|
|
{
|
|
|
|
|
$user = $this->createOrganizer('acct_456', true, true);
|
2026-04-01 14:14:29 +02:00
|
|
|
$this->userRepo->method('findAll')->willReturn([$user]);
|
2026-03-26 09:56:11 +01:00
|
|
|
|
2026-04-01 14:14:29 +02:00
|
|
|
$this->stripeService->method('retrieveAccountStatus')
|
2026-03-26 09:56:11 +01:00
|
|
|
->willReturn(['charges_enabled' => true, 'payouts_enabled' => false]);
|
|
|
|
|
|
2026-04-01 14:14:29 +02:00
|
|
|
$tester = $this->createCommandTester();
|
2026-03-26 09:56:11 +01:00
|
|
|
$tester->execute([]);
|
|
|
|
|
|
|
|
|
|
self::assertTrue($user->isStripeChargesEnabled());
|
|
|
|
|
self::assertFalse($user->isStripePayoutsEnabled());
|
|
|
|
|
self::assertStringContainsString('UPDATED', $tester->getDisplay());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testSyncWithNoOrganizers(): void
|
|
|
|
|
{
|
2026-04-01 14:14:29 +02:00
|
|
|
$this->userRepo->method('findAll')->willReturn([]);
|
2026-03-26 09:56:11 +01:00
|
|
|
|
2026-04-01 14:14:29 +02:00
|
|
|
$tester = $this->createCommandTester();
|
2026-03-26 09:56:11 +01:00
|
|
|
$tester->execute([]);
|
|
|
|
|
|
|
|
|
|
self::assertStringContainsString('No organizers', $tester->getDisplay());
|
|
|
|
|
self::assertSame(0, $tester->getStatusCode());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testSyncHandlesStripeError(): void
|
|
|
|
|
{
|
|
|
|
|
$user = $this->createOrganizer('acct_bad');
|
2026-04-01 14:14:29 +02:00
|
|
|
$this->userRepo->method('findAll')->willReturn([$user]);
|
2026-03-26 09:56:11 +01:00
|
|
|
|
2026-04-01 14:14:29 +02:00
|
|
|
$this->stripeService->method('retrieveAccountStatus')
|
2026-03-26 09:56:11 +01:00
|
|
|
->willThrowException(new \RuntimeException('Account not found'));
|
|
|
|
|
|
2026-04-01 14:14:29 +02:00
|
|
|
$tester = $this->createCommandTester();
|
2026-03-26 09:56:11 +01:00
|
|
|
$tester->execute([]);
|
|
|
|
|
|
|
|
|
|
self::assertStringContainsString('1 error', $tester->getDisplay());
|
|
|
|
|
self::assertSame(1, $tester->getStatusCode());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public function testSyncSkipsOrganizersWithoutStripeAccount(): void
|
|
|
|
|
{
|
|
|
|
|
$userWithStripe = $this->createOrganizer('acct_ok');
|
|
|
|
|
|
|
|
|
|
$userWithoutStripe = new User();
|
|
|
|
|
$userWithoutStripe->setEmail('no-stripe@test.fr');
|
|
|
|
|
$userWithoutStripe->setFirstName('No');
|
|
|
|
|
$userWithoutStripe->setLastName('Stripe');
|
|
|
|
|
$userWithoutStripe->setPassword('hashed');
|
|
|
|
|
$userWithoutStripe->setRoles(['ROLE_ORGANIZER']);
|
|
|
|
|
|
2026-04-01 14:14:29 +02:00
|
|
|
$this->userRepo->method('findAll')->willReturn([$userWithStripe, $userWithoutStripe]);
|
|
|
|
|
|
|
|
|
|
$this->stripeService->method('retrieveAccountStatus')
|
2026-03-26 09:56:11 +01:00
|
|
|
->willReturn(['charges_enabled' => true, 'payouts_enabled' => false]);
|
|
|
|
|
|
2026-04-01 14:14:29 +02:00
|
|
|
$tester = $this->createCommandTester();
|
2026-03-26 09:56:11 +01:00
|
|
|
$tester->execute([]);
|
|
|
|
|
|
|
|
|
|
self::assertStringContainsString('Syncing 1 organizer', $tester->getDisplay());
|
|
|
|
|
self::assertStringContainsString('1 synced', $tester->getDisplay());
|
|
|
|
|
self::assertTrue($userWithStripe->isStripeChargesEnabled());
|
|
|
|
|
self::assertFalse($userWithStripe->isStripePayoutsEnabled());
|
|
|
|
|
}
|
|
|
|
|
}
|