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
|
|
|
public function testBehaviors() { |
54
|
|
|
$this->arrayHasKey('access', $this->model->behaviors()); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param $data |
59
|
|
|
* @return Request |
60
|
|
|
*/ |
61
|
|
|
private function mockRequest($data){ |
62
|
|
|
// mock a request |
63
|
|
|
$_SERVER['REQUEST_URI'] = 'http://localhost'; |
64
|
|
|
$_SERVER['REMOTE_ADDR'] = '127.0.0.1'; |
65
|
|
|
\Yii::$app->requestedAction = new Action('site/index', $this->model); |
66
|
|
|
\Yii::$app->setHomeUrl('http://localhost'); |
67
|
|
|
return Stub::make(Request::class, [ |
68
|
|
|
'getUserIP' =>'127.0.0.1', |
69
|
|
|
'enableCookieValidation' => false, |
70
|
|
|
'getUserAgent' => 'Dummy User Agent', |
71
|
|
|
'getBodyParams' => [ |
72
|
|
|
'EmailsValidationForm' => $data |
73
|
|
|
], |
74
|
|
|
]); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
} |