Passed
Push — master ( 6ce97f...1fe2f9 )
by huang
05:13
created

IndexControllerTest::testHandleMiddlewareRequest()   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;
0 ignored issues
show
Bug introduced by
This use statement conflicts with another class in this namespace, TestCase.

Let’s assume that you have a directory layout like this:

.
|-- OtherDir
|   |-- Bar.php
|   `-- Foo.php
`-- SomeDir
    `-- Foo.php

and let’s assume the following content of Bar.php:

// Bar.php
namespace OtherDir;

use SomeDir\Foo; // This now conflicts the class OtherDir\Foo

If both files OtherDir/Foo.php and SomeDir/Foo.php are loaded in the same runtime, you will see a PHP error such as the following:

PHP Fatal error:  Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php

However, as OtherDir/Foo.php does not necessarily have to be loaded and the error is only triggered if it is loaded before OtherDir/Bar.php, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias:

// Bar.php
namespace OtherDir;

use SomeDir\Foo as SomeDirFoo; // There is no conflict anymore.
Loading history...
11
12
    class IndexControllerTest extends TestCase
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
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 View Code Duplication
        public function testHandleDynamicRequest()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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 View Code Duplication
        public function testHandleMiddlewareRequest()
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
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