Test Failed
Push — master ( 949f7e...8bffbc )
by Tõnis
06:00
created

SiteControllerTest::testActionIndexPost()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
cc 1
nc 1
nop 0
1
<?php
2
namespace andmemasin\emailsvalidator;
3
4
use andmemasin\emailsvalidator\controllers\SiteController;
5
use Codeception\Stub;
6
use yii\base\Action;
7
use yii\web\Request;
8
use yii\web\View;
9
10
class SiteControllerTest extends \Codeception\Test\Unit
11
{
12
    /**
13
     * @var \andmemasin\emailsvalidator\UnitTester
14
     */
15
    protected $tester;
16
17
    /** @var SiteController */
18
    public $model;
19
20
    protected function _before()
0 ignored issues
show
Coding Style introduced by
_before 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...
21
    {
22
        $_SERVER['REQUEST_URI']='index.php';
23
        $config = require( __DIR__ . "/../_config/test.php");
24
        $this->model = new SiteController('site', new \yii\web\Application($config));
25
        \Yii::$app->controller = $this->model;
26
        \Yii::$app->controller->action = new Action('fake', $this->model);
27
    }
28
29
    protected function _after()
30
    {
31
    }
32
33
    // tests
34
    public function testActionIndex()
35
    {
36
        $result = $this->model->actionIndex();
37
        $this->assertInternalType('string', $result);
38
    }
39
40
    public function testActionIndexPost() {
41
42
        $data = [
43
            'textInput' => "[email protected]\[email protected],[email protected]",
44
        ];
45
46
47
        $request = $this->mockRequest($data);
48
        \Yii::$app->set('request', $request);
49
50
        $result = $this->model->actionIndex();
0 ignored issues
show
Unused Code introduced by
$result is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
51
52
53
    }
54
55
    /**
56
     * @param $data
57
     * @return Request
58
     */
59
    private function  mockRequest($data){
0 ignored issues
show
Coding Style introduced by
mockRequest 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...
60
        // mock a request
61
        $_SERVER['REQUEST_URI'] = 'http://localhost';
62
        $_SERVER['REMOTE_ADDR'] = '127.0.0.1';
63
        \Yii::$app->requestedAction = new Action('site/index', $this->model);
64
        \Yii::$app->setHomeUrl('http://localhost');
0 ignored issues
show
Bug introduced by
The method setHomeUrl does only exist in yii\web\Application, but not in yii\console\Application.

It seems like the method you are trying to call exists only in some of the possible types.

Let’s take a look at an example:

class A
{
    public function foo() { }
}

class B extends A
{
    public function bar() { }
}

/**
 * @param A|B $x
 */
function someFunction($x)
{
    $x->foo(); // This call is fine as the method exists in A and B.
    $x->bar(); // This method only exists in B and might cause an error.
}

Available Fixes

  1. Add an additional type-check:

    /**
     * @param A|B $x
     */
    function someFunction($x)
    {
        $x->foo();
    
        if ($x instanceof B) {
            $x->bar();
        }
    }
    
  2. Only allow a single type to be passed if the variable comes from a parameter:

    function someFunction(B $x) { /** ... */ }
    
Loading history...
65
        return Stub::make(Request::class, [
66
            'getUserIP' =>'127.0.0.1',
67
            'enableCookieValidation' => false,
68
            'getUserAgent' => 'Dummy User Agent',
69
            'getBodyParams' => [
70
                'EmailsValidationForm' => $data
71
            ],
72
        ]);
73
    }
74
75
}