Skip to content

Commit

Permalink
fixes, revert ApiPlatformSecurityClient and use ApiClient instead in …
Browse files Browse the repository at this point in the history
…behat
  • Loading branch information
arti0090 committed Feb 18, 2021
1 parent 9740b92 commit 6d453ce
Show file tree
Hide file tree
Showing 12 changed files with 56 additions and 96 deletions.
37 changes: 8 additions & 29 deletions src/Sylius/Behat/Client/ApiPlatformSecurityClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,6 @@ public function prepareLoginRequest(): void
$this->request['method'] = 'POST';
}

public function preparePasswordResetRequest(): void
{
$this->request['url'] = sprintf('/api/v2/%s/password-reset-request', $this->section);
$this->request['method'] = 'POST';
}

public function setEmail(string $email): void
{
$this->request['body']['email'] = $email;
Expand All @@ -62,7 +56,14 @@ public function setPassword(string $password): void

public function call(): void
{
$this->callRequest();
$this->client->request(
$this->request['method'],
$this->request['url'],
[],
[],
['CONTENT_TYPE' => 'application/json', 'HTTP_ACCEPT' => 'application/json'],
json_encode($this->request['body'])
);

$response = $this->client->getResponse();
$content = json_decode($response->getContent(), true);
Expand Down Expand Up @@ -96,26 +97,4 @@ public function logOut(): void
$this->sharedStorage->set('cart_token', null);
}
}

public function resetPassword(): void
{
$this->callRequest();
}

private function callRequest(): void
{
$this->client->request(
$this->request['method'],
$this->request['url'],
[],
[],
['CONTENT_TYPE' => 'application/json', 'HTTP_ACCEPT' => 'application/json'],
json_encode($this->request['body'])
);
}

public function getLastResponse(): Response
{
return $this->client->getResponse();
}
}
6 changes: 0 additions & 6 deletions src/Sylius/Behat/Client/ApiSecurityClientInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ interface ApiSecurityClientInterface
{
public function prepareLoginRequest(): void;

public function preparePasswordResetRequest(): void;

public function setEmail(string $email): void;

public function setPassword(string $password): void;
Expand All @@ -32,8 +30,4 @@ public function isLoggedIn(): bool;
public function getErrorMessage(): string;

public function logOut(): void;

public function resetPassword(): void;

public function getLastResponse(): Response;
}
6 changes: 2 additions & 4 deletions src/Sylius/Behat/Context/Api/EmailContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,8 @@ class EmailContext implements Context
/** @var TranslatorInterface */
private $translator;

public function __construct(
EmailCheckerInterface $emailChecker,
TranslatorInterface $translator
) {
public function __construct(EmailCheckerInterface $emailChecker, TranslatorInterface $translator)
{
$this->emailChecker = $emailChecker;
$this->translator = $translator;
}
Expand Down
22 changes: 19 additions & 3 deletions src/Sylius/Behat/Context/Api/Shop/LoginContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,23 @@
namespace Sylius\Behat\Context\Api\Shop;

use Behat\Behat\Context\Context;
use Sylius\Behat\Client\ApiClientInterface;
use Sylius\Behat\Client\ApiSecurityClientInterface;
use Sylius\Behat\Client\Request;
use Webmozart\Assert\Assert;
use \Symfony\Component\HttpFoundation\Request as HTTPRequest;

final class LoginContext implements Context
{
/** @var ApiSecurityClientInterface */
private $client;

/** @var ApiClientInterface */
private $apiClient;

/** @var Request */
private $request;

public function __construct(ApiSecurityClientInterface $client)
{
$this->client = $client;
Expand All @@ -48,26 +57,33 @@ public function iWantToLogIn(): void
*/
public function iWantToResetPassword(): void
{
$this->client->preparePasswordResetRequest();
$this->request = Request::custom('shop/password-reset-request', HTTPRequest::METHOD_POST);
}

/**
* @When I reset it
*/
public function iResetIt(): void
{
$this->client->resetPassword();
$this->apiClient->executeCustomRequest($this->request);
}

/**
* @When I specify the username as :username
* @When I specify the email as :username
*/
public function iSpecifyTheUsername(string $username): void
{
$this->client->setEmail($username);
}

/**
* @When I specify the email as :email
*/
public function iSpecifyTheEmail(string $email): void
{
$this->request->setContent(['email' => $email]);
}

/**
* @When I specify the password as :password
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@

<service id="sylius.behat.context.api.shop.login" class="Sylius\Behat\Context\Api\Shop\LoginContext">
<argument type="service" id="sylius.behat.client.shop_api_platform_security_client" />
<argument type="service" id="sylius.behat.api_platform_client.shop.account" />
</service>

<service id="sylius.behat.context.api.shop.product" class="Sylius\Behat\Context\Api\Shop\ProductContext">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace Sylius\Bundle\ApiBundle\Command;

class ResetPasswordRequest implements ChannelCodeAwareInterface, LocaleCodeAwareInterface
class ResetPassword implements ChannelCodeAwareInterface, LocaleCodeAwareInterface
{
/** @var string */
public $email;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@

namespace Sylius\Bundle\ApiBundle\CommandHandler;

use Sylius\Bundle\ApiBundle\Command\ResetPasswordRequest;
use Sylius\Bundle\ApiBundle\Command\ResetPassword;
use Sylius\Bundle\ApiBundle\Event\ResetPasswordRequested;
use Sylius\Component\User\Repository\UserRepositoryInterface;
use Sylius\Component\User\Security\Generator\GeneratorInterface;
use Symfony\Component\Messenger\MessageBusInterface;
use Symfony\Component\Messenger\Stamp\DispatchAfterCurrentBusStamp;
use Webmozart\Assert\Assert;

class ResetPasswordRequestHandler
class ResetPasswordHandler
{
/** @var UserRepositoryInterface */
private $userRepository;
Expand All @@ -33,7 +33,7 @@ public function __construct(
$this->generator = $generator;
}

public function __invoke(ResetPasswordRequest $command): void
public function __invoke(ResetPassword $command): void
{
$user = $this->userRepository->findOneByEmail($command->getEmail());
Assert::notNull($user);
Expand All @@ -45,7 +45,5 @@ public function __invoke(ResetPasswordRequest $command): void
new ResetPasswordRequested($command->getEmail(), $command->getChannelCode(), $command->getLocaleCode()),
[new DispatchAfterCurrentBusStamp()
]);


}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ class ResetPasswordRequested
private $channelCode;

/** @var string */
private $localeCode;
protected $localeCode;

public function __construct(
string $email,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function __invoke(ResetPasswordRequested $resetPasswordRequested): void
{
$this->commandBus->dispatch(
new SendResetPasswordEmail(
$resetPasswordRequested->email,
$resetPasswordRequested->email(),
$resetPasswordRequested->channelCode(),
$resetPasswordRequested->localeCode()
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,25 @@
<attribute name="input">Sylius\Bundle\ApiBundle\Command\RegisterShopUser</attribute>
<attribute name="output">false</attribute>
</collectionOperation>

<collectionOperation name="shop_password_reset_request">
<attribute name="method">POST</attribute>
<attribute name="path">shop/password-reset-request</attribute>
<attribute name="messenger">input</attribute>
<attribute name="input">Sylius\Bundle\ApiBundle\Command\ResetPasswordRequest</attribute>
<attribute name="output">false</attribute>
<attribute name="status">202</attribute>
<attribute name="validation_groups">
<attribute>sylius</attribute>
<attribute>sylius_shop_password_reset</attribute>
</attribute>
<attribute name="normalization_context">
<attribute name="groups">shop:shop_user:password_reset</attribute>
</attribute>
<attribute name="openapi_context">
<attribute name="summary">Request password reset</attribute>
</attribute>
</collectionOperation>
</collectionOperations>

<itemOperations>
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@
<tag name="messenger.message_handler" bus="sylius_default.bus"/>
</service>

<service id="Sylius\Bundle\ApiBundle\CommandHandler\ResetPasswordRequestHandler">
<service id="ResetPasswordHandler">
<argument type="service" id="sylius.repository.shop_user" />
<argument type="service" id="sylius_event.bus" />
<argument type="service" id="sylius.shop_user.token_generator.password_reset" />
Expand Down

0 comments on commit 6d453ce

Please sign in to comment.