Passed
Branch 3.1 (d457fa)
by huang
03:02
created

TestCase   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 41.3%

Importance

Changes 0
Metric Value
dl 0
loc 100
ccs 19
cts 46
cp 0.413
rs 10
c 0
b 0
f 0
wmc 14
lcom 1
cbo 7

5 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 5 1
A createApplication() 0 4 1
B handleRequest() 0 20 6
A getConnection() 0 13 3
A getDataSet() 0 23 3
1
<?php
2
/**
3
 * @author    jan huang <[email protected]>
4
 * @copyright 2016
5
 *
6
 * @see      https://www.github.com/janhuang
7
 * @see      http://www.fast-d.cn/
8
 */
9
10
namespace FastD;
11
12
use FastD\Http\Response;
13
use FastD\Testing\WebTestCase;
14
use PHPUnit_Extensions_Database_DataSet_ArrayDataSet;
15
use PHPUnit_Extensions_Database_DataSet_CompositeDataSet;
16
use PHPUnit_Extensions_Database_DataSet_IDataSet;
17
use Psr\Http\Message\ServerRequestInterface;
18
19
/**
20
 * Class TestCase.
21
 */
22
class TestCase extends WebTestCase
23
{
24
    /**
25
     * @var Application
26
     */
27
    protected $app;
28
29
    /**
30
     * Set up unit.
31
     */
32 54
    public function setUp()
33 1
    {
34 54
        $this->app = $this->createApplication();
35 54
        parent::setUp();
36 54
    }
37
38
    /**
39
     * @return Application
40
     */
41
    public function createApplication()
42
    {
43
        return new Application(getcwd());
44
    }
45
46
    /**
47
     * @param ServerRequestInterface $request
48
     * @param array                  $params
49
     * @param array                  $headers
50
     *
51
     * @return Response
52
     */
53
    public function handleRequest(ServerRequestInterface $request, array $params = [], array $headers = [])
54
    {
55
        if (!empty($params)) {
56
            if ('GET' === $request->getMethod()) {
57
                $request->withQueryParams($params);
58
            } elseif ('POST' === $request->getMethod()) {
59
                $request->withParsedBody($params);
60
            } else {
61
                $request->getBody()->write(http_build_query($params));
62
            }
63
        }
64
65
        if (!empty($headers)) {
66
            foreach ($headers as $name => $header) {
67
                $request->withAddedHeader($name, $header);
68
            }
69
        }
70
71
        return $this->app->handleRequest($request);
72
    }
73
74
    /**
75
     * Returns the test database connection.
76
     *
77
     * @return \PHPUnit_Extensions_Database_DB_IDatabaseConnection|null
78
     */
79 54
    protected function getConnection()
80
    {
81
        try {
82 54
            $connection = env('connection');
83 54
            if (!$connection) {
84 54
                $connection = 'default';
85 54
            }
86
87 54
            return $this->createDefaultDBConnection(database($connection)->pdo);
0 ignored issues
show
Bug introduced by
It seems like $connection defined by env('connection') on line 82 can also be of type boolean; however, database() does only seem to accept string, maybe add an additional type check?

If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:

/**
 * @return array|string
 */
function returnsDifferentValues($x) {
    if ($x) {
        return 'foo';
    }

    return array();
}

$x = returnsDifferentValues($y);
if (is_array($x)) {
    // $x is an array.
}

If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.

Loading history...
88 19
        } catch (\Exception $exception) {
89 19
            return null;
90
        }
91
    }
92
93
    /**
94
     * Returns the test dataset.
95
     *
96
     * @return PHPUnit_Extensions_Database_DataSet_IDataSet
97
     */
98 35
    protected function getDataSet()
99
    {
100 35
        $path = app()->getPath().'/database/dataset/*';
101
102 35
        $composite = new PHPUnit_Extensions_Database_DataSet_CompositeDataSet();
103
104 35
        foreach (glob($path) as $file) {
105
            $dataSet = load($file);
106
            if (empty($dataSet)) {
107
                $dataSet = [];
108
            }
109
            $tableName = pathinfo($file, PATHINFO_FILENAME);
110
            $composite->addDataSet(
111
                new PHPUnit_Extensions_Database_DataSet_ArrayDataSet(
112
                    [
113
                        $tableName => $dataSet,
114
                    ]
115
                )
116
            );
117 35
        }
118
119 35
        return $composite;
120
    }
121
}
122