Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,6 @@ parameters:
count: 1
path: src/bundle/Controller/UserSettingsController.php

-
message: '#^Method Ibexa\\Contracts\\User\\Invitation\\Persistence\\Gateway\:\:getInvitationByEmail\(\) has no return type specified\.$#'
identifier: missingType.return
count: 1
path: src/contracts/Invitation/Persistence/Gateway.php

-
message: '#^Method Ibexa\\Contracts\\User\\Notification\\UserPasswordReset\:\:asEmailMessage\(\) never returns null so it can be removed from the return type\.$#'
identifier: return.unusedType
Expand Down
7 changes: 5 additions & 2 deletions src/contracts/Invitation/Persistence/Gateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,16 @@ public function addInvitation(
): array;

/**
* @return array<string, mixed>
* @phpstan-return TInvitationData
*/
public function getInvitation(string $hash): array;

public function invitationExistsForEmail(string $email): bool;

public function getInvitationByEmail(string $email);
/**
* @phpstan-return TInvitationData
*/
public function getInvitationByEmail(string $email): array;

/**
* @phpstan-return TInvitationData[]
Expand Down
23 changes: 20 additions & 3 deletions src/lib/Invitation/Persistence/DoctrineGateway.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Ibexa\Contracts\User\Invitation\Persistence\Gateway;
use Ibexa\Contracts\User\Invitation\Persistence\InvitationUpdateStruct;
use Ibexa\Contracts\User\Invitation\Query\InvitationFilter;
use Ibexa\Core\Base\Exceptions\NotFoundException;

/**
* @internal
Expand All @@ -32,6 +33,9 @@ public function __construct(
) {
}

/**
* @phpstan-return TInvitationData
*/
public function addInvitation(
string $email,
string $siteAccessName,
Expand Down Expand Up @@ -74,6 +78,9 @@ public function addInvitation(
return $this->getInvitationByEmail($email);
}

/**
* @phpstan-return TInvitationData
*/
public function getInvitation(
string $hash
): array {
Expand All @@ -86,9 +93,14 @@ public function getInvitation(
);

$statement = $query->executeQuery();
$result = $statement->fetchAssociative();

/** @var array<string, mixed> */
return $statement->fetchAssociative();
if ($result === false) {
throw new NotFoundException('invitation', $hash);
}

/** @phpstan-var TInvitationData */
return $result;
}

public function invitationExistsForEmail(
Expand Down Expand Up @@ -124,9 +136,14 @@ public function getInvitationByEmail(string $email): array
);

$statement = $query->executeQuery();
$result = $statement->fetchAssociative();

if ($result === false) {
throw new NotFoundException('invitation', $email);
}

/** @phpstan-var TInvitationData */
return $statement->fetchAssociative();
return $result;
}

private function getSelectQuery(): QueryBuilder
Expand Down
9 changes: 0 additions & 9 deletions src/lib/Invitation/Persistence/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
use Ibexa\Contracts\User\Invitation\Persistence\InvitationUpdateStruct;
use Ibexa\Contracts\User\Invitation\Persistence\Mapper;
use Ibexa\Contracts\User\Invitation\Query\InvitationFilter;
use Ibexa\Core\Base\Exceptions\NotFoundException;

class Handler implements HandlerInterface
{
Expand Down Expand Up @@ -51,10 +50,6 @@ public function getInvitation(
): Invitation {
$invitationRow = $this->gateway->getInvitation($hash);

if (empty($invitationRow)) {
throw new NotFoundException('invitation', $hash);
}

return $this->mapper->extractInvitationFromRow($invitationRow);
}

Expand All @@ -63,10 +58,6 @@ public function getInvitationForEmail(
): Invitation {
$invitationRow = $this->gateway->getInvitationByEmail($email);

if (empty($invitationRow)) {
throw new NotFoundException('invitation', $email);
}

return $this->mapper->extractInvitationFromRow($invitationRow);
}

Expand Down
6 changes: 6 additions & 0 deletions tests/integration/IbexaKernelTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,17 @@

use Ibexa\Contracts\Core\Test\IbexaKernelTestCase as BaseIbexaKernelTestCase;
use Ibexa\Contracts\User\Invitation\InvitationService;
use Ibexa\User\Invitation\Persistence\DoctrineGateway;

abstract class IbexaKernelTestCase extends BaseIbexaKernelTestCase
{
protected static function getInvitationService(): InvitationService
{
return self::getServiceByClassName(InvitationService::class);
}

protected static function getInvitationGateway(): DoctrineGateway
{
return self::getServiceByClassName(DoctrineGateway::class);
}
}
2 changes: 2 additions & 0 deletions tests/integration/IbexaTestKernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Ibexa\ContentForms\Form\ActionDispatcher\UserDispatcher;
use Ibexa\Contracts\Core\Test\IbexaTestKernel as BaseIbexaTestKernel;
use Ibexa\Contracts\User\Invitation\InvitationService;
use Ibexa\User\Invitation\Persistence\DoctrineGateway;
use LogicException;
use Symfony\Component\Config\Loader\LoaderInterface;
use Symfony\Component\DependencyInjection\ContainerBuilder;
Expand Down Expand Up @@ -51,6 +52,7 @@ protected static function getExposedServicesByClass(): iterable

yield InvitationService::class;
yield FormFactoryInterface::class;
yield DoctrineGateway::class;
}

#[\Override]
Expand Down
45 changes: 45 additions & 0 deletions tests/integration/Invitation/InvitationServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Ibexa\Contracts\User\Invitation\InvitationCreateStruct;
use Ibexa\Contracts\User\Invitation\InvitationService;
use Ibexa\Contracts\User\Invitation\Query\InvitationFilter;
use Ibexa\Core\Base\Exceptions\NotFoundException;
use Ibexa\Tests\Integration\User\IbexaKernelTestCase;
use Ibexa\User\Invitation\Persistence\Handler;
use Symfony\Bridge\PhpUnit\ClockMock;
Expand Down Expand Up @@ -168,4 +169,48 @@ public function testRefreshInvitation(): void

ClockMock::withClockMock(false);
}

public function testGetInvitationByEmailReturnsRowForExistingInvitation(): void
{
$invitation = $this->invitationService->createInvitation(
new InvitationCreateStruct(
'invitation-service@ibexa.co',
'admin',
)
);

$invitationByEmail = $this->invitationService->getInvitationByEmail($invitation->getEmail());

self::assertSame($invitation->getEmail(), $invitationByEmail->getEmail());
self::assertSame($invitation->getHash(), $invitationByEmail->getHash());
}

public function testGetInvitationByEmailThrowErrorWhenInvitationDoesNotExist(): void
{
$this->expectException(NotFoundException::class);

$this->invitationService->getInvitationByEmail('gateway-missing@ibexa.co');
}

public function testGetInvitationReturnsRowForExistingHash(): void
{
$invitation = $this->invitationService->createInvitation(
new InvitationCreateStruct(
'invitation-service-by-hash@ibexa.co',
'admin',
)
);

$invitationByHash = $this->invitationService->getInvitation($invitation->getHash());

self::assertSame($invitation->getEmail(), $invitationByHash->getEmail());
self::assertSame($invitation->getHash(), $invitationByHash->getHash());
}

public function testGetInvitationThrowExceptionWhenHashDoesNotExist(): void
{
$this->expectException(NotFoundException::class);

$this->invitationService->getInvitation('invitation-service-missing-hash');
}
}
65 changes: 65 additions & 0 deletions tests/integration/Invitation/Persistence/DoctrineGatewayTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
<?php

/**
* @copyright Copyright (C) Ibexa AS. All rights reserved.
* @license For full copyright and license information view LICENSE file distributed with this source code.
*/
declare(strict_types=1);

namespace Ibexa\Tests\Integration\User\Invitation\Persistence;

use Ibexa\Core\Base\Exceptions\NotFoundException;
use Ibexa\Tests\Integration\User\IbexaKernelTestCase;
use Ibexa\User\Invitation\Persistence\DoctrineGateway;

final class DoctrineGatewayTest extends IbexaKernelTestCase
{
private DoctrineGateway $gateway;

protected function setUp(): void
{
$this->gateway = self::getInvitationGateway();
}

public function testGetInvitationByEmailReturnsRowForExistingInvitation(): void
{
$invitation = $this->gateway->addInvitation(
'gateway-existing@ibexa.co',
'admin',
'gateway-existing-hash'
);

$invitationRow = $this->gateway->getInvitationByEmail($invitation['email']);

self::assertSame($invitation['email'], $invitationRow['email']);
self::assertSame($invitation['hash'], $invitationRow['hash']);
}

public function testGetInvitationByEmailThrowExceptionWhenInvitationDoesNotExist(): void
{
$this->expectException(NotFoundException::class);

$this->gateway->getInvitationByEmail('gateway-missing@ibexa.co');
}

public function testGetInvitationReturnsRowForExistingHash(): void
{
$invitation = $this->gateway->addInvitation(
'gateway-existing-by-hash@ibexa.co',
'admin',
'gateway-existing-by-hash-hash'
);

$invitationRow = $this->gateway->getInvitation($invitation['hash']);

self::assertSame($invitation['email'], $invitationRow['email']);
self::assertSame($invitation['hash'], $invitationRow['hash']);
}

public function testGetInvitationThrowExceptionWhenHashDoesNotExist(): void
{
$this->expectException(NotFoundException::class);

$this->gateway->getInvitation('gateway-missing-hash');
}
}
Loading