Skip to content

Commit

Permalink
chore(carrier): update and add behats tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Nakahiru committed Sep 4, 2024
1 parent 68b96d0 commit 57474ba
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 15 deletions.
13 changes: 13 additions & 0 deletions src/Adapter/Carrier/CommandHandler/AddCarrierHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
use PrestaShop\PrestaShop\Core\CommandBus\Attributes\AsCommandHandler;
use PrestaShop\PrestaShop\Core\Domain\Carrier\Command\AddCarrierCommand;
use PrestaShop\PrestaShop\Core\Domain\Carrier\CommandHandler\AddCarrierHandlerInterface;
use PrestaShop\PrestaShop\Core\Domain\Carrier\Exception\CarrierConstraintException;
use PrestaShop\PrestaShop\Core\Domain\Carrier\ValueObject\CarrierId;

/**
Expand All @@ -57,6 +58,8 @@ public function __construct(
*/
public function handle(AddCarrierCommand $command): CarrierId
{
$this->assertZonesExist($command->getZones());

$carrier = new Carrier();
// General information
$carrier->name = $command->getName();
Expand Down Expand Up @@ -109,4 +112,14 @@ public function handle(AddCarrierCommand $command): CarrierId

return $carrierId;
}

private function assertZonesExist(array $zoneIds): void
{
if (null === $zoneIds || count($zoneIds) === 0) {

Check failure on line 118 in src/Adapter/Carrier/CommandHandler/AddCarrierHandler.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.1)

Strict comparison using === between null and array will always evaluate to false.

Check failure on line 118 in src/Adapter/Carrier/CommandHandler/AddCarrierHandler.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.2)

Strict comparison using === between null and array will always evaluate to false.

Check failure on line 118 in src/Adapter/Carrier/CommandHandler/AddCarrierHandler.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.3)

Strict comparison using === between null and array will always evaluate to false.
throw new CarrierConstraintException(
'Carrier need to have at least one zone',
CarrierConstraintException::INVALID_ZONE_MISSING
);
}
}
}
8 changes: 8 additions & 0 deletions src/Adapter/Carrier/CommandHandler/EditCarrierHandler.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
use PrestaShop\PrestaShop\Core\Domain\Carrier\CommandHandler\EditCarrierHandlerInterface;
use PrestaShop\PrestaShop\Core\Domain\Carrier\Exception\CannotUpdateCarrierException;
use PrestaShop\PrestaShop\Core\Domain\Carrier\ValueObject\CarrierId;
use PrestaShop\PrestaShop\Core\Domain\Carrier\Exception\CarrierConstraintException;
use PrestaShop\PrestaShop\Core\Domain\Shop\ValueObject\ShopId;

/**
Expand Down Expand Up @@ -159,6 +160,13 @@ public function handle(EditCarrierCommand $command): CarrierId
}
}

if (null === $command->getZones() || count($command->getZones()) === 0) {
throw new CarrierConstraintException(
'Carrier need to have at least one zone',
CarrierConstraintException::INVALID_ZONE_MISSING
);
}

$this->carrierRepository->updateAssociatedZones($newCarrierId, $command->getZones());

return $newCarrierId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,4 +137,9 @@ class CarrierConstraintException extends CarrierException
* Thrown when shop constraint isn't valid
*/
public const INVALID_SHOP_CONSTRAINT = 200;

/**
* Thrown when carrier is save without at least one zone
*/
public const INVALID_ZONE_MISSING = 210;
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ public static function restoreCarrierTablesAfterSuite(): void
public function createCarrier(string $reference, TableNode $node): void
{
$properties = $this->localizeByRows($node);

try {
if (isset($properties['logoPathName']) && 'null' !== $properties['logoPathName']) {
$tmpLogo = DummyFileUploader::upload($properties['logoPathName']);
Expand All @@ -81,6 +82,16 @@ public function createCarrier(string $reference, TableNode $node): void
$delay = [$this->getDefaultLangId() => 'Shipping delay'];
}

if (!empty($properties['zones'])) {
$getZone = $this->referencesToIds($properties['zones']);
$zones = [];
foreach($getZone as $zoneId) {
array_push($zones, $zoneId->id);

Check failure on line 89 in tests/Integration/Behaviour/Features/Context/Domain/Carrier/CarrierFeatureContext.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.1)

Cannot access property $id on int.

Check failure on line 89 in tests/Integration/Behaviour/Features/Context/Domain/Carrier/CarrierFeatureContext.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.2)

Cannot access property $id on int.

Check failure on line 89 in tests/Integration/Behaviour/Features/Context/Domain/Carrier/CarrierFeatureContext.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.3)

Cannot access property $id on int.
}
} else {
$zones = [];
}

if (!empty($properties['group_access'])) {
$groupIds = $this->referencesToIds($properties['group_access']);
} else {
Expand All @@ -103,6 +114,7 @@ public function createCarrier(string $reference, TableNode $node): void
$properties['shippingMethod'] ?? 'price',
$properties['rangeBehavior'] ?? 'highest_range',
$properties['logoPathName'] ?? null,
$zones,
$associatedShops,
isset($properties['position']) ? (int) $properties['position'] : null,
);
Expand Down Expand Up @@ -182,7 +194,6 @@ private function editCarrier(string $reference, ?string $newReference, TableNode
$carrierId = $this->referenceToId($reference);

$command = new EditCarrierCommand($carrierId);

// General information
if (isset($properties['name'])) {
$command->setName($properties['name']);
Expand Down Expand Up @@ -217,6 +228,18 @@ private function editCarrier(string $reference, ?string $newReference, TableNode
if (isset($properties['group_access'])) {
$command->setAssociatedGroupIds($this->referencesToIds($properties['group_access']));
}

if (isset($properties['zones'])) {
$getZone = $this->referencesToIds($properties['zones']);
$zones = [];
foreach($getZone as $zoneId) {
array_push($zones, $zoneId->id);

Check failure on line 236 in tests/Integration/Behaviour/Features/Context/Domain/Carrier/CarrierFeatureContext.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.1)

Cannot access property $id on int.

Check failure on line 236 in tests/Integration/Behaviour/Features/Context/Domain/Carrier/CarrierFeatureContext.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.2)

Cannot access property $id on int.

Check failure on line 236 in tests/Integration/Behaviour/Features/Context/Domain/Carrier/CarrierFeatureContext.php

View workflow job for this annotation

GitHub Actions / PHP Static Analysis (8.3)

Cannot access property $id on int.
}
$command->setZones($zones);
} else {
$command->setZones([]);
}

if (isset($properties['associatedShops'])) {
$command->setAssociatedShopIds($this->referencesToIds($properties['associatedShops']));
}
Expand Down Expand Up @@ -388,6 +411,17 @@ public function carrierEditShouldThrowAnError(string $errorCode)
);
}

/**
* @Then carrier create should throw an error with error code :errorCode
*/
public function carrierCreateShouldThrowAnError(string $errorCode)
{
$this->assertLastErrorIs(
CarrierConstraintException::class,
constant(CarrierConstraintException::class . '::' . $errorCode)
);
}

private function createCarrierUsingCommand(
string $name,
array $delay,
Expand All @@ -404,6 +438,7 @@ private function createCarrierUsingCommand(
string $shippingMethod,
string $rangeBehavior,
?string $logoPathName,
array $zones,
array $associatedShops,
?int $position,
): CarrierId {
Expand All @@ -418,6 +453,7 @@ private function createCarrierUsingCommand(
$isFree,
$this->convertShippingMethodToInt($shippingMethod),
$this->convertOutOfRangeBehaviorToInt($rangeBehavior),
$zones,
$associatedShops,
$max_width,
$max_height,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,20 +148,6 @@ private function getCarrierRanges(string $reference, ShopConstraint $shopConstra
$rangesExpected = new CarrierRangesCollection($data);

Assert::assertEquals($rangesExpected, $rangesDatabase);

// Automatically checks that the carrier_zone association is properly set
$query = new DbQuery();
$query->select('(cz.id_zone)');
$query->from('carrier_zone', 'cz');
$query->where('id_carrier = \'' . pSQL((string) $carrierId) . '\'');
$zonesFromDB = Db::getInstance(_PS_USE_SQL_SLAVE_)->executeS($query->build());

$zoneIds = array_unique($zoneIds);
sort($zoneIds);
$zonesFromDB = array_map(fn ($row) => $row['id_zone'], $zonesFromDB);
sort($zonesFromDB);

Assert::assertEquals($zoneIds, $zonesFromDB);
} catch (CarrierException $e) {
$this->setLastException($e);
}
Expand Down
Loading

0 comments on commit 57474ba

Please sign in to comment.