Completed
Push — master ( 1fa76a...9562c6 )
by Randy
15s queued 12s
created

ResponseTest::testFailConversion()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 7
nc 1
nop 0
1
<?php
2
3
namespace Demv\JSend\Test;
4
5
use Demv\JSend\JSend;
6
use Demv\JSend\JSendResponse;
7
use Demv\JSend\ResponseFactory;
8
use Demv\JSend\Status;
9
use Demv\JSend\StatusInterface;
10
use PHPUnit\Framework\TestCase;
11
12
final class ResponseTest extends TestCase
13
{
14
    public function testDefaultHttpStatusCode(): void
15
    {
16
        $this->assertEquals(200, JSend::getDefaultHttpStatusCode(ResponseFactory::instance()->success()));
17
        $this->assertEquals(200, JSend::getDefaultHttpStatusCode(ResponseFactory::instance()->fail()));
18
        $this->assertEquals(500, JSend::getDefaultHttpStatusCode(ResponseFactory::instance()->error(['message' => 'wtf'])));
19
        $this->assertEquals(400, JSend::getDefaultHttpStatusCode(ResponseFactory::instance()->error(['message' => 'wtf', 'code' => 400])));
20
    }
21
22
    public function testSuccessConversion(): void
23
    {
24
        $json = '{"status": "success", "data": ["Holy", "Moly"]}';
25
26
        $success = new DummyResponse();
27
        $success->withBody(new DummyStream($json));
28
        $success->withStatus(214);
29
30
        $response = ResponseFactory::instance()->convert($success);
31
        $this->assertTrue($response->getStatus()->isSuccess());
32
        $this->assertEquals(['Holy', 'Moly'], $response->getData());
33
        $this->assertJsonStringEqualsJsonString($json, json_encode($response));
34
    }
35
36
    public function testFailConversion(): void
37
    {
38
        $fail = new DummyResponse();
39
        $fail->withBody(new DummyStream('{"status": "fail", "data": null}'));
40
41
        $response = ResponseFactory::instance()->convert($fail);
42
        $this->assertTrue($response->getStatus()->isFail());
43
        $this->assertEmpty($response->getData());
44
        $this->assertJsonStringEqualsJsonString('{"status": "fail", "data": null}', json_encode($response));
45
    }
46
47
    public function testErrorConversion(): void
48
    {
49
        $json = '{"status": "error", "data": ["Invalid"], "message": "Something is not right..."}';
50
51
        $error = new DummyResponse();
52
        $error->withBody(new DummyStream($json));
53
        $error->withStatus(501);
54
55
        $result         = json_decode($json, true);
56
        $result['code'] = $error->getStatusCode();
57
58
        $response = ResponseFactory::instance()->convert($error);
59
        $this->assertTrue($response->getStatus()->isError());
60
        $this->assertEquals(['Invalid'], $response->getData());
61
        $this->assertJsonStringEqualsJsonString(json_encode($result), json_encode($response));
62
        $this->assertEquals('Something is not right...', $response->getError()->getMessage());
63
        $this->assertEquals($error->getStatusCode(), $response->getError()->getCode());
64
    }
65
66
    public function testMapping(): void
67
    {
68
        $response = new JSendResponse(Status::translate(1), null);
69
        $this->assertTrue($response->getStatus()->isSuccess());
70
71
        $response = new JSendResponse(Status::translate(0), null);
72
        $this->assertTrue($response->getStatus()->isFail());
73
74
        $response = new JSendResponse(Status::translate(-1), null);
75
        $this->assertTrue($response->getStatus()->isError());
76
77
        $response = new JSendResponse(Status::translate(true), null);
0 ignored issues
show
Documentation introduced by
true is of type boolean, but the function expects a integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
78
        $this->assertTrue($response->getStatus()->isSuccess());
79
80
        $response = new JSendResponse(Status::translate(false), null);
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
81
        $this->assertTrue($response->getStatus()->isFail());
82
83
        $response = new JSendResponse(Status::translate(false, [false => StatusInterface::STATUS_ERROR]), null);
0 ignored issues
show
Documentation introduced by
false is of type boolean, but the function expects a integer.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
84
        $this->assertTrue($response->getStatus()->isError());
85
    }
86
}