Passed
Push — master ( 30f57b...f3320a )
by huang
02:57
created

TestCase::getConnection()   A

Complexity

Conditions 3
Paths 5

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 8
nc 5
nop 0
dl 0
loc 13
ccs 8
cts 8
cp 1
crap 3
rs 9.4285
c 0
b 0
f 0
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 53
    public function setUp()
34
    {
35 53
        $this->app = $this->createApplication();
36 53
        parent::setUp();
37 53
    }
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 ('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
        foreach ($headers as $name => $header) {
64
            $request->withAddedHeader($name, $header);
65
        }
66
67
        return $this->app->handleRequest($request);
68
    }
69
70
    /**
71
     * Returns the test database connection.
72
     *
73
     * @return PHPUnit_Extensions_Database_DB_IDatabaseConnection
74
     */
75 53
    protected function getConnection()
76
    {
77
        try {
78 53
            $connection = env('connection');
79 53
            if (!$connection) {
80 53
                $connection = 'default';
81 53
            }
82
83 53
            return $this->createDefaultDBConnection(database($connection)->pdo);
0 ignored issues
show
Bug introduced by
It seems like $connection defined by env('connection') on line 78 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...
84 19
        } catch (\Exception $exception) {
85 19
            return null;
86
        }
87
    }
88
89
    /**
90
     * Returns the test dataset.
91
     *
92
     * @return PHPUnit_Extensions_Database_DataSet_IDataSet
93
     */
94 34
    protected function getDataSet()
95
    {
96 34
        $path = app()->getPath().'/database/dataset/*';
97
98 34
        $composite = new PHPUnit_Extensions_Database_DataSet_CompositeDataSet();
99
100 34
        foreach (glob($path) as $file) {
101
            $dataSet = load($file);
102
            $tableName = pathinfo($file, PATHINFO_FILENAME);
103
            $composite->addDataSet(
104
                new PHPUnit_Extensions_Database_DataSet_ArrayDataSet(
105
                    [
106
                        $tableName => $dataSet,
107
                    ]
108
                )
109
            );
110 34
        }
111
112 34
        return $composite;
113
    }
114
}
115