|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +/** |
| 5 | + * This source file is available under the terms of the |
| 6 | + * Pimcore Open Core License (POCL) |
| 7 | + * Full copyright and license information is available in |
| 8 | + * LICENSE.md which is distributed with this source code. |
| 9 | + * |
| 10 | + * @copyright Copyright (c) Pimcore GmbH (https://www.pimcore.com) |
| 11 | + * @license Pimcore Open Core License (POCL) |
| 12 | + */ |
| 13 | + |
| 14 | +namespace Pimcore\Bundle\StudioBackendBundle\Tests\Unit\Mercure\Service; |
| 15 | + |
| 16 | +use Codeception\Test\Unit; |
| 17 | +use LogicException; |
| 18 | +use Pimcore\Bundle\StudioBackendBundle\Mercure\Service\UrlService; |
| 19 | +use Symfony\Component\HttpFoundation\Request; |
| 20 | +use Symfony\Component\HttpFoundation\RequestStack; |
| 21 | + |
| 22 | +final class UrlServiceTest extends Unit |
| 23 | +{ |
| 24 | + public function testGetServerSideUrlWithCustomUrl(): void |
| 25 | + { |
| 26 | + $service = new UrlService('https://custom/mercure', null, new RequestStack()); |
| 27 | + |
| 28 | + $this->assertSame('https://custom/mercure', $service->getServerSideUrl()); |
| 29 | + } |
| 30 | + |
| 31 | + public function testGetServerSideUrlWithDefault(): void |
| 32 | + { |
| 33 | + $service = new UrlService( |
| 34 | + null, |
| 35 | + null, |
| 36 | + $this->createRequestStackWithRequest('https://example.com') |
| 37 | + ); |
| 38 | + |
| 39 | + $this->assertSame('https://example.com/hub/.well-known/mercure', $service->getServerSideUrl()); |
| 40 | + } |
| 41 | + |
| 42 | + public function testGetClientSideUrlWithCustomUrlReplacesPlaceholder(): void |
| 43 | + { |
| 44 | + $service = new UrlService( |
| 45 | + null, |
| 46 | + '<PIMCORE_SCHEMA_HOST>/custom-hub', |
| 47 | + $this->createRequestStackWithRequest('https://example.com'), |
| 48 | + ); |
| 49 | + |
| 50 | + $this->assertSame('https://example.com/custom-hub', $service->getClientSideUrl()); |
| 51 | + } |
| 52 | + |
| 53 | + public function testGetClientSideUrlWithDefault(): void |
| 54 | + { |
| 55 | + $service = new UrlService( |
| 56 | + null, |
| 57 | + null, |
| 58 | + $this->createRequestStackWithRequest('https://example.com') |
| 59 | + ); |
| 60 | + |
| 61 | + $this->assertSame('https://example.com/hub', $service->getClientSideUrl()); |
| 62 | + } |
| 63 | + |
| 64 | + public function testThrowsLogicExceptionWithoutRequest(): void |
| 65 | + { |
| 66 | + $service = new UrlService(null, null, new RequestStack()); |
| 67 | + |
| 68 | + $this->expectException(LogicException::class); |
| 69 | + $service->getServerSideUrl(); |
| 70 | + } |
| 71 | + |
| 72 | + public function testDefaultUrlIncludesNonStandardPort(): void |
| 73 | + { |
| 74 | + $service = new UrlService( |
| 75 | + null, |
| 76 | + null, |
| 77 | + $this->createRequestStackWithRequest('http://localhost:8080') |
| 78 | + ); |
| 79 | + |
| 80 | + $this->assertSame('http://localhost:8080/hub/.well-known/mercure', $service->getServerSideUrl()); |
| 81 | + } |
| 82 | + |
| 83 | + private function createRequestStackWithRequest(string $uri): RequestStack |
| 84 | + { |
| 85 | + $requestStack = new RequestStack(); |
| 86 | + $requestStack->push(Request::create(uri: $uri)); |
| 87 | + |
| 88 | + return $requestStack; |
| 89 | + } |
| 90 | +} |
0 commit comments