Passed
Branch 3.1 (41cce1)
by huang
02:56
created

TestCase   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 100
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 7

Test Coverage

Coverage 40%

Importance

Changes 0
Metric Value
dl 0
loc 100
ccs 18
cts 45
cp 0.4
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 PHPUnit_Extensions_Database_DB_IDatabaseConnection;
18
use Psr\Http\Message\ServerRequestInterface;
19
20
/**
21
 * Class TestCase.
22
 */
23
class TestCase extends WebTestCase
24
{
25
    /**
26
     * @var Application
27
     */
28
    protected $app;
29
30
    /**
31
     * Set up unit.
32
     */
33 54
    public function setUp()
34
    {
35 54
        $this->app = $this->createApplication();
36 54
        parent::setUp();
37 54
    }
38
39
    /**
40
     * @return Application
41
     */
42
    public function createApplication()
43
    {
44
        return new Application(getcwd());
45
    }
46
47
    /**
48
     * @param ServerRequestInterface $request
49
     * @param array                  $params
50
     * @param array                  $headers
51
     *
52
     * @return Response
53
     */
54
    public function handleRequest(ServerRequestInterface $request, array $params = [], array $headers = [])
55
    {
56
        if (!empty($params)) {
57
            if ('GET' === $request->getMethod()) {
58
                $request->withQueryParams($params);
59
            } elseif ('POST' === $request->getMethod()) {
60
                $request->withParsedBody($params);
61
            } else {
62
                $request->getBody()->write(http_build_query($params));
63
            }
64
        }
65
66
        if (!empty($headers)) {
67
            foreach ($headers as $name => $header) {
68
                $request->withAddedHeader($name, $header);
69
            }
70
        }
71
72
        return $this->app->handleRequest($request);
73
    }
74
75
    /**
76
     * Returns the test database connection.
77
     *
78
     * @return PHPUnit_Extensions_Database_DB_IDatabaseConnection
79
     */
80 54
    protected function getConnection()
81
    {
82
        try {
83 54
            $connection = env('connection');
84 54
            if (!$connection) {
85 54
                $connection = 'default';
86 54
            }
87
88 54
            return $this->createDefaultDBConnection(database($connection)->pdo);
0 ignored issues
show
Bug introduced by
It seems like $connection defined by env('connection') on line 83 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...
89 19
        } catch (\Exception $exception) {
90 19
            return null;
91
        }
92
    }
93
94
    /**
95
     * Returns the test dataset.
96
     *
97
     * @return PHPUnit_Extensions_Database_DataSet_IDataSet
98
     */
99 35
    protected function getDataSet()
100
    {
101 35
        $path = app()->getPath().'/database/dataset/*';
102
103 35
        $composite = new PHPUnit_Extensions_Database_DataSet_CompositeDataSet();
104
105 35
        foreach (glob($path) as $file) {
106
            $dataSet = load($file);
107
            if (empty($dataSet)) {
108
                $dataSet = [];
109
            }
110
            $tableName = pathinfo($file, PATHINFO_FILENAME);
111
            $composite->addDataSet(
112
                new PHPUnit_Extensions_Database_DataSet_ArrayDataSet(
113
                    [
114
                        $tableName => $dataSet,
115
                    ]
116
                )
117
            );
118 35
        }
119
120 35
        return $composite;
121
    }
122
}
123