|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Extcode\CartEvents\Tests\Functional\Domain\Model; |
| 6 | + |
| 7 | +/* |
| 8 | + * This file is part of the package extcode/cart-events. |
| 9 | + * |
| 10 | + * For the full copyright and license information, please read the |
| 11 | + * LICENSE file that was distributed with this source code. |
| 12 | + */ |
| 13 | + |
| 14 | +use Extcode\Cart\Domain\Model\FrontendUserGroup; |
| 15 | +use Extcode\CartEvents\Domain\Model\EventDate; |
| 16 | +use Extcode\CartEvents\Domain\Model\PriceCategory; |
| 17 | +use Extcode\CartEvents\Domain\Model\SpecialPrice; |
| 18 | +use PHPUnit\Framework\Attributes\Test; |
| 19 | +use TYPO3\CMS\Core\Context\Context; |
| 20 | +use TYPO3\CMS\Core\Context\UserAspect; |
| 21 | +use TYPO3\CMS\Core\Utility\GeneralUtility; |
| 22 | +use TYPO3\CMS\Extbase\Persistence\ObjectStorage; |
| 23 | +use TYPO3\TestingFramework\Core\Functional\FunctionalTestCase; |
| 24 | + |
| 25 | +abstract class AbstractSpecialPriceTest extends FunctionalTestCase |
| 26 | +{ |
| 27 | + protected float $price; |
| 28 | + |
| 29 | + protected EventDate|PriceCategory $subject; |
| 30 | + |
| 31 | + protected function tearDown(): void |
| 32 | + { |
| 33 | + unset($this->subject); |
| 34 | + } |
| 35 | + |
| 36 | + #[Test] |
| 37 | + public function getBestSpecialPriceReturnsNullIfEventHasNoSpecialPrice(): void |
| 38 | + { |
| 39 | + $objectStorage = new ObjectStorage(); |
| 40 | + $this->subject->setSpecialPrices($objectStorage); |
| 41 | + |
| 42 | + self::assertNull( |
| 43 | + $this->subject->getBestSpecialPrice() |
| 44 | + ); |
| 45 | + } |
| 46 | + |
| 47 | + #[Test] |
| 48 | + public function getBestSpecialPriceReturnsNullIfEventHasSpecialPriceButUserHasNoGroup(): void |
| 49 | + { |
| 50 | + $this->setUpFrontendUserAspect(); |
| 51 | + |
| 52 | + $frontendUserGroup1 = self::createStub(FrontendUserGroup::class); |
| 53 | + $frontendUserGroup1->method('getUid')->willReturn(42); |
| 54 | + |
| 55 | + $objectStorage = new ObjectStorage(); |
| 56 | + $specialPrice1 = $this->createSpecialPriceForFrontendUserGroup(10.00, $frontendUserGroup1); |
| 57 | + $objectStorage->attach($specialPrice1); |
| 58 | + $this->subject->setSpecialPrices($objectStorage); |
| 59 | + |
| 60 | + self::assertNull( |
| 61 | + $this->subject->getBestSpecialPrice() |
| 62 | + ); |
| 63 | + |
| 64 | + self::assertSame( |
| 65 | + $this->price, |
| 66 | + $this->subject->getBestPrice() |
| 67 | + ); |
| 68 | + } |
| 69 | + |
| 70 | + #[Test] |
| 71 | + public function getBestSpecialPriceReturnsSpecialPriceIfEventHasASpecialPriceWithNoUserGroup(): void |
| 72 | + { |
| 73 | + $this->setUpFrontendUserAspect([]); |
| 74 | + |
| 75 | + $frontendUserGroup1 = self::createStub(FrontendUserGroup::class); |
| 76 | + $frontendUserGroup1->method('getUid')->willReturn(42); |
| 77 | + |
| 78 | + $objectStorage = new ObjectStorage(); |
| 79 | + $specialPrice1 = $this->createSpecialPriceForFrontendUserGroup(10.00, $frontendUserGroup1); |
| 80 | + $objectStorage->attach($specialPrice1); |
| 81 | + $this->subject->setSpecialPrices($objectStorage); |
| 82 | + |
| 83 | + self::assertNull( |
| 84 | + $this->subject->getBestSpecialPrice() |
| 85 | + ); |
| 86 | + |
| 87 | + self::assertSame( |
| 88 | + $this->price, |
| 89 | + $this->subject->getBestPrice() |
| 90 | + ); |
| 91 | + } |
| 92 | + |
| 93 | + #[Test] |
| 94 | + public function getBestSpecialPriceReturnsSpecialPriceIfEventHasASpecialPriceWithMatchingUserGroup(): void |
| 95 | + { |
| 96 | + $this->setUpFrontendUserAspect([42, 43]); |
| 97 | + |
| 98 | + $frontendUserGroup1 = self::createStub(FrontendUserGroup::class); |
| 99 | + $frontendUserGroup1->method('getUid')->willReturn(42); |
| 100 | + |
| 101 | + $objectStorage = new ObjectStorage(); |
| 102 | + $specialPrice1 = $this->createSpecialPriceForFrontendUserGroup(10.00, $frontendUserGroup1); |
| 103 | + $objectStorage->attach($specialPrice1); |
| 104 | + $this->subject->setSpecialPrices($objectStorage); |
| 105 | + |
| 106 | + self::assertSame( |
| 107 | + $specialPrice1, |
| 108 | + $this->subject->getBestSpecialPrice() |
| 109 | + ); |
| 110 | + |
| 111 | + self::assertSame( |
| 112 | + 10.00, |
| 113 | + $this->subject->getBestPrice() |
| 114 | + ); |
| 115 | + } |
| 116 | + |
| 117 | + #[Test] |
| 118 | + public function getBestSpecialPriceReturnsBestSpecialPriceIfEventHasSpecialPricesWithMatchingUserGroup(): void |
| 119 | + { |
| 120 | + $this->setUpFrontendUserAspect([42, 43]); |
| 121 | + |
| 122 | + $frontendUserGroup1 = self::createStub(FrontendUserGroup::class); |
| 123 | + $frontendUserGroup1->method('getUid')->willReturn(42); |
| 124 | + $frontendUserGroup2 = self::createStub(FrontendUserGroup::class); |
| 125 | + $frontendUserGroup2->method('getUid')->willReturn(43); |
| 126 | + |
| 127 | + $objectStorage = new ObjectStorage(); |
| 128 | + $specialPrice1 = $this->createSpecialPriceForFrontendUserGroup(10.00, $frontendUserGroup1); |
| 129 | + $objectStorage->attach($specialPrice1); |
| 130 | + $specialPrice2 = $this->createSpecialPriceForFrontendUserGroup(8.00, $frontendUserGroup2); |
| 131 | + $objectStorage->attach($specialPrice2); |
| 132 | + $this->subject->setSpecialPrices($objectStorage); |
| 133 | + |
| 134 | + self::assertSame( |
| 135 | + $specialPrice2, |
| 136 | + $this->subject->getBestSpecialPrice() |
| 137 | + ); |
| 138 | + |
| 139 | + self::assertSame( |
| 140 | + 8.00, |
| 141 | + $this->subject->getBestPrice() |
| 142 | + ); |
| 143 | + } |
| 144 | + |
| 145 | + #[Test] |
| 146 | + public function getBestSpecialPriceReturnsBestSpecialPriceForUserGroupIfEventHasSpecialPricesWithMatchingUserGroup(): void |
| 147 | + { |
| 148 | + $this->setUpFrontendUserAspect([42, 43]); |
| 149 | + |
| 150 | + $frontendUserGroup1 = self::createStub(FrontendUserGroup::class); |
| 151 | + $frontendUserGroup1->method('getUid')->willReturn(42); |
| 152 | + $frontendUserGroup2 = self::createStub(FrontendUserGroup::class); |
| 153 | + $frontendUserGroup2->method('getUid')->willReturn(43); |
| 154 | + $frontendUserGroup3 = self::createStub(FrontendUserGroup::class); |
| 155 | + $frontendUserGroup3->method('getUid')->willReturn(44); |
| 156 | + |
| 157 | + $objectStorage = new ObjectStorage(); |
| 158 | + $specialPrice1 = $this->createSpecialPriceForFrontendUserGroup(9.00, $frontendUserGroup1); |
| 159 | + $objectStorage->attach($specialPrice1); |
| 160 | + $specialPrice2 = $this->createSpecialPriceForFrontendUserGroup(11.00, $frontendUserGroup2); |
| 161 | + $objectStorage->attach($specialPrice2); |
| 162 | + $specialPrice3 = $this->createSpecialPriceForFrontendUserGroup(7.00, $frontendUserGroup3); |
| 163 | + $objectStorage->attach($specialPrice3); |
| 164 | + $this->subject->setSpecialPrices($objectStorage); |
| 165 | + |
| 166 | + self::assertSame( |
| 167 | + $specialPrice1, |
| 168 | + $this->subject->getBestSpecialPrice() |
| 169 | + ); |
| 170 | + |
| 171 | + self::assertSame( |
| 172 | + 9.00, |
| 173 | + $this->subject->getBestPrice() |
| 174 | + ); |
| 175 | + } |
| 176 | + |
| 177 | + #[Test] |
| 178 | + public function getBestSpecialPriceReturnsSpecialPriceIfEventHasASpecialPriceWithNotMatchingUserGroup(): void |
| 179 | + { |
| 180 | + $this->setUpFrontendUserAspect([41, 43]); |
| 181 | + |
| 182 | + $frontendUserGroup1 = self::createStub(FrontendUserGroup::class); |
| 183 | + $frontendUserGroup1->method('getUid')->willReturn(42); |
| 184 | + |
| 185 | + $objectStorage = new ObjectStorage(); |
| 186 | + $specialPrice1 = $this->createSpecialPriceForFrontendUserGroup(10.00, $frontendUserGroup1); |
| 187 | + $objectStorage->attach($specialPrice1); |
| 188 | + $this->subject->setSpecialPrices($objectStorage); |
| 189 | + |
| 190 | + self::assertNull( |
| 191 | + $this->subject->getBestSpecialPrice() |
| 192 | + ); |
| 193 | + |
| 194 | + self::assertSame( |
| 195 | + $this->price, |
| 196 | + $this->subject->getBestPrice() |
| 197 | + ); |
| 198 | + } |
| 199 | + |
| 200 | + #[Test] |
| 201 | + public function getBestPriceReturnsPriceIfSpecialPriceIsGreater(): void |
| 202 | + { |
| 203 | + $this->setUpFrontendUserAspect([42]); |
| 204 | + |
| 205 | + $frontendUserGroup1 = self::createStub(FrontendUserGroup::class); |
| 206 | + $frontendUserGroup1->method('getUid')->willReturn(42); |
| 207 | + |
| 208 | + $objectStorage = new ObjectStorage(); |
| 209 | + $specialPrice1 = $this->createSpecialPriceForFrontendUserGroup(20.00, $frontendUserGroup1); |
| 210 | + $objectStorage->attach($specialPrice1); |
| 211 | + $this->subject->setSpecialPrices($objectStorage); |
| 212 | + |
| 213 | + self::assertSame( |
| 214 | + $specialPrice1, |
| 215 | + $this->subject->getBestSpecialPrice() |
| 216 | + ); |
| 217 | + |
| 218 | + self::assertSame( |
| 219 | + $this->price, |
| 220 | + $this->subject->getBestPrice() |
| 221 | + ); |
| 222 | + } |
| 223 | + |
| 224 | + private function createSpecialPriceForFrontendUserGroup(float $price, FrontendUserGroup $frontendUserGroup): SpecialPrice |
| 225 | + { |
| 226 | + $specialPrice = new SpecialPrice(); |
| 227 | + $specialPrice->setPrice($price); |
| 228 | + $specialPrice->setFrontendUserGroup($frontendUserGroup); |
| 229 | + |
| 230 | + return $specialPrice; |
| 231 | + } |
| 232 | + |
| 233 | + private function setUpFrontendUserAspect(?array $groupIds = null): void |
| 234 | + { |
| 235 | + $userAspect = GeneralUtility::makeInstance( |
| 236 | + UserAspect::class, |
| 237 | + null, |
| 238 | + $groupIds |
| 239 | + ); |
| 240 | + |
| 241 | + $context = GeneralUtility::makeInstance(Context::class); |
| 242 | + $context->setAspect( |
| 243 | + 'frontend.user', |
| 244 | + $userAspect |
| 245 | + ); |
| 246 | + } |
| 247 | +} |
0 commit comments