Skip to content

Commit 64f5f8e

Browse files
committed
tests: improve code coverage
1 parent b5a9b78 commit 64f5f8e

File tree

8 files changed

+211
-10
lines changed

8 files changed

+211
-10
lines changed

infection.log

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
Escaped mutants:
2+
================
3+
4+
Timed Out mutants:
5+
==================
6+
7+
Not Covered mutants:
8+
====================

tests/Functional/MtaTest.php

Lines changed: 92 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,18 @@
1414

1515
namespace MultiTheftAuto\Sdk;
1616

17+
use GuzzleHttp\Psr7\Request;
18+
use GuzzleHttp\Psr7\Stream;
19+
use Http\Factory\Guzzle\RequestFactory;
20+
use Http\Factory\Guzzle\StreamFactory;
1721
use Http\Mock\Client;
22+
use MultiTheftAuto\Sdk\Exception\AccessDeniedException;
1823
use MultiTheftAuto\Sdk\Model\Authentication;
1924
use MultiTheftAuto\Sdk\Model\Element;
2025
use MultiTheftAuto\Sdk\Model\Resource;
2126
use MultiTheftAuto\Sdk\Model\Server;
2227
use PHPUnit\Framework\TestCase;
28+
use Prophecy\Argument;
2329
use Psr\Http\Message\ResponseInterface;
2430

2531
class MtaTest extends TestCase
@@ -29,7 +35,7 @@ public function testItReturnsResponse(): void
2935
$response = $this->prophesize(ResponseInterface::class);
3036
$response
3137
->getBody()
32-
->willReturn('["^R^someResource","someString","^E^someElementId"]');
38+
->willReturn('["^R^someResource","someString","^E^someElementId", [1,2,3]]');
3339
$response
3440
->getStatusCode()
3541
->willReturn(200);
@@ -44,6 +50,7 @@ public function testItReturnsResponse(): void
4450
$mta = new Mta($server, $credential, $client);
4551
$return = $mta->getService()->callFunction('someCallableResource', 'someCallableFunction');
4652

53+
$this->assertIsArray($return);
4754
$this->assertInstanceOf(Resource::class, $return[0]);
4855
$this->assertEquals('someResource', $return[0]->getName());
4956
$this->assertEquals('someString', $return[1]);
@@ -53,41 +60,118 @@ public function testItReturnsResponse(): void
5360

5461
public function testItReturnsResponseUsingDirectCall(): void
5562
{
63+
$response = $this->prophesize(ResponseInterface::class);
64+
$response
65+
->getBody()
66+
->willReturn('["^R^someResource","someString","^E^someElementId"]');
67+
$response
68+
->getStatusCode()
69+
->willReturn(200);
70+
$response = $response->reveal();
71+
5672
$client = new Client();
57-
$response = $this->createMock(ResponseInterface::class);
58-
$response->method('getBody')->willReturn('["^R^someResource","someString","^E^someElementId"]');
59-
$response->method('getStatusCode')->willReturn(200);
6073
$client->addResponse($response);
74+
6175
$server = new Server('127.0.0.1', 22005);
6276
$credential = new Authentication('someUser', 'somePassword');
6377

6478
$mta = new Mta($server, $credential, $client);
6579
$return = $mta->getResource('someCallableResource')->call->someCallableFunction();
6680

81+
$this->assertIsArray($return);
6782
$this->assertInstanceOf(Resource::class, $return[0]);
6883
$this->assertEquals('someResource', $return[0]->getName());
6984
$this->assertEquals('someString', $return[1]);
7085
$this->assertInstanceOf(Element::class, $return[2]);
7186
$this->assertEquals('someElementId', $return[2]->getId());
7287
}
7388

74-
public function testItPrintsSomeJson()
89+
public function testItPrintsSomeJson(): void
7590
{
7691
Mta::doReturn('someValue1', 'someValue2');
7792
$this->expectOutputString('["someValue1","someValue2"]');
7893
}
7994

8095
public function testItReturnResource()
8196
{
82-
$server = $this->createMock(Server::class);
83-
$credential = $this->createMock(Authentication::class);
97+
$server = $this->prophesize(Server::class);
98+
$credential = $this->prophesize(Authentication::class);
8499
$client = new Client();
85-
$mta = new Mta($server, $credential, $client);
100+
$mta = new Mta($server->reveal(), $credential->reveal(), $client);
86101

87102
$resourceName = 'someResource';
88103
$resource = $mta->getResource($resourceName);
89104

90105
$this->assertInstanceOf(Resource::class, $resource);
91106
$this->assertEquals($resourceName, $resource->getName());
92107
}
108+
109+
public function testItUsesRequestAndStreamFactoryParameter(): void
110+
{
111+
$request = new Request('POST', 'http://mtasa.com');
112+
$requestFactory = $this->prophesize(RequestFactory::class);
113+
$requestFactory
114+
->createRequest(Argument::type('string'), Argument::type('string'))
115+
->shouldBeCalled()
116+
->willReturn($request);
117+
$requestFactory = $requestFactory->reveal();
118+
119+
$stream = $this->prophesize(Stream::class)->reveal();
120+
$streamFactory = $this->prophesize(StreamFactory::class);
121+
$streamFactory
122+
->createStream(Argument::type('string'))
123+
->shouldBeCalled()
124+
->willReturn($stream);
125+
$streamFactory = $streamFactory->reveal();
126+
127+
$responseStream = (new StreamFactory())->createStream('["^R^someResource","someString","^E^someElementId"]');
128+
$response = $this->prophesize(ResponseInterface::class);
129+
$response
130+
->getBody()
131+
->willReturn($responseStream);
132+
$response
133+
->getStatusCode()
134+
->willReturn(200);
135+
$response = $response->reveal();
136+
137+
$client = new Client();
138+
$client->addResponse($response);
139+
140+
$server = new Server('127.0.0.1', 22005);
141+
$credential = new Authentication('someUser', 'somePassword');
142+
143+
$mta = new Mta($server, $credential, $client, $requestFactory, $streamFactory);
144+
$mta->getService()->callFunction('someCallableResource', 'someCallableFunction');
145+
}
146+
147+
public function testItThrowsExceptionIfResponseCodeIsAccessDenied(): void
148+
{
149+
$this->expectException(AccessDeniedException::class);
150+
151+
$response = $this->prophesize(ResponseInterface::class);
152+
$response
153+
->getStatusCode()
154+
->willReturn(401);
155+
$response = $response->reveal();
156+
157+
$client = new Client();
158+
$client->addResponse($response);
159+
160+
$server = new Server('127.0.0.1', 22005);
161+
$credential = new Authentication('someUser', 'somePassword');
162+
163+
$mta = new Mta($server, $credential, $client);
164+
$mta->getService()->callFunction('someCallableResource', 'someCallableFunction');
165+
}
166+
167+
public function testItAddsAResource(): void
168+
{
169+
$client = new Client();
170+
$server = new Server('127.0.0.1', 22005);
171+
$credential = new Authentication('someUser', 'somePassword');
172+
$mta = new Mta($server, $credential, $client);
173+
174+
$mta->getResource('someName');
175+
$this->assertCount(1, $mta->getResourcesInstance()->all());
176+
}
93177
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
/*****************************************************************************
4+
*
5+
* PROJECT: MTA PHP SDK
6+
* LICENSE: See LICENSE in the top level directory
7+
* FILE: AccessDeniedExceptionTest.php
8+
*
9+
* Multi Theft Auto is available from http://www.multitheftauto.com/
10+
*
11+
*****************************************************************************/
12+
13+
declare(strict_types=1);
14+
15+
namespace MultiTheftAuto\Sdk\Exception;
16+
17+
use Exception;
18+
use PHPUnit\Framework\TestCase;
19+
20+
class AccessDeniedExceptionTest extends TestCase
21+
{
22+
public function testItThrowsExceptionWithMessage(): void
23+
{
24+
$this->expectException(Exception::class);
25+
$this->expectExceptionMessage('Access Denied. This server requires authentication. Please ensure that a valid username and password combination is provided.');
26+
27+
throw new AccessDeniedException();
28+
}
29+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
/*****************************************************************************
4+
*
5+
* PROJECT: MTA PHP SDK
6+
* LICENSE: See LICENSE in the top level directory
7+
* FILE: FunctionNotFoundExceptionTest.php
8+
*
9+
* Multi Theft Auto is available from http://www.multitheftauto.com/
10+
*
11+
*****************************************************************************/
12+
13+
declare(strict_types=1);
14+
15+
namespace MultiTheftAuto\Sdk\Exception;
16+
17+
use Exception;
18+
use PHPUnit\Framework\TestCase;
19+
20+
class FunctionNotFoundExceptionTest extends TestCase
21+
{
22+
public function testItThrowsExceptionWithMessage(): void
23+
{
24+
$this->expectException(Exception::class);
25+
$this->expectExceptionMessage('Attempted function call was not found');
26+
27+
throw new FunctionNotFoundException();
28+
}
29+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
3+
/*****************************************************************************
4+
*
5+
* PROJECT: MTA PHP SDK
6+
* LICENSE: See LICENSE in the top level directory
7+
* FILE: NotFoundStatusExceptionTest.php
8+
*
9+
* Multi Theft Auto is available from http://www.multitheftauto.com/
10+
*
11+
*****************************************************************************/
12+
13+
declare(strict_types=1);
14+
15+
namespace MultiTheftAuto\Sdk\Exception;
16+
17+
use Exception;
18+
use PHPUnit\Framework\TestCase;
19+
20+
class NotFoundStatusExceptionTest extends TestCase
21+
{
22+
public function testItThrowsExceptionWithMessage(): void
23+
{
24+
$this->expectException(Exception::class);
25+
$this->expectExceptionMessage('There was a problem with the request. Ensure that the resource exists and that the name is spelled correctly.');
26+
27+
throw new NotFoundStatusException();
28+
}
29+
}

tests/Unit/Model/ResourcesTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,15 @@
1818

1919
class ResourcesTest extends TestCase
2020
{
21+
public function testItAddsAResource(): void
22+
{
23+
$resources = new Resources();
24+
$resource = new Resource('someName');
25+
$resources->add($resource);
26+
27+
$this->assertCount(1, $resources->all());
28+
}
29+
2130
public function testItFindsExistingResource(): void
2231
{
2332
$resources = new Resources();

tests/Unit/Response/HttpStatusVerificationTest.php

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
namespace MultiTheftAuto\Sdk\Response;
1616

1717
use Exception;
18+
use Http\Factory\Guzzle\StreamFactory;
1819
use MultiTheftAuto\Sdk\Exception\AccessDeniedException;
1920
use MultiTheftAuto\Sdk\Exception\FunctionNotFoundException;
2021
use MultiTheftAuto\Sdk\Exception\NotFoundStatusException;
@@ -61,13 +62,15 @@ public function testItThrowsExceptionForNotReturningSuccessfulCode(): void
6162
$this->expectException(Exception::class);
6263
$this->expectExceptionMessage('Something went wrong. HTTP Status Code: 201 | Body: someBody');
6364

65+
$stream = (new StreamFactory())->createStream('someBody');
66+
6467
$response = $this->prophesize(ResponseInterface::class);
6568
$response
6669
->getStatusCode()
6770
->willReturn(201);
6871
$response
6972
->getBody()
70-
->willReturn('someBody');
73+
->willReturn($stream);
7174
$response = $response->reveal();
7275

7376
$validator = new HttpStatusValidator($response);
@@ -78,13 +81,15 @@ public function testItThrowsExceptionIfFunctionWasNotFound(): void
7881
{
7982
$this->expectException(FunctionNotFoundException::class);
8083

84+
$stream = (new StreamFactory())->createStream('error: not found');
85+
8186
$response = $this->prophesize(ResponseInterface::class);
8287
$response
8388
->getStatusCode()
8489
->willReturn(200);
8590
$response
8691
->getBody()
87-
->willReturn('error: not found');
92+
->willReturn($stream);
8893
$response = $response->reveal();
8994

9095
$validator = new HttpStatusValidator($response);

tests/Unit/Transformer/ElementTransformerTest.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,4 +46,12 @@ public function testItConvertsToServerFormat(): void
4646
$output = ElementTransformer::toServer($array);
4747
$this->assertEquals('["^R^someResource","String",["^E^someElement"],"^E^someElement2"]', $output);
4848
}
49+
50+
public function testItReturnsNullIfDataIsEmpty(): void
51+
{
52+
$input = '';
53+
$fromInput = ElementTransformer::fromServer($input);
54+
55+
$this->assertNull($fromInput);
56+
}
4957
}

0 commit comments

Comments
 (0)