-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d380d74
commit a02fcb3
Showing
18 changed files
with
949 additions
and
22 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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 @@ | ||
[] |
Binary file not shown.
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,128 @@ | ||
<?php | ||
/* | ||
* This file is part of the Calculation package. | ||
* | ||
* (c) bibi.nu <bibi@bibi.nu> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Tests\Service; | ||
|
||
use App\Entity\User; | ||
use App\Mime\RegistrationEmail; | ||
use App\Repository\UserRepository; | ||
use App\Service\EmailVerifier; | ||
use App\Tests\Entity\IdTrait; | ||
use App\Tests\TranslatorMockTrait; | ||
use PHPUnit\Framework\Attributes\CoversClass; | ||
use PHPUnit\Framework\MockObject\Exception; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\HttpFoundation\Request; | ||
use Symfony\Component\HttpFoundation\UriSigner; | ||
use Symfony\Component\Mailer\Exception\TransportExceptionInterface; | ||
use Symfony\Component\Mailer\MailerInterface; | ||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; | ||
use SymfonyCasts\Bundle\VerifyEmail\Exception\VerifyEmailExceptionInterface; | ||
use SymfonyCasts\Bundle\VerifyEmail\Generator\VerifyEmailTokenGenerator; | ||
use SymfonyCasts\Bundle\VerifyEmail\Model\VerifyEmailSignatureComponents; | ||
use SymfonyCasts\Bundle\VerifyEmail\Util\VerifyEmailQueryUtility; | ||
use SymfonyCasts\Bundle\VerifyEmail\VerifyEmailHelper; | ||
use SymfonyCasts\Bundle\VerifyEmail\VerifyEmailHelperInterface; | ||
|
||
#[CoversClass(EmailVerifier::class)] | ||
class EmailVerifierTest extends TestCase | ||
{ | ||
use IdTrait; | ||
use TranslatorMockTrait; | ||
|
||
/** | ||
* @throws Exception | ||
* @throws VerifyEmailExceptionInterface | ||
* @throws \ReflectionException | ||
*/ | ||
public function testHandleEmail(): void | ||
{ | ||
$helper = $this->createVerifyEmailHelper(); | ||
$mailer = $this->createMock(MailerInterface::class); | ||
$repository = $this->createMock(UserRepository::class); | ||
$translator = $this->createMockTranslator(); | ||
$service = new EmailVerifier($helper, $mailer, $repository, $translator); | ||
|
||
$user = $this->createUser(); | ||
$request = new Request(['expires' => \time() + 3600]); | ||
$service->handleEmail($request, $user); | ||
self::assertTrue($user->isVerified()); | ||
} | ||
|
||
/** | ||
* @throws Exception | ||
* @throws TransportExceptionInterface | ||
* @throws \ReflectionException | ||
*/ | ||
public function testSendEmail(): void | ||
{ | ||
$helper = $this->createMockVerifyEmailHelper(); | ||
$mailer = $this->createMock(MailerInterface::class); | ||
$repository = $this->createMock(UserRepository::class); | ||
$translator = $this->createMockTranslator(); | ||
$service = new EmailVerifier($helper, $mailer, $repository, $translator); | ||
|
||
$user = $this->createUser(); | ||
$email = new RegistrationEmail(); | ||
$service->sendEmail('route', $user, $email); | ||
self::assertFalse($user->isVerified()); | ||
} | ||
|
||
/** | ||
* @throws Exception | ||
*/ | ||
private function createMockVerifyEmailHelper(): MockObject&VerifyEmailHelperInterface | ||
{ | ||
$date = new \DateTime(); | ||
$component = new VerifyEmailSignatureComponents($date, 'uri', $date->getTimestamp()); | ||
$helper = $this->createMock(VerifyEmailHelperInterface::class); | ||
$helper->method('generateSignature') | ||
->willReturn($component); | ||
|
||
return $helper; | ||
} | ||
|
||
/** | ||
* @throws \ReflectionException | ||
*/ | ||
private function createUser(): User | ||
{ | ||
$user = new User(); | ||
$user->setUsername('username') | ||
->setEmail('email@example.com'); | ||
|
||
return self::setId($user); | ||
} | ||
|
||
/** | ||
* @throws Exception | ||
*/ | ||
private function createVerifyEmailHelper(): VerifyEmailHelper | ||
{ | ||
$router = $this->createMock(UrlGeneratorInterface::class); | ||
$uriSigner = $this->createMock(UriSigner::class); | ||
$uriSigner->method('checkRequest') | ||
->willReturn(true); | ||
|
||
$queryUtility = $this->createMock(VerifyEmailQueryUtility::class); | ||
$generator = $this->createMock(VerifyEmailTokenGenerator::class); | ||
|
||
return new VerifyEmailHelper( | ||
$router, | ||
$uriSigner, | ||
$queryUtility, | ||
$generator, | ||
3600 | ||
); | ||
} | ||
} |
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,94 @@ | ||
<?php | ||
/* | ||
* This file is part of the Calculation package. | ||
* | ||
* (c) bibi.nu <bibi@bibi.nu> | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace App\Tests\Service; | ||
|
||
use App\Entity\User; | ||
use App\Enums\Importance; | ||
use App\Model\Comment; | ||
use App\Service\MailerService; | ||
use App\Tests\TranslatorMockTrait; | ||
use PHPUnit\Framework\Attributes\CoversClass; | ||
use PHPUnit\Framework\MockObject\Exception; | ||
use PHPUnit\Framework\TestCase; | ||
use Symfony\Component\HttpFoundation\File\UploadedFile; | ||
use Symfony\Component\Mailer\Exception\TransportExceptionInterface; | ||
use Symfony\Component\Mailer\MailerInterface; | ||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface; | ||
use Twig\Extra\Markdown\MarkdownInterface; | ||
|
||
#[CoversClass(MailerService::class)] | ||
class MailerServiceTest extends TestCase | ||
{ | ||
use TranslatorMockTrait; | ||
|
||
/** | ||
* @throws Exception | ||
* @throws TransportExceptionInterface | ||
*/ | ||
public function testSendComment(): void | ||
{ | ||
$comment = new Comment(); | ||
$comment->setFromAddress('from@example.com') | ||
->setToAddress('to@example.com') | ||
->setSubject('subject') | ||
->setMessage('message') | ||
->setAttachments([$this->createAttachement()]); | ||
|
||
$service = $this->createService(); | ||
$service->sendComment($comment); | ||
self::assertTrue(true); | ||
} | ||
|
||
/** | ||
* @throws Exception | ||
* @throws TransportExceptionInterface | ||
*/ | ||
public function testSendNotification(): void | ||
{ | ||
$toUser = new User(); | ||
$toUser->setUsername('username') | ||
->setEmail('to@example.com'); | ||
$service = $this->createService(); | ||
$service->sendNotification( | ||
'from@example.com', | ||
$toUser, | ||
'message', | ||
Importance::LOW, | ||
[$this->createAttachement()] | ||
); | ||
self::assertTrue(true); | ||
} | ||
|
||
private function createAttachement(): UploadedFile | ||
{ | ||
return new UploadedFile(__FILE__, \basename(__FILE__), test: true); | ||
} | ||
|
||
/** | ||
* @throws Exception | ||
*/ | ||
private function createService(): MailerService | ||
{ | ||
$generator = $this->createMock(UrlGeneratorInterface::class); | ||
$markdown = $this->createMock(MarkdownInterface::class); | ||
$mailer = $this->createMock(MailerInterface::class); | ||
$translator = $this->createMockTranslator(); | ||
|
||
return new MailerService( | ||
$generator, | ||
$markdown, | ||
$mailer, | ||
$translator | ||
); | ||
} | ||
} |
Oops, something went wrong.