Skip to content

Commit

Permalink
Merge pull request PrestaShop#36656 from tleon/Issue-36556-improve-be…
Browse files Browse the repository at this point in the history
…hat-test-replace-object-model-by-cqrs-in-old-steps

Clean behat scenarios with new carrier steps
  • Loading branch information
jolelievre authored Aug 23, 2024
2 parents 359c9b9 + 3b04561 commit a39a38d
Show file tree
Hide file tree
Showing 46 changed files with 709 additions and 866 deletions.
7 changes: 6 additions & 1 deletion src/Adapter/Carrier/CommandHandler/AddCarrierHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,19 @@ public function handle(AddCarrierCommand $command): CarrierId
$carrier->name = $command->getName();
$carrier->grade = $command->getGrade();
$carrier->url = $command->getTrackingUrl();
$carrier->position = $command->getPosition();
$carrier->active = $command->getActive();
$carrier->delay = $command->getLocalizedDelay();
$carrier->max_width = $command->getMaxWidth();
$carrier->max_height = $command->getMaxHeight();
$carrier->max_weight = $command->getMaxWeight();
$carrier->max_depth = $command->getMaxDepth();

if (null !== $command->getPosition()) {
$carrier->position = $command->getPosition();
} else {
$this->carrierRepository->getLastPosition() + 1;
}

// Shipping information
$carrier->shipping_handling = $command->hasAdditionalHandlingFee();
$carrier->is_free = $command->isFree();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public function handle(GetCarrierForEditing $query): EditableCarrier
(int) $carrier->range_behavior,
$carrier->getAssociatedShops(),
$logoPath,
$this->carrierRepository->ordersCount($query->getCarrierId()),
$this->carrierRepository->getOrdersCount($query->getCarrierId()),
);
}
}
17 changes: 17 additions & 0 deletions src/Adapter/Carrier/Repository/CarrierRangeRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,15 @@ public function set(CarrierId $carrierId, CarrierRangesCollection $rangesCollect
]
);
}

// We add the association between the carrier and the zone.
$this->connection->insert(
$this->dbPrefix . 'carrier_zone',
[
'id_carrier' => $carrierId->getValue(),
'id_zone' => $idZone,
]
);
}

// Commit transaction
Expand Down Expand Up @@ -186,6 +195,14 @@ private function reset(CarrierId $carrierId, ShopConstraint $shopConstraint): vo
->setParameter('carrierId', $carrierId->getValue())
->executeQuery();
}

// Then , we delete carriers association with zones
$this->connection->delete(
$this->dbPrefix . 'carrier_zone',
[
'id_carrier' => $carrierId->getValue(),
]
);
}

private function getRangeMethodTable(int $calculatingMethod): string
Expand Down
17 changes: 15 additions & 2 deletions src/Adapter/Carrier/Repository/CarrierRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public function getAssociatedShopIdsFromGroup(CarrierId $carrierId, ShopGroupId
public function getEditableOrNewVersion(CarrierId $carrierId): Carrier
{
// If the carrier don't have orders linked, we can return it as is
if ($this->ordersCount($carrierId) === 0) {
if ($this->getOrdersCount($carrierId) === 0) {
return $this->get($carrierId);
}

Expand Down Expand Up @@ -278,7 +278,7 @@ private function deleteTaxRulesGroup(CarrierId $carrierId, array $shopIds): void
$qb->executeStatement();
}

public function ordersCount(CarrierId $carrierId): int
public function getOrdersCount(CarrierId $carrierId): int
{
$qb = $this->connection->createQueryBuilder();

Expand All @@ -291,4 +291,17 @@ public function ordersCount(CarrierId $carrierId): int

return $count;
}

public function getLastPosition(): int
{
$qb = $this->connection->createQueryBuilder();

return $qb->select('c.position')
->from($this->prefix . 'carrier', 'c')
->orderBy('c.position', 'DESC')
->setMaxResults(1)
->executeQuery()
->fetchOne()
;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@ public function handle(UpdateCartCarrierCommand $command)
$cart->setDeliveryOption([
(int) $cart->id_address_delivery => $this->formatLegacyDeliveryOptionFromCarrierId($command->getNewCarrierId()),
]);

$cart->update();
} finally {
$this->contextStateManager->restorePreviousContext();
Expand Down
10 changes: 8 additions & 2 deletions src/Core/Domain/Carrier/Command/AddCarrierCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ class AddCarrierCommand
*/
private array $associatedShopIds;

private ?int $position = null;

/**
* @throws CarrierConstraintException
*/
Expand All @@ -55,7 +57,6 @@ public function __construct(
private array $localizedDelay,
private int $grade,
private string $trackingUrl,
private int $position,
private bool $active,
private array $associatedGroupIds,
private bool $hasAdditionalHandlingFee,
Expand Down Expand Up @@ -95,11 +96,16 @@ public function getTrackingUrl(): string
return $this->trackingUrl;
}

public function getPosition(): int
public function getPosition(): ?int
{
return $this->position;
}

public function setPosition(int $position): void
{
$this->position = $position;
}

public function getActive(): bool
{
return $this->active;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,6 @@ public function create(array $data)
$data['general_settings']['localized_delay'],
$data['general_settings']['grade'],
$data['general_settings']['tracking_url'] ?? '',
0, // @todo: should not be in the add command but auto-computed or at least be optional
(bool) $data['general_settings']['active'],
$data['general_settings']['group_access'],
(bool) $data['shipping_settings']['has_additional_handling_fee'],
Expand Down
174 changes: 7 additions & 167 deletions tests/Integration/Behaviour/Features/Context/CarrierFeatureContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,7 @@
use Context;
use Country;
use Exception;
use Group;
use RangePrice;
use RangeWeight;
use RuntimeException;
use State;
use Zone;
Expand Down Expand Up @@ -97,31 +95,11 @@ public function before(BeforeScenarioScope $scope)
$this->customerFeatureContext = $customerFeatureContext;
}

/**
* @Given /^there is a zone named "(.+)"$/
*/
public function createZone($zoneName)
{
$zone = new Zone();
$zone->name = $zoneName;
$zone->add();
$this->zones[$zoneName] = $zone;
}

/**
* @param string $zoneName
*/
public function checkZoneWithNameExists(string $zoneName): void
{
$this->checkFixtureExists($this->zones, 'Zone', $zoneName);
}

/**
* @Given /^there is a country named "(.+)" and iso code "(.+)" in zone "(.+)"$/
*/
public function createCountry($countryName, $isoCode, $zoneName)
{
$this->checkZoneWithNameExists($zoneName);
$countryId = Country::getByIso($isoCode, false);
if (!$countryId) {
throw new Exception('Country not found with iso code = ' . $isoCode);
Expand All @@ -130,7 +108,7 @@ public function createCountry($countryName, $isoCode, $zoneName)
// clone country to be able to properly reset previous data
$this->previousCountries[$countryName] = clone $country;
$this->countries[$countryName] = $country;
$country->id_zone = $this->zones[$zoneName]->id;
$country->id_zone = $this->getSharedStorage()->get($zoneName)->id;
$country->active = true;
$country->save();

Expand Down Expand Up @@ -160,15 +138,15 @@ public function checkCountryWithNameExists(string $countryName): void
*/
public function createState($stateName, $stateIsoCode, $countryName, $zoneName)
{
$this->checkZoneWithNameExists($zoneName);
$this->checkCountryWithNameExists($countryName);
$state = new State();
$state->name = $stateName;
$state->iso_code = $stateIsoCode;
$state->id_zone = $this->zones[$zoneName]->id;
$state->id_country = $this->countries[$countryName]->id;
$state->id_zone = $this->getSharedStorage()->get($zoneName)->id;
$state->id_country = $this->getSharedStorage()->get($countryName);
$state->add();
$this->states[$stateName] = $state;

$this->getSharedStorage()->set($stateName, (int) $state->id);
}

/**
Expand Down Expand Up @@ -229,114 +207,6 @@ public function checkAddressWithNameExists(string $addressName): void
$this->checkFixtureExists($this->addresses, 'Address', $addressName);
}

/**
* @Given /^there is a carrier named "(.+)"$/
*/
public function createCarrier($carrierName)
{
$carrier = new Carrier(null, (int) Configuration::get('PS_LANG_DEFAULT'));
$carrier->name = $carrierName;
$carrier->shipping_method = Carrier::SHIPPING_METHOD_PRICE;
$carrier->delay = '28 days later';
$carrier->active = true;
$carrier->add();
$this->carriers[$carrierName] = $carrier;
SharedStorage::getStorage()->set($carrierName, (int) $carrier->id);

$groups = Group::getGroups(Context::getContext()->language->id);
$groupIds = [];
foreach ($groups as $group) {
$groupIds[] = $group['id_group'];
}
$carrier->setGroups($groupIds);
}

/**
* @Given /^carrier "(.+)" ships to all groups$/
*/
public function setCarrierShipsToAllGroups($carrierName)
{
$this->checkCarrierWithNameExists($carrierName);
$carrier = $this->carriers[$carrierName];

$groups = Group::getGroups(Context::getContext()->language->id);
$groupIds = [];
foreach ($groups as $group) {
$groupIds[] = $group['id_group'];
}
$carrier->setGroups($groupIds);
}

/**
* @Given /^the carrier "(.+)" uses "(.+)" as tracking url$/
*/
public function setCarrierTrackingUrl(string $carrierName, string $url): void
{
$this->checkCarrierWithNameExists($carrierName);
$carrier = $this->carriers[$carrierName];
$carrier->url = $url;
$carrier->save();
}

/**
* @param string $carrierName
*/
public function checkCarrierWithNameExists(string $carrierName): void
{
$this->checkFixtureExists($this->carriers, 'Carrier', $carrierName);
}

/**
* @param string $carrierName
*
* @return Carrier
*/
public function getCarrierWithName(string $carrierName): Carrier
{
return $this->carriers[$carrierName];
}

/**
* Be careful: this method REPLACES shipping fees for carrier
*
* @Given /^carrier "(.+)" applies shipping fees of (\d+\.\d+) in zone "(.+)" for (weight|price) between (\d+) and (\d+)$/
*/
public function setCarrierFees($carrierName, $shippingPrice, $zoneName, $rangeType, $from, $to)
{
$this->checkCarrierWithNameExists($carrierName);
$this->checkZoneWithNameExists($zoneName);
if (empty($this->carriers[$carrierName]->getZone((int) $this->zones[$zoneName]->id))) {
$this->carriers[$carrierName]->addZone((int) $this->zones[$zoneName]->id);
}
$rangeClass = $rangeType == 'weight' ? RangeWeight::class : RangePrice::class;
$primary = $rangeType == 'weight' ? 'id_range_weight' : 'id_range_price';
$rangeRows = $rangeClass::getRanges($this->carriers[$carrierName]->id);
$rangeId = false;
foreach ($rangeRows as $rangeRow) {
if ($rangeRow['delimiter1'] == $from) {
$rangeId = $rangeRow[$primary];
}
}
if (!empty($rangeId)) {
$range = new $rangeClass($rangeId);
} else {
$range = new $rangeClass();
$range->id_carrier = $this->carriers[$carrierName]->id;
$range->delimiter1 = $from;
$range->delimiter2 = $to;
$range->add();
$this->priceRanges[] = $range;
}
$carrierPriceRange = [
'id_range_price' => (int) $range->id,
'id_range_weight' => null,
'id_carrier' => (int) $this->carriers[$carrierName]->id,
'id_zone' => (int) $this->zones[$zoneName]->id,
'price' => $shippingPrice,
];
$this->carriers[$carrierName]->addDeliveryPrice([$carrierPriceRange], true);
}

/**
* @AfterScenario
*/
Expand Down Expand Up @@ -374,10 +244,9 @@ public function cleanFixtures()
/**
* @When /^I select carrier "(.+)" in my cart$/
*/
public function setCartCarrier($carrierName)
public function setCartCarrier(string $carrierReference)
{
$this->checkCarrierWithNameExists($carrierName);
$this->getCurrentCart()->id_carrier = $this->carriers[$carrierName]->id;
$this->getCurrentCart()->id_carrier = $this->getSharedStorage()->get($carrierReference);

$this->getCurrentCart()->update();

Expand Down Expand Up @@ -417,33 +286,4 @@ public function checkExistingCarrier(string $carrierReference, string $carrierNa
$carrierName
));
}

/**
* @Given I enable carrier :carrierReference
*
* @param string $carrierReference
*/
public function enableCarrier(string $carrierReference)
{
$carrierId = SharedStorage::getStorage()->get($carrierReference);
$carrier = new Carrier($carrierId);
$carrier->active = true;
$carrier->save();
// Reset cache so that the carrier becomes selectable
Carrier::resetStaticCache();
}

/**
* @Then I associate the tax rule group :taxRulesGroupReference to carrier :carrierReference
*
* @param string $taxRulesGroupReference
* @param string $carrierReference
*/
public function associateCarrierTaxRulesGroup(string $taxRulesGroupReference, string $carrierReference)
{
$carrierId = SharedStorage::getStorage()->get($carrierReference);
$taxRulesGroupId = SharedStorage::getStorage()->get($taxRulesGroupReference);
$carrier = new Carrier($carrierId);
$carrier->setTaxRulesGroup($taxRulesGroupId);
}
}
Loading

0 comments on commit a39a38d

Please sign in to comment.