This project demonstrates very simple realization closed API on Yii 2.
Main API function splitting array of numbers by simple rule.
Rule code you can find in \app\models\Calculation::calculateSplitPoint()
function.
API located in a separate module named api
.
Module contains controller, named TaskController
, and provide 2 actions (index
- for calculating, and list
for listing previous calculations)
Command task
give you available make this calculations without setting-up web environment.
- Clone this repository
- Install all Сomposer dependencies by
composer install
For work with project you must create databases, and configurate config/db.php
file. I used standard database name, and MySQL user root
with empty password on my local machine.
Databases in my case:
- Production -
yii2basic
(inconfig/db.php
) - For Codeception -
yii2basic_test
(inconfig/test_db.php
)
After this manipulations, you can run migrations:
- Production -
php yii migrate/up
- Production -
php ./tests/bin/yii migrate/up
Migrations seeds two users to database after create table.
You can use any method to deploy it.
I used php yii serve
command, for easily working with this project on local machine.
All methods need Basic Auth header for authorize user. You can use demo:demo
or admin:admin
users (certainly you can add your user to database).
Available 2 API methods:
/api/task
(POST) - make calculation, and get answer/api/task/list
(GET) - view list of your calculations
You can use Postman for easily make requests
It's main method, only POST request allowed.
This request must contain Authorization: Basic ***
header with user credentials.
Request body must contain JSON object with 2 fields:
number
(integer) - number for splitting ruledata
(array of integers) - array for splitting
Sample JSON object in request:
{
"number": 5,
"data": [5,5,1,7,2,3,5]
}
Succesfully response contains index of item before which must split array, or -1
if splitting impossible.
Sample JSON object in response:
{
"result": 4
}
On fail you get standard HttpException object with errors list in 'message' property.
It's method for view all of your requests, only GET request allowed.
This request must contain Authorization: Basic ***
header with user credentials.
Response contains JSON array of objects, each object present you calculated early data (number, array, index of splitting, timestamp of calculation)
You can run command php yii task
with arguments:
number
* - number for splitting rule (e.g.:5
)data
* - comma separated digits string (e.g.:5,5,7,1,32,5
)
and options:
uid
- user id from database, for bind calculation to the user (e.g.:--uid=1
)
Sample commands:
php yii task 5 5,5,1,7,2,3,5
returns4
to console, write data to DB, withuser_id
been set tonull
php yii task 5 5,5,1,7,2,3,5 --uid=1
returns4
to console, write data to DB, withuser_id
been set to1
(admin
user).
For testing you can run this command:
./vendor/bin/codecept run
You can specify this for run only unit tests (without functional and api):./vendor/bin/codecept run unit
For API tests you must run web environment (yii serve
, Docker with this project, OpenServer, or any...).
And change domain in /tests/api.suite.yml
on your. In my case domain set to localhost:8080
.
Simply change domain (or verify it, if you have equal):
- REST:
depends: PhpBrowser
url: http://localhost:8080/api
You can specify this for run only api tests (without functional and unit):
./vendor/bin/codecept run api
API tests create Calculations data in real DB (not tests)