-
Notifications
You must be signed in to change notification settings - Fork 7
fix(insurance): refresh stored carrier data so insurance keeps working #624
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
FreekVR
wants to merge
5
commits into
main
Choose a base branch
from
fix/INT-1699-refresh-carrier-capabilities
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
edd073a
test: stop unit tests reaching the live api
FreekVR 290403c
fix(insurance): refresh stored carrier data so insurance keeps working
FreekVR 0732273
fix(insurance): log the full exception when the carrier refresh fails
FreekVR 7438681
fix(migration): keep the shop up when the carrier refresh cannot reac…
FreekVR ff95a69
build(deps): require pdk 4.7.0 for markFailed()
FreekVR File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
55 changes: 55 additions & 0 deletions
55
src/Migration/2026_07_29_113506_refresh_carrier_capabilities.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| use MyParcelNL\Pdk\App\Account\Contract\PdkAccountRepositoryInterface; | ||
| use MyParcelNL\Pdk\App\Installer\Migration\AbstractTimestampedMigration; | ||
| use MyParcelNL\Pdk\Carrier\Repository\CarrierCapabilitiesRepository; | ||
| use MyParcelNL\Pdk\Facade\Logger; | ||
| use MyParcelNL\Pdk\Facade\Pdk; | ||
|
|
||
| /** | ||
| * Re-fetches the stored carrier data so insurance limits are in the flat format. | ||
| * | ||
| * Carrier data stored before this release holds insurance limits in the nested wrapper the | ||
| * MyParcel API is removing. The PDK now reads the flat limits, which are absent from that older | ||
| * data, so insurance would be unavailable until something refreshed it. Fetching the contract | ||
| * definitions again rewrites the stored carriers in the shape the PDK expects. | ||
| */ | ||
| return new class extends AbstractTimestampedMigration { | ||
| public function up(): void | ||
| { | ||
| /** @var PdkAccountRepositoryInterface $accountRepository */ | ||
| $accountRepository = Pdk::get(PdkAccountRepositoryInterface::class); | ||
| $account = $accountRepository->getAccount(true); | ||
| // PHPStan types Account::$shops as a non-null ShopCollection, but the guard is kept | ||
| // intentionally to stay safe against partial/corrupted account data during upgrade. | ||
| $shop = $account && $account->shops ? $account->shops->first() : null; | ||
|
Comment on lines
+22
to
+27
|
||
|
|
||
| if (! $shop) { | ||
| Logger::debug('No account or shop available; skipping carrier capabilities refresh.'); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| /** @var CarrierCapabilitiesRepository $capabilitiesRepository */ | ||
| $capabilitiesRepository = Pdk::get(CarrierCapabilitiesRepository::class); | ||
|
|
||
| try { | ||
| $shop->carriers = $capabilitiesRepository->getContractDefinitions(); | ||
|
FreekVR marked this conversation as resolved.
|
||
| } catch (Throwable $exception) { | ||
| // Reporting failure leaves the migration unrecorded, so it is attempted again on the | ||
| // next load. Throwing would do that too, but it would take the page down with it. | ||
| $this->markFailed('Failed to refresh carrier capabilities.', [ | ||
| 'message' => $exception->getMessage(), | ||
| 'file' => $exception->getFile() . ':' . $exception->getLine(), | ||
| 'class' => get_class($exception), | ||
| 'trace' => $exception->getTraceAsString(), | ||
| ]); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| $accountRepository->store($account); | ||
| } | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
146 changes: 146 additions & 0 deletions
146
tests/Unit/Migration/RefreshCarrierCapabilitiesMigrationTest.php
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,146 @@ | ||
| <?php | ||
|
|
||
| /** @noinspection PhpUnhandledExceptionInspection,StaticClosureCanBeUsedInspection */ | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| namespace MyParcelNL\PrestaShop\Migration; | ||
|
|
||
| use MyParcelNL\Pdk\App\Account\Contract\PdkAccountRepositoryInterface; | ||
| use MyParcelNL\Pdk\App\Installer\Contract\TimestampedMigrationInterface; | ||
| use MyParcelNL\Pdk\Carrier\Collection\CarrierCollection; | ||
| use MyParcelNL\Pdk\Carrier\Repository\CarrierCapabilitiesRepository; | ||
| use MyParcelNL\Pdk\Facade\Pdk; | ||
| use MyParcelNL\Pdk\SdkApi\Service\CoreApi\Shipment\CapabilitiesService; | ||
| use MyParcelNL\Pdk\Storage\Contract\StorageInterface; | ||
| use MyParcelNL\Pdk\Tests\Api\Response\ExampleGetAccountsResponse; | ||
| use MyParcelNL\Pdk\Tests\Bootstrap\MockApi; | ||
| use MyParcelNL\Pdk\Tests\Bootstrap\TestBootstrapper; | ||
| use MyParcelNL\Pdk\Tests\SdkApi\MockSdkApiHandler; | ||
| use MyParcelNL\Pdk\Tests\SdkApi\Response\ExampleContractDefinitionsResponse; | ||
| use MyParcelNL\PrestaShop\Tests\Uses\UsesMockPsPdkInstance; | ||
| use RuntimeException; | ||
| use function MyParcelNL\Pdk\Tests\mockPdkProperties; | ||
| use function MyParcelNL\Pdk\Tests\usesShared; | ||
|
|
||
| usesShared(new UsesMockPsPdkInstance()); | ||
|
|
||
| /** | ||
| * Loads the migration the same way the installer does: require the file and take the | ||
| * returned anonymous-class instance. | ||
| */ | ||
| function loadRefreshCarrierCapabilitiesMigration(): TimestampedMigrationInterface | ||
| { | ||
| return require __DIR__ . '/../../../src/Migration/2026_07_29_113506_refresh_carrier_capabilities.php'; | ||
| } | ||
|
|
||
| it('is a timestamped migration the installer can discover', function () { | ||
| $migration = loadRefreshCarrierCapabilitiesMigration(); | ||
|
|
||
| // The installer injects identity from the filename, so the migration must accept it | ||
| // and report it back rather than deriving one itself. | ||
| $migration->setIdentity('2026_07_29_113506_refresh_carrier_capabilities'); | ||
|
|
||
| expect($migration)->toBeInstanceOf(TimestampedMigrationInterface::class) | ||
| ->and($migration->getId())->toBe('2026_07_29_113506_refresh_carrier_capabilities'); | ||
| }); | ||
|
|
||
| it('skips without failing when no account or shop is available', function () { | ||
| /** @var PdkAccountRepositoryInterface $accountRepo */ | ||
| $accountRepo = Pdk::get(PdkAccountRepositoryInterface::class); | ||
|
|
||
| // No account configured, so a forced refresh returns null. Skipping beats fataling: | ||
| // a fresh install has nothing to refresh. | ||
| loadRefreshCarrierCapabilitiesMigration()->up(); | ||
|
|
||
| expect($accountRepo->getAccount())->toBeNull(); | ||
| }); | ||
|
|
||
| it('reports failure instead of throwing when fetching carrier definitions fails', function () { | ||
| TestBootstrapper::hasAccount(); | ||
| // The migration forces an account refresh before it gets as far as the carriers. Without a | ||
| // response queued that call throws, and the test would pass on the wrong exception. | ||
| MockApi::enqueue(new ExampleGetAccountsResponse()); | ||
|
|
||
| $throwingRepo = new class( | ||
| Pdk::get(StorageInterface::class), | ||
| Pdk::get(CapabilitiesService::class) | ||
| ) extends CarrierCapabilitiesRepository { | ||
| public function getContractDefinitions(?string $carrier = null): CarrierCollection | ||
| { | ||
| throw new RuntimeException('API unavailable'); | ||
| } | ||
| }; | ||
|
|
||
| mockPdkProperties([CarrierCapabilitiesRepository::class => $throwingRepo]); | ||
|
|
||
| $migration = loadRefreshCarrierCapabilitiesMigration(); | ||
| $migration->up(); | ||
|
|
||
| // Reporting failure keeps the migration out of applied_migrations, so it is attempted again | ||
| // on the next load. Reaching this line at all is the other half of the point: a carrier API | ||
| // that is briefly unavailable no longer takes the page down with it. | ||
| expect($migration->hasFailed())->toBeTrue(); | ||
| }); | ||
|
|
||
| dataset('insurance shapes from the api', [ | ||
| // What the API sends today. The nested wrapper carries different amounts, so if the wrong | ||
| // set of limits ever survived, the assertions below would catch it. | ||
| 'flat limits alongside the deprecated nested wrapper' => [ | ||
| [ | ||
| 'min' => ['amount' => 0, 'currency' => 'EUR'], | ||
| 'max' => ['amount' => 500_000, 'currency' => 'EUR'], | ||
| 'default' => ['amount' => 0, 'currency' => 'EUR'], | ||
| 'insuredAmount' => [ | ||
| 'min' => ['amount' => 1, 'currency' => 'EUR'], | ||
| 'max' => ['amount' => 2, 'currency' => 'EUR'], | ||
| 'default' => ['amount' => 3, 'currency' => 'EUR'], | ||
| ], | ||
| ], | ||
| ], | ||
| // What the API sends once the nested wrapper is removed. | ||
| 'flat limits only' => [ | ||
| [ | ||
| 'min' => ['amount' => 0, 'currency' => 'EUR'], | ||
| 'max' => ['amount' => 500_000, 'currency' => 'EUR'], | ||
| 'default' => ['amount' => 0, 'currency' => 'EUR'], | ||
| ], | ||
| ], | ||
| ]); | ||
|
|
||
| it('stores only the flat insurance limits', function (array $insurance) { | ||
| TestBootstrapper::hasAccount(); | ||
| // The migration forces an account refresh, which calls the accounts endpoint. | ||
| MockApi::enqueue(new ExampleGetAccountsResponse()); | ||
| // Goes through the real CapabilitiesService and repository, so the nested wrapper is | ||
| // dropped by the code that actually does it rather than by a stub. | ||
| MockSdkApiHandler::enqueue(new ExampleContractDefinitionsResponse([ | ||
| [ | ||
| 'carrier' => 'POSTNL', | ||
| 'packageTypes' => ['PACKAGE'], | ||
| 'deliveryTypes' => ['STANDARD_DELIVERY'], | ||
| 'transactionTypes' => ['B2C'], | ||
| 'options' => [ | ||
| 'insurance' => array_merge( | ||
| ['isSelectedByDefault' => false, 'isRequired' => false], | ||
| $insurance | ||
| ), | ||
| ], | ||
| ], | ||
| ])); | ||
|
|
||
| loadRefreshCarrierCapabilitiesMigration()->up(); | ||
|
|
||
| /** @var PdkAccountRepositoryInterface $accountRepo */ | ||
| $accountRepo = Pdk::get(PdkAccountRepositoryInterface::class); | ||
| $carrier = $accountRepo->getAccount() | ||
| ->shops->first() | ||
| ->carriers->firstWhere('carrier', 'POSTNL'); | ||
| $stored = $carrier->options->getInsurance(); | ||
|
|
||
| // Same stored result either way: the flat limits, and no nested wrapper left behind. | ||
| expect($stored->getInsuredAmount())->toBeNull() | ||
| ->and($stored->getMin()->getAmount())->toBe(0) | ||
| ->and($stored->getMax()->getAmount())->toBe(500_000) | ||
| ->and($stored->getDefault()->getAmount())->toBe(0); | ||
|
Comment on lines
+141
to
+145
|
||
| })->with('insurance shapes from the api'); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.