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
17 changes: 15 additions & 2 deletions Classes/Domain/Model/EventDate.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
* For the full copyright and license information, please read the
* LICENSE file that was distributed with this source code.
*/

use TYPO3\CMS\Core\Context\Context;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Annotation\ORM\Cascade;
use TYPO3\CMS\Extbase\Annotation\Validate;
use TYPO3\CMS\Extbase\Domain\Model\FileReference;
Expand Down Expand Up @@ -227,8 +230,13 @@ public function setPriceCategories(ObjectStorage $priceCategories): void
$this->priceCategories = $priceCategories;
}

public function getBestSpecialPrice(array $frontendUserGroupIds = []): ?SpecialPrice
public function getBestSpecialPrice(?array $frontendUserGroupIds = null): ?SpecialPrice
{
if (is_null($frontendUserGroupIds)) {
$context = GeneralUtility::makeInstance(Context::class);
$frontendUserGroupIds = $context->getPropertyFromAspect('frontend.user', 'groupIds');
}

$bestSpecialPrice = null;

if ($this->specialPrices) {
Expand All @@ -246,8 +254,13 @@ public function getBestSpecialPrice(array $frontendUserGroupIds = []): ?SpecialP
return $bestSpecialPrice;
}

public function getBestPrice(array $frontendUserGroupIds = []): float
public function getBestPrice(?array $frontendUserGroupIds = null): float
{
if (is_null($frontendUserGroupIds)) {
$context = GeneralUtility::makeInstance(Context::class);
$frontendUserGroupIds = $context->getPropertyFromAspect('frontend.user', 'groupIds');
}

$price = $this->getPrice();

$specialPrice = $this->getBestSpecialPrice($frontendUserGroupIds);
Expand Down
17 changes: 15 additions & 2 deletions Classes/Domain/Model/PriceCategory.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
* For the full copyright and license information, please read the
* LICENSE file that was distributed with this source code.
*/

use TYPO3\CMS\Core\Context\Context;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Annotation\ORM\Cascade;
use TYPO3\CMS\Extbase\Annotation\Validate;
use TYPO3\CMS\Extbase\DomainObject\AbstractEntity;
Expand Down Expand Up @@ -135,8 +138,13 @@ public function getSeatsAvailable(): int
/**
* Returns best Special Price
*/
public function getBestSpecialPrice(array $frontendUserGroupIds = []): ?SpecialPrice
public function getBestSpecialPrice(?array $frontendUserGroupIds = null): ?SpecialPrice
{
if (is_null($frontendUserGroupIds)) {
$context = GeneralUtility::makeInstance(Context::class);
$frontendUserGroupIds = $context->getPropertyFromAspect('frontend.user', 'groupIds');
}

$bestSpecialPrice = null;

if ($this->specialPrices) {
Expand All @@ -157,8 +165,13 @@ public function getBestSpecialPrice(array $frontendUserGroupIds = []): ?SpecialP
/**
* Returns price of best Special Price
*/
public function getBestPrice(array $frontendUserGroupIds = []): float
public function getBestPrice(?array $frontendUserGroupIds = null): float
{
if (is_null($frontendUserGroupIds)) {
$context = GeneralUtility::makeInstance(Context::class);
$frontendUserGroupIds = $context->getPropertyFromAspect('frontend.user', 'groupIds');
}

$price = $this->getPrice();

$specialPrice = $this->getBestSpecialPrice($frontendUserGroupIds);
Expand Down
247 changes: 247 additions & 0 deletions Tests/Functional/Domain/Model/AbstractSpecialPrice.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,247 @@
<?php

declare(strict_types=1);

namespace Extcode\CartEvents\Tests\Functional\Domain\Model;

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

use Extcode\Cart\Domain\Model\FrontendUserGroup;
use Extcode\CartEvents\Domain\Model\EventDate;
use Extcode\CartEvents\Domain\Model\PriceCategory;
use Extcode\CartEvents\Domain\Model\SpecialPrice;
use PHPUnit\Framework\Attributes\Test;
use TYPO3\CMS\Core\Context\Context;
use TYPO3\CMS\Core\Context\UserAspect;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Persistence\ObjectStorage;
use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase;

abstract class AbstractSpecialPrice extends FunctionalTestCase
{
protected float $price;

protected EventDate|PriceCategory $subject;

protected function tearDown(): void
{
unset($this->subject);
}

#[Test]
public function getBestSpecialPriceReturnsNullIfEventHasNoSpecialPrice(): void
{
$objectStorage = new ObjectStorage();
$this->subject->setSpecialPrices($objectStorage);

self::assertNull(
$this->subject->getBestSpecialPrice()
);
}

#[Test]
public function getBestSpecialPriceReturnsNullIfEventHasSpecialPriceButUserHasNoGroup(): void
{
$this->setUpFrontendUserAspect();

$frontendUserGroup1 = self::createStub(FrontendUserGroup::class);
$frontendUserGroup1->method('getUid')->willReturn(42);

$objectStorage = new ObjectStorage();
$specialPrice1 = $this->createSpecialPriceForFrontendUserGroup(10.00, $frontendUserGroup1);
$objectStorage->attach($specialPrice1);
$this->subject->setSpecialPrices($objectStorage);

self::assertNull(
$this->subject->getBestSpecialPrice()
);

self::assertSame(
$this->price,
$this->subject->getBestPrice()
);
}

#[Test]
public function getBestSpecialPriceReturnsSpecialPriceIfEventHasASpecialPriceWithNoUserGroup(): void
{
$this->setUpFrontendUserAspect([]);

$frontendUserGroup1 = self::createStub(FrontendUserGroup::class);
$frontendUserGroup1->method('getUid')->willReturn(42);

$objectStorage = new ObjectStorage();
$specialPrice1 = $this->createSpecialPriceForFrontendUserGroup(10.00, $frontendUserGroup1);
$objectStorage->attach($specialPrice1);
$this->subject->setSpecialPrices($objectStorage);

self::assertNull(
$this->subject->getBestSpecialPrice()
);

self::assertSame(
$this->price,
$this->subject->getBestPrice()
);
}

#[Test]
public function getBestSpecialPriceReturnsSpecialPriceIfEventHasASpecialPriceWithMatchingUserGroup(): void
{
$this->setUpFrontendUserAspect([42, 43]);

$frontendUserGroup1 = self::createStub(FrontendUserGroup::class);
$frontendUserGroup1->method('getUid')->willReturn(42);

$objectStorage = new ObjectStorage();
$specialPrice1 = $this->createSpecialPriceForFrontendUserGroup(10.00, $frontendUserGroup1);
$objectStorage->attach($specialPrice1);
$this->subject->setSpecialPrices($objectStorage);

self::assertSame(
$specialPrice1,
$this->subject->getBestSpecialPrice()
);

self::assertSame(
10.00,
$this->subject->getBestPrice()
);
}

#[Test]
public function getBestSpecialPriceReturnsBestSpecialPriceIfEventHasSpecialPricesWithMatchingUserGroup(): void
{
$this->setUpFrontendUserAspect([42, 43]);

$frontendUserGroup1 = self::createStub(FrontendUserGroup::class);
$frontendUserGroup1->method('getUid')->willReturn(42);
$frontendUserGroup2 = self::createStub(FrontendUserGroup::class);
$frontendUserGroup2->method('getUid')->willReturn(43);

$objectStorage = new ObjectStorage();
$specialPrice1 = $this->createSpecialPriceForFrontendUserGroup(10.00, $frontendUserGroup1);
$objectStorage->attach($specialPrice1);
$specialPrice2 = $this->createSpecialPriceForFrontendUserGroup(8.00, $frontendUserGroup2);
$objectStorage->attach($specialPrice2);
$this->subject->setSpecialPrices($objectStorage);

self::assertSame(
$specialPrice2,
$this->subject->getBestSpecialPrice()
);

self::assertSame(
8.00,
$this->subject->getBestPrice()
);
}

#[Test]
public function getBestSpecialPriceReturnsBestSpecialPriceForUserGroupIfEventHasSpecialPricesWithMatchingUserGroup(): void
{
$this->setUpFrontendUserAspect([42, 43]);

$frontendUserGroup1 = self::createStub(FrontendUserGroup::class);
$frontendUserGroup1->method('getUid')->willReturn(42);
$frontendUserGroup2 = self::createStub(FrontendUserGroup::class);
$frontendUserGroup2->method('getUid')->willReturn(43);
$frontendUserGroup3 = self::createStub(FrontendUserGroup::class);
$frontendUserGroup3->method('getUid')->willReturn(44);

$objectStorage = new ObjectStorage();
$specialPrice1 = $this->createSpecialPriceForFrontendUserGroup(9.00, $frontendUserGroup1);
$objectStorage->attach($specialPrice1);
$specialPrice2 = $this->createSpecialPriceForFrontendUserGroup(11.00, $frontendUserGroup2);
$objectStorage->attach($specialPrice2);
$specialPrice3 = $this->createSpecialPriceForFrontendUserGroup(7.00, $frontendUserGroup3);
$objectStorage->attach($specialPrice3);
$this->subject->setSpecialPrices($objectStorage);

self::assertSame(
$specialPrice1,
$this->subject->getBestSpecialPrice()
);

self::assertSame(
9.00,
$this->subject->getBestPrice()
);
}

#[Test]
public function getBestSpecialPriceReturnsSpecialPriceIfEventHasASpecialPriceWithNotMatchingUserGroup(): void
{
$this->setUpFrontendUserAspect([41, 43]);

$frontendUserGroup1 = self::createStub(FrontendUserGroup::class);
$frontendUserGroup1->method('getUid')->willReturn(42);

$objectStorage = new ObjectStorage();
$specialPrice1 = $this->createSpecialPriceForFrontendUserGroup(10.00, $frontendUserGroup1);
$objectStorage->attach($specialPrice1);
$this->subject->setSpecialPrices($objectStorage);

self::assertNull(
$this->subject->getBestSpecialPrice()
);

self::assertSame(
$this->price,
$this->subject->getBestPrice()
);
}

#[Test]
public function getBestPriceReturnsPriceIfSpecialPriceIsGreater(): void
{
$this->setUpFrontendUserAspect([42]);

$frontendUserGroup1 = self::createStub(FrontendUserGroup::class);
$frontendUserGroup1->method('getUid')->willReturn(42);

$objectStorage = new ObjectStorage();
$specialPrice1 = $this->createSpecialPriceForFrontendUserGroup(20.00, $frontendUserGroup1);
$objectStorage->attach($specialPrice1);
$this->subject->setSpecialPrices($objectStorage);

self::assertSame(
$specialPrice1,
$this->subject->getBestSpecialPrice()
);

self::assertSame(
$this->price,
$this->subject->getBestPrice()
);
}

private function createSpecialPriceForFrontendUserGroup(float $price, FrontendUserGroup $frontendUserGroup): SpecialPrice
{
$specialPrice = new SpecialPrice();
$specialPrice->setPrice($price);
$specialPrice->setFrontendUserGroup($frontendUserGroup);

return $specialPrice;
}

private function setUpFrontendUserAspect(?array $groupIds = null): void
{
$userAspect = GeneralUtility::makeInstance(
UserAspect::class,
null,
$groupIds
);

$context = GeneralUtility::makeInstance(Context::class);
$context->setAspect(
'frontend.user',
$userAspect
);
}
}
27 changes: 27 additions & 0 deletions Tests/Functional/Domain/Model/EventDateTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Extcode\CartEvents\Tests\Functional\Domain\Model;

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

use Extcode\CartEvents\Domain\Model\EventDate;
use PHPUnit\Framework\Attributes\CoversClass;

#[CoversClass(EventDate::class)]
class EventDateTest extends AbstractSpecialPrice
{
protected function setUp(): void
{
$this->price = 17.49;

$this->subject = new EventDate();
$this->subject->setPrice($this->price);
}
}
27 changes: 27 additions & 0 deletions Tests/Functional/Domain/Model/PriceCategoryTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace Extcode\CartEvents\Tests\Functional\Domain\Model;

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

use Extcode\CartEvents\Domain\Model\PriceCategory;
use PHPUnit\Framework\Attributes\CoversClass;

#[CoversClass(PriceCategory::class)]
class PriceCategoryTest extends AbstractSpecialPrice
{
protected function setUp(): void
{
$this->price = 13.44;

$this->subject = new PriceCategory();
$this->subject->setPrice($this->price);
}
}