Skip to content

Commit

Permalink
Merge pull request #2 from NystronSolar/AddParamsInRoute
Browse files Browse the repository at this point in the history
Add Api Check User Route and Request
  • Loading branch information
TheDevick authored May 6, 2023
2 parents f57b14b + 222d949 commit 8b84250
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/PHPUnit/ApiCheckUserResponse.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"error_msg": "error_Username already exists",
"data": "",
"error_code": 10003
}
44 changes: 44 additions & 0 deletions src/Response/ApiCheckUserResponse.php
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
);
}
}
50 changes: 50 additions & 0 deletions src/Route/ApiCheckUserRoute.php
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(),
];
}
}
25 changes: 25 additions & 0 deletions tests/Response/ApiCheckUserResponse.php
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());
}
}

0 comments on commit 8b84250

Please sign in to comment.