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){ |
|
|
|
|
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'); |
|
|
|
|
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: