Passed
Push — master ( b4b8e8...3089dd )
by Arthur
26:17 queued 02:28
created

HttpTest::http()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 4
dl 0
loc 7
ccs 3
cts 4
cp 0.75
crap 2.0625
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: arthur
5
 * Date: 09.10.18
6
 * Time: 21:56.
7
 */
8
9
namespace Foundation\Abstracts\Tests;
10
11
use Illuminate\Foundation\Testing\TestResponse;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, Foundation\Abstracts\Tests\TestResponse. Consider defining an alias.

Let?s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let?s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
12
13
abstract class HttpTest extends \Foundation\Abstracts\Tests\TestCase
14
{
15 18
    protected function decodeHttpResponse($content, $unwrap = true)
16
    {
17 18
        if ($content instanceof TestResponse) {
18 16
            $content = $content->content();
19
        }
20
21 18
        if ($unwrap) {
22 18
            return json_decode($content, true)['data'] ?? json_decode($content, true);
23
        }
24
25
        return json_decode($content, true);
26
    }
27
28 52
    protected function http(string $method, string $route, array $payload = [], array $headers = []) : \Foundation\Abstracts\Tests\TestResponse
29
    {
30 52
        if (!in_array($method, ['GET','POST','PATCH','DELETE','PUT'])) {
31
            throw new \Exception("Invalid Http Method");
32
        }
33
34 52
        return new \Foundation\Abstracts\Tests\TestResponse($this->json($method, env('API_URL').$route, $payload, $headers)->baseResponse);
35
    }
36
}
37