Skip to content

Commit

Permalink
Merge pull request #28 from shopware/next-39768/check-if-the-shop-url…
Browse files Browse the repository at this point in the history
…-is-reachable

NEXT-39768 - Dispatch an event before storing the shop in the DB during the registration process
  • Loading branch information
shyim authored Dec 3, 2024
2 parents 4d78e47 + 3c771b2 commit 47939ad
Show file tree
Hide file tree
Showing 4 changed files with 185 additions and 11 deletions.
12 changes: 12 additions & 0 deletions src/Event/BeforeRegistrationStartsEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?php

declare(strict_types=1);

namespace Shopware\App\SDK\Event;

/**
* This event is fired before the registration process for new or already existing shops.
*/
class BeforeRegistrationStartsEvent extends AbstractAppLifecycleEvent
{
}
9 changes: 8 additions & 1 deletion src/Registration/RegistrationService.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Shopware\App\SDK\Authentication\RequestVerifier;
use Shopware\App\SDK\Authentication\ResponseSigner;
use Shopware\App\SDK\Event\BeforeRegistrationCompletedEvent;
use Shopware\App\SDK\Event\BeforeRegistrationStartsEvent;
use Shopware\App\SDK\Event\RegistrationCompletedEvent;
use Shopware\App\SDK\Exception\MissingShopParameterException;
use Shopware\App\SDK\Exception\ShopNotFoundException;
Expand Down Expand Up @@ -61,9 +62,15 @@ public function register(RequestInterface $request): ResponseInterface
$this->shopSecretGeneratorInterface->generate()
);

$this->eventDispatcher?->dispatch(new BeforeRegistrationStartsEvent($request, $shop));

$this->shopRepository->createShop($shop);
} else {
$this->shopRepository->updateShop($shop->setShopUrl($queries['shop-url']));
$shop->setShopUrl($queries['shop-url']);

$this->eventDispatcher?->dispatch(new BeforeRegistrationStartsEvent($request, $shop));

$this->shopRepository->updateShop($shop);
}

$this->logger->info('Shop registration request received', [
Expand Down
29 changes: 29 additions & 0 deletions tests/Event/BeforeRegistrationStartsEventTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace Event;

use Nyholm\Psr7\Request;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\TestCase;
use Shopware\App\SDK\Event\BeforeRegistrationStartsEvent;
use Shopware\App\SDK\Test\MockShop;

#[CoversClass(BeforeRegistrationStartsEvent::class)]
class BeforeRegistrationStartsEventTest extends TestCase
{
public function testEvent(): void
{
$event = new BeforeRegistrationStartsEvent(
new Request('GET', 'http://localhost?shop-id=123&shop-url=https://my-shop.com&timestamp=1234567890'),
new MockShop('shop-id', 'shop-url', 'shop-secret'),
);

static::assertSame('GET', $event->getRequest()->getMethod());
static::assertSame('http://localhost?shop-id=123&shop-url=https://my-shop.com&timestamp=1234567890', (string) $event->getRequest()->getUri());
static::assertSame('shop-id', $event->getShop()->getShopId());
static::assertSame('shop-url', $event->getShop()->getShopUrl());
static::assertSame('shop-secret', $event->getShop()->getShopSecret());
}
}
146 changes: 136 additions & 10 deletions tests/Registration/RegistrationServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@
use Shopware\App\SDK\Authentication\RequestVerifier;
use Shopware\App\SDK\Authentication\ResponseSigner;
use Shopware\App\SDK\Event\BeforeRegistrationCompletedEvent;
use Shopware\App\SDK\Event\BeforeRegistrationStartsEvent;
use Shopware\App\SDK\Event\RegistrationCompletedEvent;
use Shopware\App\SDK\Exception\MissingShopParameterException;
use Shopware\App\SDK\Exception\ShopNotFoundException;
use Shopware\App\SDK\Registration\RandomStringShopSecretGenerator;
use Shopware\App\SDK\Registration\RegistrationService;
use PHPUnit\Framework\TestCase;
use Shopware\App\SDK\Shop\ShopRepositoryInterface;
use Shopware\App\SDK\Test\MockShop;
use Shopware\App\SDK\Test\MockShopRepository;

Expand Down Expand Up @@ -55,16 +57,89 @@ public function testRegisterMissingParameters(): void

public function testRegisterCreate(): void
{
$request = new Request('GET', 'http://localhost?shop-id=123&shop-url=https://my-shop.com&timestamp=1234567890');
$events = [];

$response = $this->registerService->register($request);
$eventDispatcher = $this->createMock(EventDispatcherInterface::class);
$eventDispatcher
->expects(static::once())
->method('dispatch')
->willReturnCallback(function ($event) use (&$events) {
$events[] = $event;
});

$shop = $this->shopRepository->getShopFromId('123');
static::assertNotNull($shop);
$shopRepository = $this->createMock(ShopRepositoryInterface::class);

static::assertEquals('123', $shop->getShopId());
static::assertEquals('https://my-shop.com', $shop->getShopUrl());
static::assertNotNull($shop->getShopSecret());
$registrationService = new RegistrationService(
$this->appConfiguration,
$shopRepository,
$this->createMock(RequestVerifier::class),
new ResponseSigner(),
new RandomStringShopSecretGenerator(),
new NullLogger(),
$eventDispatcher
);

$shopRepository
->expects(static::once())
->method('getShopFromId')
->willReturn(null);

$shop = new MockShop('123', 'https://my-shop.com', '1234567890');

$shopRepository
->expects(static::once())
->method('createShopStruct')
->willReturn($shop);

$eventDispatcher
->expects(static::once())
->method('dispatch');

$response = $registrationService->register(
new Request('GET', 'http://localhost?shop-id=123&shop-url=https://my-shop.com&timestamp=1234567890')
);

static::assertSame(200, $response->getStatusCode());
$json = json_decode((string) $response->getBody()->getContents(), true);

static::assertCount(1, $events);
static::assertInstanceOf(BeforeRegistrationStartsEvent::class, $events[0]);

static::assertIsArray($json);
static::assertArrayHasKey('proof', $json);
static::assertArrayHasKey('confirmation_url', $json);
static::assertArrayHasKey('secret', $json);
}

public function testRegisterCreateMustNotDispatchBeforeRegistrationStartsEvent(): void
{
$shopRepository = $this->createMock(ShopRepositoryInterface::class);

$registrationService = new RegistrationService(
$this->appConfiguration,
$shopRepository,
$this->createMock(RequestVerifier::class),
new ResponseSigner(),
new RandomStringShopSecretGenerator(),
new NullLogger(),
null
);

$shopRepository
->expects(static::once())
->method('getShopFromId')
->willReturn(null);

$shop = new MockShop('123', 'https://my-shop.com', '1234567890');

$shopRepository
->expects(static::once())
->method('createShopStruct')
->willReturn($shop);

$response = $registrationService->register(
new Request('GET', 'http://localhost?shop-id=123&shop-url=https://my-shop.com&timestamp=1234567890')
);

static::assertSame(200, $response->getStatusCode());
$json = json_decode((string) $response->getBody()->getContents(), true);
Expand All @@ -77,13 +152,64 @@ public function testRegisterCreate(): void

public function testRegisterUpdate(): void
{
$request = new Request('GET', 'http://localhost?shop-id=123&shop-url=https://my-shop.com&timestamp=1234567890');
$events = [];

$this->shopRepository->createShop(new MockShop('123', 'https://foo.com', '1234567890'));
$eventDispatcher = $this->createMock(EventDispatcherInterface::class);
$eventDispatcher
->expects(static::once())
->method('dispatch')
->willReturnCallback(function ($event) use (&$events) {
$events[] = $event;
});

$this->registerService->register($request);
$registrationService = new RegistrationService(
$this->appConfiguration,
$this->shopRepository,
$this->createMock(RequestVerifier::class),
new ResponseSigner(),
new RandomStringShopSecretGenerator(),
new NullLogger(),
$eventDispatcher
);

$registrationService->register(
new Request('GET', 'http://localhost?shop-id=123&shop-url=https://my-shop.com&timestamp=1234567890')
);

$shop = $this->shopRepository->getShopFromId('123');

$this->shopRepository->updateShop($shop);

static::assertNotNull($shop);

static::assertCount(1, $events);
static::assertInstanceOf(BeforeRegistrationStartsEvent::class, $events[0]);

static::assertEquals('123', $shop->getShopId());
static::assertEquals('https://my-shop.com', $shop->getShopUrl());
static::assertNotNull($shop->getShopSecret());
}

public function testRegisterUpdateMustNotDispatchBeforeRegistrationStartsEvent(): void
{
$registrationService = new RegistrationService(
$this->appConfiguration,
$this->shopRepository,
$this->createMock(RequestVerifier::class),
new ResponseSigner(),
new RandomStringShopSecretGenerator(),
new NullLogger(),
null
);

$request = new Request('GET', 'http://localhost?shop-id=123&shop-url=https://my-shop.com&timestamp=1234567890');

$registrationService->register($request);

$shop = $this->shopRepository->getShopFromId('123');

$this->shopRepository->updateShop($shop);

static::assertNotNull($shop);

static::assertEquals('123', $shop->getShopId());
Expand Down

0 comments on commit 47939ad

Please sign in to comment.