-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(checkout): use show delivery options on backorders setting (#1136)
INT-582
- Loading branch information
1 parent
575d640
commit f03b2a1
Showing
6 changed files
with
288 additions
and
13 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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,93 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MyParcelNL\WooCommerce\Tests\Mock; | ||
|
||
use MyParcelNL\Pdk\Base\Support\Arr; | ||
use MyParcelNL\Pdk\Base\Support\Collection; | ||
|
||
final class MockWpEnqueue implements StaticMockInterface | ||
{ | ||
/** | ||
* @var \MyParcelNL\Pdk\Base\Support\Collection | ||
*/ | ||
private static $queuedItems; | ||
|
||
/** | ||
* @param $handle | ||
* @param $src | ||
* @param $deps | ||
* @param $ver | ||
* @param $in_footer | ||
* | ||
* @return void | ||
*/ | ||
public static function add($handle, $src, $deps, $ver, $in_footer): void | ||
{ | ||
$existing = array_filter(Arr::wrap(self::get($handle))); | ||
|
||
self::$queuedItems->put( | ||
$handle, | ||
array_merge($existing, [ | ||
[ | ||
'src' => $src, | ||
'deps' => $deps, | ||
'ver' => $ver, | ||
'in_footer' => $in_footer, | ||
], | ||
]) | ||
); | ||
} | ||
|
||
/** | ||
* @return \MyParcelNL\Pdk\Base\Support\Collection | ||
*/ | ||
public static function all(): Collection | ||
{ | ||
return self::getQueuedItems(); | ||
} | ||
|
||
/** | ||
* @param string $tag | ||
* | ||
* @return array | ||
*/ | ||
public static function get(string $tag): array | ||
{ | ||
return self::getQueuedItems() | ||
->get($tag, []); | ||
} | ||
|
||
public static function reset(): void | ||
{ | ||
self::$queuedItems = new Collection(); | ||
} | ||
|
||
public static function toArray(): array | ||
{ | ||
return self::getQueuedItems() | ||
->map(static function (array $actions) { | ||
return (new Collection(Arr::pluck($actions, 'function')))->map(static function ($function) { | ||
if (! is_array($function)) { | ||
return $function; | ||
} | ||
|
||
return implode('::', [get_class($function[0]), $function[1]]); | ||
}); | ||
}) | ||
->toArray(); | ||
} | ||
|
||
/** | ||
* @return \MyParcelNL\Pdk\Base\Support\Collection | ||
*/ | ||
private static function getQueuedItems(): Collection | ||
{ | ||
if (null === self::$queuedItems) { | ||
self::reset(); | ||
} | ||
|
||
return self::$queuedItems; | ||
} | ||
} |
This file contains 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,83 @@ | ||
<?php | ||
/** @noinspection StaticClosureCanBeUsedInspection */ | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MyParcelNL\WooCommerce\Hooks; | ||
|
||
use MyParcelNL\Pdk\Facade\Pdk; | ||
use MyParcelNL\Pdk\Settings\Model\CheckoutSettings; | ||
use MyParcelNL\WooCommerce\Tests\Mock\MockWpEnqueue; | ||
use MyParcelNL\WooCommerce\Tests\Uses\UsesMockWcPdkInstance; | ||
use WC_Product; | ||
use function MyParcelNL\Pdk\Tests\factory; | ||
use function MyParcelNL\Pdk\Tests\usesShared; | ||
use function MyParcelNL\WooCommerce\Tests\wpFactory; | ||
|
||
usesShared(new UsesMockWcPdkInstance()); | ||
|
||
it( | ||
'enqueues frontend scripts', | ||
function ( | ||
bool $enableDeliveryOptions, | ||
bool $enableDeliveryOptionsWhenNotInStock, | ||
array $productData, | ||
array $expected | ||
) { | ||
factory(CheckoutSettings::class) | ||
->withEnableDeliveryOptions($enableDeliveryOptions) | ||
->withEnableDeliveryOptionsWhenNotInStock($enableDeliveryOptionsWhenNotInStock) | ||
->store(); | ||
|
||
$product = wpFactory(WC_Product::class) | ||
->with($productData) | ||
->make(); | ||
|
||
WC()->cart->add_to_cart($product->get_id(), 2); | ||
|
||
/** @var \MyParcelNL\WooCommerce\Hooks\CheckoutScriptHooks $class */ | ||
$class = Pdk::get(CheckoutScriptHooks::class); | ||
|
||
$class->enqueueFrontendScripts(); | ||
|
||
$all = | ||
MockWpEnqueue::all() | ||
->all(); | ||
|
||
expect($all) | ||
->toHaveKeys($expected['toContain']) | ||
->and($all)->not->toHaveKeys($expected['notToContain']); | ||
|
||
MockWpEnqueue::reset(); | ||
WC()->cart->empty_cart(); | ||
} | ||
) | ||
->with([ | ||
'enable all, in stock' => [ | ||
'enableDeliveryOptions' => true, | ||
'enableDeliveryOptionsWhenNotInStock' => true, | ||
'productData' => ['id' => 1, 'is_on_backorder' => false], | ||
'expected' => [ | ||
'toContain' => ['myparcelnl-delivery-options'], | ||
'notToContain' => [], | ||
], | ||
], | ||
'enable delivery options, on backorder' => [ | ||
'enableDeliveryOptions' => true, | ||
'enableDeliveryOptionsWhenNotInStock' => false, | ||
'productData' => ['id' => 1, 'is_on_backorder' => true], | ||
'expected' => [ | ||
'toContain' => [], | ||
'notToContain' => ['myparcelnl-delivery-options'], | ||
], | ||
], | ||
'enable all, on backorder' => [ | ||
'enableDeliveryOptions' => true, | ||
'enableDeliveryOptionsWhenNotInStock' => true, | ||
'productData' => ['id' => 1, 'is_on_backorder' => true], | ||
'expected' => [ | ||
'toContain' => ['myparcelnl-delivery-options'], | ||
'notToContain' => [], | ||
], | ||
], | ||
]); |
This file contains 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
This file contains 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