Based on yiisoft/yii2-app-advanced, create a new repository yii2-app-advanced on github, and create a new interface application (implement RESTful-style web service services. API), prepare some examples of automated tests (API tests) for user-related operations in the tests directory of the API to ensure that the application will not affect existing functions when changing or adding new functions (4)
1. Delete the controller \API\Controllers\SiteController.php, delete the model \api\models\contactform.php, \api\models\loginform.php, \api\models\PasswordResetRequ estform.php, \api\models\resetpasswordform.php, \api\models\signupform.php, delete the directory (view related) \api\views, \api\assets, \api\web\css
2. Delete the relevant files of the test (unit test, functional test, acceptance test), delete \api\tests\acceptance\homecest.php, delete \API\Tests\Functional\AboutCest.php, \API\Tests\Functional\ContactCest.php, \API\Tests\Functio NAL\HOMECEST.php, \API\Tests\Functional\logincest.php, \api\tests\functional\signupcest.php, delete \API\Tests\Unit\Models\ContactFormTest.php, \API\Tests\Unit\Models\PasswordResetRequestFormTest .php, \api\tests\unit\models\resetpasswordformtest.php, \api\tests\unit\models\signupformtest.php
3. Run all the sample tests, and report an error: Failures! Tests: 24, assertions: 57, failures: 5, as shown in Figure 1
Codeception php testing framework v2.4.1
Powered by PHPUnit 7.0.3 by Sebastian Bergmann and Contributors.
frontend\tests.functional tests (12) --------------------------------------------------------------------------------------------------
+ AboutCest: Check About (0.10s)
+ ContactCest: Check Contact (0.19s)
X ContactCest: Check Contact Submit No Data (0.04s)
x ContactCest: Check contact submit not correct email (0.02s)
+ ContactCest: Check Contact Submit Correct Data (0.08s)
+ HomeCest: Check Open (0.01s)
X LoginCest: Check Empty (0.02s)
+ LoginCest: Check Wrong Password (0.03s)
+ logincest: check valid login (0.58s)
X SignupCest: Signup with Empty Fields (0.02s)
X SignupCest: Signup with Wrong Email (0.03s)
+ SignupCest: Signup Successfully (0.59s)
-------------------------------------------------------------------- --------------------------------------------------------------------
frontend\tests.unit tests (8) ---------------------------------------------------------------------------------------------------------
+ ContactFormTest: send email (0.02s)
+ PasswordRequestFormTest: send message with wrong email address (0.03s)
+ PasswordRequestFormTest: not send emails to inactive user (0.03s)
+ PasswordRequestFormTest: send email successfully (0.04s)
+ ResetPasswordFormTest: Reset Wrong Token (0.03s)
+ ResetPasswordFormTest: Reset correct token (0.59s)
+ SignupFormTest: correct signup (1.14s)
+ SignupFormTest: Not Correct Signup (0.03s)
-------------------------------------------------------------------- --------------------------------------------------------------------
Time: 4.83 seconds, memory: 26.00MB
There were 5 failures:
----------
1) Contact: Check contact submit no data
test tests\functional\contactcest.php:CheckContactSubmitNoData
step see "name cannot be black",".help-block"
Fail failed asserting that any element by.help-blockon user /index-test.php/site/contact
+ <p class="help-block help-block-error">name cannot be empty. </p>
+ <p class="help-block help-block-error">email cannot be empty. </p>
+ <p class="help-block help-block-error">subject cannot be empty. </p>
+ <p class="help-block help-block-error">body cannot be empty. </p>
+ <p class="help-block help-block-error"> The verification code is incorrect. </p>
contains textName Cannot Be Blank
Scenario steps:
4. $i->see("name control be blank",".help-block") at tests\_support\functionaltester.php:26
3. $i->see("contact", "h1") at tests\functional\contactcest.php:23
2. $i->submitForm("#contact-form",[]) at tests\functional\contactcest.php:22
1. $i->amonuser(["site/contact"]) at tests\functional\contactcest.php:12
----------
2) Contact: Check contact submit not correct email
test tests\functional\contactcest.php:CheckContactSubmitNotCorrecTemail
Step see "Email is not a valid email address.",".help-block"
Fail failed asserting that any element by.help-blockon user /index-test.php/site/contact
+ <p class="help-block help-block-error"></p>
+ <p class="help-block help-block-error">email is not a valid email address. </p>
+ <p class="help-block help-block-error"></p>
+ <p class="help-block help-block-error"></p>
+ <p class="help-block help-block-error"></p>
contains textEmail is not a valid email address.
Scenario steps:
3. $i->see("Email is not a valid email address.",".help-block") at tests\_support\functionaltester.php:26
2. $I->SubmitForm("#Contact-Form",{"ContactForm[name]":"tester","ContactForm[email]":"tester.email","ContactForm[subject]":"test subject","ContactForm[b...})
at tests\functional\contactcest.php:34
1. $i->amonUser(["site/contact"]#atfp_close_translate_span#) at tests\functional\contactcest.php:12
----------
3) Logincest: check empty
test tests\functional\logincest.php:checkempty
step see "username cannot be blank.",".help-block"
Fail failed asserting that any element by.help-blockon user /index-test.php/site/login
+ <p class="help-block help-block-error">username cannot be empty. </p>
+ <p class="help-block help-block-error">password cannot be empty. </p>
+ <p class="help-block help-block-error"></p>
contains textUsername cannot be black.
Scenario steps:
3. $i->see("username cannot be blank.",".help-block") at tests\_support\FunctionalTester.php:26
2. $i->submitform("#login-form",{"loginform[username]":"","loginform[password]":""}) at tests\functional\logincest.php:42
1. $i->amonroute("site/login") at tests\functional\logincest.php:29
----------
4) Signupcest: Signup with empty fields
test tests\functional\signupcest.php:signupWithEmptyFields
step see "username cannot be blank.",".help-block"
Fail failed asserting that any element by.help-blockon user /index-test.php/site/signup
+ <p class="help-block help-block-error">username cannot be empty. </p>
+ <p class="help-block help-block-error">email cannot be empty. </p>
+ <p class="help-block help-block-error">password cannot be empty. </p>
contains textUsername cannot be black.
Scenario steps:
5. $i->see("username cannot be blank.",".help-block") at tests\_support\FunctionalTester.php:26
4. $i->SubmitForm("#form-signup",[]) at tests\functional\signupcest.php:21
3. $i->see("Please fill out the following fields to signup:") at tests\functional\signupcest.php:20
2. $i->see("signup", "h1") at tests\functional\signupcest.php:19
1. $i->amonroute("site/signup") at tests\functional\signupcest.php:14
----------
5) signupcest: signup with wrong email
test tests\functional\signupcest.php:signupWithWranggeMail
Step see "Email is not a valid email address.",".help-block"
Fail failed asserting that any element by.help-blockon user /index-test.php/site/signup
+ <p class="help-block help-block-error"></p>
+ <p class="help-block help-block-error">email is not a valid email address. </p>
+ <p class="help-block help-block-error"></p>
contains textEmail is not a valid email address.
Scenario steps:
5. $i->see("Email is not a valid email address.",".help-block") at tests\functional\signupcest.php:39
4. $i->dontsee("password cannot be blank.",".help-block") at tests\functional\signupcest.php:38
3. $i->dontsee("username cannot be blank.",".help-block") at tests\functional\signupcest.php:37
2. $i->submitform("#form-signup",{"signupform[username]":"tester","signupform[email]":"tttt","signupform[password]":"tester_password"}) at tests\functional\
signupcest.php:32
1. $i->amonroute("site/signup") at tests\functional\signupcest.php:14
Failures!
tests: 20, assertions: 48, failures: 5.
vendor/bin/codecept run
4. The 22nd step of (3) needs to be restored. This step causes the sample test to report an error, and it is adjusted to only support the content negotiation function in the case of a web application.
(1) Restore the editing of \common\config\main.php in step 22
(2) Edit \backend\config\main.php, \frontend\config\main.php
Note: If no language is detected in the request, use[[languages]] The first configuration item.
bootstrap=>['log', 'contentNegotiator'], components=>[ contentnegotiator=> [ class=>yii\filters\contentnegotiator, Languages=> [ en-us, en, ]#ATFP_CLOSE_Translate_span#, ], ],
(3) Edit \api\config\main.php
bootstrap=>['log', 'contentNegotiator'], components=>[ contentnegotiator=> [ class=>yii\filters\contentnegotiator, Formats=> [ application/json=> yii\web\response::format_json, application/xml=> yii\web\response::format_xml, ]#ATFP_CLOSE_Translate_span#, Languages=>[ en-us, en, ]#ATFP_CLOSE_Translate_span#, ], ],
Delete
Response=>[ Format=> yii\web\response::format_json, ]#ATFP_CLOSE_Translate_span#,
5. Open Windows PowerShell, execute the init command and select dev as the environment, and then reconfigure the database connection
.\init 0 yes yes yes yes yes yes
6. Run all the sample tests, which are in line with expectations, as shown in Figure 2
vendor/bin/codecept run
7. To start writing the API test, create the API test suite and run the command, as shown in Figure 3
vendor/bin/codecept generate:suite API -c API
Helper \API\Tests\Helper\API was created in E:\wwwroot\github-shuijingwan-yii2-app-advanced\api\tests/_support\helper\api.php Actor apitester was created in e:\wwwroot\github-shuijingwan-yii2-app-advanced\api\tests/_support\apitestter.php suite config api.suite.yml was created. Next steps: 1. Edit api.suite.yml to enable modules for this suite 2. Create first test with generate:cest testname ( or test|cept) command 3. Run tests of this suite with codecept Suite API Generated
8. Edit \api\tests\api.suite.yml to enable the module of the suite
Actor: Apitester modules: Enabled: - REST: url: /v1 Depends: yii2 -\API\Tests\Helper\API config: - Yii2
9. Create a test: get the user list (user/indexempty), as shown in Figure 4
vendor/bin/codecept generate:cest api user/indexempty -c api
test was created in e:\wwwroot\github-shuijingwan-yii2-app-advanced\api\tests\api\user\indexEmptyCest.php
10,[[yii\base\Application::version|version]], this property specifies the version of the application, the default is1.0, the configuration is: 1.0.0, edit \api\config\main.php, this value should be consistent with the tag on git
version=>1.0.0,
11. Edit \api\tests\_support\help\api.php, add the method of obtaining the current version number (subversion number. revision number)
<?php
namespace API\tests\help;
use yii;
use yii\helpers\stringhelper;
// here you can define custom actions
// all public methods declared in helper class will be available in $i
Class API extends \codeception\module
{
// Get the current version number (second version number. Revision number)
public function getMinorPatch() {
$version = StringHelper::explode(yii::$app->version,.);
return $Version[1]...$Version[2];
}
}
12. Run the build command, the new function has been added to the Apitester class, as shown in Figure 5
vendor/bin/codecept build -- -c API
13, \api\tests\_support\_generated\ApitesterActions.php was successfully generated, a new method getMinorPatch(), view, as shown in Figure 6
14. Install flow/jsonpath to check the structure of the response, as shown in Figure 7
composer require --prefer-dist flow/jsonpath
Using version ^0.4.0 for flow/jsonpath ./composer.json has been updated Loading composer repositories with package information Updating dependencies (included require-dev) Package operations: 1 install, 0 updates, 0 removes - Installing flow/jsonpath (0.4.0): downloading (100%) Writing lock file GENERATING AUTOLOAD FILES
15. It is necessary to implement a public method for each test, let the indexisJSON obtain the user list through the REST API test (the user list is empty, the JSON response), and let the indexisXML pass the REST API Test get user list (user list is empty, xml response), edit \api\tests\api\user\indexemptycest.php
<?php
namespace API\tests\user;
use yii;
use API\tests\apitester;
use codeception\util\httpcode;
use codeception\util\xml;
class indexEmptycest
{
public function _before(apitester $i)
{
}
public function _after(apitester $i)
{
}
// Get user list (user list is empty, json response)
public function indexesjson(apitestter $i)
{
$I->HaveHttpHeader(accept,application/json; version=. $i->getMinorPatch() .);
$i->sendGet(/users);
$I->SeeResponseCodeIs(httpcode::ok); // 200
$I->SeerEsponseIsJson();
// check the structure of the response
$I->SeerEsponseJsonMatchesJsonPath($.code);
$I->SeerEsponseJsonMatchesJsonPath($.message);
// check the response data
$I->SeeResponseContainsJSON([
code=> 20001,
Message=> yii::t(Error,20001,
]#atfp_close_translate_span#);
}
// Get user list (user list is empty, xml response)
public function indexXML(apitester $i)
{
$I->HaveHttpHeader(accept,application/xml; version=. $i->getMinorPatch() .);
$i->sendGet(/users);
$I->SeeResponseCodeIs(httpcode::ok); // 200
$I->SeeResponseIsXml();
// check the structure of the response
$I->SeExmlResponseMatchesXpath(//code);
$I->SeExmlResponseMatchesXpath(//message);
// check the response data
$I->SeExmlResponseIncludes(xml::toxml(['code' => 20001]));
$I->SeExmlResponseIncludes(xml::toxml(['message' => Yii::t('error', '20001')]));
}
}
16. Run the test, get the detailed output, and you can see the step by step behavior report, which is in line with expectations, as shown in Figure 8
vendor/bin/codecept run --steps -- -C API
Codeception php testing framework v2.4.1
Powered by PHPUnit 7.1.2 by Sebastian Bergmann and Contributors.
API\tests.api tests (2) ----------------------------------------------
IndexEmptyCest: Index is JSON
SIGNATURE: API\Tests\User\IndexEmptyCest:IndexIsJSON
test: tests\api\user\indexemptycest.php:indexisjson
Scenario --
I get minor patch
I have http header "accept","application/json; version=0.0"
I send get "/users"
I See Response Code Is 200
I See Response Is JSON
I See Response JSON MATCHES JSON Path "$.code"
I See Response JSON MATCHES JSON Path "$.message"
I see response contains json {"code":20001,"message":"user list is empty"}
Passed
IndexEmptyCest: Index is XML
SIGNATURE: API\Tests\User\IndexEmptyCest:IndexIsXML
test: tests\api\user\indexemptycest.php:indexisxml
Scenario --
I get minor patch
I have http header "accept","application/xml; version=0.0"
I send get "/users"
I See Response Code Is 200
I See Response Is XML
I See XML Response Matches XPath "//code"
I See XML Response Matches XPath "//message"
I See XML Response includes "DomDocument"
I See XML Response includes "DomDocument"
Passed
-----------------------------------------------------------------
API\tests.Functional tests (0) -----------------------------------------
-----------------------------------------------------------------
API\tests.unit tests (0) ---------------------------------------------
-----------------------------------------------------------------
Time: 1.11 seconds, memory: 16.00MB
OK (2 tests, 12 assertions)
17. Create a test: get the user list (user/index)
vendor/bin/codecept generate:cest api user/index -c API
18. You need to implement a public method for each test, use fixtures, let the indexisjson get the user list through the REST API test (get the user list success, json response), let the indexisxml pass the REST API Test to get user list (get user list success, xml response), edit \api\tests\api\user\indexcest.php
<?php
namespace API\tests\user;
use yii;
use API\tests\apitester;
use codeception\util\httpcode;
use codeception\util\xml;
Use API\Fixtures\UserFixture;
class index
{
public function _before(apitester $i)
{
}
public function _after(apitester $i)
{
}
/**
* @return array
*/
public function _fixtures()
{
return[
user=> [
class=> userfixture::classname(),
datafile=> codecept_data_dir() .user.php
]#ATFP_CLOSE_Translate_span#
];
}
// Get user list (get user list success, json response)
public function indexesjson(apitestter $i)
{
$I->HaveHttpHeader(accept,application/json; version=. $i->getMinorPatch() .);
$i->sendGet(/users);
$I->SeeResponseCodeIs(httpcode::ok); // 200
$I->SeerEsponseIsJson();
// check the structure of the response
$I->SeerEsponseJsonMatchesJsonPath($.code);
$I->SeerEsponseJsonMatchesJsonPath($.message);
$I->SeerEsponseJsonMatchesJsonPath($.data);
$I->SeerEsponseJsonMatchesJsonPath($.data.items);
$I->SeerEsponseJsonMatchesJsonPath($.data._links);
$I->SeerEsponseJsonMatchesJsonPath($.data._meta);
// check the response data
$I->SeeResponseContainsJSON([
code=> 10000,
Message=> yii::t(succeed,10001,
]#atfp_close_translate_span#);
}
// Get user list (get user list success, xml response)
public function indexXML(apitester $i)
{
$I->HaveHttpHeader(accept,application/xml; version=. $i->getMinorPatch() .);
$i->sendGet(/users);
$I->SeeResponseCodeIs(httpcode::ok); // 200
$I->SeeResponseIsXml();
// check the structure of the response
$I->SeExmlResponseMatchesXpath(//code);
$I->SeExmlResponseMatchesXpath(//message);
$I->SeExmlResponseMatchesXpath(//data);
$I->SeExmlResponseMatchesXpath(//data/items);
$I->SeExmlResponseMatchesXpath(//data/_links);
$I->SeExmlResponseMatchesXpath(//data/_meta);
// check the response data
$I->SeExmlResponseIncludes(xml::toxml(['code' => 10000]));
$I->SeExmlResponseIncludes(xml::toxml(['message' => Yii::t('success', '10001')]));
}
}
19. Run the test, get detailed output, you can see step by step behavior report, in line with expectations
vendor/bin/codecept run --steps -- -C API
index: index is json
SIGNATURE: API\Tests\User\IndExCEST:IndexIsJSON
test: tests\api\user\indexcest.php:indexisjson
Scenario --
I get minor patch
I have http header "accept","application/json; version=0.0"
I send get "/users"
I See Response Code Is 200
I See Response Is JSON
I See Response JSON MATCHES JSON Path "$.code"
I See Response JSON MATCHES JSON Path "$.message"
I See Response JSON MATCHES JSON Path "$.data"
I See Response JSON MATCHES JSON Path "$.data.items"
I See Response JSON MATCHES JSON Path "$.data._links"
I See Response JSON MATCHES JSON Path "$.data._meta"
I see response contains json {"code":10000,"message":"get user list success"}
Passed
index: index is xml
Signature: API\Tests\User\IndExCEST:IndexIsXML
test: tests\api\user\indexcest.php:indexisxml
Scenario --
I get minor patch
I have http header "accept","application/xml; version=0.0"
I send get "/users"
I See Response Code Is 200
I See Response Is XML
I See XML Response Matches XPath "//code"
I See XML Response Matches XPath "//message"
I See XML Response Matches XPath "//data"
I See XML Response Matches XPath "//data/items"
I See XML Response Matches XPath "//data/_links"
I See XML Response Matches XPath "//data/_meta"
I See XML Response includes "DomDocument"
I See XML Response includes "DomDocument"
Passed
20, get /users/{id}: return the details of the user {id}, create a test: get user details
vendor/bin/codecept generate:cest API user/view -c API
21. Edit \api\tests\api\user\viewcest.php to get user details (user ID: {id}, does not exist/user id: {id}, the status is deleted/obtained user details successfully)
<?php
namespace API\tests\user;
use yii;
use API\tests\apitester;
use codeception\util\httpcode;
use codeception\util\xml;
Use API\Fixtures\UserFixture;
class viewcest
{
const status_deleted = 0; //status: deleted
const status_active = 10; // status: active
public function _before(apitester $i)
{
}
public function _after(apitester $i)
{
}
/**
* @return array
*/
public function _fixtures()
{
return[
user=> [
class=> userfixture::classname(),
datafile=> codecept_data_dir() .user.php
]#ATFP_CLOSE_Translate_span#
];
}
// Get user details (Get user details success, JSON response)
public function viewwisjson(apitestter $i)
{
$id = 1;
$I->HaveHttpHeader(accept,application/json; version=. $i->getMinorPatch() .);
$i->sendGet(/users/. $id);
$I->SeeResponseCodeIs(httpcode::ok); // 200
$I->SeerEsponseIsJson();
// check the structure of the response
$I->SeerEsponseJsonMatchesJsonPath($.code);
$I->SeerEsponseJsonMatchesJsonPath($.message);
$I->SeerEsponseJsonMatchesJsonPath($.data);
// check the response data
$I->SeeResponseContainsJSON([
code=> 10000,
Message=> yii::t(succeed,10002,
]#atfp_close_translate_span#);
}
// Get user details (Get user details success, XML response)
public function viewIWISXML(apitester $i)
{
$id = 1;
$I->HaveHttpHeader(accept,application/xml; version=. $i->getMinorPatch() .);
$i->sendGet(/users/. $id);
$I->SeeResponseCodeIs(httpcode::ok); // 200
$I->SeeResponseIsXml();
// check the structure of the response
$I->SeExmlResponseMatchesXpath(//code);
$I->SeExmlResponseMatchesXpath(//message);
$I->SeExmlResponseMatchesXpath(//data);
// check the response data
$I->SeExmlResponseIncludes(xml::toxml(['code' => 10000]));
$I->SeExmlResponseIncludes(xml::toxml(['message' => Yii::t('success', '10002')]));
}
// Get user details, use the ID that does not exist (user ID: {id}, does not exist, json response)
public function viewWithNotExistIdisJSON(apitestter $i)
{
$id = 9999;
$I->HaveHttpHeader(accept,application/json; version=. $i->getMinorPatch() .);
$i->sendGet(/users/. $id);
$I->SeeResponseCodeIs(httpcode::not_found); // 404
$I->SeerEsponseIsJson();
// check the structure of the response
$I->SeerEsponseJsonMatchesJsonPath($.code);
$I->SeerEsponseJsonMatchesJsonPath($.message);
// check the response data
$I->SeeResponseContainsJSON([
code=> 20002,
Message=> yii::t(Error, yii::t(Error, yii::t(Error,20002), [ID=> $id]#atfp_close_translate_span#)),
]);
}
// Get user details, use the ID that does not exist (user ID: {id}, does not exist, xml response)
public function viewWithNotExitIdisXML(apitestter $i)
{
$id = 9999;
$I->HaveHttpHeader(accept,application/xml; version=. $i->getMinorPatch() .);
$i->sendGet(/users/. $id);
$I->SeeResponseCodeIs(httpcode::not_found); // 404
$I->SeeResponseIsXml();
// check the structure of the response
$I->SeExmlResponseMatchesXpath(//code);
$I->SeExmlResponseMatchesXpath(//message);
// check the response data
$I->SeExmlResponseIncludes(xml::toxml(['code' => 20002]));
$I->SeExmlResponseIncludes(xml::toxml(['message' => Yii::t('error', Yii::t('error', Yii::t('error', '20002'), ['id' => $id])])));
}
// Get user details (user ID: {id}, the status is deleted, json response)
public function viewStatusDeletedisJSON(apitestter $i)
{
$id = 2;
$I->HaveHttpHeader(accept,application/json; version=. $i->getMinorPatch() .);
$i->sendGet(/users/. $id);
$I->SeeResponseCodeIs(httpcode::ok); // 200
$I->SeerEsponseIsJson();
// check the structure of the response
$I->SeerEsponseJsonMatchesJsonPath($.code);
$I->SeerEsponseJsonMatchesJsonPath($.message);
// check the response data
$I->SeeResponseContainsJSON([
code=> 20003,
Message=> yii::t(Error, yii::t(Error, yii::t(Error,20003), [ID=> $id]#atfp_close_translate_span#)),
]);
}
// Get user details (user ID: {id}, the status is deleted, xml response)
public function viewStatusDeletedIsXML(Pitester $i)
{
$id = 2;
$I->HaveHttpHeader(accept,application/xml; version=. $i->getMinorPatch() .);
$i->sendGet(/users/. $id);
$I->SeeResponseCodeIs(httpcode::ok); // 200
$I->SeeResponseIsXml();
// check the structure of the response
$I->SeExmlResponseMatchesXpath(//code);
$I->SeExmlResponseMatchesXpath(//message);
// check the response data
$I->SeExmlResponseIncludes(xml::toxml(['code' => 20003]));
$I->SeExmlResponseIncludes(xml::toxml(['message' => Yii::t('error', Yii::t('error', Yii::t('error', '20003'), ['id' => $id])])));
}
}
22. Run the test, obtain detailed output, and you can see the step by step behavior report, which is in line with expectations
vendor/bin/codecept run --steps -- -C API
View Cest: View is JSON
signature: api\tests\user\viewcest:viewisjson
test: tests\api\user\viewcest.php:viewisjson
Scenario --
I get minor patch
I have http header "accept","application/json; version=0.0"
I send get "/users/1"
I See Response Code Is 200
I See Response Is JSON
I See Response JSON MATCHES JSON Path "$.code"
I See Response JSON MATCHES JSON Path "$.message"
I See Response JSON MATCHES JSON Path "$.data"
I see response contains json {"code":10000,"message":"get user details success"}
Passed
ViewCest: View is XML
SIGNATURE: API\Tests\User\ViewCest:ViewISXML
test: tests\api\user\viewcest.php:viewisxml
Scenario --
I get minor patch
I have http header "accept","application/xml; version=0.0"
I send get "/users/1"
I See Response Code Is 200
I See Response Is XML
I See XML Response Matches XPath "//code"
I See XML Response Matches XPath "//message"
I See XML Response Matches XPath "//data"
I See XML Response includes "DomDocument"
I See XML Response includes "DomDocument"
Passed
View with not exit id is json
SIGNATURE: API\Tests\User\ViewCest:ViewWithNotExistIdisJSON
test: tests\api\user\viewcest.php:viewWithNotExistIdisjson
Scenario --
I get minor patch
I have http header "accept","application/json; version=0.0"
I send get "/users/9999"
I See Response Code Is 404
I See Response Is JSON
I See Response JSON MATCHES JSON Path "$.code"
I See Response JSON MATCHES JSON Path "$.message"
I see response contains json {"code":20002,"message":"user id: 9999, does not exist"}
Passed
View Cest: View with Not Exist ID Is XML
Signature: API\Tests\User\ViewCest:ViewWithNotExistIdisXML
test: tests\api\user\viewcest.php:viewWithNotExistIdisxml
Scenario --
I get minor patch
I have http header "accept","application/xml; version=0.0"
I send get "/users/9999"
I See Response Code Is 404
I See Response Is XML
I See XML Response Matches XPath "//code"
I See XML Response Matches XPath "//message"
I See XML Response includes "DomDocument"
I See XML Response includes "DomDocument"
Passed
View Cest: View Status Deleted Is JSON
Signature: API\Tests\User\ViewCest:ViewStatusDeletedIsJSON
test: tests\api\user\viewcest.php:viewStatusDeletedisjson
Scenario --
I get minor patch
I have http header "accept","application/json; version=0.0"
I send get "/users/2"
I See Response Code Is 200
I See Response Is JSON
I See Response JSON MATCHES JSON Path "$.code"
I See Response JSON MATCHES JSON Path "$.message"
I see response contains json {"code":20003,"message":"user id: 2, status is deleted"}
Passed
View Cest: View Status Deleted Is XML
Signature: API\Tests\User\ViewCest:ViewStatusDeletedIsXML
test: tests\api\user\viewcest.php:viewStatusDeletedisXML
Scenario --
I get minor patch
I have http header "accept","application/xml; version=0.0"
I send get "/users/2"
I See Response Code Is 200
I See Response Is XML
I See XML Response Matches XPath "//code"
I See XML Response Matches XPath "//message"
I See XML Response includes "DomDocument"
I See XML Response includes "DomDocument"
Passed
23. POST /users: Create a new user, create a test: create a user
vendor/bin/codecept generate:cest API user/create -c API
24. Edit \API\Tests\API\User\CreateCest.php, support (creation user success, data validation failed: {firsErrors})
<?php
namespace API\tests\user;
use yii;
use API\tests\apitester;
use codeception\util\httpcode;
use codeception\util\xml;
Use API\Fixtures\UserFixture;
class createcest
{
public function _before(apitester $i)
{
}
public function _after(apitester $i)
{
}
/**
* @return array
*/
public function _fixtures()
{
return[
user=> [
class=> userfixture::classname(),
datafile=> codecept_data_dir() .user.php
]#ATFP_CLOSE_Translate_span#
];
}
// Create user (create user success, json response)
public function createIsJSON(Pitester $i)
{
$data =[
username=>1111111,
email=>111111@163.com,
Password=>1111111,
]#atfp_close_translate_span#;
$I->HaveHttpHeader(accept,application/json; version=. $i->getMinorPatch() .);
$i->sendPost(/users, $data);
$I->SeeResponseCodeIs(httpCode::Created); // 201
$I->SeerEsponseIsJson();
// check the structure of the response
$I->SeerEsponseJsonMatchesJsonPath($.code);
$I->SeerEsponseJsonMatchesJsonPath($.message);
$I->SeerEsponseJsonMatchesJsonPath($.data);
$I->SeerEsponseJsonMatchesJsonPath($.data.username);
$I->SeerEsponseJsonMatchesJsonPath($.data.email);
// check the response data
$I->SeeResponseContainsJSON([
code=> 10000,
Message=> yii::t(succeed,10003,
data=> [
username=> $data[username]#ATFP_CLOSE_Translate_span#,
email=> $data['email'],
],
]);
}
// Create a user (create user success, XML response)
public function createIsXML(PiteSter $i)
{
$data =[
username=>1111111,
email=>111111@163.com,
Password=>1111111,
]#atfp_close_translate_span#;
$I->HaveHttpHeader(accept,application/xml; version=. $i->getMinorPatch() .);
$i->sendPost(/users, $data);
$I->SeeResponseCodeIs(httpCode::Created); // 201
$I->SeeResponseIsXml();
// check the structure of the response
$I->SeExmlResponseMatchesXpath(//code);
$I->SeExmlResponseMatchesXpath(//message);
$I->SeExmlResponseMatchesXpath(//data);
$I->SeExmlResponseMatchesXpath(//data//username);
$I->SeExmlResponseMatchesXpath(//data//email);
// check the response data
$I->SeExmlResponseIncludes(xml::toxml(['code' => 10000]));
$I->SeExmlResponseIncludes(xml::toxml(['message' => Yii::t('success', '10003')]));
$I->SeExmlResponseIncludes(xml::toxml(['username' => $data['username']]));
$I->SeExmlResponseIncludes(xml::toxml(['email' => $data['email']]));
}
// Create user, use empty field values (data validation failed: {firsErrors}, json response)
public function createWithEmptyFieldsisJson(apitester $i)
{
$data =[];
$I->HaveHttpHeader(accept,application/json; version=. $i->getMinorPatch() .);
$i->sendPost(/users, $data);
$I->SeeResponseCodeIs(httpCode::Unprocessable_entity); // 422
$I->SeerEsponseIsJson();
// check the structure of the response
$I->SeerEsponseJsonMatchesJsonPath($.code);
$I->SeerEsponseJsonMatchesJsonPath($.message);
// check the response data
$I->SeeResponseContainsJSON(['code' => 20004]);
$I->SeeResponseContainsJSON(['message' => 'Data validation failed: Username cannot be blank.']);
}
// Create user, use empty field value (data validation failed: {firsErrors}, xml response)
public function createWithEmptyFieldsisxml(apitester $i)
{
$data =[];
$I->HaveHttpHeader(accept,application/xml; version=. $i->getMinorPatch() .);
$i->sendPost(/users, $data);
$I->SeeResponseCodeIs(httpCode::Unprocessable_entity); // 422
$I->SeeResponseIsXml();
// check the structure of the response
$I->SeExmlResponseMatchesXpath(//code);
$I->SeExmlResponseMatchesXpath(//message);
// check the response data
$I->SeExmlResponseIncludes(xml::toxml(['code' => 20004]));
$I->SeExmlResponseIncludes(xml::toxml(['message' => 'Data validation failed: Username cannot be blank.']));
}
// Create a user, use the wrong mailbox (data verification failed: {firsErrors}, json response)
public function createWithWronggeMailIsJSON(Pitestter $i)
{
$data =[
username=>1111111,
email=>1111111,
Password=>1111111,
]#atfp_close_translate_span#;
$I->HaveHttpHeader(accept,application/json; version=. $i->getMinorPatch() .);
$i->sendPost(/users, $data);
$I->SeeResponseCodeIs(httpCode::Unprocessable_entity); // 422
$I->SeerEsponseIsJson();
// check the structure of the response
$I->SeerEsponseJsonMatchesJsonPath($.code);
$I->SeerEsponseJsonMatchesJsonPath($.message);
// check the response data
$I->SeeResponseContainsJSON(['code' => 20004]);
$I->SeeResponseContainsJSON(['message' => 'Data validation failed: Email is not a valid email address.']);
}
// Create a user, use the wrong mailbox (data verification failed: {firsErrors}, xml response)
public function createWithWronggeMailSxml(apitestter $i)
{
$data =[
username=>1111111,
email=>1111111,
Password=>1111111,
]#atfp_close_translate_span#;
$I->HaveHttpHeader(accept,application/xml; version=. $i->getMinorPatch() .);
$i->sendPost(/users, $data);
$I->SeeResponseCodeIs(httpCode::Unprocessable_entity); // 422
$I->SeeResponseIsXml();
// check the structure of the response
$I->SeExmlResponseMatchesXpath(//code);
$I->SeExmlResponseMatchesXpath(//message);
// check the response data
$I->SeExmlResponseIncludes(xml::toxml(['code' => 20004]));
$I->SeExmlResponseIncludes(xml::toxml(['message' => 'Data validation failed: Email is not a valid email address.']));
}
// Create user, use the existing field value (data validation failed: {firsErrors}, json response)
public function createWithExistFieldsisJson(apitestter $i)
{
$data =[
username=>Troy.Becker,
email=>nicolas.dianna@hotmail.com,
Password=>some_password,
]#atfp_close_translate_span#;
$I->HaveHttpHeader(accept,application/json; version=. $i->getMinorPatch() .);
$i->sendPost(/users, $data);
$I->SeeResponseCodeIs(httpCode::Unprocessable_entity); // 422
$I->SeerEsponseIsJson();
// check the structure of the response
$I->SeerEsponseJsonMatchesJsonPath($.code);
$I->SeerEsponseJsonMatchesJsonPath($.message);
// check the response data
$I->SeeResponseContainsJSON(['code' => 20004]);
$I->SeeResponseContainsJSON(['message' => 'Data validation failed: Username "troy.becker" has already been taken.']);
}
// Create user, use the existing field value (data validation failed: {firsErrors}, xml response)
public function createWithExistFieldsisXML(apitestter $i)
{
$data =[
username=>Troy.Becker,
email=>nicolas.dianna@hotmail.com,
Password=>some_password,
]#atfp_close_translate_span#;
$I->HaveHttpHeader(accept,application/xml; version=. $i->getMinorPatch() .);
$i->sendPost(/users, $data);
$I->SeeResponseCodeIs(httpCode::Unprocessable_entity); // 422
$I->SeeResponseIsXml();
// check the structure of the response
$I->SeExmlResponseMatchesXpath(//code);
$I->SeExmlResponseMatchesXpath(//message);
// check the response data
$I->SeExmlResponseIncludes(xml::toxml(['code' => 20004]));
$I->SeExmlResponseIncludes(xml::toxml(['message' => 'Data validation failed: Username "troy.becker" has already been taken.']));
}
}
25. Run the test, get detailed output, you can see step by step behavior report, which is in line with expectations
vendor/bin/codecept run --steps -- -C API
Create is JSON
Signature: API\Tests\User\CreateCest:CreateIsJson
test: tests\api\user\createcest.php:createisjson
Scenario --
I get minor patch
I have http header "accept","application/json; version=0.0"
I send post "/users",{"username":"111111","email":"111111@163.com","password":"111111"}
I See Response Code Is 201
I See Response Is JSON
I See Response JSON MATCHES JSON Path "$.code"
I See Response JSON MATCHES JSON Path "$.message"
I See Response JSON MATCHES JSON Path "$.data"
I See Response JSON MATCHES JSON Path "$.data.username"
I See Response JSON MATCHES JSON Path "$.data.email"
I see response contains json {"code":10000,"message":"create user Success","data":{"username":"111111","email":"111...}
Passed
CreateCest: create is xml
Signature: API\Tests\User\CreateCest:CreateIsXML
test: tests\api\user\createcest.php:createisxml
Scenario --
I get minor patch
I have http header "accept","application/xml; version=0.0"
I send post "/users",{"username":"111111","email":"111111@163.com","password":"111111"}
I See Response Code Is 201
I See Response Is XML
I See XML Response Matches XPath "//code"
I See XML Response Matches XPath "//message"
I See XML Response Matches XPath "//data"
I see XML Response Matches XPath "//data//username"
I See XML Response Matches XPath "//data//email"
I See XML Response includes "DomDocument"
I See XML Response includes "DomDocument"
I See XML Response includes "DomDocument"
I See XML Response includes "DomDocument"
Passed
Create with empty fields is JSON
Signature: API\Tests\User\CreateCest:CreateWithEmptyFieldsisJSON
test: tests\api\user\createcest.php:CreateWithEmptyFieldsisJson
Scenario --
I get minor patch
I have http header "accept","application/json; version=0.0"
I send post "/users",[]
I See Response Code Is 422
I See Response Is JSON
I See Response JSON MATCHES JSON Path "$.code"
I See Response JSON MATCHES JSON Path "$.message"
I see response contains json {"code":20004}
I see response contains json {"message":"data validation failed: username cannot be blank."}
Passed
Create with empty fields is XML
SIGNATURE: API\Tests\User\CreateCest:CreateWithEmptyFieldsisXML
test: tests\api\user\createcest.php:CreateWithEmptyFieldsisXML
Scenario --
I get minor patch
I have http header "accept","application/xml; version=0.0"
I send post "/users",[]
I See Response Code Is 422
I See Response Is XML
I See XML Response Matches XPath "//code"
I See XML Response Matches XPath "//message"
I See XML Response includes "DomDocument"
I See XML Response includes "DomDocument"
Passed
Create with wrong email is json
SIGNATURE: API\Tests\User\CreateCest:CreateWithWrongEmailisJSON
test: tests\api\user\createcest.php:createwithwronegmailisjson
Scenario --
I get minor patch
I have http header "accept","application/json; version=0.0"
I send post "/users",{"username":"111111","email":"111111","password":"111111"}
I See Response Code Is 422
I See Response Is JSON
I See Response JSON MATCHES JSON Path "$.code"
I See Response JSON MATCHES JSON Path "$.message"
I see response contains json {"code":20004}
I see response contains json {"message":"data validation failed: email is not a valid email address."}
Passed
Create with wrong email is XML
SIGNATURE: API\Tests\User\CreateCest:CreateWithWrongEmailIsXML
test: tests\api\user\createcest.php:createWithWrongEmailIsXML
Scenario --
I get minor patch
I have http header "accept","application/xml; version=0.0"
I send post "/users",{"username":"111111","email":"111111","password":"111111"}
I See Response Code Is 422
I See Response Is XML
I See XML Response Matches XPath "//code"
I See XML Response Matches XPath "//message"
I See XML Response includes "DomDocument"
I See XML Response includes "DomDocument"
Passed
Create with existing fields is JSON
SIGNATURE: API\Tests\User\CreateCest:CreateWithExistFieldsisJson
test: tests\api\user\createcest.php:createWithExistFieldsisJson
Scenario --
I get minor patch
I have http header "accept","application/json; version=0.0"
I send post "/users",{"username":"troy.becker","email":"nicolas.dianna@hotmail.com","password":"some_password"}
I See Response Code Is 422
I See Response Is JSON
I See Response JSON MATCHES JSON Path "$.code"
I See Response JSON MATCHES JSON Path "$.message"
I see response contains json {"code":20004}
I See Response Contains JSON {"message":"Data Validation Failed: Username "Troy.Becker" has already been taken."}
Passed
Create with existing fields is XML
Signature: API\Tests\User\CreateCest:CreateWithExistFieldSISXML
test: tests\api\user\createcest.php:createWithExistFieldSiSXML
Scenario --
I get minor patch
I have http header "accept","application/xml; version=0.0"
I send post "/users",{"username":"troy.becker","email":"nicolas.dianna@hotmail.com","password":"some_password"}
I See Response Code Is 422
I See Response Is XML
I See XML Response Matches XPath "//code"
I See XML Response Matches XPath "//message"
I See XML Response includes "DomDocument"
I See XML Response includes "DomDocument"
Passed
26, put /users/{id}: update a user, copy \api\tests\api\user\createcest.php to \API\Tests\API\User\UpdateCest.php, update user (user ID: {id}, does not exist/data authentication failed: {firsErrors}/update user success), edit
<?php
namespace API\tests\user;
use yii;
use API\tests\apitester;
use codeception\util\httpcode;
use codeception\util\xml;
Use API\Fixtures\UserFixture;
class updatecest
{
public function _before(apitester $i)
{
}
public function _after(apitester $i)
{
}
/**
* @return array
*/
public function _fixtures()
{
return[
user=> [
class=> userfixture::classname(),
datafile=> codecept_data_dir() .user.php
]#ATFP_CLOSE_Translate_span#
];
}
// Update user (update user success, json response)
public function updateIsJSON(apitester $i)
{
$id = 1;
$data =[
email=>111111@163.com,
Password=>1111111,
status=> 0,
]#atfp_close_translate_span#;
$I->HaveHttpHeader(accept,application/json; version=. $i->getMinorPatch() .);
$I->sendput(/users/. $id, $data);
$I->SeeResponseCodeIs(httpcode::ok); // 200
$I->SeerEsponseIsJson();
// check the structure of the response
$I->SeerEsponseJsonMatchesJsonPath($.code);
$I->SeerEsponseJsonMatchesJsonPath($.message);
$I->SeerEsponseJsonMatchesJsonPath($.data);
$I->SeerEsponseJsonMatchesJsonPath($.data.email);
$I->SeerEsponseJsonMatchesJsonPath($.data.status);
// check the response data
$I->SeeResponseContainsJSON([
code=> 10000,
Message=> yii::t(succeed,10004,
data=> [
email=> $data[email]#ATFP_CLOSE_Translate_span#,
status=> $data['status'],
],
]);
}
// Update user (update user success, XML response)
public function updateIsXML(Pitester $i)
{
$id = 1;
$data =[
email=>111111@163.com,
Password=>1111111,
status=> 0,
]#atfp_close_translate_span#;
$I->HaveHttpHeader(accept,application/xml; version=. $i->getMinorPatch() .);
$I->sendput(/users/. $id, $data);
$I->SeeResponseCodeIs(httpcode::ok); // 200
$I->SeeResponseIsXml();
// check the structure of the response
$I->SeExmlResponseMatchesXpath(//code);
$I->SeExmlResponseMatchesXpath(//message);
$I->SeExmlResponseMatchesXpath(//data);
$I->SeExmlResponseMatchesXpath(//data//email);
$I->SeExmlResponseMatchesXpath(//data//status);
// check the response data
$I->SeExmlResponseIncludes(xml::toxml(['code' => 10000]));
$I->SeExmlResponseIncludes(xml::toxml(['message' => Yii::t('success', '10004')]));
$I->SeExmlResponseIncludes(xml::toxml(['email' => $data['email']]));
$I->SeExmlResponseIncludes(xml::toxml(['status' => $data['status']]));
}
// Update the user, use the ID that does not exist (data verification failed: {firsErrors}, json response)
public function updateWithNotExitIdisJSON(apitester $i)
{
$id = 9999;
$data =[
email=>111111@163.com,
Password=>1111111,
status=> 0,
]#atfp_close_translate_span#;
$I->HaveHttpHeader(accept,application/json; version=. $i->getMinorPatch() .);
$I->sendput(/users/. $id, $data);
$I->SeeResponseCodeIs(httpcode::not_found); // 404
$I->SeerEsponseIsJson();
// check the structure of the response
$I->SeerEsponseJsonMatchesJsonPath($.code);
$I->SeerEsponseJsonMatchesJsonPath($.message);
// check the response data
$I->SeeResponseContainsJSON(['code' => 20002]);
$I->SeeResponseContainsJSON(['message' => 'User ID: 9999, does not exist']);
}
// Update the user, use the ID that does not exist (data verification failed: {firsErrors}, json response)
public function updateWithNotExitIdisXML(apitestter $i)
{
$id = 9999;
$data =[
email=>111111@163.com,
Password=>1111111,
status=> 0,
]#atfp_close_translate_span#;
$I->HaveHttpHeader(accept,application/xml; version=. $i->getMinorPatch() .);
$I->sendput(/users/. $id, $data);
$I->SeeResponseCodeIs(httpcode::not_found); // 404
$I->SeeResponseIsXml();
// check the structure of the response
$I->SeExmlResponseMatchesXpath(//code);
$I->SeExmlResponseMatchesXpath(//message);
// check the response data
$I->SeExmlResponseIncludes(xml::toxml(['code' => 20002]));
$I->SeExmlResponseIncludes(xml::toxml(['message' => 'User ID: 9999, does not exist']));
}
// Update user, use empty field value (data validation failed: {firsErrors}, json response)
public function updateWithEmptyFieldsisJSON(apitester $i)
{
$id = 1;
$data =[];
$I->HaveHttpHeader(accept,application/json; version=. $i->getMinorPatch() .);
$I->sendput(/users/. $id, $data);
$I->SeeResponseCodeIs(httpCode::Unprocessable_entity); // 422
$I->SeerEsponseIsJson();
// check the structure of the response
$I->SeerEsponseJsonMatchesJsonPath($.code);
$I->SeerEsponseJsonMatchesJsonPath($.message);
// check the response data
$I->SeeResponseContainsJSON(['code' => 20004]);
$I->SeeResponseContainsJSON(['message' => 'Data validation failed: Email cannot be blank.']);
}
// Update user, use empty field value (data validation failed: {firsErrors}, json response)
public function updateWithEmptyFieldsisxml(apitester $i)
{
$id = 1;
$data =[];
$I->HaveHttpHeader(accept,application/xml; version=. $i->getMinorPatch() .);
$I->sendput(/users/. $id, $data);
$I->SeeResponseCodeIs(httpCode::Unprocessable_entity); // 422
$I->SeeResponseIsXml();
// check the structure of the response
$I->SeExmlResponseMatchesXpath(//code);
$I->SeExmlResponseMatchesXpath(//message);
// check the response data
$I->SeExmlResponseIncludes(xml::toxml(['code' => 20004]));
$I->SeExmlResponseIncludes(xml::toxml(['message' => 'Data validation failed: Email cannot be blank.']));
}
// Update the user, use the wrong mailbox (data verification failed: {firsErrors}, json response)
public function updateWithWranggeMailIsJSON(Pitester $i)
{
$id = 1;
$data =[
email=>1111111,
Password=>1111111,
status=> 0,
]#atfp_close_translate_span#;
$I->HaveHttpHeader(accept,application/json; version=. $i->getMinorPatch() .);
$I->sendput(/users/. $id, $data);
$I->SeeResponseCodeIs(httpCode::Unprocessable_entity); // 422
$I->SeerEsponseIsJson();
// check the structure of the response
$I->SeerEsponseJsonMatchesJsonPath($.code);
$I->SeerEsponseJsonMatchesJsonPath($.message);
// check the response data
$I->SeeResponseContainsJSON(['code' => 20004]);
$I->SeeResponseContainsJSON(['message' => 'Data validation failed: Email is not a valid email address.']);
}
// Update the user, use the wrong mailbox (data verification failed: {firsErrors}, json response)
public function updateWithWronggeMailSXML(apitestter $i)
{
$id = 1;
$data =[
email=>1111111,
Password=>1111111,
status=> 0,
]#atfp_close_translate_span#;
$I->HaveHttpHeader(accept,application/xml; version=. $i->getMinorPatch() .);
$I->sendput(/users/. $id, $data);
$I->SeeResponseCodeIs(httpCode::Unprocessable_entity); // 422
$I->SeeResponseIsXml();
// check the structure of the response
$I->SeExmlResponseMatchesXpath(//code);
$I->SeExmlResponseMatchesXpath(//message);
// check the response data
$I->SeExmlResponseIncludes(xml::toxml(['code' => 20004]));
$I->SeExmlResponseIncludes(xml::toxml(['message' => 'Data validation failed: Email is not a valid email address.']));
}
// Update the user, use the existing field value (data verification failed: {firsErrors}, json response)
public function updateWithExistFieldsisJson(apitester $i)
{
$id = 1;
$data =[
email=>nicolas.dianna@hotmail.com,
Password=>1111111,
status=> 0,
]#atfp_close_translate_span#;
$I->HaveHttpHeader(accept,application/json; version=. $i->getMinorPatch() .);
$I->sendput(/users/. $id, $data);
$I->SeeResponseCodeIs(httpCode::Unprocessable_entity); // 422
$I->SeerEsponseIsJson();
// check the structure of the response
$I->SeerEsponseJsonMatchesJsonPath($.code);
$I->SeerEsponseJsonMatchesJsonPath($.message);
// check the response data
$I->SeeResponseContainsJSON(['code' => 20004]);
$I->SeeResponseContainsJSON(['message' => 'Data validation failed: Email "nicolas.dianna@hotmail.com" has already been taken.']);
}
// Update the user, use the existing field value (data verification failed: {firsErrors}, json response)
public function updateWithExistFieldsisXML(apitestter $i)
{
$id = 1;
$data =[
email=>nicolas.dianna@hotmail.com,
Password=>1111111,
status=> 0,
]#atfp_close_translate_span#;
$I->HaveHttpHeader(accept,application/xml; version=. $i->getMinorPatch() .);
$I->sendput(/users/. $id, $data);
$I->SeeResponseCodeIs(httpCode::Unprocessable_entity); // 422
$I->SeeResponseIsXml();
// check the structure of the response
$I->SeExmlResponseMatchesXpath(//code);
$I->SeExmlResponseMatchesXpath(//message);
// check the response data
$I->SeExmlResponseIncludes(xml::toxml(['code' => 20004]));
$I->SeExmlResponseIncludes(xml::toxml(['message' => 'Data validation failed: Email "nicolas.dianna@hotmail.com" has already been taken.']));
}
// Update the user, use the state value that is out of range (data validation failed: {firsErrors}, json response)
public function updateWithNoTrangeStatusisJSON(apitester $i)
{
$id = 1;
$data =[
email=>111111@163.com,
Password=>1111111,
status=> 5,
]#atfp_close_translate_span#;
$I->HaveHttpHeader(accept,application/json; version=. $i->getMinorPatch() .);
$I->sendput(/users/. $id, $data);
$I->SeeResponseCodeIs(httpCode::Unprocessable_entity); // 422
$I->SeerEsponseIsJson();
// check the structure of the response
$I->SeerEsponseJsonMatchesJsonPath($.code);
$I->SeerEsponseJsonMatchesJsonPath($.message);
// check the response data
$I->SeeResponseContainsJSON(['code' => 20004]);
$I->SeeResponseContainsJSON(['message' => 'Data validation failed: Status is invalid.']);
}
// Update the user, use the state value that is out of range (data validation failed: {firsErrors}, json response)
public function updateWithNoTrangeStatusisXML(apitestter $i)
{
$id = 1;
$data =[
email=>111111@163.com,
Password=>1111111,
status=> 5,
]#atfp_close_translate_span#;
$I->HaveHttpHeader(accept,application/xml; version=. $i->getMinorPatch() .);
$I->sendput(/users/. $id, $data);
$I->SeeResponseCodeIs(httpCode::Unprocessable_entity); // 422
$I->SeeResponseIsXml();
// check the structure of the response
$I->SeExmlResponseMatchesXpath(//code);
$I->SeExmlResponseMatchesXpath(//message);
// check the response data
$I->SeExmlResponseIncludes(xml::toxml(['code' => 20004]));
$I->SeExmlResponseIncludes(xml::toxml(['message' => 'Data validation failed: Status is invalid.']));
}
}
27. Run the test, obtain detailed output, and you can see the step by step behavior report, which is in line with expectations
vendor/bin/codecept run --steps -- -C API
updatecest: update is json
Signature: API\Tests\User\UpdateCest:UpdateIsJSON
test: tests\api\user\updatecest.php:updateisjson
Scenario --
I get minor patch
I have http header "accept","application/json; version=0.0"
I send put "/users/1",{"email":"111111@163.com","password":"111111","status":0}
I See Response Code Is 200
I See Response Is JSON
I See Response JSON MATCHES JSON Path "$.code"
I See Response JSON MATCHES JSON Path "$.message"
I See Response JSON MATCHES JSON Path "$.data"
I See Response JSON MATCHES JSON Path "$.data.email"
I See Response JSON MATCHES JSON Path "$.data.status"
I see response contains json {"code":10000,"message":"update user success","data":{"email":"111111@163.com","status...}
Passed
updatecest: update is xml
Signature: API\Tests\User\UpdateCest:UpdateIsXML
test: tests\api\user\updatecest.php:updateIsXML
Scenario --
I get minor patch
I have http header "accept","application/xml; version=0.0"
I send put "/users/1",{"email":"111111@163.com","password":"111111","status":0}
I See Response Code Is 200
I See Response Is XML
I See XML Response Matches XPath "//code"
I See XML Response Matches XPath "//message"
I See XML Response Matches XPath "//data"
I See XML Response Matches XPath "//data//email"
I see XML Response Matches XPath "//data//status"
I See XML Response includes "DomDocument"
I See XML Response includes "DomDocument"
I See XML Response includes "DomDocument"
I See XML Response includes "DomDocument"
Passed
UpdateCest: update with not exist id is json
Signature: API\Tests\User\UpdateCest:UpdateWithNotExistIdisJSON
test: tests\API\User\UpdateCest.php:UpdateWithNotExistIdisJSON
Scenario --
I get minor patch
I have http header "accept","application/json; version=0.0"
I send put "/users/9999",{"email":"111111@163.com","password":"111111","status":0}
I See Response Code Is 404
I See Response Is JSON
I See Response JSON MATCHES JSON Path "$.code"
I See Response JSON MATCHES JSON Path "$.message"
I see response contains json {"code":20002}
I see response contains json {"message":"user id: 9999, does not exist"}
Passed
UpdateCest: Update with not exist ID is XML
SIGNATURE: API\Tests\User\UpdateCest:UpdateWithNotExistIdisXML
test: tests\api\user\updatecest.php:UpdateWithNotExistIdisXML
Scenario --
I get minor patch
I have http header "accept","application/xml; version=0.0"
I send put "/users/9999",{"email":"111111@163.com","password":"111111","status":0}
I See Response Code Is 404
I See Response Is XML
I See XML Response Matches XPath "//code"
I See XML Response Matches XPath "//message"
I See XML Response includes "DomDocument"
I See XML Response includes "DomDocument"
Passed
UpdateCest: Update with empty fields is JSON
SIGNATURE: API\Tests\User\UpdateCest:UpdateWithEmptyFieldsisJson
test: tests\api\user\updatecest.php:UpdateWithEmptyFieldsisJson
Scenario --
I get minor patch
I have http header "accept","application/json; version=0.0"
I send put "/users/1",[]
I See Response Code Is 422
I See Response Is JSON
I See Response JSON MATCHES JSON Path "$.code"
I See Response JSON MATCHES JSON Path "$.message"
I see response contains json {"code":20004}
I see response contains json {"message":"data validation failed: email cannot be blank."}
Passed
UpdateCest: Update with empty fields is XML
SIGNATURE: API\Tests\User\UpdateCest:UpdateWithEmptyFieldsisXML
test: tests\API\User\UpdateCest.php:UpdateWithEmptyFieldsisXML
Scenario --
I get minor patch
I have http header "accept","application/xml; version=0.0"
I send put "/users/1",[]
I See Response Code Is 422
I See Response Is XML
I See XML Response Matches XPath "//code"
I See XML Response Matches XPath "//message"
I See XML Response includes "DomDocument"
I See XML Response includes "DomDocument"
Passed
UpdateCest: Update with wrong Email is JSON
SIGNATURE: API\Tests\User\UpdateCest:UpdateWithWronggeMailIsJSON
test: tests\api\user\updatecest.php:UpdateWithWrangEmailisJSON
Scenario --
I get minor patch
I have http header "accept","application/json; version=0.0"
I send put "/users/1",{"email":"111111","password":"111111","status":0}
I See Response Code Is 422
I See Response Is JSON
I See Response JSON MATCHES JSON Path "$.code"
I See Response JSON MATCHES JSON Path "$.message"
I see response contains json {"code":20004}
I see response contains json {"message":"data validation failed: email is not a valid email address."}
Passed
UpdateCest: Update with wrong Email is XML
SIGNATURE: API\Tests\User\UpdateCest:UpdateWithWrangEmailIsXML
test: tests\api\user\updatecest.php:UpdateWithWronggeMailIsXML
Scenario --
I get minor patch
I have http header "accept","application/xml; version=0.0"
I send put "/users/1",{"email":"111111","password":"111111","status":0}
I See Response Code Is 422
I See Response Is XML
I See XML Response Matches XPath "//code"
I See XML Response Matches XPath "//message"
I See XML Response includes "DomDocument"
I See XML Response includes "DomDocument"
Passed
UpdateCest: Update with existing fields is JSON
Signature: API\Tests\User\UpdateCest:UpdateWithExistFieldsisJson
test: tests\api\user\updatecest.php:updateWithExistFieldsisjson
Scenario --
I get minor patch
I have http header "accept","application/json; version=0.0"
I send put "/users/1",{"email":"nicolas.dianna@hotmail.com","password":"111111","status":0}
I See Response Code Is 422
I See Response Is JSON
I See Response JSON MATCHES JSON Path "$.code"
I See Response JSON MATCHES JSON Path "$.message"
I see response contains json {"code":20004}
I see response contains json {"message":"data validation failed: email "nicolas.dianna@hotmail.com" has already bee...}
Passed
UpdateCest: Update with existing fields is XML
SIGNATURE: API\Tests\User\UpdateCest:UpdateWithExistFieldSISXML
test: tests\API\User\UpdateCest.php:UpdateWithExistFieldsisXML
Scenario --
I get minor patch
I have http header "accept","application/xml; version=0.0"
I send put "/users/1",{"email":"nicolas.dianna@hotmail.com","password":"111111","status":0}
I See Response Code Is 422
I See Response Is XML
I See XML Response Matches XPath "//code"
I See XML Response Matches XPath "//message"
I See XML Response includes "DomDocument"
I See XML Response includes "DomDocument"
Passed
UpdateCest: update with not range status is json
SIGNATURE: API\Tests\User\UpdateCest:UpdateWithNoTrangeStatusJSON
test: tests\api\user\updatecest.php:updateWithNoTrangeStatusisJSON
Scenario --
I get minor patch
I have http header "accept","application/json; version=0.0"
I send put "/users/1",{"email":"111111@163.com","password":"111111","status":5}
I See Response Code Is 422
I See Response Is JSON
I See Response JSON MATCHES JSON Path "$.code"
I See Response JSON MATCHES JSON Path "$.message"
I see response contains json {"code":20004}
I see response contains json {"message":"data validation failed: status is invalid."}
Passed
UpdateCest: Update with not range status is XML
Signature: API\Tests\User\UpdateCest:UpdateWithNoRangeStatuSXML
test: tests\api\user\updatecest.php:UpdateWithNoTrangeStatuSXML
Scenario --
I get minor patch
I have http header "accept","application/xml; version=0.0"
I send put "/users/1",{"email":"111111@163.com","password":"111111","status":5}
I See Response Code Is 422
I See Response Is XML
I See XML Response Matches XPath "//code"
I See XML Response Matches XPath "//message"
I See XML Response includes "DomDocument"
I See XML Response includes "DomDocument"
Passed
28, delete /users/{id}: delete the user, copy \api\tests\api\user\viewcest.php as \API\Tests\API\User\DeleteCest.php, delete user (user ID: {id}, no user exists/delete user success), edit
<?php
namespace API\tests\user;
use yii;
use API\tests\apitester;
use codeception\util\httpcode;
use codeception\util\xml;
Use API\Fixtures\UserFixture;
class deletecest
{
public function _before(apitester $i)
{
}
public function _after(apitester $i)
{
}
/**
* @return array
*/
public function _fixtures()
{
return[
user=> [
class=> userfixture::classname(),
datafile=> codecept_data_dir() .user.php
]#ATFP_CLOSE_Translate_span#
];
}
// delete user (delete user success, json response)
public function deleteIsJSON(Pitester $i)
{
$id = 1;
$I->HaveHttpHeader(accept,application/json; version=. $i->getMinorPatch() .);
$i->sendDelete(/users/. $id);
$I->SeeResponseCodeIs(httpcode::ok); // 200
$I->SeerEsponseIsJson();
// check the structure of the response
$I->SeerEsponseJsonMatchesJsonPath($.code);
$I->SeerEsponseJsonMatchesJsonPath($.message);
// check the response data
$I->SeeResponseContainsJSON([
code=> 10000,
Message=> yii::t(succeed,10005,
]#atfp_close_translate_span#);
}
// delete user (delete user success, xml response)
public function deleteIsXML(apitester $i)
{
$id = 1;
$I->HaveHttpHeader(accept,application/xml; version=. $i->getMinorPatch() .);
$i->sendDelete(/users/. $id);
$I->SeeResponseCodeIs(httpcode::ok); // 200
$I->SeeResponseIsXml();
// check the structure of the response
$I->SeExmlResponseMatchesXpath(//code);
$I->SeExmlResponseMatchesXpath(//message);
// check the response data
$I->SeExmlResponseIncludes(xml::toxml(['code' => 10000]));
$I->SeExmlResponseIncludes(xml::toxml(['message' => Yii::t('success', '10005')]));
}
// Get user details, use the ID that does not exist (user ID: {id}, does not exist, json response)
public function deleteWithNotExitIdisJson(apitestter $i)
{
$id = 9999;
$I->HaveHttpHeader(accept,application/json; version=. $i->getMinorPatch() .);
$i->sendDelete(/users/. $id);
$I->SeeResponseCodeIs(httpcode::not_found); // 404
$I->SeerEsponseIsJson();
// check the structure of the response
$I->SeerEsponseJsonMatchesJsonPath($.code);
$I->SeerEsponseJsonMatchesJsonPath($.message);
// check the response data
$I->SeeResponseContainsJSON([
code=> 20002,
Message=> yii::t(Error, yii::t(Error, yii::t(Error,20002), [ID=> $id]#atfp_close_translate_span#)),
]);
}
// Get user details, use the ID that does not exist (user ID: {id}, does not exist, xml response)
public function deleteWithNotExitIdisXML(apitestter $i)
{
$id = 9999;
$I->HaveHttpHeader(accept,application/xml; version=. $i->getMinorPatch() .);
$i->sendDelete(/users/. $id);
$I->SeeResponseCodeIs(httpcode::not_found); // 404
$I->SeeResponseIsXml();
// check the structure of the response
$I->SeExmlResponseMatchesXpath(//code);
$I->SeExmlResponseMatchesXpath(//message);
// check the response data
$I->SeExmlResponseIncludes(xml::toxml(['code' => 20002]));
$I->SeExmlResponseIncludes(xml::toxml(['message' => Yii::t('error', Yii::t('error', Yii::t('error', '20002'), ['id' => $id])])));
}
}
29. Run the test, only test the API user/createcest.php, get the detailed output, you can see the step by step behavior report, which is in line with expectations, as shown in Figure 9
vendor/bin/codecept run --steps ---C API user/deletecest.php
Codeception php testing framework v2.4.1
Powered by PHPUnit 7.1.2 by Sebastian Bergmann and Contributors.
API\tests.api tests (4) --------------------------------------------------------------------------------
deletecest: delete is json
Signature: API\Tests\User\DeleteCest:DeleteIsJSON
test: tests\api\user\deletecest.php:deleteisjson
Scenario --
I get minor patch
I have http header "accept","application/json; version=0.0"
I send delete "/users/1"
I See Response Code Is 200
I See Response Is JSON
I See Response JSON MATCHES JSON Path "$.code"
I See Response JSON MATCHES JSON Path "$.message"
I see response contains json {"code":10000,"message":"delete user success"}
Passed
deletecest: delete is xml
SIGNATURE: API\Tests\User\DeleteCest:DeleteISXML
test: tests\api\user\deletecest.php:deleteISXML
Scenario --
I get minor patch
I have http header "accept","application/xml; version=0.0"
I send delete "/users/1"
I See Response Code Is 200
I See Response Is XML
I See XML Response Matches XPath "//code"
I See XML Response Matches XPath "//message"
I See XML Response includes "DomDocument"
I See XML Response includes "DomDocument"
Passed
DeleteCest: delete with not exist id is json
SIGNATURE: API\Tests\User\DeleteCest:DeleteWithNotExistIdisJSON
test: tests\api\user\deletecest.php:deleteWithNotExistIdisJSON
Scenario --
I get minor patch
I have http header "accept","application/json; version=0.0"
I send delete "/users/9999"
I See Response Code Is 404
I See Response Is JSON
I See Response JSON MATCHES JSON Path "$.code"
I See Response JSON MATCHES JSON Path "$.message"
I see response contains json {"code":20002,"message":"user id: 9999, does not exist"}
Passed
DeleteCest: delete with not exit id is xml
Signature: API\Tests\User\DeleteCest:DeleteWithNotExistIdisXML
test: tests\api\user\deletecest.php:deleteWithNotExistIdisxml
Scenario --
I get minor patch
I have http header "accept","application/xml; version=0.0"
I send delete "/users/9999"
I See Response Code Is 404
I See Response Is XML
I See XML Response Matches XPath "//code"
I See XML Response Matches XPath "//message"
I See XML Response includes "DomDocument"
I See XML Response includes "DomDocument"
Passed
---------------------------------------------------------------------------------------------------
Time: 1.25 seconds, memory: 16.00MB
OK (4 tests, 24 assertions)
30. Options /Users: Display verbs about end /users support; options /users/{id}: Display verbs about end /users/{id} support, copy \api\tests\api\user\indexcest.php is \api\tests\api\user\optionscest.php, edit
<?php
namespace API\tests\user;
use yii;
use API\tests\apitester;
use codeception\util\httpcode;
use codeception\util\xml;
Use API\Fixtures\UserFixture;
class optionsscest
{
public function _before(apitester $i)
{
}
public function _after(apitester $i)
{
}
/**
* @return array
*/
public function _fixtures()
{
return[
user=> [
class=> userfixture::classname(),
datafile=> codecept_data_dir() .user.php
]#ATFP_CLOSE_Translate_span#
];
}
// Display verbs (JSON response) supported by end/users
public function optionsisjson(apitestter $i)
{
$I->HaveHttpHeader(accept,application/json; version=. $i->getMinorPatch() .);
$I->sendOptions(/users);
$I->SeeResponseCodeIs(httpcode::ok); // 200
// check the response data
$I->SeeHttpHeader(allow,get, post, head, options);
$i->SeeResponseEquals();
}
// Display verbs (XML response) supported by end /users
public function optionsisxml(apitester $i)
{
$I->HaveHttpHeader(accept,application/xml; version=. $i->getMinorPatch() .);
$I->sendOptions(/users);
$I->SeeResponseCodeIs(httpcode::ok); // 200
// check the response data
$I->SeeHttpHeader(allow,get, post, head, options);
$i->SeeResponseEquals();
}
// Show the verbs supported by the end /users/{id} (JSON response)
public function optionSidIsJSON(Pitester $i)
{
$id = 1;
$I->HaveHttpHeader(accept,application/json; version=. $i->getMinorPatch() .);
$I->sendOptions(/users/. $id);
$I->SeeResponseCodeIs(httpcode::ok); // 200
// check the response data
$I->SeeHttpHeader(allow,get, put, patch, delete, head, options);
$i->SeeResponseEquals();
}
// Show verbs (XML response) supported by end /users/{id}
public function optionSIDISXML(apitestter $i)
{
$id = 1;
$I->HaveHttpHeader(accept,application/xml; version=. $i->getMinorPatch() .);
$I->sendOptions(/users/. $id);
$I->SeeResponseCodeIs(httpcode::ok); // 200
// check the response data
$I->SeeHttpHeader(allow,get, put, patch, delete, head, options);
$i->SeeResponseEquals();
}
}
31. Run the test, only test the API user/optionscest.php, get the detailed output, and you can see the behavior report step by step, which is in line with expectations
vendor/bin/codecept run --steps ---C API user/optionscest.php
Codeception php testing framework v2.4.1 Powered by PHPUnit 7.1.2 by Sebastian Bergmann and Contributors. API\tests.api tests (4) -------------------------------------------------------------------------------- OptionsCest: Options is JSON SIGNATURE: API\Tests\User\OptionsCest:OptionsIsJSON test: tests\api\user\optionscest.php:OptionsIsJson Scenario -- I get minor patch I have http header "accept","application/json; version=0.0" I send options "/users" I See Response Code Is 200 I see http header "allow", "get, post, head, options" I See Response Equals "" Passed OptionsCest: Options is XML Signature: API\Tests\User\OptionsCest:OptionsXML test: tests\api\user\optionscest.php:OptionsIsXML Scenario -- I get minor patch I have http header "accept","application/xml; version=0.0" I send options "/users" I See Response Code Is 200 I see http header "allow", "get, post, head, options" I See Response Equals "" Passed OptionsCest: Options ID is JSON Signature: api\tests\user\optionscest:optionsidisjson test: tests\api\user\optionscest.php:OptionsIsJson Scenario -- I get minor patch I have http header "accept","application/json; version=0.0" I send options "/users/1" I See Response Code Is 200 I see HTTP header "allow","get, put, patch, delete, head, options" I See Response Equals "" Passed OptionsCest: Options ID is XML Signature: API\Tests\User\OptionsCest:OptionsIDISXML test: tests\api\user\optionscest.php:OptionsIsXml Scenario -- I get minor patch I have http header "accept","application/xml; version=0.0" I send options "/users/1" I See Response Code Is 200 I see HTTP header "allow","get, put, patch, delete, head, options" I See Response Equals "" Passed --------------------------------------------------------------------------------------------------- Time: 1.19 seconds, memory: 14.00MB OK (4 tests, 12 assertions)
32. Run all the sample tests, which are in line with expectations, as shown in Figure 10
vendor/bin/codecept run









