Skip to content

Commit

Permalink
NEXT-39768 - Removed the service definition form inside the compiler …
Browse files Browse the repository at this point in the history
…pass and moved to the service.xml
  • Loading branch information
nsaliu committed Nov 28, 2024
1 parent c0189ca commit c330b95
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 41 deletions.
17 changes: 0 additions & 17 deletions src/DependencyInjection/ShopwareAppExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,12 @@
use Shopware\App\SDK\Shop\ShopRepositoryInterface;
use Shopware\App\SDK\Test\MockShopRepository;
use Shopware\AppBundle\Entity\AbstractShop;
use Shopware\AppBundle\EventListener\BeforeRegistrationStartsListener;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
use Symfony\Component\DependencyInjection\Extension\Extension;
use Symfony\Component\DependencyInjection\Loader\XmlFileLoader;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Contracts\HttpClient\HttpClientInterface;

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

$this->registerListener($container, $config);
}

/**
* @param array<string, bool> $config
*/
private function registerListener(ContainerBuilder $container, array $config): void
{
$container->setParameter(
sprintf('%s.check_if_shop_url_is_reachable', Configuration::EXTENSION_ALIAS),
$config['check_if_shop_url_is_reachable']
);

$listener = new Definition(BeforeRegistrationStartsListener::class);
$listener->setArgument(0, new Reference(HttpClientInterface::class));
$listener->setArgument(1, $config['check_if_shop_url_is_reachable']);
$listener->addTag('kernel.event_listener');

$container->setDefinition(BeforeRegistrationStartsListener::class, $listener);
}
}
12 changes: 9 additions & 3 deletions src/EventListener/BeforeRegistrationStartsListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,18 @@
use Symfony\Contracts\HttpClient\HttpClientInterface;

#[AsEventListener]
final class BeforeRegistrationStartsListener
class BeforeRegistrationStartsListener
{
private HttpClientInterface $httpClient;

private bool $checkShopURLIsReachable;

public function __construct(
private readonly HttpClientInterface $httpClient,
private readonly bool $checkShopURLIsReachable
HttpClientInterface $httpClient,
bool $checkShopURLIsReachable
) {
$this->httpClient = $httpClient;
$this->checkShopURLIsReachable = $checkShopURLIsReachable;
}

public function __invoke(BeforeRegistrationStartsEvent $event): void

Check failure on line 27 in src/EventListener/BeforeRegistrationStartsListener.php

View workflow job for this annotation

GitHub Actions / static-analyse

Parameter $event of method Shopware\AppBundle\EventListener\BeforeRegistrationStartsListener::__invoke() has invalid type Shopware\App\SDK\Event\BeforeRegistrationStartsEvent.
Expand Down
4 changes: 4 additions & 0 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,10 @@

<service id="Shopware\AppBundle\ArgumentValueResolver\ContextArgumentResolver"/>
<service id="Shopware\AppBundle\EventListener\ResponseSignerListener"/>
<service id="Shopware\AppBundle\EventListener\BeforeRegistrationStartsListener">
<argument type="service" id="Symfony\Contracts\HttpClient\HttpClientInterface"/>
<argument>%shopware_app.check_if_shop_url_is_reachable%</argument>
</service>

<!-- PSR Integration -->
<service id="Symfony\Bridge\PsrHttpMessage\HttpFoundationFactoryInterface" class="Symfony\Bridge\PsrHttpMessage\Factory\HttpFoundationFactory"/>
Expand Down
21 changes: 0 additions & 21 deletions tests/DependencyInjection/ShopwareAppExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@
use Shopware\AppBundle\DependencyInjection\AppConfigurationFactory;
use Shopware\AppBundle\DependencyInjection\ShopwareAppExtension;
use Shopware\AppBundle\Entity\AbstractShop;
use Shopware\AppBundle\EventListener\BeforeRegistrationStartsListener;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Reference;
use Symfony\Contracts\HttpClient\HttpClientInterface;

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

static::assertTrue($container->hasParameter('shopware_app.check_if_shop_url_is_reachable'));

static::assertTrue($container->hasDefinition(BeforeRegistrationStartsListener::class));

static::assertSame(
'kernel.event_listener',
array_key_first($container->getDefinition(BeforeRegistrationStartsListener::class)->getTags())
);

static::assertCount(
2,
$container->getDefinition(BeforeRegistrationStartsListener::class)->getArguments()
);

static::assertSame(
HttpClientInterface::class,
$container->getDefinition(BeforeRegistrationStartsListener::class)->getArgument(0)->__toString()
);

static::assertFalse($container->getDefinition(BeforeRegistrationStartsListener::class)->getArgument(1));
}
}
112 changes: 112 additions & 0 deletions tests/EventListener/BeforeRegistrationStartsListenerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
<?php

declare(strict_types=1);

namespace EventListener;

use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\RequestInterface;
use Shopware\App\SDK\Event\BeforeRegistrationStartsEvent;
use Shopware\App\SDK\Shop\ShopInterface;
use Shopware\AppBundle\EventListener\BeforeRegistrationStartsListener;
use Shopware\AppBundle\Exception\ShopURLIsNotReachableException;
use Symfony\Contracts\HttpClient\HttpClientInterface;

#[CoversClass(BeforeRegistrationStartsListener::class)]
final class BeforeRegistrationStartsListenerTest extends TestCase
{
private HttpClientInterface&MockObject $httpClient;

protected function setUp(): void
{
$this->httpClient = $this->createMock(HttpClientInterface::class);
}

public function testListenerMustReturnBecauseTheCheckIsSetToFalseInBundleConfiguration(): void
{
$shop = $this->createMock(ShopInterface::class);
$shop
->expects(self::never())
->method('getShopUrl');

$this->httpClient
->expects(self::never())
->method('request');

$listener = new BeforeRegistrationStartsListener(
$this->httpClient,
false
);

$listener->__invoke(
new BeforeRegistrationStartsEvent(
$this->createMock(RequestInterface::class),
$shop
)
);
}

public function testListenerMustBeExecutedWithoutErrorsIfTheCheckIsSetToTrueInConfiguration(): void
{
$shop = $this->createMock(ShopInterface::class);
$shop
->expects(self::once())
->method('getShopUrl')
->willReturn('https://shop-url.com');

$this->httpClient
->expects(self::once())
->method('request')
->with('HEAD', 'https://shop-url.com', [
'timeout' => 10,
'max_redirects' => 0,
]);

$listener = new BeforeRegistrationStartsListener(
$this->httpClient,
true
);

$listener->__invoke(
new BeforeRegistrationStartsEvent(
$this->createMock(RequestInterface::class),
$shop
)
);
}

public function testListenerMustThrowExceptionBecauseTheShopURLIsNotReachable(): void
{
$this->expectException(ShopURLIsNotReachableException::class);
$this->expectExceptionMessage('Shop URL "https://shop-url.com" is not reachable from the internet and cannot be registered.');

$shop = $this->createMock(ShopInterface::class);
$shop
->expects(self::exactly(2))
->method('getShopUrl')
->willReturn('https://shop-url.com');

$this->httpClient
->expects(self::once())
->method('request')
->with('HEAD', 'https://shop-url.com', [
'timeout' => 10,
'max_redirects' => 0,
])
->willThrowException(new \Exception('Shop url is not reachable'));

$listener = new BeforeRegistrationStartsListener(
$this->httpClient,
true
);

$listener->__invoke(
new BeforeRegistrationStartsEvent(
$this->createMock(RequestInterface::class),
$shop
)
);
}
}

0 comments on commit c330b95

Please sign in to comment.