Skip to content

Commit

Permalink
fix(cronservice): prevent webhook callback never being executed
Browse files Browse the repository at this point in the history
INT-572
  • Loading branch information
joerivanveen committed Oct 28, 2024
1 parent 65dbf8f commit e7b5e39
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 9 deletions.
24 changes: 20 additions & 4 deletions src/Service/WpCronService.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,28 @@ public function dispatch($callback, ...$args): void
*/
public function schedule($callback, int $timestamp, ...$args): void
{
if (! is_string($callback) && ! is_array($callback)) {
throw new InvalidArgumentException('Invalid callback');
}

$hook = $callback;

/**
* TODO
* Temporarily execute webhooks, passed as a pseudo-instantiated class by the pdk,
* immediately until the cronservice in wp is fixed (ie it works 100% of the time)
*/
if (is_array($callback)) {
[$class, $method] = $callback;
$instance = Pdk::get(get_class($class));

if (method_exists($instance, $method)) {
$instance->{$method}(...$args);

return;
}
}

if (is_callable($callback)) {
$hook = md5(uniqid('', true));

Expand All @@ -51,10 +71,6 @@ public function schedule($callback, int $timestamp, ...$args): void
*/
private function getActions($callback, $hook)
{
if (! is_string($callback) && ! is_array($callback)) {
throw new InvalidArgumentException('Invalid callback');
}

$callable = $callback;

if (is_array($callback)) {
Expand Down
5 changes: 0 additions & 5 deletions tests/Datasets/callbacks.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,8 @@

declare(strict_types=1);

use MyParcelNL\WooCommerce\Tests\Mock\MockCallableClass;

dataset('callbacks', function () {
$mockClass = new MockCallableClass();

return [
'instantiated class callback' => [[$mockClass, 'mock']],
'static class callback' => ['MyParcelNL\WooCommerce\Tests\Mock\MockCallableClass::mock'],
'function callback' => '\MyParcelNL\WooCommerce\Tests\mockFunction',
];
Expand Down
5 changes: 5 additions & 0 deletions tests/Mock/MockCallableClass.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,9 @@ public function mock(): string
{
return 'mocked';
}

public function updateOption(string $option, $value): void
{
update_option($option, $value);
}
}
7 changes: 7 additions & 0 deletions tests/Unit/Service/WpCronServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use InvalidArgumentException;
use MyParcelNL\Pdk\Base\Contract\CronServiceInterface;
use MyParcelNL\Pdk\Facade\Pdk;
use MyParcelNL\WooCommerce\Tests\Mock\MockCallableClass;
use MyParcelNL\WooCommerce\Tests\Mock\WordPressScheduledTasks;
use MyParcelNL\WooCommerce\Tests\Uses\UsesMockWcPdkInstance;
use function MyParcelNL\Pdk\Tests\usesShared;
Expand Down Expand Up @@ -73,6 +74,12 @@
update_option(Pdk::get('webhookAddActions'), $actions);
})->with('callbacks');

it('executes instantiated class directly', function () {
$cronService = Pdk::get(CronServiceInterface::class);
$cronService->schedule([new MockCallableClass(), 'updateOption'], 2, 'arg1', 'arg2');
expect(get_option('arg1', []))->toBe('arg2');
});

it('throws exception when input is not a string or array', function () {
/** @var \MyParcelNL\Pdk\Base\Contract\CronServiceInterface $cronService */
$cronService = Pdk::get(CronServiceInterface::class);
Expand Down

0 comments on commit e7b5e39

Please sign in to comment.