@param and @var
Following attributes can be used with @param
, they also apply for @var
comment
that is added to model class properties.
Just replace
@param [type] $name ...
with
@var [type] ...
in the examples below
@from
Syntax:
@param [type] $name [Description] {@from path|body|query|head}
Example:
@param string $name user name {@from body}
Overrides the parameter mapping, defines where to expect the parameter from. Value should be one of the following
- path as part of url
- query as part of the query string
- body as part of the body of the request
- head as part of the http header
Please note that unlike path and head other values are only suggestive and primarily used by API Explorer to build the interface
@type
Syntax:
string
@param string $name [Description] {@type email|date|datetime|timestamp}
array
@param array $name [Description] {@type className}
Examples:
@param string $email email id {@type email}
Sub types for validation and documentation purpose. Email will validate the given string as email. Date and datetime will be validated as standard mysql date formats respectively. Timestamp will be validated as Unix timestamp.
@param array $author array of Authors {@type Author}
States that the given array of Author instances.
Code:Following api class shows various usage examples
<?php
class Type
{
/**
* Email validation
*
* @param string $email {@from body}{@type email}
*/
function postEmail($email)
{
return $email;
}
/**
* Date validation
*
* @param string $date {@from body}{@type date}
*/
function postDate($date)
{
return $date;
}
/**
* Array of dates
*
* @param array $dates Dates array{@from body}{@type date}
*/
function postDates(array $dates)
{
return $dates;
}
/**
* DateTime validation
*
* @param string $datetime {@from body}{@type datetime}
*/
function postDatetime($datetime)
{
return $datetime;
}
/**
* time validation
*
* @param string $time {@from body}{@type time}
*/
function postTime($time)
{
return $time;
}
/**
* time validation in 12 hour format
*
* @param string $time {@from body}{@type time12}
*/
function postTime12($time12)
{
return $time12;
}
/**
* Timestamp validation
*
* @param string $timestamp {@from body}{@type timestamp}
*/
function postTimestamp($timestamp)
{
return $timestamp;
}
/**
* Integer validation
*
* @param array $integers {@type int}
*/
function postIntegers(array $integers)
{
return $integers;
}
/**
* Array of numbers
*
* @param array $numbers {@type float}
*/
function postNumbers(array $numbers)
{
return $numbers;
}
/**
* Array of time strings
*
* @param array $timestamp {@from body}{@type time}
*/
function postTimes(array $timestamps)
{
return $timestamps;
}
/**
* Array of timestamps
*
* @param array $timestamp {@from body}{@type timestamp}
*/
function postTimestamps(array $timestamps)
{
return $timestamps;
}
/**
* Custom class parameter
*
* @param Author $author
*
* @return Author
*/
function postAuthor(Author $author)
{
return $author;
}
/**
* Array of authors
*
* @param array $authors {@type Author}
*
* @return mixed
*/
function postAuthors(array $authors)
{
return $authors;
}
/**
* An associative array
*
* @param array $object {@type associative}
*
* @return array
*/
function postObject(array $object)
{
return $object;
}
/**
* An indexed array
*
* @param array $array {@type indexed}
*
* @return array
*/
function postArray(array $array)
{
return $array;
}
/**
* An array indexed or associative
*
* @param array $array
*
* @return array
*/
function postArrayOrObject(array $array)
{
return $array;
}
}
class Author
{
/**
* @var string {@from body} {@min 3}{@max 100}
* name of the Author {@required true}
*/
public $name = 'Name';
/**
* @var string {@type email} {@from body} {@required false}
* email id of the Author
*/
public $email = '[email protected]';
}
Test:Running the following command on the terminal from the project root
vendor/bin/behat features/tests/param/type.feature
Will test the above api for the following features
@param @type
Feature: Type Attribute
Scenario Outline: Email
Given that I send {"email":<email>}
And the request is sent as JSON
When I request "tests/param/type/email"
Then the response status code should be 200
And the response is JSON
And the type is "string"
And the response equals <email>
Examples:
| email |
| "[email protected]" |
| "!def!xyz%[email protected]" |
| "[email protected]" |
Scenario Outline: Email as the only body
Given that I send <email>
And the request is sent as JSON
When I request "tests/param/type/email"
Then the response status code should be 200
And the response is JSON
And the type is "string"
And the response equals <email>
Examples:
| email |
| "[email protected]" |
| "!def!xyz%[email protected]" |
| "[email protected]" |
Scenario Outline: Invalid Email
Given that I send {"email":<email>}
And the request is sent as JSON
When I request "tests/param/type/email"
Then the response status code should be 400
And the response is JSON
Examples:
| email |
| "some.thing@wrong" |
| "missing@dot" |
| "missing.at" |
Scenario Outline: Invalid Email as the only body
Given that I send <email>
And the request is sent as JSON
When I request "tests/param/type/email"
Then the response status code should be 400
And the response is JSON
Examples:
| email |
| "some.thing@wrong" |
| "missing@dot" |
| "missing.at" |
Scenario Outline: Date
Given that I send {"date":<date>}
And the request is sent as JSON
When I request "tests/param/type/date"
Then the response status code should be 200
And the response is JSON
And the type is "string"
And the response equals <date>
Examples:
| date |
| "2013-08-10" |
| "1974-03-30" |
| "1600-12-17" |
Scenario Outline: Date as the only body
Given that I send <date>
And the request is sent as JSON
When I request "tests/param/type/date"
Then the response status code should be 200
And the response is JSON
And the type is "string"
And the response equals <date>
Examples:
| date |
| "2013-08-10" |
| "1974-03-30" |
| "1600-12-17" |
Scenario Outline: Invalid Date
Given that I send {"date":<date>}
And the request is sent as JSON
When I request "tests/param/type/date"
Then the response status code should be 400
And the response is JSON
Examples:
| date |
| "YYY-MM-DD" |
| "10-08-2013" |
| "2013-13-10" |
| "2013-02-30" |
| "2013/08/10" |
Scenario Outline: Invalid Date as the only body
Given that I send <date>
And the request is sent as JSON
When I request "tests/param/type/date"
Then the response status code should be 400
And the response is JSON
Examples:
| date |
| "YYY-MM-DD" |
| "10-08-2013" |
| "2013-13-10" |
| "2013-02-30" |
| "2013/08/10" |
Scenario Outline: Date and Time
Given that I send {"datetime":<datetime>}
And the request is sent as JSON
When I request "tests/param/type/datetime"
Then the response status code should be 200
And the response is JSON
And the type is "string"
And the response equals <datetime>
Examples:
| datetime |
| "2013-08-10 00:34:18" |
Scenario Outline: Invalid Date and Time
Given that I send {"datetime":<datetime>}
And the request is sent as JSON
When I request "tests/param/type/datetime"
Then the response status code should be 400
And the response is JSON
Examples:
| datetime |
| "2013-08-10" |
| "2013-08-10 25:70:11" |
| "2013/08/10 00:34:18" |
Scenario Outline: Time Stamp
Given that I send {"timestamp":<timestamp>}
And the request is sent as JSON
When I request "tests/param/type/timestamp"
Then the response status code should be 200
And the response is JSON
And the type is "int"
And the response equals <expected>
Examples:
| timestamp | expected |
| "1376094488" | 1376094488 |
| 1376094488 | 1376094488 |
| "0123" | 123 |
| 123.0 | 123 |
| 1 | 1 |
| 0 | 0 |
Scenario Outline: Time Stamp as the only body
Given that I send <timestamp>
And the request is sent as JSON
When I request "tests/param/type/timestamp"
Then the response status code should be 200
And the response is JSON
And the type is "int"
And the response equals <expected>
Examples:
| timestamp | expected |
| "1376094488" | 1376094488 |
| 1376094488 | 1376094488 |
| "0123" | 123 |
| 123.0 | 123 |
| 1 | 1 |
Scenario Outline: Invalid Time Stamp
Given that I send {"timestamp":<timestamp>}
And the request is sent as JSON
When I request "tests/param/type/timestamp"
Then the response status code should be 400
And the response is JSON
Examples:
| timestamp |
| "2013-08-10" |
| "2013-08-10 00:34:18" |
| "0xFF" |
| 1.1 |
Scenario Outline: Invalid Time Stamp as the only body
Given that I send <timestamp>
And the request is sent as JSON
When I request "tests/param/type/timestamp"
Then the response status code should be 400
And the response is JSON
Examples:
| timestamp |
| "2013-08-10" |
| "2013-08-10 00:34:18" |
| "0xFF" |
| 1.1 |
Scenario Outline: An Array of Integers
Given that I send {"integers":<integers>}
And the request is sent as JSON
When I request "tests/param/type/integers"
Then the response status code should be 200
And the response is JSON
And the response equals <expected>
Examples:
| integers | expected |
| [] | [] |
| {} | [] |
| [1,3298473984,23] | [1,3298473984,23] |
| [0] | [0] |
Scenario Outline: An Array of Integers as the only body
Given that I send <integers>
And the request is sent as JSON
When I request "tests/param/type/integers"
Then the response status code should be 200
And the response is JSON
And the response equals <expected>
Examples:
| integers | expected |
| [] | [] |
| {} | [] |
| [1,3298473984,23] | [1,3298473984,23] |
| [0] | [0] |
Scenario Outline: Invalid Array of Integers
Given that I send {"integers":<integers>}
And the request is sent as JSON
When I request "tests/param/type/integers"
Then the response status code should be 400
And the response is JSON
Examples:
| integers |
| "" |
| [true,false] |
| [null] |
| [1.34] |
| {"key":"value"} |
Scenario Outline: Invalid Array of Integers as the only body
Given that I send <integers>
And the request is sent as JSON
When I request "tests/param/type/integers"
Then the response status code should be 400
And the response is JSON
Examples:
| integers |
| "" |
| [true,false] |
| [null] |
| [1.34] |
| {"key":"value"} |
Scenario Outline: An Array of Floating Points
Given that I send {"numbers":<numbers>}
And the request is sent as JSON
When I request "tests/param/type/numbers"
Then the response status code should be 200
And the response is JSON
And the response equals <expected>
Examples:
| numbers | expected |
| [] | [] |
| {} | [] |
| [1,329.8473984,23] | [1,329.8473984,23] |
| [0.00001] | [1.0e-5] |
Scenario Outline: An Array of Floating Points as the only body
Given that I send <numbers>
And the request is sent as JSON
When I request "tests/param/type/numbers"
Then the response status code should be 200
And the response is JSON
And the response equals <expected>
Examples:
| numbers | expected |
| [] | [] |
| {} | [] |
| [1,329.8473984,23] | [1,329.8473984,23] |
| [0.00001] | [1.0e-5] |
| [0.10e-4] | [1.0e-5] |
Scenario Outline: Invalid Array of Floats as the only body
Given that I send <numbers>
And the request is sent as JSON
When I request "tests/param/type/numbers"
Then the response status code should be 400
And the response is JSON
Examples:
| numbers |
| "" |
| [true,false] |
| [null] |
| {"key":"value"} |
Scenario Outline: Custom Class
Given that I send <author>
And the request is sent as JSON
When I request "tests/param/type/author"
Then the response status code should be 200
And the response is JSON
And the response equals <expected>
Examples:
| author | expected |
| {"name":"Arul"} | {"name":"Arul","email":"[email protected]"} |
| {"name":"Arul","email":"[email protected]"} | {"name":"Arul","email":"[email protected]"} |
| {"name":"name","email":"[email protected]"} | {"name":"name","email":"[email protected]"} |
Scenario Outline: Invalid data for Custom Class
Given that I send <author>
And the request is sent as JSON
When I request "tests/param/type/author"
Then the response status code should be 400
And the response is JSON
Examples:
| author |
| "" |
| [true,false] |
| [null] |
| {"key":"value"} |
| {"name":"12","email":"[email protected]"} |
| {"email":"[email protected]"} |
| {"email":"ab.c","name":"1234"} |
Scenario Outline: Array of Custom Class objects
Given that I send <authors>
And the request is sent as JSON
When I request "tests/param/type/authors"
Then the response status code should be 200
And the response is JSON
And the response equals <expected>
Examples:
| authors | expected |
| [{"name":"Arul"}] | [{"name":"Arul","email":"[email protected]"}] |
| [{"name":"Arul","email":"[email protected]"}] | [{"name":"Arul","email":"[email protected]"}] |
| [{"name":"name","email":"[email protected]"}] | [{"name":"name","email":"[email protected]"}] |
Scenario Outline: Invalid data for array of Custom Class objects
Given that I send <authors>
And the request is sent as JSON
When I request "tests/param/type/authors"
Then the response status code should be 400
And the response is JSON
Examples:
| authors |
| "" |
| [true,false] |
| [null] |
| {"key":"value"} |
| {"name":"12","email":"[email protected]"} |
| {"email":"[email protected]"} |
| {"email":"ab.c","name":"1234"} |
@choice
Syntax:
@param string $name [Description] {@choice option1,option2...}
Example:
@param string $gender {@choice male,female,third}
Value for the parameter should match one of the choices, used for validation.
@min & @max
Syntax:
@param string|int|float|array $name [Description] {@min value}{@max value}
Examples:
string
@param string $name 3 to 10 characters in length {@min 3}{@max 10}
array
@param array $items at least one item is needed {@min 1}
integer
@param int $age valid age should be 18 or above {@min 18}
Minimum and maximum values for a parameter. For string and array parameters it is applied to the length. For numeric parameters it sets the range for the value.
Code:Following api class shows various usage examples
<?php
/**
* Class MinMax provides an api to check min and max
* attributes for array, int, and string
*/
class MinMax
{
/**
* Restrict the number of items in an array
*
* @param array $array {@min 2}{@max 5}
*
* @return array
*/
function postArray(array $array)
{
return $array;
}
/**
* Restrict the value
*
* @param int $int {@min 2}{@max 5}{@from path}
*
* @return int
*/
function getInt($int)
{
return $int;
}
/**
* Restrict the length of the string
*
* @param string $string {@min 2}{@max 5}{@from path}
*
* @return string
*/
function getString($string)
{
return $string;
}
}
Test:Running the following command on the terminal from the project root
vendor/bin/behat features/tests/param/minmax.feature
Will test the above api for the following features
@param @min @max
Feature: Minimum and Maximum
Scenario Outline: Int
When I request "tests/param/minmax/int/<number>"
Then the response status code should be 200
And the response is JSON
And the type is "int"
And the value equals <number>
Examples:
| number |
| 2 |
| 3 |
| 4 |
| 5 |
Scenario: Int lower than the minimum
When I request "tests/param/minmax/int/1"
Then the response status code should be 400
And the response is JSON
Scenario: Int higher than maximum
When I request "tests/param/minmax/int/6"
Then the response status code should be 400
And the response is JSON
Scenario: String
When I request "tests/param/minmax/string/me"
Then the response status code should be 200
And the response is JSON
And the type is "string"
And the value equals "me"
Scenario: Short String
When I request "tests/param/minmax/string/i"
Then the response status code should be 400
And the response is JSON
Scenario: Lengthy String
When I request "tests/param/minmax/string/arulkumaran"
Then the response status code should be 400
And the response is JSON
Scenario Outline: Array
Given that I send <data>
And the request is sent as JSON
When I request "tests/param/minmax/array"
Then the response status code should be 200
And the response is JSON
Examples:
| data |
| [1,2] |
| [1,2,3] |
| [1,2,3,4] |
| [1,2,3,4,5] |
Scenario Outline: Array out of range
Given that I send <data>
And the request is sent as JSON
When I request "tests/param/minmax/array"
Then the response status code should be 400
And the response is JSON
Examples:
| data |
| [] |
| [1] |
| [1,2,3,4,5,6] |
| [1,2,3,4,5,6,7] |
| [1,2,3,4,5,6,7,8] |
@fix
Syntax:
@param string|int|float|array $name [Description] {@fix true}
Example:
@param string $name 3 to 10 characters in length {@max 10}{@fix true}
Suggests the validator to attempt fixing the validation problem. In the above example Validator will trim off the excess characters instead of throwing an exception
Code:Following api class shows various usage examples
<?php
/**
* Class MinMax provides an api to check min and max
* attributes for array, int, and string
*/
class MinMaxFix
{
/**
* Restrict the number of items in an array
*
* @param array $array {@min 2}{@max 5}{@fix true}
*
* @return array
*/
function postArray(array $array)
{
return $array;
}
/**
* Restrict the value
*
* @param int $int {@min 2}{@max 5}{@fix true}{@from path}
*
* @return int
*/
function getInt($int)
{
return $int;
}
/**
* Restrict the length of the string
*
* @param string $string {@min 2}{@max 5}{@fix true}{@from body}
*
* @return string
*/
function postString($string)
{
return $string;
}
}
Test:Running the following command on the terminal from the project root
vendor/bin/behat features/tests/param/minmaxfix.feature
Will test the above api for the following features
@param @min @max @fix
Feature: Minimum and Maximum with Fix
Scenario Outline: Int
When I request "tests/param/minmaxfix/int/<number>"
Then the response status code should be 200
And the response is JSON
And the type is "int"
And the response equals <expected>
Examples:
| number | expected |
| 0 | 2 |
| 1 | 2 |
| 2 | 2 |
| 3 | 3 |
| 4 | 4 |
| 5 | 5 |
| 6 | 5 |
| 7 | 5 |
Scenario Outline: String
Given that I send {"string":<string>}
And the request is sent as JSON
When I request "tests/param/minmaxfix/string"
Then the response status code should be 200
And the response is JSON
And the type is "string"
And the response equals <expected>
Examples:
| string | expected |
| "a" | "aa" |
| "ab" | "ab" |
| "abc" | "abc" |
| "abcd" | "abcd" |
| "abcde" | "abcde" |
| "abcdef" | "abcde" |
| "abcdefg" | "abcde" |
| "abcdefh" | "abcde" |
Scenario Outline: Array out of maximum range
Given that I send <array>
And the request is sent as JSON
When I request "tests/param/minmaxfix/array"
Then the response status code should be 200
And the response is JSON
And the response equals <expected>
Examples:
| array | expected |
| [1,2] | [1,2] |
| [1,2,3] | [1,2,3] |
| [1,2,3,4] | [1,2,3,4] |
| [1,2,3,4,5] | [1,2,3,4,5] |
| [1,2,3,4,5,6] | [1,2,3,4,5] |
| [1,2,3,4,5,6,7] | [1,2,3,4,5] |
| [1,2,3,4,5,6,7,8] | [1,2,3,4,5] |
Scenario Outline: Array short of minimum is not expected
Given that I send <array>
And the request is sent as JSON
When I request "tests/param/minmaxfix/array"
Then the response status code should be 400
And the response is JSON
Examples:
| array |
| [] |
| [1] |
@pattern
Syntax:
@param string $name [Description] {@pattern /REGEX_HERE/REGEX_OPTIONS}
Example:
@param string $password at least one alpha and one numeric character
{@pattern /^(?:[0-9]+[a-z]|[a-z]+[0-9])[a-z0-9]*$/i}
Used by the validator to make sure the parammeter value matches the regex pattern. It uses preg_match for this verification. Please note that /
should be used as the delimiter.
Following api class shows various usage examples
<?php
/**
* Class Validation provides api to check validation
*/
class Validation
{
/**
* Validate with regex
*
* @param string $password {@pattern /^(?:[0-9]+[a-z]|[a-z]+[0-9])[a-z0-9]*$/i}
* {@message Strong password with at least
* one alpha and one numeric character is required}
*
* @return string
*/
function postPattern($password)
{
return $password;
}
}
Test:Running the following command on the terminal from the project root
vendor/bin/behat features/tests/param/validation.feature
Will test the above api for the following features
@param @type
Feature: Validation
Scenario Outline: Valid Password
Given that I send {"password":<password>}
And the request is sent as JSON
When I request "tests/param/validation/pattern"
Then the response status code should be 200
And the response is JSON
And the type is "string"
And the response equals <password>
Examples:
| password |
| "1a" |
| "b2" |
| "some1" |
Scenario Outline: Invalid Password
Given that I send {"password":<password>}
And the request is sent as JSON
When I request "tests/param/validation/pattern"
Then the response status code should be 400
And the response is JSON
And the type is "object"
And the response contains "Bad Request: Strong password with at least one alpha and one numeric character is required"
Examples:
| password |
| "arul" |
| "12345678" |
| "ONEtwo" |
@message
Syntax:
@param string|int|float $name [Description] {@message value}
Example:
@param string $password Password
{@message Strong password with at least one alpha and one numeric character is required}
Used by the validator to show a custom error message when invalid value is submitted. Use it to list the requirements for a parameter
Code:Following api class shows various usage examples
<?php
/**
* Class Validation provides api to check validation
*/
class Validation
{
/**
* Validate with regex
*
* @param string $password {@pattern /^(?:[0-9]+[a-z]|[a-z]+[0-9])[a-z0-9]*$/i}
* {@message Strong password with at least
* one alpha and one numeric character is required}
*
* @return string
*/
function postPattern($password)
{
return $password;
}
}
Test:Running the following command on the terminal from the project root
vendor/bin/behat features/tests/param/validation.feature
Will test the above api for the following features
@param @type
Feature: Validation
Scenario Outline: Valid Password
Given that I send {"password":<password>}
And the request is sent as JSON
When I request "tests/param/validation/pattern"
Then the response status code should be 200
And the response is JSON
And the type is "string"
And the response equals <password>
Examples:
| password |
| "1a" |
| "b2" |
| "some1" |
Scenario Outline: Invalid Password
Given that I send {"password":<password>}
And the request is sent as JSON
When I request "tests/param/validation/pattern"
Then the response status code should be 400
And the response is JSON
And the type is "object"
And the response contains "Bad Request: Strong password with at least one alpha and one numeric character is required"
Examples:
| password |
| "arul" |
| "12345678" |
| "ONEtwo" |
@example
Syntax:
@param string|int|float $name [Description] {@example value}
Example:
@param string $name Name {@example Arul Kumaran}
Used by the explorer to prefill the value for the parameter