Passed
Pull Request — master (#6)
by Arina
03:18
created

JsonResponseTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 65
Duplicated Lines 0 %

Importance

Changes 2
Bugs 1 Features 0
Metric Value
eloc 18
c 2
b 1
f 0
dl 0
loc 65
rs 10
wmc 5
1
<?php
2
3
namespace ArinaSystems\JsonResponse\Tests;
4
5
use ArinaSystems\JsonResponse\Facades\JsonResponse;
6
use ArinaSystems\JsonResponse\Facades\Option;
7
use Exception;
8
use Illuminate\Support\Facades\Config;
9
10
class JsonResponseTest extends TestCase
11
{
12
    /**
13
     * Setup the test environment.
14
     *
15
     * @return void
16
     */
17
    public function setUp(): void
18
    {
19
        parent::setUp();
20
    }
21
22
    /**
23
     * @test
24
     */
25
    public function it_builds_default_json()
26
    {
27
        $defaults = Option::defaults();
28
29
        $response = JsonResponse::json()->getData();
30
31
        $this->assertEquals($response->success, $defaults['success']);
32
        $this->assertEquals($response->http_code, $defaults['http_code']);
33
        $this->assertEquals($response->code, $defaults['code']);
34
    }
35
36
    /**
37
     * @test
38
     */
39
    public function it_returns_with_response_structure()
40
    {
41
        $responseStructure = (array) JsonResponse::json()->getData();
42
        $builderStructure = JsonResponse::structure();
43
44
        $this->assertEquals(array_keys($responseStructure), array_keys($builderStructure));
45
    }
46
47
    /**
48
     * @test
49
     */
50
    public function it_returns_with_error_structure()
51
    {
52
        // Enable debugging mode.
53
        Config::set('json-response.debug', true);
54
55
        // Disable internal code builder.
56
        Config::set('json-response.attributes.code.builder', null);
57
58
        $responseStructure = (array) JsonResponse::error(new Exception('Some throwable object.'))->getData();
59
60
        $builderStructure = JsonResponse::structure(true);
61
62
        $this->assertEquals(array_keys($responseStructure), array_keys($builderStructure));
63
    }
64
65
    /**
66
     * @test
67
     */
68
    public function it_returns_with_certain_headers()
69
    {
70
        JsonResponse::attributes(['headers' => ['App-Name' => 'Laravel']]);
71
72
        $response = JsonResponse::json();
73
74
        $this->assertEquals($response->headers->get('App-Name'), 'Laravel');
75
    }
76
}
77