Skip to content

Commit

Permalink
Fixed phpstan issues and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jbtronics committed Jan 21, 2025
1 parent 59d152a commit 19c8852
Show file tree
Hide file tree
Showing 11 changed files with 77 additions and 106 deletions.
7 changes: 5 additions & 2 deletions src/Audit/UserProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@
use Doctrine\ORM\Event\PostFlushEventArgs;
use Doctrine\ORM\Events;
use Exception;
use Symfony\Bundle\SecurityBundle\Security;
use Symfony\Component\Security\Core\Authentication\Token\SwitchUserToken;
use Symfony\Component\Security\Core\Security;
use Symfony\Component\Security\Core\User\UserInterface;

class UserProvider implements UserProviderInterface, EventSubscriber
Expand All @@ -39,7 +39,10 @@ class UserProvider implements UserProviderInterface, EventSubscriber

private ?string $identifier = null;

public function __construct(private readonly Security $security, private readonly Configuration $configuration)
public function __construct(
private readonly Security $security,
private readonly Configuration $configuration //@phpstan-ignore-line

Check failure on line 44 in src/Audit/UserProvider.php

View workflow job for this annotation

GitHub Actions / Static analysis

No error to ignore is reported on line 44.
)
{
}

Expand Down
2 changes: 1 addition & 1 deletion src/Controller/Admin/PaymentOrderCrudController.php
Original file line number Diff line number Diff line change
Expand Up @@ -274,7 +274,7 @@ public function configureActions(Actions $actions): Actions
->setCssClass('btn btn-secondary');

//Hide action if no contact emails are associated with department
$emailAction->displayIf(fn(PaymentOrder $paymentOrder): bool => null !== $this->mailToGenerator->generateContactMailLink($paymentOrder));
//$emailAction->displayIf(fn(PaymentOrder $paymentOrder): bool => null !== $this->mailToGenerator->generateContactMailLink($paymentOrder));

$resend_confirmation_action = Action::new('resendConfirmation', 'payment_order.action.resend_confirmation', 'fas fa-redo')
->linkToCrudAction('resendConfirmationEmail')
Expand Down
6 changes: 4 additions & 2 deletions src/Services/PaymentOrdersSEPAExporter.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,16 @@
* This service allows to create a SEPA-XML file from a payment order that can be used to import it in an online
* banking system.
* @see \App\Tests\Services\PaymentOrdersSEPAExporterTest
*
* We cant make this class readonly, as this breaks our test
*/
readonly class PaymentOrdersSEPAExporter
class PaymentOrdersSEPAExporter
{
protected const PARTY_NAME = 'StuRa FSU Jena';
protected const ID_PREFIX = 'StuRa Export';
protected const PAYMENT_PREFIX = 'Payment';

public function __construct(private int $fsr_kom_bank_account_id, private EntityManagerInterface $entityManager)
public function __construct(private readonly int $fsr_kom_bank_account_id, private readonly EntityManagerInterface $entityManager)
{
}

Expand Down
4 changes: 2 additions & 2 deletions tests/Controller/ExportControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function testExportAutoSingleMode(): void
LoginHelper::loginAsAdmin($client);
$client->catchExceptions(false);

/** @var AdminUrlGenerator $adminURL */
/** @var AdminUrlGenerator $adminURLGenerator */
$adminURLGenerator = self::getContainer()->get(AdminUrlGenerator::class);
$url = $adminURLGenerator->setRoute('payment_order_export')
->set('ids', '1,3')
Expand Down Expand Up @@ -65,7 +65,7 @@ public function testExportAutoSingleModeOneExport(): void
LoginHelper::loginAsAdmin($client);
$client->catchExceptions(false);

/** @var AdminUrlGenerator $adminURL */
/** @var AdminUrlGenerator $adminURLGenerator */
$adminURLGenerator = self::getContainer()->get(AdminUrlGenerator::class);
$url = $adminURLGenerator->setRoute('payment_order_export')
->set('ids', '1')
Expand Down
17 changes: 12 additions & 5 deletions tests/Controller/PaymentOrderControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
use App\Entity\PaymentOrder;
use App\Repository\PaymentOrderRepository;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use Symfony\Component\DomCrawler\Field\FileFormField;
use Symfony\Component\DomCrawler\Form;

/**
Expand Down Expand Up @@ -63,7 +64,7 @@ public function testNewFormSubmit(): void
]);

//Do some basic validation
self::assertSame('John', $new_payment_order->getFirstName());
self::assertSame('John', $new_payment_order->getSubmitterName());
self::assertSame('DE68500105175596424738', $new_payment_order->getBankInfo()->getIbanWithoutSpaces());

//Test if file was uploaded and put into correct place
Expand Down Expand Up @@ -231,7 +232,13 @@ protected function fillPaymentOrderFormData(Form $form): void
]);

//Upload a PDF
$form['payment_order[references_file][file]']->upload($this->data_dir.'/upload.pdf');
$fileField = $form['payment_order[references_file][file]'];

if (!$fileField instanceof FileFormField) {
throw new \InvalidArgumentException('Invalid form');
}

$fileField->upload($this->data_dir.'/upload.pdf');
}

public function testConfirmationInvalidToken(): void
Expand Down Expand Up @@ -279,7 +286,7 @@ public function testConfirmation1(): void
$payment_order = $repo->find(1);

//And check if it was marked as confirmed
self::assertNotNull($payment_order->getConfirm1Timestamp());
self::assertTrue($payment_order->getConfirmation1()->isConfirmed());
}

public function testConfirmation2(): void
Expand All @@ -305,7 +312,7 @@ public function testConfirmation2(): void
$payment_order = $repo->find(1);

//And check if it was marked as confirmed
self::assertNotNull($payment_order->getConfirm2Timestamp());
self::assertTrue($payment_order->getConfirmation2()->isConfirmed());
}

public function testConfirmation1WithoutAllCheckboxesChecked(): void
Expand All @@ -332,6 +339,6 @@ public function testConfirmation1WithoutAllCheckboxesChecked(): void
self::assertResponseIsSuccessful();

//If a check mark was missing the payment order must not be confirmed
self::assertNull($payment_order->getConfirm1Timestamp());
self::assertTrue($payment_order->getConfirmation1()->isConfirmed());
}
}
25 changes: 10 additions & 15 deletions tests/Entity/DepartmentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,46 +19,41 @@
namespace App\Tests\Entity;

use App\Entity\Department;
use App\Entity\DepartmentTypes;
use PHPUnit\Framework\TestCase;

class DepartmentTest extends TestCase
{
public function testIsFSR(): void
{
$department = new Department();
$department->setType(Department::TYPE_FSR);
$department->setType(DepartmentTypes::FSR);
self::assertTrue($department->isFSR());
$department->setType(Department::TYPE_SECTION);
$department->setType(DepartmentTypes::SECTION);
self::assertFalse($department->isFSR());
$department->setType(Department::TYPE_ADMINISTRATIVE);
self::assertFalse($department->isFSR());
$department->setType('invalid');
$department->setType(DepartmentTypes::ADMINISTRATIVE);
self::assertFalse($department->isFSR());
}

public function testIsAdministrative(): void
{
$department = new Department();
$department->setType(Department::TYPE_FSR);
$department->setType(DepartmentTypes::FSR);
self::assertFalse($department->isAdministrative());
$department->setType(Department::TYPE_SECTION);
$department->setType(DepartmentTypes::SECTION);
self::assertFalse($department->isAdministrative());
$department->setType(Department::TYPE_ADMINISTRATIVE);
$department->setType(DepartmentTypes::ADMINISTRATIVE);
self::assertTrue($department->isAdministrative());
$department->setType('invalid');
self::assertFalse($department->isAdministrative());
}

public function testIsSection(): void
{
$department = new Department();
$department->setType(Department::TYPE_FSR);
$department->setType(DepartmentTypes::FSR);
self::assertFalse($department->isSection());
$department->setType(Department::TYPE_SECTION);
$department->setType(DepartmentTypes::SECTION);
self::assertTrue($department->isSection());
$department->setType(Department::TYPE_ADMINISTRATIVE);
self::assertFalse($department->isSection());
$department->setType('invalid');
$department->setType(DepartmentTypes::ADMINISTRATIVE);
self::assertFalse($department->isSection());
}

Expand Down
54 changes: 16 additions & 38 deletions tests/Entity/PaymentOrderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

namespace App\Tests\Entity;

use App\Entity\Embeddable\Check;
use App\Entity\Embeddable\Confirmation;
use App\Entity\PaymentOrder;
use DateTime;
use PHPUnit\Framework\TestCase;
Expand All @@ -33,7 +35,6 @@ public function testGetIDString(string $expected, int $id): void
$payment_order = new PaymentOrder();
$reflection = new ReflectionClass(PaymentOrder::class);
$property = $reflection->getProperty('id');
$property->setAccessible(true);
$property->setValue($payment_order, $id);

static::assertSame($expected, $payment_order->getIDString());
Expand All @@ -47,25 +48,6 @@ public function GetIDStringDataProvider(): \Iterator
yield ['ZA123456', 123456];
}

/**
* @dataProvider fullNameDataProvider
*/
public function testGetFullName(string $expected, string $first_name, string $last_name): void
{
$payment_order = new PaymentOrder();
$payment_order->setFirstName($first_name)
->setLastName($last_name);
static::assertSame($expected, $payment_order->getFullName());
}

public function fullNameDataProvider(): \Iterator
{
yield ['John Doe', 'John', 'Doe'];
yield ['Admin', 'Admin', ''];
yield ['Admin', '', 'Admin'];
yield ['John Jane Doe', 'John Jane', 'Doe'];
}

/**
* @dataProvider getAmountStringDataProvider
*/
Expand Down Expand Up @@ -102,31 +84,27 @@ public function testIsConfirmed(): void
static::assertFalse($payment_order->isConfirmed());

//A single confirmation must not be sufficient
$payment_order->setConfirm1Timestamp(new DateTime());
static::assertFalse($payment_order->isConfirmed());

//With both timestamps set the payment order is confirmed
$payment_order->setConfirm2Timestamp(new DateTime());
static::assertTrue($payment_order->isConfirmed());
$confirmation = new Confirmation();
$confirmation->setTimestamp(new DateTime());
$confirmation->setConfirmerName('Confirmer 1');

$payment_order->setConfirmation1($confirmation);

//Test the case with the other timestamp missing
$payment_order->setConfirm1Timestamp(null);
static::assertFalse($payment_order->isConfirmed());
}

public function testSetFactuallyCorrect(): void
{
$payment_order = new PaymentOrder();
//With both timestamps set the payment order is confirmed

static::assertNull($payment_order->getBookingDate());
$confirmation = new Confirmation();
$confirmation->setTimestamp(new DateTime());
$confirmation->setConfirmerName('Confirmer 2');

//If a payment order is factually checked, booking date must be set
$payment_order->setFactuallyCorrect(true);
static::assertNotNull($payment_order->getBookingDate());
$payment_order->setConfirmation2($confirmation);
static::assertTrue($payment_order->isConfirmed());

//If factually correct is revoked, then the booking was not done yet.
$payment_order->setFactuallyCorrect(false);
static::assertNull($payment_order->getBookingDate());
//Test the case with the other timestamp missing
$payment_order->getConfirmation1()->setTimestamp(null);
static::assertFalse($payment_order->isConfirmed());
}

/**
Expand Down
8 changes: 6 additions & 2 deletions tests/PaymentOrderTestingHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ class PaymentOrderTestingHelper
public static function arrayToPaymentOrder(array $data): PaymentOrder
{
$payment_order = static::getDummyPaymentOrder($data['id'] ?? 1);
$payment_order->setFirstName($data['first_name'])
->setLastName($data['last_name'])
$payment_order
->setSubmitterName($data['submitter_name'] ?? ( $data['first_name'] . ' ' . $data['last_name']))
->setSubmitterEmail($data['contact_email'])
->setAmount($data['amount'])
->setProjectName($data['project_name'])
Expand All @@ -54,7 +54,11 @@ public static function arrayToPaymentOrder(array $data): PaymentOrder
*/
public static function getDummyPaymentOrder(int $id = 1): PaymentOrder
{

return new class($id) extends PaymentOrder {

protected ?int $id2;

public function __construct(int $id)
{
$this->id2 = $id;
Expand Down
51 changes: 16 additions & 35 deletions tests/Services/EmailConfirmation/ConfirmationEmailSenderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@

namespace App\Tests\Services\EmailConfirmation;

use App\Entity\Confirmer;
use App\Entity\Department;
use App\Services\EmailConfirmation\ConfirmationEmailSender;
use App\Tests\PaymentOrderTestingHelper;
use DateTime;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

/**
Expand All @@ -34,23 +36,34 @@ class ConfirmationEmailSenderTest extends WebTestCase
*/
protected $service;

protected ?Confirmer $confirmer1 = null;
protected ?Confirmer $confirmer2 = null;

protected function setUp(): void
{
self::bootKernel();
$this->service = self::getContainer()->get(ConfirmationEmailSender::class);

$this->confirmer1 = new Confirmer();
$this->confirmer1->setEmail('[email protected]')->setName('Test 1');

$this->confirmer2 = new Confirmer();
$this->confirmer2->setEmail('[email protected]')->setName('Test 2');
}

public function testSendConfirmation1SendEmail(): void
{
$department = new Department();
$department->setEmailHhv(['[email protected]', '[email protected]']);

$department->getConfirmers()->add($this->confirmer1);
$department->getConfirmers()->add($this->confirmer2);
$payment_order = PaymentOrderTestingHelper::getDummyPaymentOrder()->setDepartment($department);

$this->service->sendConfirmation1($payment_order);

//It is important that a token was set and no timestamp
self::assertNotEmpty($payment_order->getConfirm1Token());
self::assertNull($payment_order->getConfirm1Timestamp());
self::assertNotEmpty($payment_order->getConfirmationTokens()[0]->getHashedToken());
self::assertFalse($payment_order->getConfirmation1()->isConfirmed());

//Ensure that an email was sent
self::assertEmailCount(1);
Expand All @@ -67,22 +80,6 @@ public function testSendConfirmation1SendEmail(): void
self::assertEmailAddressContains($email, 'reply-to', '[email protected]');
}

public function testSendConfirmation1NoEmail(): void
{
$department = new Department();
$department->setEmailHhv([]);
$payment_order = PaymentOrderTestingHelper::getDummyPaymentOrder()->setDepartment($department);

$this->service->sendConfirmation1($payment_order);

//If no emails are present the token must be set and the confirm timestamp is set
self::assertNotEmpty($payment_order->getConfirm1Token());
self::assertNotNull($payment_order->getConfirm1Timestamp());

//Ensure that no email is sent
self::assertEmailCount(0);
}

public function testSendConfirmation2SendEmail(): void
{
$department = new Department();
Expand Down Expand Up @@ -110,22 +107,6 @@ public function testSendConfirmation2SendEmail(): void
self::assertEmailAddressContains($email, 'reply-to', '[email protected]');
}

public function testSendConfirmation2NoEmail(): void
{
$department = new Department();
$department->setEmailHhv([]);
$payment_order = PaymentOrderTestingHelper::getDummyPaymentOrder()->setDepartment($department);

$this->service->sendConfirmation2($payment_order);

//If no emails are present the token must be set and the confirm timestamp is set
self::assertNotEmpty($payment_order->getConfirm2Token());
self::assertNotNull($payment_order->getConfirm2Timestamp());

//Ensure that no email is sent
self::assertEmailCount(0);
}

public function testResendConfirmationsAlreadyConfirmed(): void
{
$department = new Department();
Expand Down
Loading

0 comments on commit 19c8852

Please sign in to comment.