-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add Api Check User Route and Request
- Loading branch information
Showing
4 changed files
with
124 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"error_msg": "error_Username already exists", | ||
"data": "", | ||
"error_code": 10003 | ||
} |
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,44 @@ | ||
<?php | ||
|
||
namespace NystronSolar\GrowattApi\Response; | ||
|
||
use Psr\Http\Message\ResponseInterface; | ||
|
||
class ApiCheckUserResponse extends ApiResponse | ||
{ | ||
private bool $userExists; | ||
|
||
public function __construct(ResponseInterface $httpResponse, string $errorMessage, int $errorCode, \stdClass $responseJson, bool $userExists) | ||
{ | ||
parent::__construct($httpResponse, $errorMessage, $errorCode, $responseJson); | ||
|
||
$this->userExists = $userExists; | ||
} | ||
|
||
public function userExists(): bool | ||
{ | ||
return $this->userExists; | ||
} | ||
|
||
public static function generate(ResponseInterface $httpResponse): ApiCheckUserResponse|false | ||
{ | ||
$primitiveApiResponse = parent::generate($httpResponse); | ||
if (!$primitiveApiResponse) { | ||
return false; | ||
} | ||
|
||
$responseJson = $primitiveApiResponse->getResponseJson(); | ||
$errorMessage = $primitiveApiResponse->getErrorMessage(); | ||
$errorCode = $primitiveApiResponse->getErrorCode(); | ||
|
||
$userExists = 10003 === $errorCode ? true : false; | ||
|
||
return new ApiCheckUserResponse( | ||
$httpResponse, | ||
$errorMessage, | ||
$errorCode, | ||
$responseJson, | ||
$userExists | ||
); | ||
} | ||
} |
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,50 @@ | ||
<?php | ||
|
||
namespace NystronSolar\GrowattApi\Route; | ||
|
||
use NystronSolar\GrowattApi\Response\ApiCheckUserResponse; | ||
|
||
final class ApiCheckUserRoute extends ApiRoute | ||
{ | ||
private string $userName; | ||
|
||
public function __construct(string $userName) | ||
{ | ||
$this->userName = $userName; | ||
} | ||
|
||
public function getUserName(): string | ||
{ | ||
return $this->userName; | ||
} | ||
|
||
public static function getDescription(): string | ||
{ | ||
return 'Check if the User Name already exists'; | ||
} | ||
|
||
public static function getRequestRoute(): string | ||
{ | ||
return 'user/check_user'; | ||
} | ||
|
||
public static function getRequestMethod(): string | ||
{ | ||
return 'POST'; | ||
} | ||
|
||
/** | ||
* @return class-string<ApiCheckUserResponse> | ||
*/ | ||
public static function getApiResponse(): string | ||
{ | ||
return ApiCheckUserResponse::class; | ||
} | ||
|
||
public function getAllParams(): array | ||
{ | ||
return [ | ||
'user_name' => $this->getUserName(), | ||
]; | ||
} | ||
} |
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,25 @@ | ||
<?php | ||
|
||
namespace NystronSolar\GrowattApiTests\Response; | ||
|
||
use NystronSolar\GrowattApi\PHPUnit\GrowattApiTestData; | ||
use NystronSolar\GrowattApi\Response\ApiCheckUserResponse; | ||
use NystronSolar\GrowattApi\Route\ApiCheckUserRoute; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class ApiCheckUserResponseTest extends TestCase | ||
{ | ||
use GrowattApiTestData; | ||
|
||
public function testRequest() | ||
{ | ||
$route = new ApiCheckUserRoute(''); | ||
/** @var ApiCheckUserResponse|false $response */ | ||
$this->assertNotFalse($response = $this->makeApiClientRequest($route)); | ||
$this->assertSame('', $response->getErrorMessage()); | ||
$this->assertSame(0, $response->getErrorCode()); | ||
$this->assertFalse($response->hasErrors()); | ||
|
||
$this->assertFalse($response->userExists()); | ||
} | ||
} |