-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from dimitriBouteille/develop
Release 1.0.1
- Loading branch information
Showing
13 changed files
with
279 additions
and
39 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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
<?php | ||
/** | ||
* Copyright (c) 2024 Dimitri BOUTEILLE (https://github.com/dimitriBouteille) | ||
* Copyright (c) Dimitri BOUTEILLE (https://github.com/dimitriBouteille) | ||
* See LICENSE.txt for license details. | ||
* | ||
* Author: Dimitri BOUTEILLE <[email protected]> | ||
|
@@ -13,23 +13,24 @@ | |
use Rector\Set\ValueObject\SetList; | ||
use Rector\TypeDeclaration\Rector\Property\TypedPropertyFromStrictConstructorRector; | ||
|
||
return static function (RectorConfig $rectorConfig): void { | ||
$rectorConfig->paths([ | ||
return RectorConfig::configure() | ||
->withPaths([ | ||
__DIR__ . '/src', | ||
]); | ||
|
||
// register single rule | ||
$rectorConfig->rule(TypedPropertyFromStrictConstructorRector::class); | ||
|
||
$rectorConfig | ||
->skip([ | ||
SimplifyBoolIdenticalTrueRector::class, | ||
CallableThisArrayToAnonymousFunctionRector::class, | ||
SimplifyIfReturnBoolRector::class, | ||
]); | ||
|
||
$rectorConfig->sets([ | ||
SetList::CODE_QUALITY, | ||
__DIR__ . '/tests', | ||
]) | ||
->withRules([ | ||
TypedPropertyFromStrictConstructorRector::class, | ||
]) | ||
->withPreparedSets( | ||
codeQuality: true, | ||
typeDeclarations: true, | ||
instanceOf: true, | ||
) | ||
->withSets([ | ||
SetList::PHP_81, | ||
]) | ||
->withSkip([ | ||
SimplifyBoolIdenticalTrueRector::class, | ||
CallableThisArrayToAnonymousFunctionRector::class, | ||
SimplifyIfReturnBoolRector::class, | ||
]); | ||
}; |
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
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,120 @@ | ||
<?php | ||
/** | ||
* Copyright (c) Dimitri BOUTEILLE (https://github.com/dimitriBouteille) | ||
* See LICENSE.txt for license details. | ||
* | ||
* Author: Dimitri BOUTEILLE <[email protected]> | ||
*/ | ||
|
||
namespace Dbout\WpRestApi\Tests\Wrappers; | ||
|
||
use Dbout\WpRestApi\RouteAction; | ||
use Dbout\WpRestApi\Tests\fixtures\RouteWithException; | ||
use Dbout\WpRestApi\Tests\fixtures\RouteWithNotFoundException; | ||
use Dbout\WpRestApi\Tests\fixtures\RouteWithRouteException; | ||
use Dbout\WpRestApi\Wrappers\RestWrapper; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
/** | ||
* @coversDefaultClass \Dbout\WpRestApi\Wrappers\RestWrapper | ||
*/ | ||
class RestWrapperTest extends TestCase | ||
{ | ||
/** | ||
* @param string $className | ||
* @param bool $debug | ||
* @param string $expectedMessage | ||
* @param int $expectedHttpCode | ||
* @return void | ||
* @dataProvider providerActionThrowException | ||
* @covers ::execute | ||
* @covers ::onError | ||
*/ | ||
public function testActionThrowException( | ||
string $className, | ||
bool $debug, | ||
string $expectedMessage, | ||
int $expectedHttpCode | ||
): void { | ||
$action = new RouteAction($className, 'execute', ['GET'], null); | ||
$wrapper = new RestWrapper($action, $debug); | ||
|
||
$response = $wrapper->execute(new \WP_REST_Request()); | ||
$error = $response->get_data()['error'] ?? null; | ||
$this->exceptionAsserts($response, $expectedMessage, $expectedHttpCode); | ||
if ($debug === true) { | ||
$data = $error['data'] ?? []; | ||
$this->assertArrayHasKey('exception', $data, 'Key error.data.exception not found.'); | ||
} | ||
} | ||
|
||
/** | ||
* @return \Generator | ||
*/ | ||
public static function providerActionThrowException(): \Generator | ||
{ | ||
yield 'With \Exception and debug mode' => [ | ||
RouteWithException::class, | ||
true, | ||
'My custom exception.', | ||
500, | ||
]; | ||
|
||
yield 'With \Exception and without debug mode' => [ | ||
RouteWithException::class, | ||
false, | ||
'Something went wrong. Please try again.', | ||
500, | ||
]; | ||
|
||
yield 'With RouteException and debug mode' => [ | ||
RouteWithRouteException::class, | ||
true, | ||
'My route exception.', | ||
400, | ||
]; | ||
|
||
yield 'With RouteException and without debug mode' => [ | ||
RouteWithRouteException::class, | ||
false, | ||
'My route exception.', | ||
400, | ||
]; | ||
} | ||
|
||
/** | ||
* @return void | ||
* @covers ::execute | ||
*/ | ||
public function testNotFoundException(): void | ||
{ | ||
$action = new RouteAction( | ||
RouteWithNotFoundException::class, | ||
'execute', | ||
['GET'], | ||
null | ||
); | ||
|
||
$wrapper = new RestWrapper($action); | ||
|
||
$response = $wrapper->execute(new \WP_REST_Request()); | ||
$this->exceptionAsserts($response, 'Object not found.', 404); | ||
} | ||
|
||
/** | ||
* @param \WP_REST_Response $response | ||
* @param string $expectedMessage | ||
* @param int $expectedHttpCode | ||
* @return void | ||
*/ | ||
protected function exceptionAsserts( | ||
\WP_REST_Response $response, | ||
string $expectedMessage, | ||
int $expectedHttpCode | ||
): void { | ||
$error = $response->get_data()['error'] ?? null; | ||
$this->assertInstanceOf(\WP_REST_Response::class, $response); | ||
$this->assertEquals($expectedHttpCode, $response->get_status()); | ||
$this->assertEquals($expectedMessage, $error['message'] ?? null); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -6,15 +6,19 @@ | |
* Author: Dimitri BOUTEILLE <[email protected]> | ||
*/ | ||
|
||
define( 'WPINC', 'wp-includes' ); | ||
define('ABSPATH', __DIR__ . '/../web/wordpress/'); | ||
$includeDirectory = __DIR__ . '/../web/wordpress/wp-includes'; | ||
|
||
$paths = [ | ||
'/functions.php', | ||
'/plugin.php', | ||
'/class-wp-http-response.php', | ||
'/rest-api.php', | ||
'/rest-api/class-wp-rest-server.php', | ||
'/rest-api/class-wp-rest-response.php', | ||
'/rest-api/class-wp-rest-request.php', | ||
'/class-wp-error.php' | ||
]; | ||
|
||
foreach ($paths as $path) { | ||
|
Oops, something went wrong.