Skip to content
Merged
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
12 changes: 0 additions & 12 deletions Build/phpstan-baseline.neon
Original file line number Diff line number Diff line change
Expand Up @@ -36,24 +36,12 @@ parameters:
count: 1
path: ../Classes/EventListener/RetrieveProductsFromRequest.php

-
message: '#^Method Extcode\\CartBooks\\EventListener\\RetrieveProductsFromRequest\:\:checkRequestArguments\(\) return type has no value type specified in iterable type array\.$#'
identifier: missingType.iterableValue
count: 1
path: ../Classes/EventListener/RetrieveProductsFromRequest.php

-
message: '#^Method Extcode\\CartBooks\\EventListener\\RetrieveProductsFromRequest\:\:getCartProductFromBook\(\) has parameter \$taxClasses with no value type specified in iterable type array\.$#'
identifier: missingType.iterableValue
count: 1
path: ../Classes/EventListener/RetrieveProductsFromRequest.php

-
message: '#^Parameter \#1 \$book of method Extcode\\CartBooks\\EventListener\\RetrieveProductsFromRequest\:\:getCartProductFromBook\(\) expects Extcode\\CartBooks\\Domain\\Model\\Book, TYPO3\\CMS\\Extbase\\DomainObject\\DomainObjectInterface\|null given\.$#'
identifier: argument.type
count: 1
path: ../Classes/EventListener/RetrieveProductsFromRequest.php

-
message: '#^Parameter \#2 \$productId of class Extcode\\Cart\\Domain\\Model\\Cart\\Product constructor expects int, int\<1, max\>\|null given\.$#'
identifier: argument.type
Expand Down
32 changes: 22 additions & 10 deletions Classes/EventListener/RetrieveProductsFromRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Extcode\Cart\Event\RetrieveProductsFromRequestEvent;
use Extcode\CartBooks\Domain\Model\Book;
use Extcode\CartBooks\Domain\Repository\BookRepository;
use TYPO3\CMS\Core\Messaging\FlashMessage;
use TYPO3\CMS\Core\Type\ContextualFeedbackSeverity;
use TYPO3\CMS\Extbase\Utility\LocalizationUtility;

Expand Down Expand Up @@ -43,6 +44,12 @@ public function __invoke(RetrieveProductsFromRequestEvent $event): void
}

$book = $this->bookRepository->findByUid((int)$requestArguments['book']);
if (!$book instanceof Book) {
$event->setErrors([
$this->createError('tx_cartbooks.error.book_not_found', ContextualFeedbackSeverity::ERROR),
]);
return;
}

$quantity = (int)$requestArguments['quantity'];

Expand All @@ -69,28 +76,33 @@ protected function getCartProductFromBook(
);
}

/**
* @return FlashMessage[]
*/
protected function checkRequestArguments(array $requestArguments): array
{
if (!(int)$requestArguments['book']) {
return [
'messageBody' => LocalizationUtility::translate(
'tx_cartbooks.error.book_not_found',
'CartBooks'
),
ContextualFeedbackSeverity::ERROR,
$this->createError('tx_cartbooks.error.book_not_found', ContextualFeedbackSeverity::ERROR),
];
}

if ((int)$requestArguments['quantity'] < 0) {
return [
'messageBody' => LocalizationUtility::translate(
'tx_cart.error.invalid_quantity',
'CartBooks'
),
'severity' => ContextualFeedbackSeverity::WARNING,
$this->createError('tx_cartbooks.error.invalid_quantity', ContextualFeedbackSeverity::WARNING),
];
}

return [];
}

private function createError(string $key, ContextualFeedbackSeverity $severity): FlashMessage
{
return new FlashMessage(
LocalizationUtility::translate($key, 'CartBooks') ?? $key,
'',
$severity,
false
);
}
}
6 changes: 6 additions & 0 deletions Resources/Private/Language/locallang.xlf
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@
<trans-unit id="tx_cartbooks.plugin.form.submit.error.not_enough_seets_available">
<source>Not enough Books available.</source>
</trans-unit>
<trans-unit id="tx_cartbooks.error.book_not_found">
<source>The book could not be found.</source>
</trans-unit>
<trans-unit id="tx_cartbooks.error.invalid_quantity">
<source>The quantity is invalid.</source>
</trans-unit>

<trans-unit id="tx_books_domain_model_book.sku">
<source>Stock keeping unit</source>
Expand Down
128 changes: 128 additions & 0 deletions Tests/Unit/EventListener/RetrieveProductsFromRequestTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
<?php

declare(strict_types=1);

namespace Extcode\CartBooks\Tests\Unit\EventListener;

/*
* This file is part of the package extcode/cart-books.
*
* For the full copyright and license information, please read the
* LICENSE file that was distributed with this source code.
*/

use Extcode\Cart\Domain\Model\Cart\Cart;
use Extcode\Cart\Event\RetrieveProductsFromRequestEvent;
use Extcode\CartBooks\Domain\Repository\BookRepository;
use Extcode\CartBooks\EventListener\RetrieveProductsFromRequest;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Test;
use TYPO3\CMS\Core\Localization\LanguageService;
use TYPO3\CMS\Core\Localization\LanguageServiceFactory;
use TYPO3\CMS\Core\Localization\Locale;
use TYPO3\CMS\Core\Localization\Locales;
use TYPO3\CMS\Core\Type\ContextualFeedbackSeverity;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Mvc\RequestInterface;
use TYPO3\TestingFramework\Core\Unit\UnitTestCase;

#[CoversClass(RetrieveProductsFromRequest::class)]
class RetrieveProductsFromRequestTest extends UnitTestCase
{
protected bool $resetSingletonInstances = true;

#[Test]
public function ignoresRequestsForOtherProductTypes(): void
{
$event = $this->createEvent(['productType' => 'CartProducts']);

$this->createSubject()($event);

self::assertSame([], $event->getErrors());
self::assertSame([], $event->getProducts());
}

#[Test]
public function setsErrorAsFlashMessageForMissingBookArgument(): void
{
$event = $this->createEvent(['productType' => 'CartBooks', 'book' => '0', 'quantity' => '1']);

$this->mockTranslation();
$this->createSubject()($event);

$this->assertSingleError($event, 'tx_cartbooks.error.book_not_found', ContextualFeedbackSeverity::ERROR);
}

#[Test]
public function setsErrorAsFlashMessageForInvalidQuantity(): void
{
$event = $this->createEvent(['productType' => 'CartBooks', 'book' => '1', 'quantity' => '-1']);

$this->mockTranslation();
$this->createSubject()($event);

$this->assertSingleError($event, 'tx_cartbooks.error.invalid_quantity', ContextualFeedbackSeverity::WARNING);
}

#[Test]
public function setsErrorAsFlashMessageForNotExistingBook(): void
{
$event = $this->createEvent(['productType' => 'CartBooks', 'book' => '42', 'quantity' => '1']);

$bookRepository = self::createStub(BookRepository::class);
$bookRepository->method('findByUid')->willReturn(null);

$this->mockTranslation();
(new RetrieveProductsFromRequest($bookRepository))($event);

$this->assertSingleError($event, 'tx_cartbooks.error.book_not_found', ContextualFeedbackSeverity::ERROR);
self::assertSame([], $event->getProducts());
}

/**
* Lets LocalizationUtility::translate() return the given key.
*/
private function mockTranslation(): void
{
$locales = self::createStub(Locales::class);
$locales->method('createLocaleFromRequest')->willReturn(new Locale());
GeneralUtility::setSingletonInstance(Locales::class, $locales);

$languageService = self::createStub(LanguageService::class);
$languageService->method('translate')->willReturnArgument(0);
$languageServiceFactory = self::createStub(LanguageServiceFactory::class);
$languageServiceFactory->method('create')->willReturn($languageService);
GeneralUtility::addInstance(LanguageServiceFactory::class, $languageServiceFactory);
}

/**
* @param array<string, string> $arguments
*/
private function createEvent(array $arguments): RetrieveProductsFromRequestEvent
{
$request = self::createStub(RequestInterface::class);
$request->method('getArguments')->willReturn($arguments);

$cart = self::createStub(Cart::class);
$cart->method('getTaxClasses')->willReturn([]);

return new RetrieveProductsFromRequestEvent($request, $cart);
}

private function createSubject(): RetrieveProductsFromRequest
{
return new RetrieveProductsFromRequest(self::createStub(BookRepository::class));
}

private function assertSingleError(
RetrieveProductsFromRequestEvent $event,
string $message,
ContextualFeedbackSeverity $severity
): void {
$errors = $event->getErrors();

self::assertCount(1, $errors);
self::assertSame($message, $errors[0]->getMessage());
self::assertSame($severity, $errors[0]->getSeverity());
}
}
Loading