Helpers for feature tests, like API sorting, authentication, registration, etc.
Install the saritasa/laravel-testbed
package:
$ composer require saritasa/laravel-testbed
Trait to check if sorting works correctly.
Using trait:
use Saritasa\LaravelTestbed\Traits\ApiListSortingCheck;
/**
* Vendor tests.
*/
class VendorTest extends TestCase
{
use ApiListSortingCheck;
}
/**
* Check that API returns list sorted by specified field (order by single field - check for each of passed fields).
*
* @param string $url Api endpoint to check
* @param int $count Count of created models
* @param array|string[] $sortingFields Sorting fields to check
* @param array|string[] $auth Auth
* @param string|null $envelope Results envelope (like 'results', 'items', etc.)
*
* @return void
*/
public function assertSortingWorks(string $url, int $count, array $sortingFields, array $auth, ?string $envelope = null): void
/**
* Check that API returns list sorted by specified fields
* (order by multiple fields - check for combinations of passed fields).
*
* @param string $url Api endpoint to check
* @param int $count Count of created models
* @param array|string[] $sortingFields Sorting fields to check
* @param array|string[] $auth Auth
* @param string|null $envelope Results envelope (like 'results', 'items', etc.)
*
* @return void
*/
public function assertMultiSortingWorks(string $url, int $count, array $sortingFields, array $auth, ?string $envelope = null): void
/** Sorting options test */
public function testOrderBy()
{
$count = 15;
$auth = Helpers::createAndAuthenticateUser();
factory(Vendor::class, $count)->create();
$envelope = 'results';
$this->assertSortingWorks("/api/vendors", $count, VendorListRequest::SORTING_FIELDS, $auth, $envelope);
}
/** Multi sorting options test */
public function testMultiOrderBy()
{
$count = 15;
$auth = Helpers::createAndAuthenticateUser();
factory(Vendor::class, $count)->create();
$envelope = 'results';
$this->assertMultiSortingWorks("api/vendors", $count, VendorListRequest::SORTING_FIELDS, $auth, $envelope);
}
NOTE: If the response does not contain an envelope (such as "results", "items", etc.), you do not need to send this parameter.
- api/vendors?order_by=name
- api/vendors?order_by=contacts.name
- api/vendors?order_by=-name
- api/vendors?order_by=-contacts.name
- api/vendors?order_by=id,name
- api/vendors?order_by=name,contacts.name
- api/vendors?order_by=-id,name
- api/vendors?order_by=-id,-contacts.name
See CONTRIBUTING and Code of Conduct, if you want to make contribution (pull request) or just build and test project on your own.