These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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() |
||
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(); |
||
51 | |||
52 | |||
53 | } |
||
54 | |||
55 | /** |
||
56 | * @param $data |
||
57 | * @return Request |
||
58 | */ |
||
59 | private function mockRequest($data){ |
||
0 ignored issues
–
show
|
|||
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
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
![]() |
|||
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 | } |
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: