Skip to content

Commit 9ca46a0

Browse files
committed
edit configuration to tests
1 parent 6038ddd commit 9ca46a0

File tree

9 files changed

+164
-19
lines changed

9 files changed

+164
-19
lines changed

api/config/test.php

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,24 @@
11
<?php
2+
/*
3+
$config = yii\helpers\ArrayHelper::merge(
4+
require(__DIR__ . '/main.php'),
5+
require(__DIR__ . '/main-local.php'),
6+
[
7+
'id' => 'app-tests',
8+
'components' => [
9+
'db' => [
10+
'dsn' => 'mysql:host=localhost;dbname=yii_app_test',
11+
]
12+
]
13+
]
14+
);
15+
return $config;
16+
*/
217

318
return [
419
'id' => 'app-api-tests',
5-
];
20+
'basePath' => dirname(__DIR__),
21+
];
22+
23+
24+

api/modules/v1/controllers/UserController.php

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
namespace api\modules\v1\controllers;
44

55

6+
use api\modules\v1\models\User;
67
use base\rest\ActiveController;
8+
use Yii;
79
use yii\filters\auth\CompositeAuth;
810
use yii\filters\auth\HttpBearerAuth;
911
use yii\helpers\ArrayHelper;
@@ -28,7 +30,15 @@ public function behaviors()
2830

2931
public function actionSignup()
3032
{
31-
return ['message' => 'working'];
33+
$model = new User();
34+
$request = Yii::$app->request->post();
35+
36+
$model->username = $request["username"];
37+
$model->password = $request["password"];
38+
39+
$response = $model->login();
40+
41+
return $response;
3242

3343
}
3444

api/modules/v1/models/User.php

Lines changed: 98 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,110 @@
22

33
namespace api\modules\v1\models;
44

5+
use Yii;
6+
use yii\base\Model;
7+
use base\db\ActiveRecord;
8+
use yii\web\IdentityInterface;
9+
510
/**
611
* Class User
712
* @package api\modules\v1\models
813
*
914
* {@inheritdoc}
1015
*/
11-
class User extends \common\models\User
16+
class User extends ActiveRecord
1217
{
18+
public $username;
19+
public $password;
20+
public $rememberMe = true;
21+
22+
private $_user;
23+
24+
const STATUS_DELETED = 0;
25+
const STATUS_ACTIVE = 10;
26+
27+
28+
/**
29+
* {@inheritdoc}
30+
*/
31+
public static function tableName()
32+
{
33+
return '{{%user}}';
34+
}
35+
36+
public function rules()
37+
{
38+
return [
39+
// username and password are both required
40+
[['username', 'password'], 'required'],
41+
// rememberMe must be a boolean value
42+
['rememberMe', 'boolean'],
43+
// password is validated by validatePassword()
44+
['password', 'validatePassword'],
45+
];
46+
}
47+
48+
/**
49+
* {@inheritdoc}
50+
*/
51+
public function getId()
52+
{
53+
return $this->getPrimaryKey();
54+
}
55+
56+
/**
57+
* Validates the password.
58+
* This method serves as the inline validation for password.
59+
*
60+
* @param string $attribute the attribute currently being validated
61+
* @param array $params the additional name-value pairs given in the rule
62+
*/
63+
public function validatePassword($attribute, $params)
64+
{
65+
if (!$this->hasErrors()) {
66+
$user = $this->getUser();
67+
if (!$user || !$user->validatePassword($this->password)) {
68+
$this->addError($attribute, 'Incorrect username or password.');
69+
}
70+
}
71+
}
72+
73+
/**
74+
* Finds user by [[username]]
75+
*
76+
* @return User|null
77+
*/
78+
protected function getUser()
79+
{
80+
if ($this->_user === null) {
81+
$this->_user = User::findByUsername($this->username);
82+
}
83+
84+
return $this->_user;
85+
}
86+
87+
/**
88+
* Finds user by username
89+
*
90+
* @param string $username
91+
* @return static|null
92+
*/
93+
public static function findByUsername($username)
94+
{
95+
return static::findOne(['username' => $username, 'status' => self::STATUS_ACTIVE]);
96+
}
97+
98+
/**
99+
* Logs in a user using the provided username and password.
100+
*
101+
* @return bool whether the user is logged in successfully
102+
*/
103+
public function login()
104+
{
105+
if ($this->validate()) {
106+
return Yii::$app->user->login($this->getUser(), $this->rememberMe ? 3600 * 24 * 30 : 0);
107+
}
13108

109+
return false;
110+
}
14111
}

codeception.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,4 @@ actor_suffix: Tester
88
extensions:
99
enabled:
1010
- Codeception\Extension\RunFailed
11+

common/config/test.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@
88
'identityClass' => 'common\models\User',
99
],
1010
],
11-
];
11+
];

tests/api.suite.yml

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1+
class_name: ApiTester
12
actor: ApiTester
23
modules:
3-
enabled:
4-
- REST:
5-
url: /api/v1
6-
depends: Yii2
7-
- \Helper\Api
8-
9-
config:
10-
- Yii2
4+
enabled:
5+
- \Helper\Api
6+
- REST:
7+
url: http://127.0.0.1
8+
depends: PhpBrowser
9+
part: Json

tests/api/UserCest.php

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,37 @@
1-
<?php
1+
<?php
2+
3+
use Codeception\Util\HttpCode;
24

35
class UserCest
46
{
57
public function _before(ApiTester $I)
68
{
9+
//$file = 'var-local.php';
10+
/*if (file_exists($file)) {
11+
require $file;
12+
$this->path = $path ?? "";
13+
} */
14+
$this->path = "http://127.0.0.1/~mariale/yii2-api-template/api/web/v1/";
715
}
816

917
// tests
1018
public function tryToTest(ApiTester $I)
1119
{
20+
$I->sendGET( $this->path.'user/status');
21+
$I->seeResponseCodeIs(HttpCode::OK); // 200
22+
$I->seeResponseIsJson();
23+
1224
}
1325

1426
public function trySigUp(ApiTester $I){
15-
$I->sendGET('user/signup');
27+
$I->sendGET( $this->path.'user/signup');
1628
$I->seeResponseCodeIs(HttpCode::OK); // 200
1729
$I->seeResponseIsJson();
18-
$I->seeResponseMatchesJsonType([
19-
'message' => 'string']);
30+
$I->canSeeResponseContainsJson([
31+
'success' => true,
32+
]);
33+
34+
35+
2036
}
2137
}

tests/api/UserLoginCept.php

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
1-
<?php
1+
<?php
2+
3+
use Codeception\Util\HttpCode;
4+
25
$I = new ApiTester($scenario);
36
$I->wantTo('Test SignUp');
47

58
//$I->sendPOST('user/signup', ['username' => 'uname', 'password' => '123456']);
6-
$I->sendGET('user/signup');
9+
$I->sendGET('/user/singup');
710
$I->seeResponseCodeIs(HttpCode::OK); // 200
811
$I->seeResponseIsJson();
912
$I->seeResponseMatchesJsonType([

tests/unit.suite.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ actor: UnitTester
66
modules:
77
enabled:
88
- Asserts
9-
- \Helper\Unit
9+
- \Helper\Unit

0 commit comments

Comments
 (0)