Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit c7fa511

Browse files
committed
Add functional test and fixes
1 parent f591f6e commit c7fa511

14 files changed

Lines changed: 369 additions & 89 deletions

File tree

features/cart/shopping_cart/allowing_access_only_for_correctly_logged_in_users.feature

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ Feature: Allowing access only for correctly logged in users
7070
Given the customer logged in
7171
And the customer has product "Stark T-Shirt" in the cart
7272
When the customer specify the billing address as "Ankh Morpork", "Frost Alley", "90210", "United States" for "Jon Snow"
73-
And the customer complete the addressing step
73+
And the customer completes the addressing step
7474
Then the customer should have checkout address step completed
7575

7676
@api

features/checkout/addressing_order/changing_address_during_checkout.feature

Lines changed: 0 additions & 21 deletions
This file was deleted.

src/Sylius/Behat/Context/Api/AddressContext.php

Lines changed: 0 additions & 48 deletions
This file was deleted.

src/Sylius/Behat/Context/Api/Shop/CheckoutContext.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,8 @@ public function iSpecifyTheEmailAs(?string $email): void
191191

192192
/**
193193
* @When /^I specify the billing (address as "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)" for "([^"]+)")$/
194-
* @When /^the (?:customer|visitor) (?:specifies|changes) the billing (address to "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)" for "([^"]+)")$/
194+
* @When /^the (?:customer|visitor) specify the billing (address as "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)" for "([^"]+)")$/
195+
* @When /^the (?:customer|visitor) changes the billing (address to "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)" for "([^"]+)")$/
195196
* @When /^I specify the billing (address for "([^"]+)" from "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)")$/
196197
* @Given /^the (?:visitor|customer) has specified (address as "([^"]+)", "([^"]+)", "([^"]+)", "([^"]+)" for "([^"]+)")$/
197198
*/

src/Sylius/Behat/Resources/config/services/contexts/api/email.xml

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,5 @@
1818
<argument type="service" id="sylius.behat.email_checker" />
1919
<argument type="service" id="translator" />
2020
</service>
21-
22-
<service id="sylius.behat.context.api.address" class="Sylius\Behat\Context\Api\AddressContext">
23-
<argument type="service" id="sylius.repository.address" />
24-
</service>
2521
</services>
2622
</container>

src/Sylius/Behat/Resources/config/suites/api/checkout/checkout.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ default:
4747

4848
- sylius.behat.context.api.admin.managing_orders
4949

50-
- sylius.behat.context.api.address
5150
- sylius.behat.context.api.shop.cart
5251
- sylius.behat.context.api.shop.channel
5352
- sylius.behat.context.api.shop.address

src/Sylius/Bundle/ApiBundle/CommandHandler/Checkout/AddressOrderHandler.php

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Doctrine\Persistence\ObjectManager;
1717
use SM\Factory\FactoryInterface as StateMachineFactoryInterface;
1818
use Sylius\Bundle\ApiBundle\Command\Checkout\AddressOrder;
19+
use Sylius\Bundle\ApiBundle\Mapper\AddressMapperInterface;
1920
use Sylius\Component\Core\Model\AddressInterface;
2021
use Sylius\Component\Core\Model\CustomerInterface;
2122
use Sylius\Component\Core\Model\OrderInterface;
@@ -44,18 +45,23 @@ final class AddressOrderHandler implements MessageHandlerInterface
4445
/** @var StateMachineFactoryInterface */
4546
private $stateMachineFactory;
4647

48+
/** @var AddressMapperInterface */
49+
private $addressMapper;
50+
4751
public function __construct(
4852
OrderRepositoryInterface $orderRepository,
4953
CustomerRepositoryInterface $customerRepository,
5054
FactoryInterface $customerFactory,
5155
ObjectManager $manager,
52-
StateMachineFactoryInterface $stateMachineFactory
56+
StateMachineFactoryInterface $stateMachineFactory,
57+
AddressMapperInterface $addressMapper
5358
) {
5459
$this->orderRepository = $orderRepository;
5560
$this->customerRepository = $customerRepository;
5661
$this->customerFactory = $customerFactory;
5762
$this->manager = $manager;
5863
$this->stateMachineFactory = $stateMachineFactory;
64+
$this->addressMapper = $addressMapper;
5965
}
6066

6167
public function __invoke(AddressOrder $addressOrder): OrderInterface
@@ -77,22 +83,28 @@ public function __invoke(AddressOrder $addressOrder): OrderInterface
7783
$order->setCustomer($this->provideCustomerByEmail($addressOrder->email));
7884
}
7985

86+
/** @var AddressInterface $newBillingAddress */
87+
$newBillingAddress = $addressOrder->billingAddress;
88+
/** @var AddressInterface|null $newShippingAddress */
89+
$newShippingAddress = $newShippingAddress ?? clone $newBillingAddress;
90+
8091
/** @var AddressInterface|null $billingAddress */
8192
$billingAddress = $order->getBillingAddress();
8293
/** @var AddressInterface|null $shippingAddress */
8394
$shippingAddress = $order->getShippingAddress();
8495

8596
if ($billingAddress !== null) {
86-
$this->manager->remove($billingAddress);
97+
$order->setBillingAddress($this->addressMapper->mapExisting($billingAddress, $newBillingAddress));
98+
} else {
99+
$order->setBillingAddress($newBillingAddress);
87100
}
88101

89102
if ($shippingAddress !== null) {
90-
$this->manager->remove($shippingAddress);
103+
$order->setShippingAddress($this->addressMapper->mapExisting($shippingAddress, $newShippingAddress));
104+
} else {
105+
$order->setShippingAddress($newShippingAddress);
91106
}
92107

93-
$order->setBillingAddress($addressOrder->billingAddress);
94-
$order->setShippingAddress($addressOrder->shippingAddress ?? clone $addressOrder->billingAddress);
95-
96108
$stateMachine->apply(OrderCheckoutTransitions::TRANSITION_ADDRESS);
97109

98110
$this->manager->persist($order);
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Sylius package.
5+
*
6+
* (c) Paweł Jędrzejewski
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Sylius\Bundle\ApiBundle\Mapper;
15+
16+
use Sylius\Component\Core\Model\AddressInterface;
17+
18+
final class AddressMapper implements AddressMapperInterface
19+
{
20+
public function mapExisting(AddressInterface $currentAddress, AddressInterface $targetAddress): AddressInterface
21+
{
22+
$currentAddress->setFirstName($targetAddress->getFirstName());
23+
$currentAddress->setLastName($targetAddress->getLastName());
24+
$currentAddress->setCompany($targetAddress->getCompany());
25+
$currentAddress->setStreet($targetAddress->getStreet());
26+
$currentAddress->setCountryCode($targetAddress->getCountryCode());
27+
$currentAddress->setCity($targetAddress->getCity());
28+
$currentAddress->setPostcode($targetAddress->getPostcode());
29+
$currentAddress->setPhoneNumber($targetAddress->getPhoneNumber());
30+
31+
if (null !== $targetAddress->getProvinceCode()) {
32+
$currentAddress->setProvinceCode($targetAddress->getProvinceCode());
33+
$currentAddress->setProvinceName($targetAddress->getProvinceName());
34+
}
35+
36+
return $currentAddress;
37+
}
38+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Sylius package.
5+
*
6+
* (c) Paweł Jędrzejewski
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
declare(strict_types=1);
13+
14+
namespace Sylius\Bundle\ApiBundle\Mapper;
15+
16+
use Sylius\Component\Core\Model\AddressInterface;
17+
18+
interface AddressMapperInterface
19+
{
20+
public function mapExisting(AddressInterface $currentAddress, AddressInterface $targetAddress): AddressInterface;
21+
}

src/Sylius/Bundle/ApiBundle/Resources/config/services.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,5 +165,7 @@
165165
<argument type="service" id="api_platform.metadata.resource.metadata_factory.yaml.inner" />
166166
<argument type="service" id="Sylius\Bundle\ApiBundle\ApiPlatform\ResourceMetadataPropertyValueResolver" />
167167
</service>
168+
169+
<service id="Sylius\Bundle\ApiBundle\Mapper\AddressMapperInterface" class="Sylius\Bundle\ApiBundle\Mapper\AddressMapper" public="true"/>
168170
</services>
169171
</container>

0 commit comments

Comments
 (0)