Passed
Push — master ( 1fe2f9...d2939f )
by huang
05:07
created

IndexControllerTest::testHandleDynamicRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 7
Ratio 100 %

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 0
dl 7
loc 7
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
    use FastD\Application;
10
    use FastD\TestCase;
11
12
    class IndexControllerTest extends TestCase
13
    {
14
        public function createApplication()
15
        {
16
            return new Application(__DIR__.'/../../default');
17
        }
18
19
        public function testSayHello()
20
        {
21
            $response = $this->app->handleRequest($this->request('GET', '/'));
22
            $this->equalsJson($response, ['foo' => 'bar']);
23
            $response = $this->app->handleRequest($this->request('GET', '/?foo=var'));
24
            $this->equalsJson($response, ['foo' => 'var']);
25
            $response = $this->app->handleRequest($this->request('GET', '/?foo=bar'));
26
            $this->equalsJson($response, ['foo' => 'bar']);
27
        }
28
29
        public function testDb()
30
        {
31
            $response = $this->app->handleRequest($this->request('GET', '/db'));
32
33
            $this->equalsStatus($response, 200);
34
        }
35
36
        public function testHandleDynamicRequest()
37
        {
38
            $response = $this->app->handleRequest($this->request('GET', '/foo/bar'));
39
            $this->equalsJson($response, ['foo' => 'bar']);
40
            $response = $this->app->handleRequest($this->request('GET', '/foo/foobar'));
41
            $this->equalsJson($response, ['foo' => 'foobar']);
42
        }
43
44
        public function testHandleMiddlewareRequest()
45
        {
46
            $response = $this->app->handleRequest($this->request('POST', '/foo/bar'));
47
            $this->equalsJson($response, ['foo' => 'middleware']);
48
            $response = $this->app->handleRequest($this->request('POST', '/foo/not'));
49
            $this->equalsJson($response, ['foo' => 'bar']);
50
        }
51
52
        public function testModel()
53
        {
54
            $response = $this->app->handleRequest($this->request('GET', '/model'));
55
            $this->assertEquals(200, $response->getStatusCode());
56
            $this->isSuccessful($response);
57
        }
58
59
        public function testAuth()
60
        {
61
            $response = $this->app->handleRequest($this->request('GET', '/auth'));
62
63
            $this->assertEquals(401, $response->getStatusCode());
64
65
            $this->equalsJson($response, [
66
                              'msg' => 'not allow access',
67
                              'code' => 401,
68
                              ]);
69
70
            $response = $this->app->handleRequest($this->request('GET', 'http://foo:[email protected]/auth', [
71
                                                                 'PHP_AUTH_USER' => 'foo',
72
                                                                 'PHP_AUTH_PW' => 'bar',
73
                                                                 ]));
74
75
            $this->assertEquals(200, $response->getStatusCode());
76
77
            $this->equalsJson($response, [
78
                              'foo' => 'bar',
79
                              ]);
80
        }
81
    }
82