Skip to content

Commit 355e424

Browse files
[6.x] Re-do the UpdateClassReferences update script (#985)
* Re-do the UpdateClassReferences update script * wip * Fix styling --------- Co-authored-by: duncanmcclean <duncanmcclean@users.noreply.github.com>
1 parent f267354 commit 355e424

File tree

4 files changed

+89
-22
lines changed

4 files changed

+89
-22
lines changed

src/UpdateScripts/v6_0/UpdateClassReferences.php

+49-15
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33
namespace DoubleThreeDigital\SimpleCommerce\UpdateScripts\v6_0;
44

5+
use DoubleThreeDigital\SimpleCommerce\Contracts\Order as OrderContract;
56
use DoubleThreeDigital\SimpleCommerce\Facades\Order;
7+
use Illuminate\Support\Arr;
8+
use Illuminate\Support\Collection;
9+
use Illuminate\Support\Str;
610
use Statamic\UpdateScripts\UpdateScript;
711

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

1519
public function update()
1620
{
17-
Order::query()
18-
->where('gateway', '!=', null)
19-
->chunk(50, function ($orders) {
20-
$orders->each(function ($order) {
21-
// When the gateway reference is still a class, change it to the handle.
22-
if ($order->gateway() && class_exists($order->gateway()['use'])) {
23-
$order->gateway(array_merge($order->gateway(), [
24-
'use' => $order->gateway()['use']::handle(),
25-
]));
26-
27-
$order->save();
21+
$this
22+
->updateReferencesToGateways()
23+
->updateReferencesToShippingMethods();
24+
}
25+
26+
protected function updateReferencesToGateways(): self
27+
{
28+
Order::query()->whereNotNull('gateway')->chunk(100, function (Collection $orders) {
29+
$orders
30+
->filter(fn (OrderContract $order) => str_contains(Arr::get($order->gateway, 'use'), '\\'))
31+
->each(function (OrderContract $order) {
32+
$class = Arr::get($order->gateway, 'use');
33+
34+
// Adjust the class name before new'ing it up since the namespace has changed.
35+
if (Str::startsWith($class, 'DoubleThreeDigital')) {
36+
// $class = str_replace('DoubleThreeDigital', 'DuncanMcClean', $class);
2837
}
2938

30-
// When the shipping method reference is still a class, change it to the handle.
31-
if ($order->has('shipping_method') && class_exists($order->get('shipping_method'))) {
32-
$order->set('shipping_method', $order->get('shipping_method')::handle())->saveQuietly();
39+
$handle = $class::handle();
40+
41+
$order->gatewayData(gateway: $handle);
42+
$order->save();
43+
});
44+
});
45+
46+
return $this;
47+
}
48+
49+
protected function updateReferencesToShippingMethods(): self
50+
{
51+
Order::query()->whereNotNull('shipping_method')->chunk(100, function (Collection $orders) {
52+
$orders
53+
->filter(fn (OrderContract $order) => str_contains($order->get('shipping_method'), '\\'))
54+
->each(function (OrderContract $order) {
55+
$class = $order->get('shipping_method');
56+
57+
// Adjust the class name before new'ing it up since the namespace has changed.
58+
if (Str::startsWith($class, 'DoubleThreeDigital')) {
59+
// $class = str_replace('DoubleThreeDigital', 'DuncanMcClean', $class);
3360
}
61+
62+
$handle = $class::handle();
63+
64+
$order->set('shipping_method', $handle);
65+
$order->save();
3466
});
35-
});
67+
});
68+
69+
return $this;
3670
}
3771
}

tests/Pest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,12 @@
1414
use Statamic\Statamic;
1515

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

1919
uses(
2020
SetupCollections::class,
2121
RefreshContent::class
22-
)->in('Actions', 'Coupons', 'Customers', 'Listeners', 'Products');
22+
)->in('Actions', 'Coupons', 'Customers', 'Listeners', 'Products', 'UpdateScripts');
2323

2424
/*
2525
|--------------------------------------------------------------------------

tests/Products/EntryProductRepositoryTest.php

-5
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,6 @@
33
use DoubleThreeDigital\SimpleCommerce\Contracts\Product as ProductContract;
44
use DoubleThreeDigital\SimpleCommerce\Facades\Product;
55
use DoubleThreeDigital\SimpleCommerce\Products\EntryQueryBuilder;
6-
use Statamic\Facades\Collection;
7-
8-
afterEach(function () {
9-
Collection::find('products')->queryEntries()->get()->each->delete();
10-
});
116

127
it('can get all products', function () {
138
Product::make()->id('one')->price(1500)->save();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php
2+
3+
use DoubleThreeDigital\SimpleCommerce\UpdateScripts\v6_0\UpdateClassReferences;
4+
use Statamic\Facades\Entry;
5+
6+
it('updates reference to gateway class', function () {
7+
$orderEntry = Entry::make()
8+
->collection('orders')
9+
->id('test')
10+
->data(['gateway' => ['use' => 'DoubleThreeDigital\SimpleCommerce\Gateways\Builtin\StripeGateway', 'data' => ['id' => 'pi_1234']]]);
11+
12+
$orderEntry->save();
13+
14+
(new UpdateClassReferences('doublethreedigital/simple-commerce', '6.0.0'))->update();
15+
16+
$orderEntry->fresh();
17+
18+
expect($orderEntry->get('gateway'))->toBe([
19+
'use' => 'stripe',
20+
'data' => ['id' => 'pi_1234'],
21+
'refund' => null,
22+
]);
23+
});
24+
25+
it('updates reference to shipping method class', function () {
26+
$orderEntry = Entry::make()
27+
->collection('orders')
28+
->id('test')
29+
->data(['shipping_method' => 'DoubleThreeDigital\SimpleCommerce\Shipping\FreeShipping']);
30+
31+
$orderEntry->save();
32+
33+
(new UpdateClassReferences('doublethreedigital/simple-commerce', '6.0.0'))->update();
34+
35+
$orderEntry->fresh();
36+
37+
expect($orderEntry->get('shipping_method'))->toBe('free_shipping');
38+
});

0 commit comments

Comments
 (0)