-
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(webhooks): fix webhooks not working
INT-591
- Loading branch information
1 parent
57f0a32
commit 08bd614
Showing
13 changed files
with
419 additions
and
47 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
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,10 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MyParcelNL\WooCommerce\Tests\Mock; | ||
|
||
class MockWpClass extends BaseMock | ||
{ | ||
use MocksGettersAndSetters; | ||
} |
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,36 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MyParcelNL\WooCommerce\Tests\Mock; | ||
|
||
/** | ||
* @extends \WP_REST_Request | ||
* @see \WP_REST_Request | ||
*/ | ||
class MockWpRestRequest extends BaseMock | ||
{ | ||
use MocksGettersAndSetters; | ||
|
||
/** | ||
* @param string $method | ||
* @param string $route | ||
* @param array $attributes | ||
*/ | ||
public function __construct(string $method = '', string $route = '', array $attributes = []) | ||
{ | ||
$this->fill([ | ||
'route' => $route, | ||
'method' => $method, | ||
'attributes' => $attributes, | ||
'params' => [], | ||
'headers' => [], | ||
'file_params' => [], | ||
]); | ||
} | ||
|
||
public function get_attributes(): array | ||
{ | ||
return $this->attributes; | ||
} | ||
} |
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,28 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MyParcelNL\WooCommerce\Tests\Mock; | ||
|
||
/** | ||
* @extends \WP_REST_Response | ||
* @see \WP_REST_Response | ||
*/ | ||
class MockWpRestResponse extends BaseMock | ||
{ | ||
use MocksGettersAndSetters; | ||
|
||
/** | ||
* @param $data | ||
* @param int $status | ||
* @param array $headers | ||
*/ | ||
public function __construct($data = null, int $status = 200, array $headers = []) | ||
{ | ||
$this->fill([ | ||
'data' => $data, | ||
'status' => $status, | ||
'headers' => $headers, | ||
]); | ||
} | ||
} |
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,55 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MyParcelNL\WooCommerce\Tests\Mock; | ||
|
||
use MyParcelNL\Sdk\src\Concerns\HasInstance; | ||
use Symfony\Contracts\Service\ResetInterface; | ||
|
||
/** | ||
* @see \WP_REST_Server | ||
*/ | ||
class MockWpRestServer extends MockWpClass implements ResetInterface | ||
{ | ||
use HasInstance; | ||
|
||
public const CREATABLE = 'creatable'; | ||
|
||
private $routes = []; | ||
|
||
/** | ||
* @return array | ||
*/ | ||
public function get_routes(): array | ||
{ | ||
return $this->routes; | ||
} | ||
|
||
/** | ||
* @param string $route_namespace | ||
* @param string $route | ||
* @param array $args | ||
* @param bool $override | ||
* | ||
* @return void | ||
*/ | ||
public function register_route( | ||
Check notice on line 37 in tests/Mock/MockWpRestServer.php Codacy Production / Codacy Static Code Analysistests/Mock/MockWpRestServer.php#L37
|
||
string $route_namespace, | ||
string $route, | ||
array $args = [], | ||
bool $override = false | ||
): void { | ||
$path = "$route_namespace/$route"; | ||
|
||
$this->routes[$path] = [ | ||
'override' => $override, | ||
'args' => $args, | ||
]; | ||
} | ||
|
||
public function reset(): void | ||
{ | ||
$this->routes = []; | ||
} | ||
} |
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,107 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace MyParcelNL\WooCommerce\Tests\Mock; | ||
|
||
use BadMethodCallException; | ||
use MyParcelNL\Sdk\src\Support\Str; | ||
|
||
trait MocksGettersAndSetters | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private static $getterPrefix = 'get_'; | ||
|
||
/** | ||
* @var string | ||
*/ | ||
private static $setterPrefix = 'set_'; | ||
|
||
/** | ||
* @var array<string, mixed> | ||
*/ | ||
protected $attributes = []; | ||
|
||
/** | ||
* @param $name | ||
* @param $arguments | ||
* | ||
* @return null|mixed | ||
*/ | ||
public function __call($name, $arguments) | ||
{ | ||
if (Str::startsWith($name, ['is_', 'needs_'])) { | ||
$method = self::$getterPrefix . $name; | ||
|
||
return $this->{$method}(); | ||
} | ||
|
||
if (Str::startsWith($name, self::$getterPrefix)) { | ||
return $this->getAttribute($name); | ||
} | ||
|
||
if (Str::startsWith($name, self::$setterPrefix)) { | ||
$this->setAttribute($name, $arguments[0]); | ||
|
||
return null; | ||
} | ||
|
||
throw new BadMethodCallException("Method $name does not exist"); | ||
} | ||
|
||
/** | ||
* @return array<string, mixed> | ||
*/ | ||
public function getAttributes(): array | ||
{ | ||
return $this->attributes; | ||
} | ||
|
||
/** | ||
* @param array $data | ||
* | ||
* @return void | ||
*/ | ||
protected function fill(array $data): void | ||
{ | ||
$this->attributes = array_replace($this->attributes, $data); | ||
} | ||
|
||
/** | ||
* @param string $name | ||
* | ||
* @return null|mixed | ||
*/ | ||
private function getAttribute(string $name) | ||
{ | ||
$attribute = $this->getAttributeName($name, self::$getterPrefix); | ||
|
||
return $this->attributes[$attribute] ?? null; | ||
} | ||
|
||
/** | ||
* @param string $method | ||
* @param string $prefix | ||
* | ||
* @return string | ||
*/ | ||
private function getAttributeName(string $method, string $prefix): string | ||
{ | ||
return substr($method, strlen($prefix)); | ||
} | ||
|
||
/** | ||
* @param string $name | ||
* @param mixed $value | ||
* | ||
* @return void | ||
*/ | ||
private function setAttribute(string $name, $value): void | ||
{ | ||
$attribute = $this->getAttributeName($name, self::$setterPrefix); | ||
|
||
$this->attributes[$attribute] = $value; | ||
} | ||
} |
Oops, something went wrong.