Test Failed
Push — master ( 6d1358...8db816 )
by huang
02:59
created

TestCase::handleRequest()   A

Complexity

Conditions 3
Paths 4

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 8
c 0
b 0
f 0
nc 4
nop 3
dl 0
loc 13
ccs 4
cts 4
cp 1
crap 3
rs 9.4285
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\Test;
11
12
use FastD\Application;
13
use FastD\Http\Response;
14
use FastD\Testing\WebTestCase;
15
use PHPUnit_Extensions_Database_DataSet_IDataSet;
16
use PHPUnit_Extensions_Database_DB_IDatabaseConnection;
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 13
     * Set up unit.
31
     */
32 13
    public function setUp()
33 13
    {
34 13
        $this->app = $this->createApplication();
35
        parent::setUp();
36
    }
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 13
     * @param array                  $headers
50
     *
51 13
     * @return Response
52 13
     */
53 13
    public function handleRequest(ServerRequestInterface $request, array $params = [], array $headers = [])
54 13
    {
55
        if ('GET' === $request->getMethod()) {
56 13
            $request->withQueryParams($params);
57
        } else {
58
            $request->withParsedBody($params);
59
        }
60
        foreach ($headers as $name => $header) {
61
            $request->withAddedHeader($name, $header);
62
        }
63
64 13
        return $this->app->handleRequest($request);
65
    }
66 13
67
    /**
68 13
     * Returns the test database connection.
69
     *
70 13
     * @return PHPUnit_Extensions_Database_DB_IDatabaseConnection
71 13
     */
72 13
    protected function getConnection()
73 13
    {
74 13
        $connection = env('connection');
75 13
        if (!$connection) {
76 13
            $connection = 'default';
77
        }
78 13
79
        return $this->createDefaultDBConnection(database($connection)->pdo);
0 ignored issues
show
Bug introduced by
It seems like $connection defined by env('connection') on line 74 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...
80
    }
81
82
    /**
83
     * Returns the test dataset.
84
     *
85
     * @return PHPUnit_Extensions_Database_DataSet_IDataSet
86
     */
87
    protected function getDataSet()
88
    {
89
        $path = app()->getPath().'/database/dataset/*';
90
91
        $composite = new \PHPUnit_Extensions_Database_DataSet_CompositeDataSet();
92
93
        foreach (glob($path) as $file) {
94
            $dataSet = load($file);
95
            $tableName = pathinfo($file, PATHINFO_FILENAME);
96
            $composite->addDataSet(
97
                new \PHPUnit_Extensions_Database_DataSet_ArrayDataSet(
98
                    [
99
                        $tableName => $dataSet,
100
                    ]
101
                )
102
            );
103
        }
104
105
        return $composite;
106
    }
107
}
108