TestCase::setUp()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
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      https://fastdlabs.com
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
    protected $connection = 'default';
25
26
    /**
27
     * @var Application
28
     */
29
    protected $app;
30
31
    /**
32
     * Set up unit.
33
     */
34
    public function setUp()
35
    {
36
        $this->app = $this->createApplication();
37
        parent::setUp();
38
    }
39
40
    /**
41
     * @return Application
42
     */
43
    public function createApplication()
44
    {
45
        return new Application(getcwd());
46
    }
47
48
    /**
49
     * @param ServerRequestInterface $request
50
     * @param array                  $params
51
     * @param array                  $headers
52
     *
53
     * @return Response
54
     */
55
    public function handleRequest(ServerRequestInterface $request, array $params = [], array $headers = [])
56
    {
57
        if (!empty($params)) {
58
            if ('GET' === $request->getMethod()) {
59
                $request->withQueryParams($params);
60
            } elseif ('POST' === $request->getMethod()) {
61
                $request->withParsedBody($params);
62
            } else {
63
                $request->getBody()->write(http_build_query($params));
64
            }
65
        }
66
67
        if (!empty($headers)) {
68
            foreach ($headers as $name => $header) {
69
                $request->withAddedHeader($name, $header);
70
            }
71
        }
72
73
        return $this->app->handleRequest($request);
0 ignored issues
show
Bug Compatibility introduced by
The expression $this->app->handleRequest($request); of type FastD\Http\Response|Symf...HttpFoundation\Response adds the type Symfony\Component\HttpFoundation\Response to the return on line 73 which is incompatible with the return type documented by FastD\TestCase::handleRequest of type FastD\Http\Response.
Loading history...
74
    }
75
76
    /**
77
     * Returns the test database connection.
78
     *
79
     * @return \PHPUnit_Extensions_Database_DB_IDatabaseConnection|null
80
     */
81
    protected function getConnection()
82
    {
83
        try {
84
            return $this->createDefaultDBConnection(database($this->connection)->pdo);
85
        } catch (\Exception $exception) {
86
            return null;
87
        }
88
    }
89
90
    /**
91
     * Returns the test dataset.
92
     *
93
     * @return PHPUnit_Extensions_Database_DataSet_IDataSet
94
     */
95
    protected function getDataSet()
96
    {
97
        $path = app()->getPath().'/database/dataset/'.$this->connection;
98
99
        if (!file_exists($path) && !empty($this->connection)) {
100
            $path = dirname($path);
101
        }
102
103
        $composite = new PHPUnit_Extensions_Database_DataSet_CompositeDataSet();
104
105
        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
        }
119
120
        return $composite;
121
    }
122
}
123