|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace EventListener; |
| 6 | + |
| 7 | +use PHPUnit\Framework\Attributes\CoversClass; |
| 8 | +use PHPUnit\Framework\MockObject\MockObject; |
| 9 | +use PHPUnit\Framework\TestCase; |
| 10 | +use Psr\Http\Message\RequestInterface; |
| 11 | +use Shopware\App\SDK\Event\BeforeRegistrationStartsEvent; |
| 12 | +use Shopware\App\SDK\Shop\ShopInterface; |
| 13 | +use Shopware\AppBundle\EventListener\BeforeRegistrationStartsListener; |
| 14 | +use Shopware\AppBundle\Exception\ShopURLIsNotReachableException; |
| 15 | +use Symfony\Contracts\HttpClient\HttpClientInterface; |
| 16 | + |
| 17 | +#[CoversClass(BeforeRegistrationStartsListener::class)] |
| 18 | +final class BeforeRegistrationStartsListenerTest extends TestCase |
| 19 | +{ |
| 20 | + private HttpClientInterface&MockObject $httpClient; |
| 21 | + |
| 22 | + protected function setUp(): void |
| 23 | + { |
| 24 | + $this->httpClient = $this->createMock(HttpClientInterface::class); |
| 25 | + } |
| 26 | + |
| 27 | + public function testListenerMustReturnBecauseTheCheckIsSetToFalseInBundleConfiguration(): void |
| 28 | + { |
| 29 | + $shop = $this->createMock(ShopInterface::class); |
| 30 | + $shop |
| 31 | + ->expects(self::never()) |
| 32 | + ->method('getShopUrl'); |
| 33 | + |
| 34 | + $this->httpClient |
| 35 | + ->expects(self::never()) |
| 36 | + ->method('request'); |
| 37 | + |
| 38 | + $listener = new BeforeRegistrationStartsListener( |
| 39 | + $this->httpClient, |
| 40 | + false |
| 41 | + ); |
| 42 | + |
| 43 | + $listener->__invoke( |
| 44 | + new BeforeRegistrationStartsEvent( |
| 45 | + $this->createMock(RequestInterface::class), |
| 46 | + $shop |
| 47 | + ) |
| 48 | + ); |
| 49 | + } |
| 50 | + |
| 51 | + public function testListenerMustBeExecutedWithoutErrorsIfTheCheckIsSetToTrueInConfiguration(): void |
| 52 | + { |
| 53 | + $shop = $this->createMock(ShopInterface::class); |
| 54 | + $shop |
| 55 | + ->expects(self::once()) |
| 56 | + ->method('getShopUrl') |
| 57 | + ->willReturn('https://shop-url.com'); |
| 58 | + |
| 59 | + $this->httpClient |
| 60 | + ->expects(self::once()) |
| 61 | + ->method('request') |
| 62 | + ->with('HEAD', 'https://shop-url.com', [ |
| 63 | + 'timeout' => 10, |
| 64 | + 'max_redirects' => 0, |
| 65 | + ]); |
| 66 | + |
| 67 | + $listener = new BeforeRegistrationStartsListener( |
| 68 | + $this->httpClient, |
| 69 | + true |
| 70 | + ); |
| 71 | + |
| 72 | + $listener->__invoke( |
| 73 | + new BeforeRegistrationStartsEvent( |
| 74 | + $this->createMock(RequestInterface::class), |
| 75 | + $shop |
| 76 | + ) |
| 77 | + ); |
| 78 | + } |
| 79 | + |
| 80 | + public function testListenerMustThrowExceptionBecauseTheShopURLIsNotReachable(): void |
| 81 | + { |
| 82 | + $this->expectException(ShopURLIsNotReachableException::class); |
| 83 | + $this->expectExceptionMessage('Shop URL "https://shop-url.com" is not reachable from the internet and cannot be registered.'); |
| 84 | + |
| 85 | + $shop = $this->createMock(ShopInterface::class); |
| 86 | + $shop |
| 87 | + ->expects(self::exactly(2)) |
| 88 | + ->method('getShopUrl') |
| 89 | + ->willReturn('https://shop-url.com'); |
| 90 | + |
| 91 | + $this->httpClient |
| 92 | + ->expects(self::once()) |
| 93 | + ->method('request') |
| 94 | + ->with('HEAD', 'https://shop-url.com', [ |
| 95 | + 'timeout' => 10, |
| 96 | + 'max_redirects' => 0, |
| 97 | + ]) |
| 98 | + ->willThrowException(new \Exception('Shop url is not reachable')); |
| 99 | + |
| 100 | + $listener = new BeforeRegistrationStartsListener( |
| 101 | + $this->httpClient, |
| 102 | + true |
| 103 | + ); |
| 104 | + |
| 105 | + $listener->__invoke( |
| 106 | + new BeforeRegistrationStartsEvent( |
| 107 | + $this->createMock(RequestInterface::class), |
| 108 | + $shop |
| 109 | + ) |
| 110 | + ); |
| 111 | + } |
| 112 | +} |
0 commit comments