Completed
Branch master (9dcfc4)
by Daniel
24:32
created

DevHealthControllerTest::testIndexCreatesChecker()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 17
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 0
1
<?php
2
3
namespace SilverStripe\EnvironmentCheck\Tests\Controllers;
4
5
use SilverStripe\Control\HTTPRequest;
6
use SilverStripe\Dev\SapphireTest;
7
use SilverStripe\EnvironmentCheck\Controllers\DevHealthController;
8
use SilverStripe\EnvironmentCheck\EnvironmentChecker;
9
10
/**
11
 * Class DevHealthControllerTest
12
 *
13
 * @mixin PHPUnit_Framework_TestCase
14
 *
15
 * @package environmentcheck
16
 */
17
class DevHealthControllerTest extends SapphireTest
18
{
19
    /**
20
     * {@inheritDoc}
21
     * @var array
22
     */
23
    protected $usesDatabase = true;
24
25
    public function testIndexCreatesChecker()
0 ignored issues
show
Coding Style introduced by
testIndexCreatesChecker uses the super-global variable $_SERVER which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
26
    {
27
        $controller = new DevHealthController();
28
29
        $request = new HTTPRequest('GET', 'example.com');
30
31
        // we need to fake authenticated access as BasicAuth::requireLogin doesn't like empty
32
        // permission type strings, which is what health check uses.
33
34
        putenv('ENVCHECK_BASICAUTH_USERNAME="foo"');
35
        putenv('ENVCHECK_BASICAUTH_PASSWORD="bar"');
36
37
        $_SERVER['PHP_AUTH_USER'] = 'foo';
38
        $_SERVER['PHP_AUTH_PW'] = 'bar';
39
40
        $this->assertInstanceOf(EnvironmentChecker::class, $controller->index($request));
0 ignored issues
show
Unused Code introduced by
The call to DevHealthController::index() has too many arguments starting with $request.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
41
    }
42
}
43