|
| 1 | +<?php |
| 2 | + |
| 3 | +/** |
| 4 | + * Copyright © . All rights reserved. |
| 5 | + * See LICENSE file for license details. |
| 6 | + */ |
| 7 | + |
| 8 | +declare(strict_types=1); |
| 9 | + |
| 10 | +namespace OxidEsales\ExamplesModule\Tests\Unit\Greeting\Service\Decorator; |
| 11 | + |
| 12 | +use OxidEsales\Eshop\Application\Model\User; |
| 13 | +use OxidEsales\ExamplesModule\Greeting\Service\Decorator\GreetingValidationDecorator; |
| 14 | +use OxidEsales\ExamplesModule\Greeting\Service\GreetingMessageServiceInterface; |
| 15 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 16 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 17 | +use PHPUnit\Framework\Attributes\Test; |
| 18 | +use PHPUnit\Framework\TestCase; |
| 19 | + |
| 20 | +#[CoversClass(GreetingValidationDecorator::class)] |
| 21 | +final class GreetingValidationDecoratorTest extends TestCase |
| 22 | +{ |
| 23 | + #[Test] |
| 24 | + public function getGeneralGreetingDelegatesToInnerService(): void |
| 25 | + { |
| 26 | + $expectedGreeting = uniqid('greeting'); |
| 27 | + $innerService = $this->createMock(GreetingMessageServiceInterface::class); |
| 28 | + $innerService->expects($this->once()) |
| 29 | + ->method('getGeneralGreeting') |
| 30 | + ->willReturn($expectedGreeting); |
| 31 | + |
| 32 | + $sut = $this->getSut(originalService: $innerService); |
| 33 | + |
| 34 | + $this->assertSame($expectedGreeting, $sut->getGeneralGreeting()); |
| 35 | + } |
| 36 | + |
| 37 | + #[Test] |
| 38 | + public function getGreetingDelegatesToInnerService(): void |
| 39 | + { |
| 40 | + $expectedGreeting = uniqid('greeting'); |
| 41 | + $userMock = $this->createMock(User::class); |
| 42 | + |
| 43 | + $innerService = $this->createMock(GreetingMessageServiceInterface::class); |
| 44 | + $innerService->expects($this->once()) |
| 45 | + ->method('getGreeting') |
| 46 | + ->with($userMock) |
| 47 | + ->willReturn($expectedGreeting); |
| 48 | + |
| 49 | + $sut = $this->getSut(originalService: $innerService); |
| 50 | + |
| 51 | + $this->assertSame($expectedGreeting, $sut->getGreeting($userMock)); |
| 52 | + } |
| 53 | + |
| 54 | + #[Test] |
| 55 | + #[DataProvider('truncationProvider')] |
| 56 | + public function saveGreetingForCurrentUserTruncatesLongMessages( |
| 57 | + string $inputMessage, |
| 58 | + string $expectedMessage |
| 59 | + ): void { |
| 60 | + $innerService = $this->createMock(GreetingMessageServiceInterface::class); |
| 61 | + $innerService->expects($this->once()) |
| 62 | + ->method('saveGreetingForCurrentUser') |
| 63 | + ->with($expectedMessage); |
| 64 | + |
| 65 | + $sut = $this->getSut(originalService: $innerService); |
| 66 | + $sut->saveGreetingForCurrentUser($inputMessage); |
| 67 | + } |
| 68 | + |
| 69 | + public static function truncationProvider(): array |
| 70 | + { |
| 71 | + return [ |
| 72 | + 'exactly 50 chars' => [ |
| 73 | + str_repeat('a', 50), |
| 74 | + str_repeat('a', 50), |
| 75 | + ], |
| 76 | + 'exactly 51 chars - truncated to 50' => [ |
| 77 | + str_repeat('a', 51), |
| 78 | + str_repeat('a', 50), |
| 79 | + ], |
| 80 | + 'long message - 100 chars truncated to 50' => [ |
| 81 | + str_repeat('a', 100), |
| 82 | + str_repeat('a', 50), |
| 83 | + ], |
| 84 | + 'message with multibyte chars' => [ |
| 85 | + 'Это очень длинное приветствие которое должно быть обрезано', |
| 86 | + 'Это очень длинное приветствие которое должно быть ', |
| 87 | + ], |
| 88 | + 'short message passed through' => [ |
| 89 | + 'Hello', |
| 90 | + 'Hello', |
| 91 | + ], |
| 92 | + 'empty string passed through' => [ |
| 93 | + '', |
| 94 | + '', |
| 95 | + ], |
| 96 | + ]; |
| 97 | + } |
| 98 | + |
| 99 | + private function getSut( |
| 100 | + ?GreetingMessageServiceInterface $originalService = null |
| 101 | + ): GreetingValidationDecorator { |
| 102 | + return new GreetingValidationDecorator( |
| 103 | + originalService: $originalService ?? $this->createStub(GreetingMessageServiceInterface::class) |
| 104 | + ); |
| 105 | + } |
| 106 | +} |
0 commit comments