-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' of github-yena:systemli/userli
- Loading branch information
Showing
13 changed files
with
284 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
features/voucherCreationCommand.feature → features/voucherCreateCommand.feature
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
Feature: VoucherCreationCommand | ||
Feature: VoucherCreateCommand | ||
|
||
Background: | ||
Given the database is clean | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<?php | ||
|
||
namespace App\Validator\Constraints; | ||
|
||
use Symfony\Component\Validator\Constraint; | ||
|
||
class VoucherUser extends Constraint | ||
{ | ||
public function getTargets(): string | ||
{ | ||
return self::CLASS_CONSTRAINT; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace App\Validator\Constraints; | ||
|
||
use App\Entity\User; | ||
use App\Entity\Voucher; | ||
use App\Enum\Roles; | ||
use Symfony\Component\Validator\Constraint; | ||
use Symfony\Component\Validator\ConstraintValidator; | ||
use Symfony\Component\Validator\Exception\UnexpectedTypeException; | ||
|
||
class VoucherUserValidator extends ConstraintValidator | ||
{ | ||
/** | ||
* Checks if the passed value is valid. | ||
* | ||
* @param mixed $value The value that should be validated | ||
* @param Constraint $constraint The constraint for the validation | ||
*/ | ||
public function validate(mixed $value, Constraint $constraint): void | ||
{ | ||
if (!$constraint instanceof VoucherUser) { | ||
throw new UnexpectedTypeException($constraint, VoucherUser::class); | ||
} | ||
|
||
if (!$value instanceof Voucher) { | ||
return; | ||
} | ||
|
||
/** @var User $user */ | ||
$user = $value->getUser(); | ||
if ($user->hasRole(Roles::SUSPICIOUS)) { | ||
$this->context->addViolation('voucher.suspicious-user'); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,135 @@ | ||
<?php | ||
|
||
namespace App\Tests\Command; | ||
|
||
use App\Command\VoucherCreateCommand; | ||
use App\Creator\VoucherCreator; | ||
use App\Entity\User; | ||
use App\Entity\Voucher; | ||
use App\Exception\ValidationException; | ||
use App\Repository\UserRepository; | ||
use Doctrine\ORM\EntityManagerInterface; | ||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\Console\Application; | ||
use Symfony\Component\Console\Tester\CommandTester; | ||
use Symfony\Component\Routing\RouterInterface; | ||
use Symfony\Component\Security\Core\Exception\UserNotFoundException; | ||
|
||
class VoucherCreateCommandTest extends TestCase | ||
{ | ||
private VoucherCreateCommand $command; | ||
private UserRepository $repository; | ||
private RouterInterface $router; | ||
private VoucherCreator $creator; | ||
private string $baseUrl = 'https://users.example.org/register'; | ||
private string $voucherCode = 'code'; | ||
|
||
public function setUp(): void | ||
{ | ||
$manager = $this->createMock(EntityManagerInterface::class); | ||
$this->repository = $this->createMock(UserRepository::class); | ||
$manager->method('getRepository')->willReturn($this->repository); | ||
|
||
$this->router = $this->createMock(RouterInterface::class); | ||
|
||
$this->creator = $this->createMock(VoucherCreator::class); | ||
|
||
$this->command = new VoucherCreateCommand($manager, $this->router, $this->creator, $this->baseUrl); | ||
} | ||
|
||
public function testExecuteWithUnknownUser(): void | ||
{ | ||
$this->repository->method('findByEmail') | ||
->willReturn(null); | ||
|
||
$application = new Application(); | ||
$application->add($this->command); | ||
|
||
$command = $application->find('app:voucher:create'); | ||
$commandTester = new CommandTester($command); | ||
|
||
$this->expectException(UserNotFoundException::class); | ||
$commandTester->execute([ | ||
'--user' => '[email protected]', | ||
'--count' => 1, | ||
'--print' | ||
]); | ||
|
||
$output = $commandTester->getDisplay(); | ||
self::assertStringContainsString('', $output); | ||
} | ||
|
||
public function testExecuteWithUser(): void | ||
{ | ||
$user = new User(); | ||
$user->setEmail('[email protected]'); | ||
$this->repository->method('findByEmail') | ||
->willReturn($user); | ||
|
||
$this->router->method('generate') | ||
->willReturn($this->baseUrl . '/' . $this->voucherCode); | ||
|
||
$voucher = new Voucher(); | ||
$voucher->setCode($this->voucherCode); | ||
$this->creator->method('create') | ||
->willReturn($voucher); | ||
|
||
$application = new Application(); | ||
$application->add($this->command); | ||
|
||
$command = $application->find('app:voucher:create'); | ||
$commandTester = new CommandTester($command); | ||
|
||
// Test show vouchers | ||
|
||
$commandTester->execute([ | ||
'--user' => $user->getEmail(), | ||
'--count' => 1, | ||
'--print' => true, | ||
]); | ||
|
||
$commandTester->assertCommandIsSuccessful(); | ||
|
||
$output = $commandTester->getDisplay(); | ||
$this->assertStringContainsString($this->voucherCode, $output); | ||
|
||
// Test show links to vouchers | ||
|
||
$commandTester->execute([ | ||
'--user' => $user->getEmail(), | ||
'--count' => 1, | ||
'--print-links' => true, | ||
]); | ||
|
||
$commandTester->assertCommandIsSuccessful(); | ||
|
||
$output = $commandTester->getDisplay(); | ||
$this->assertStringContainsString($this->baseUrl . '/' . $this->voucherCode, $output); | ||
} | ||
|
||
public function testExecuteWithSuspiciousUser(): void | ||
{ | ||
$user = new User(); | ||
$user->setEmail('[email protected]'); | ||
$this->repository->method('findByEmail') | ||
->willReturn($user); | ||
|
||
$voucher = new Voucher(); | ||
$voucher->setCode($this->voucherCode); | ||
$exception = $this->createMock(ValidationException::class); | ||
$this->creator->method('create') | ||
->willThrowException($exception); | ||
|
||
$application = new Application(); | ||
$application->add($this->command); | ||
|
||
$command = $application->find('app:voucher:create'); | ||
$commandTester = new CommandTester($command); | ||
|
||
$this->expectException(ValidationException::class); | ||
$commandTester->execute([ | ||
'--user' => $user->getEmail(), | ||
'--count' => 1, | ||
]); | ||
} | ||
} |
Oops, something went wrong.