Skip to content

Commit

Permalink
[6.x] Re-do the UpdateClassReferences update script (#985)
Browse files Browse the repository at this point in the history
* Re-do the UpdateClassReferences update script

* wip

* Fix styling

---------

Co-authored-by: duncanmcclean <[email protected]>
  • Loading branch information
duncanmcclean and duncanmcclean authored Feb 3, 2024
1 parent f267354 commit 355e424
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 22 deletions.
64 changes: 49 additions & 15 deletions src/UpdateScripts/v6_0/UpdateClassReferences.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

namespace DoubleThreeDigital\SimpleCommerce\UpdateScripts\v6_0;

use DoubleThreeDigital\SimpleCommerce\Contracts\Order as OrderContract;
use DoubleThreeDigital\SimpleCommerce\Facades\Order;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
use Illuminate\Support\Str;
use Statamic\UpdateScripts\UpdateScript;

class UpdateClassReferences extends UpdateScript
Expand All @@ -14,24 +18,54 @@ public function shouldUpdate($newVersion, $oldVersion)

public function update()
{
Order::query()
->where('gateway', '!=', null)
->chunk(50, function ($orders) {
$orders->each(function ($order) {
// When the gateway reference is still a class, change it to the handle.
if ($order->gateway() && class_exists($order->gateway()['use'])) {
$order->gateway(array_merge($order->gateway(), [
'use' => $order->gateway()['use']::handle(),
]));

$order->save();
$this
->updateReferencesToGateways()
->updateReferencesToShippingMethods();
}

protected function updateReferencesToGateways(): self
{
Order::query()->whereNotNull('gateway')->chunk(100, function (Collection $orders) {
$orders
->filter(fn (OrderContract $order) => str_contains(Arr::get($order->gateway, 'use'), '\\'))
->each(function (OrderContract $order) {
$class = Arr::get($order->gateway, 'use');

// Adjust the class name before new'ing it up since the namespace has changed.
if (Str::startsWith($class, 'DoubleThreeDigital')) {
// $class = str_replace('DoubleThreeDigital', 'DuncanMcClean', $class);
}

// When the shipping method reference is still a class, change it to the handle.
if ($order->has('shipping_method') && class_exists($order->get('shipping_method'))) {
$order->set('shipping_method', $order->get('shipping_method')::handle())->saveQuietly();
$handle = $class::handle();

$order->gatewayData(gateway: $handle);
$order->save();
});
});

return $this;
}

protected function updateReferencesToShippingMethods(): self
{
Order::query()->whereNotNull('shipping_method')->chunk(100, function (Collection $orders) {
$orders
->filter(fn (OrderContract $order) => str_contains($order->get('shipping_method'), '\\'))
->each(function (OrderContract $order) {
$class = $order->get('shipping_method');

// Adjust the class name before new'ing it up since the namespace has changed.
if (Str::startsWith($class, 'DoubleThreeDigital')) {
// $class = str_replace('DoubleThreeDigital', 'DuncanMcClean', $class);
}

$handle = $class::handle();

$order->set('shipping_method', $handle);
$order->save();
});
});
});

return $this;
}
}
4 changes: 2 additions & 2 deletions tests/Pest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@
use Statamic\Statamic;

uses(TestCase::class)
->in('Actions', 'Console', 'Coupons', 'Customers', 'Data', 'Fieldtypes', '__fixtures__', 'Gateways', 'Helpers', 'Http', 'Listeners', 'Modifiers', 'Orders', 'Products', 'Rules', 'Tags', 'Tax');
->in('Actions', 'Console', 'Coupons', 'Customers', 'Data', 'Fieldtypes', '__fixtures__', 'Gateways', 'Helpers', 'Http', 'Listeners', 'Modifiers', 'Orders', 'Products', 'Rules', 'Tags', 'Tax', 'UpdateScripts');

uses(
SetupCollections::class,
RefreshContent::class
)->in('Actions', 'Coupons', 'Customers', 'Listeners', 'Products');
)->in('Actions', 'Coupons', 'Customers', 'Listeners', 'Products', 'UpdateScripts');

/*
|--------------------------------------------------------------------------
Expand Down
5 changes: 0 additions & 5 deletions tests/Products/EntryProductRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@
use DoubleThreeDigital\SimpleCommerce\Contracts\Product as ProductContract;
use DoubleThreeDigital\SimpleCommerce\Facades\Product;
use DoubleThreeDigital\SimpleCommerce\Products\EntryQueryBuilder;
use Statamic\Facades\Collection;

afterEach(function () {
Collection::find('products')->queryEntries()->get()->each->delete();
});

it('can get all products', function () {
Product::make()->id('one')->price(1500)->save();
Expand Down
38 changes: 38 additions & 0 deletions tests/UpdateScripts/UpdateClassReferencesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php

use DoubleThreeDigital\SimpleCommerce\UpdateScripts\v6_0\UpdateClassReferences;
use Statamic\Facades\Entry;

it('updates reference to gateway class', function () {
$orderEntry = Entry::make()
->collection('orders')
->id('test')
->data(['gateway' => ['use' => 'DoubleThreeDigital\SimpleCommerce\Gateways\Builtin\StripeGateway', 'data' => ['id' => 'pi_1234']]]);

$orderEntry->save();

(new UpdateClassReferences('doublethreedigital/simple-commerce', '6.0.0'))->update();

$orderEntry->fresh();

expect($orderEntry->get('gateway'))->toBe([
'use' => 'stripe',
'data' => ['id' => 'pi_1234'],
'refund' => null,
]);
});

it('updates reference to shipping method class', function () {
$orderEntry = Entry::make()
->collection('orders')
->id('test')
->data(['shipping_method' => 'DoubleThreeDigital\SimpleCommerce\Shipping\FreeShipping']);

$orderEntry->save();

(new UpdateClassReferences('doublethreedigital/simple-commerce', '6.0.0'))->update();

$orderEntry->fresh();

expect($orderEntry->get('shipping_method'))->toBe('free_shipping');
});

0 comments on commit 355e424

Please sign in to comment.