Skip to content

Commit 017f539

Browse files
committed
Add tests cases
1 parent 5f12e57 commit 017f539

19 files changed

+447
-131
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
},
1414
"require-dev": {
1515
"friendsofphp/php-cs-fixer": "^2.13",
16+
"guzzlehttp/psr7": "^1.5",
1617
"php-http/mock-client": "^1.1",
1718
"phpstan/phpstan": "^0.10.7",
1819
"phpunit/phpunit": "^7"

examples/ajax/ajax_interface.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22

33
use MultiTheftAuto\Sdk\Mta;
4-
use MultiTheftAuto\Sdk\Authentication\Credential;
4+
use MultiTheftAuto\Sdk\Authentication\CredentialTest;
55
use MultiTheftAuto\Sdk\Model\Server;
66

77
// =============================
@@ -27,7 +27,7 @@
2727
}
2828

2929
$server = new Server($host, $port);
30-
$credential = new Credential($http_username, $http_password);
30+
$credential = new CredentialTest($http_username, $http_password);
3131
$mta = new Mta($server, $credential);
3232

3333
$val = explode(",", $val);

phpunit.xml.dist

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/6.5/phpunit.xsd"
4+
backupGlobals="false"
5+
colors="true"
6+
>
7+
<php>
8+
<ini name="error_reporting" value="-1" />
9+
<env name="SHELL_VERBOSITY" value="-1" />
10+
</php>
11+
12+
<testsuites>
13+
<testsuite name="Test Suite">
14+
<directory>tests</directory>
15+
</testsuite>
16+
</testsuites>
17+
18+
<filter>
19+
<whitelist>
20+
<directory>src</directory>
21+
</whitelist>
22+
</filter>
23+
</phpunit>

src/Factory/RequestFactory.php

Lines changed: 0 additions & 107 deletions
This file was deleted.

src/Model/Element.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,16 @@ class Element
1919
{
2020
protected $id;
2121

22-
public function __construct($id)
22+
public function __construct(string $id)
2323
{
2424
$this->id = $id;
2525
}
2626

27+
public function getId(): string
28+
{
29+
return $this->id;
30+
}
31+
2732
public function __toString()
2833
{
2934
return '^E^' . $this->id;

src/Model/Resources.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,12 @@ public function findByName(string $name): ?Resource
2828
return $resource->getName() == $name;
2929
});
3030

31-
return current($found)?? null;
31+
$resource = current($found);
32+
return $resource? $resource : null;
3233
}
3334

3435
public function add(Resource $resource): void
3536
{
36-
$resources[] = $resource;
37+
$this->resources[] = $resource;
3738
}
3839
}

src/Model/Server.php

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515

1616
namespace MultiTheftAuto\Sdk\Model;
1717

18+
use InvalidArgumentException;
19+
1820
class Server
1921
{
2022
/**
@@ -25,12 +27,15 @@ class Server
2527
/**
2628
* @var int
2729
*/
28-
protected $port;
30+
protected $httpPort;
2931

30-
public function __construct(string $host, int $port)
32+
public function __construct(string $host, int $httpPort)
3133
{
34+
if (!filter_var($host, FILTER_VALIDATE_IP, FILTER_FLAG_IPV4)) {
35+
throw new InvalidArgumentException('Invalid IP');
36+
}
3237
$this->host = $host;
33-
$this->port = $port;
38+
$this->httpPort = $httpPort;
3439
}
3540

3641
public function getHost(): string
@@ -40,11 +45,11 @@ public function getHost(): string
4045

4146
public function getPort(): int
4247
{
43-
return $this->port;
48+
return $this->httpPort;
4449
}
4550

4651
public function getBaseUri(): string
4752
{
48-
return sprintf('http://%s:%s', $this->host, $this->port);
53+
return sprintf('http://%s:%s', $this->host, $this->httpPort);
4954
}
5055
}

src/Mta.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,20 @@
1515

1616
namespace MultiTheftAuto\Sdk;
1717

18+
use GuzzleHttp\Psr7\Stream;
1819
use Http\Client\HttpClient;
1920
use Http\Discovery\HttpClientDiscovery;
2021
use Http\Discovery\MessageFactoryDiscovery;
2122
use Http\Message\MessageFactory;
2223
use MultiTheftAuto\Sdk\Authentication\Credential;
23-
use MultiTheftAuto\Sdk\Factory\RequestFactory;
2424
use MultiTheftAuto\Sdk\Model\Resource;
2525
use MultiTheftAuto\Sdk\Model\Resources;
2626
use MultiTheftAuto\Sdk\Model\Server;
27+
use MultiTheftAuto\Sdk\Response\HandleResponse;
2728
use MultiTheftAuto\Sdk\Response\HttpStatusVerification;
2829
use MultiTheftAuto\Sdk\Utils\Input;
2930
use MultiTheftAuto\Sdk\Utils\Translator;
31+
use Http\Message\Authentication\BasicAuth;
3032

3133
class Mta
3234
{
@@ -102,15 +104,13 @@ public function callFunction(string $resourceName, string $function, array $argu
102104

103105
protected function do_post_request($path, $json_data): string
104106
{
105-
$request = RequestFactory::useMessageFactory($this->messageFactory);
106-
$request->setMethod('POST');
107-
$request->setUri(sprintf('%s/%s', $this->server->getBaseUri(), $path));
108-
$request->withBody($json_data);
109-
$request->authenticate($this->credential);
107+
$request = $this->messageFactory->createRequest('POST', sprintf('%s/%s', $this->server->getBaseUri(), $path), [], $json_data);
108+
$auth = new BasicAuth($this->credential->getUser(), $this->credential->getPassword());
109+
$auth->authenticate($request);
110110

111-
$response = $this->httpClient->sendRequest($request->build());
111+
$response = $this->httpClient->sendRequest($request);
112112
HttpStatusVerification::validateStatus($response);
113113

114-
return $response->getBody()->getContents();
114+
return HandleResponse::getBody($response);
115115
}
116116
}

src/Response/HandleResponse.php

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?php
2+
3+
/*****************************************************************************
4+
*
5+
* PROJECT: MTA PHP SDK
6+
* LICENSE: See LICENSE in the top level directory
7+
* FILE: HandleResponse.php
8+
* VERSION: 1.0.0
9+
*
10+
* Multi Theft Auto is available from http://www.multitheftauto.com/
11+
*
12+
*****************************************************************************/
13+
14+
declare(strict_types=1);
15+
16+
namespace MultiTheftAuto\Sdk\Response;
17+
18+
use Psr\Http\Message\ResponseInterface;
19+
use Psr\Http\Message\StreamInterface;
20+
21+
class HandleResponse
22+
{
23+
public static function getBody(ResponseInterface $response): string
24+
{
25+
$stream = $response->getBody();
26+
if ($stream instanceof StreamInterface) {
27+
$body = $stream->getContents();
28+
} else {
29+
$body = $stream;
30+
}
31+
return $body;
32+
}
33+
}

src/Response/HttpStatusVerification.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public static function validateStatus(ResponseInterface $response): void
3434
}
3535

3636
if ($statusCode != 200) {
37-
throw new Exception(sprintf('Something went wrong. HTTP Status Code: %s | Body: %s', $statusCode, $response->getBody()->getContents()));
37+
throw new Exception(sprintf('Something went wrong. HTTP Status Code: %s | Body: %s', $statusCode, HandleResponse::getBody($response)));
3838
}
3939
}
4040
}

0 commit comments

Comments
 (0)