Skip to content

Commit c330b95

Browse files
committed
NEXT-39768 - Removed the service definition form inside the compiler pass and moved to the service.xml
1 parent c0189ca commit c330b95

File tree

5 files changed

+125
-41
lines changed

5 files changed

+125
-41
lines changed

src/DependencyInjection/ShopwareAppExtension.php

-17
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,12 @@
1010
use Shopware\App\SDK\Shop\ShopRepositoryInterface;
1111
use Shopware\App\SDK\Test\MockShopRepository;
1212
use Shopware\AppBundle\Entity\AbstractShop;
13-
use Shopware\AppBundle\EventListener\BeforeRegistrationStartsListener;
1413
use Symfony\Component\Config\FileLocator;
1514
use Symfony\Component\DependencyInjection\ContainerBuilder;
1615
use Symfony\Component\DependencyInjection\Definition;
1716
use Symfony\Component\DependencyInjection\Extension\Extension;
1817
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
1918
use Symfony\Component\DependencyInjection\Reference;
20-
use Symfony\Contracts\HttpClient\HttpClientInterface;
2119

2220
final class ShopwareAppExtension extends Extension
2321
{
@@ -60,24 +58,9 @@ public function load(array $configs, ContainerBuilder $container): void
6058
->replaceArgument(1, $config['secret'])
6159
->replaceArgument(2, $config['confirmation_url']);
6260

63-
$this->registerListener($container, $config);
64-
}
65-
66-
/**
67-
* @param array<string, bool> $config
68-
*/
69-
private function registerListener(ContainerBuilder $container, array $config): void
70-
{
7161
$container->setParameter(
7262
sprintf('%s.check_if_shop_url_is_reachable', Configuration::EXTENSION_ALIAS),
7363
$config['check_if_shop_url_is_reachable']
7464
);
75-
76-
$listener = new Definition(BeforeRegistrationStartsListener::class);
77-
$listener->setArgument(0, new Reference(HttpClientInterface::class));
78-
$listener->setArgument(1, $config['check_if_shop_url_is_reachable']);
79-
$listener->addTag('kernel.event_listener');
80-
81-
$container->setDefinition(BeforeRegistrationStartsListener::class, $listener);
8265
}
8366
}

src/EventListener/BeforeRegistrationStartsListener.php

+9-3
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,18 @@
1010
use Symfony\Contracts\HttpClient\HttpClientInterface;
1111

1212
#[AsEventListener]
13-
final class BeforeRegistrationStartsListener
13+
class BeforeRegistrationStartsListener
1414
{
15+
private HttpClientInterface $httpClient;
16+
17+
private bool $checkShopURLIsReachable;
18+
1519
public function __construct(
16-
private readonly HttpClientInterface $httpClient,
17-
private readonly bool $checkShopURLIsReachable
20+
HttpClientInterface $httpClient,
21+
bool $checkShopURLIsReachable
1822
) {
23+
$this->httpClient = $httpClient;
24+
$this->checkShopURLIsReachable = $checkShopURLIsReachable;
1925
}
2026

2127
public function __invoke(BeforeRegistrationStartsEvent $event): void

src/Resources/config/services.xml

+4
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@
6565

6666
<service id="Shopware\AppBundle\ArgumentValueResolver\ContextArgumentResolver"/>
6767
<service id="Shopware\AppBundle\EventListener\ResponseSignerListener"/>
68+
<service id="Shopware\AppBundle\EventListener\BeforeRegistrationStartsListener">
69+
<argument type="service" id="Symfony\Contracts\HttpClient\HttpClientInterface"/>
70+
<argument>%shopware_app.check_if_shop_url_is_reachable%</argument>
71+
</service>
6872

6973
<!-- PSR Integration -->
7074
<service id="Symfony\Bridge\PsrHttpMessage\HttpFoundationFactoryInterface" class="Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory"/>

tests/DependencyInjection/ShopwareAppExtensionTest.php

-21
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,8 @@
1313
use Shopware\AppBundle\DependencyInjection\AppConfigurationFactory;
1414
use Shopware\AppBundle\DependencyInjection\ShopwareAppExtension;
1515
use Shopware\AppBundle\Entity\AbstractShop;
16-
use Shopware\AppBundle\EventListener\BeforeRegistrationStartsListener;
1716
use Symfony\Component\DependencyInjection\ContainerBuilder;
1817
use Symfony\Component\DependencyInjection\Reference;
19-
use Symfony\Contracts\HttpClient\HttpClientInterface;
2018

2119
class ShopwareAppExtensionTest extends TestCase
2220
{
@@ -136,24 +134,5 @@ public function testContainerMustHaveBeforeRegistrationStartsListener(): void
136134
$extension->load([], $container);
137135

138136
static::assertTrue($container->hasParameter('shopware_app.check_if_shop_url_is_reachable'));
139-
140-
static::assertTrue($container->hasDefinition(BeforeRegistrationStartsListener::class));
141-
142-
static::assertSame(
143-
'kernel.event_listener',
144-
array_key_first($container->getDefinition(BeforeRegistrationStartsListener::class)->getTags())
145-
);
146-
147-
static::assertCount(
148-
2,
149-
$container->getDefinition(BeforeRegistrationStartsListener::class)->getArguments()
150-
);
151-
152-
static::assertSame(
153-
HttpClientInterface::class,
154-
$container->getDefinition(BeforeRegistrationStartsListener::class)->getArgument(0)->__toString()
155-
);
156-
157-
static::assertFalse($container->getDefinition(BeforeRegistrationStartsListener::class)->getArgument(1));
158137
}
159138
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
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

Comments
 (0)