Skip to content

Commit 9d4cb7d

Browse files
author
Angel Garcia
committed
Merge remote-tracking branch 'origin/master'
# Conflicts: # .idea/php.xml # .idea/yii2-api-template.iml # composer.json
2 parents 0a10441 + 33b1ef1 commit 9d4cb7d

29 files changed

+717
-14
lines changed

.idea/php.xml

Lines changed: 5 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

.idea/yii2-api-template.iml

Lines changed: 25 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Helper/Api.php

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
namespace Helper;
3+
4+
// here you can define custom actions
5+
// all public methods declared in helper class will be available in $I
6+
7+
class Api extends \Codeception\Module
8+
{
9+
10+
}

README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,36 @@ To apply the migrations execute the command
150150

151151
To revert the migrations execute the command
152152
```./yii migrate/down```
153+
154+
### Congiguration of tests
155+
install codeception
156+
``composer require codeception/codeception --dev``
157+
158+
create folder named `tests` in your project after execute follow command
159+
```./vendor/bin/codecept bootstrap```
160+
161+
to active test to api execute
162+
``` ./vendor/bin/codecept generate:suite api ```
163+
164+
for create a new test of api execute
165+
``` ./vendor/bin/codecept generate:cest api CreateUser```
166+
167+
create unit test execute the follow command
168+
``` ./vendor/bin/codecept generate:test unit Example```
169+
170+
create advanced tests execute
171+
```./vendor/bin/codecept generate:cest suitename CestName ```
172+
173+
174+
for execute all tests execute
175+
```./vendor/bin/codecept run ```
176+
177+
for execute only the unit test execute
178+
``` ./vendor/bin/codecept run unit ```
179+
180+
run test of only file
181+
``` ./vendor/bin/codecept run tests/api/UserCest.php```
182+
183+
184+
185+

api/config/main.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,14 @@
6565
}
6666
},
6767
],
68+
'db' => [
69+
'class' => 'yii\db\Connection',
70+
'dsn' => 'mysql:host=127.0.0.1;port=3306;dbname=yii_app',
71+
'username' => 'root',
72+
'password' => 'm4r14l3j',
73+
'charset' => 'utf8',
74+
75+
]
6876
],
6977
'params' => $params,
7078
'controllerMap' => [

api/config/test.php

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,33 @@
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+
'components' => [
22+
'db2' => [
23+
'class' => 'yii\db\Connection',
24+
'dsn' => 'mysql:host=127.0.0.1;dbname=yii_app_test',
25+
'username' => 'root',
26+
'password' => 'm4r14l3j',
27+
'charset' => 'utf8',
28+
]
29+
]
30+
];
31+
32+
33+

api/config/urlRules.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
'extraPatterns' => [
77
'GET status' => 'status',
88
'POST signup' => 'signup',
9+
'POST register' => 'register'
910
],
1011
'except' => ['create', 'delete', 'update', 'view', 'index'],
1112
],

api/modules/v1/controllers/UserController.php

Lines changed: 43 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,10 @@
33
namespace api\modules\v1\controllers;
44

55

6+
use api\modules\v1\models\User;
67
use base\rest\ActiveController;
8+
use phpDocumentor\Reflection\Types\Null_;
9+
use Yii;
710
use yii\filters\auth\CompositeAuth;
811
use yii\filters\auth\HttpBearerAuth;
912
use yii\helpers\ArrayHelper;
@@ -18,7 +21,7 @@ public function behaviors()
1821
[
1922
'class' => CompositeAuth::class,
2023
// Status endpoint is inherited from the ActiveController class
21-
'except' => ['status', 'signup'],
24+
'except' => ['status', 'signup', 'register'],
2225
'authMethods' => [
2326
HttpBearerAuth::class,
2427
],
@@ -28,7 +31,45 @@ public function behaviors()
2831

2932
public function actionSignup()
3033
{
31-
return 'working';
34+
$model = new User();
35+
$request = Yii::$app->request->post();
36+
37+
$model->username = $request["username"];
38+
$model->password = $request["password"];
39+
40+
$response = $model->login();
41+
42+
return $response;
43+
3244
}
3345

46+
public function actionRegister(){
47+
$model = new User();
48+
$request = Yii::$app->request->post();
49+
50+
$model->username = $request["username"];
51+
$model->password = $request["password"];
52+
$model->email = $request["username"];
53+
54+
$user = User::find()
55+
->where(['username' => $model->username])
56+
->one();
57+
58+
if($user == null){
59+
$register = User::register($model);
60+
return $register;
61+
62+
}
63+
else{
64+
return ["message" => "username is used by other user"];
65+
66+
}
67+
$response = $model->register();
68+
69+
return $response;
70+
71+
}
72+
73+
74+
3475
}

0 commit comments

Comments
 (0)