Skip to content

Commit

Permalink
add copyright blocks, add phpspec for handler, fix issues
Browse files Browse the repository at this point in the history
  • Loading branch information
arti0090 committed Feb 19, 2021
1 parent 656719d commit 5bc148c
Show file tree
Hide file tree
Showing 14 changed files with 154 additions and 20 deletions.
9 changes: 9 additions & 0 deletions src/Sylius/Behat/Context/Api/EmailContext.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\Behat\Context\Api;
Expand Down
2 changes: 1 addition & 1 deletion src/Sylius/Behat/Context/Api/Shop/LoginContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public function iWantToLogIn(): void
*/
public function iWantToResetPassword(): void
{
$this->request = Request::create('shop', 'password-reset-request', 'Bearer');
$this->request = Request::create('shop', 'request-reset-password', 'Bearer');
}

/**
Expand Down
4 changes: 1 addition & 3 deletions src/Sylius/Behat/Resources/config/services/contexts/api.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd"
>
<imports>
<import resource="api/admin.xml" />
<import resource="api/shop.xml" />
<import resource="api/email.xml" />
<import resource="api/**/*.xml" />
</imports>
</container>
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\Bundle\ApiBundle\Command;

class ResetPassword implements ChannelCodeAwareInterface, LocaleCodeAwareInterface
class RequestResetPasswordToken implements ChannelCodeAwareInterface, LocaleCodeAwareInterface
{
/** @var string */
public $email;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\Bundle\ApiBundle\Command;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\Bundle\ApiBundle\CommandHandler;

use Sylius\Bundle\ApiBundle\Command\ResetPassword;
use Sylius\Bundle\ApiBundle\Command\RequestResetPasswordToken;
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 ResetPasswordHandler
final class RequestResetPasswordTokenHandler
{
/** @var UserRepositoryInterface */
private $userRepository;
Expand All @@ -33,7 +42,7 @@ public function __construct(
$this->generator = $generator;
}

public function __invoke(ResetPassword $command): void
public function __invoke(RequestResetPasswordToken $command): void
{
$user = $this->userRepository->findOneByEmail($command->getEmail());
Assert::notNull($user);
Expand All @@ -43,7 +52,7 @@ public function __invoke(ResetPassword $command): void

$this->eventBus->dispatch(
new ResetPasswordRequested($command->getEmail(), $command->getChannelCode(), $command->getLocaleCode()),
[new DispatchAfterCurrentBusStamp()
]);
[new DispatchAfterCurrentBusStamp()]
);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\Bundle\ApiBundle\CommandHandler;
Expand All @@ -9,8 +18,9 @@
use Sylius\Component\Channel\Repository\ChannelRepositoryInterface;
use Sylius\Component\Mailer\Sender\SenderInterface;
use Sylius\Component\User\Repository\UserRepositoryInterface;
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;

class SendResetPasswordEmailHandler
final class SendResetPasswordEmailHandler implements MessageHandlerInterface
{
/** @var SenderInterface */
private $emailSender;
Expand Down Expand Up @@ -42,7 +52,7 @@ public function __invoke(SendResetPasswordEmail $command)
[
'user' => $user,
'localeCode' => $command->localeCode(),
'channel' => $channel
'channel' => $channel,
]
);
}
Expand Down
9 changes: 9 additions & 0 deletions src/Sylius/Bundle/ApiBundle/Event/ResetPasswordRequested.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\Bundle\ApiBundle\Event;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,14 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Sylius\Bundle\ApiBundle\EventHandler;
Expand All @@ -8,7 +17,8 @@
use Sylius\Bundle\ApiBundle\Event\ResetPasswordRequested;
use Symfony\Component\Messenger\MessageBusInterface;

class ResetPasswordRequestedHandler
/** @experimental */
final class ResetPasswordRequestedHandler
{
/** @var MessageBusInterface */
private $commandBus;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,17 @@

<collectionOperation name="shop_password_reset_request">
<attribute name="method">POST</attribute>
<attribute name="path">shop/password-reset-request</attribute>
<attribute name="path">shop/request-reset-password</attribute>
<attribute name="messenger">input</attribute>
<attribute name="input">Sylius\Bundle\ApiBundle\Command\ResetPassword</attribute>
<attribute name="input">Sylius\Bundle\ApiBundle\Command\RequestResetPasswordToken</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:customer:password_reset</attribute>
<attribute name="groups">shop:password_reset:create</attribute>
</attribute>
<attribute name="openapi_context">
<attribute name="summary">Request password reset</attribute>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@
>
<class name="Sylius\Bundle\ApiBundle\Command\ResetPassword">
<attribute name="email">
<group>shop:customer:password_reset</group>
<group>shop:password_reset:create</group>
</attribute>
<attribute name="localeCode">
<group>shop:customer:password_reset</group>
<group>shop:password_reset:create</group>
</attribute>
</class>
</serializer>
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\ResetPasswordHandler">
<service id="Sylius\Bundle\ApiBundle\CommandHandler\RequestResetPasswordTokenHandler">
<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
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/constraint-mapping http://symfony.com/schema/dic/services/constraint-mapping-1.0.xsd">

<class name="Sylius\Bundle\ApiBundle\Command\ResetPassword">
<class name="Sylius\Bundle\ApiBundle\Command\RequestResetPasswordToken">
<property name="email">
<constraint name="NotBlank">
<option name="message">sylius.customer.email.not_blank</option>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
<?php

/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace spec\Sylius\Bundle\ApiBundle\CommandHandler;

use PhpSpec\ObjectBehavior;
use Sylius\Bundle\ApiBundle\Command\SendResetPasswordEmail;
use Sylius\Bundle\CoreBundle\Mailer\Emails;
use Sylius\Component\Channel\Repository\ChannelRepositoryInterface;
use Sylius\Component\Core\Model\ChannelInterface;
use Sylius\Component\Mailer\Sender\SenderInterface;
use Sylius\Component\User\Model\UserInterface;
use Sylius\Component\User\Repository\UserRepositoryInterface;
use Symfony\Component\Messenger\Handler\MessageHandlerInterface;

final class SendResetPasswordEmailHandlerSpec extends ObjectBehavior
{
function let(
SenderInterface $emailSender,
ChannelRepositoryInterface $channelRepository,
UserRepositoryInterface $userRepository
): void {
$this->beConstructedWith($emailSender, $channelRepository, $userRepository);
}

function it_is_a_message_handler(): void
{
$this->shouldImplement(MessageHandlerInterface::class);
}

function it_sends_message_with_reset_password_token(
SenderInterface $sender,
UserRepositoryInterface $userRepository,
SendResetPasswordEmail $sendResetPasswordEmail,
UserInterface $user,
ChannelRepositoryInterface $channelRepository,
ChannelInterface $channel
): void {
$sendResetPasswordEmail->email()->willReturn('iAmAnEmail@spaghettiCode.php');

$userRepository->findOneByEmail('iAmAnEmail@spaghettiCode.php')->willReturn($user);

$sendResetPasswordEmail->channelCode()->willReturn('WEB');

$channelRepository->findOneByCode('WEB')->willReturn($channel);

$sendResetPasswordEmail->localeCode()->willReturn('en_US');

$sender->send(
Emails::PASSWORD_RESET,
['iAmAnEmail@spaghettiCode.php'],
[
'user' => $user->getWrappedObject(),
'localeCode' => 'en_US',
'channel' => $channel->getWrappedObject(),
]
);

$this(new SendResetPasswordEmail('iAmAnEmail@spaghettiCode.php', 'WEB', 'en_US'));
}
}

0 comments on commit 5bc148c

Please sign in to comment.