Abstract Class luya\testsuite\cases\NgRestTestCase
Inheritance | luya\testsuite\cases\NgRestTestCase » luya\testsuite\cases\WebApplicationTestCase » luya\testsuite\cases\BaseTestSuite » Yoast\PHPUnitPolyfills\TestCases\TestCase |
---|---|
Uses Traits | luya\testsuite\traits\AdminDatabaseTableTrait |
Available since version | 1.0.10 |
Source Code | https://github.com/luyadev/luya-testsuite/blob/master/src/cases/NgRestTestCase.php |
NgRest Test Case.
The NgRestTestCase extends the {{luya\testsuite\fixture\NgRestModelFixture}} by auto setup the right Database connection and allows you to test main components Model, API and Controller very easy.
The API and Controller tests are optional, this means you don't have to provide {{$apiClass}} or {{$controllerClass}} in order to setup the test case correctly. The basic tests is just doing some basic execution test to see if properties and methods does have values and does not return any php exception, parse or runtime error.
With this test case you can easy access the Model, API and Controller object in order to test your custom functionality.
public function testSomeCustomFunctions()
{
// accessing the controller object
$this->assertSame('FooBar', $this->controller->actionFooBar()); // runs the action method `actionFooBar`
// accessing the api object
$this->assertSame('FooBar', $this->api->actionFooBar()); // runs the action method `actionFooBar`
}
Full example usage and definition:
class NgRestTestCaseTest extends NgRestTestCase
{
public $modelClass = 'luya\testsuite\tests\data\NgRestTestModel';
public $apiClass = 'luya\testsuite\tests\data\NgRestTestApi';
public $controllerClass = 'luya\testsuite\tests\data\NgRestTestController';
public $modelFixtureData = [
'model1' => [
'id' => 1,
'user_id' => 1,
'group_id' => 1,
],
];
public function getConfigArray()
{
return [
'id' => 'ngresttest',
'basePath' => dirname(__DIR__),
];
}
public function testAssertion()
{
$model1 = $this->modelFixture->getModel('model1');
$this->assertSame(1, $model1->id);
// do inserts, updates or deletions with the model
// $model = $this->modelFixture->newModel;
}
}
How to call an API Endpoint:
public function testMyEndpoints()
{
// test a custom api endpoint (action) with auth checks
$this->runControllerAction($this->api, 'test'); // where test is the action name.
// or you can access this action directly without auth checks.
$this->api->actionTest();
}
Public Properties
Property | Type | Description | Defined By |
---|---|---|---|
$apiClass | string | The path to the ngrest api. | luya\testsuite\cases\NgRestTestCase |
$app | luya\web\Application | luya\testsuite\cases\BaseTestSuite | |
$boot | luya\Boot | luya\testsuite\cases\BaseTestSuite | |
$controllerClass | string | The path to the ngrest controller. | luya\testsuite\cases\NgRestTestCase |
$modelClass | string | The path to the ngrest model. | luya\testsuite\cases\NgRestTestCase |
$modelFixtureData | array | An array with fixture data for the model. | luya\testsuite\cases\NgRestTestCase |
$modelSchema | array | An array with schema defintion for the fixture model. | luya\testsuite\cases\NgRestTestCase |
Protected Properties
Public Methods
Protected Methods
Method | Description | Defined By |
---|---|---|
apiCanCreate() | Gives the test user create api permission or removes it | luya\testsuite\cases\NgRestTestCase |
apiCanDelete() | Gives the test user delete api permission or removes it | luya\testsuite\cases\NgRestTestCase |
apiCanList() | Gives the test user list api permission or removes access | luya\testsuite\cases\NgRestTestCase |
apiCanUpdate() | Gives the test user update api permission or removes it | luya\testsuite\cases\NgRestTestCase |
controllerCanAccess() | Gives the test user access to the controller action | luya\testsuite\cases\NgRestTestCase |
mockBasicAdminModels() | Basic admin module env model mocking. | luya\testsuite\cases\NgRestTestCase |
removeApiPermissions() | Disables api access for test user | luya\testsuite\cases\NgRestTestCase |
resetApiPermissions() | Helps to initialize api access permissions | luya\testsuite\cases\NgRestTestCase |
runControllerAction() | Run a certain action insdie a controller, whether its an api or "controller" context. | luya\testsuite\cases\NgRestTestCase |
setQueryAuthToken() | Set the query parameter as auth token. | luya\testsuite\cases\NgRestTestCase |
set_up() | luya\testsuite\cases\BaseTestSuite | |
tear_down() | luya\testsuite\cases\BaseTestSuite | |
trimContent() | No Spaces and No Newline Trims the given text. Remove whitespaces, tabs and other chars in order to compare readable formated texts. | luya\testsuite\cases\BaseTestSuite |
trimSpaces() | No Spaces with Newline | luya\testsuite\cases\BaseTestSuite |
Constants
Constant | Value | Description | Defined By |
---|---|---|---|
ID_AUTH_API | 1 | luya\testsuite\cases\NgRestTestCase | |
ID_AUTH_CONTROLLER | 2 | luya\testsuite\cases\NgRestTestCase | |
ID_GROUP_AUTH_API | 1 | luya\testsuite\cases\NgRestTestCase | |
ID_GROUP_AUTH_CONTROLLER | 2 | luya\testsuite\cases\NgRestTestCase | |
ID_GROUP_TESTER | 1 | luya\testsuite\cases\NgRestTestCase | |
ID_USER_TESTER | 1 | luya\testsuite\cases\NgRestTestCase |
Property Details
The path to the ngrest controller.
An array with fixture data for the model.
An array with schema defintion for the fixture model.
Method Details
Defined in: luya\testsuite\traits\AdminDatabaseTableTrait::addPermissionApi()
Add permission api entry
public integer addPermissionApi ( $id, $api, $isCrud = true ) | ||
$id | ||
$api | string | |
$isCrud | boolean | |
return | integer |
Returns the "fake" id fro the given api |
---|
public function addPermissionApi($id, $api, $isCrud = true)
{
$this->insertRow('admin_auth', [
'id' => $id,
'module_name' => '@app',
'alias_name' => $api,
'is_crud' => (int) $isCrud,
'api' => $api,
]);
return $id;
}
Defined in: luya\testsuite\traits\AdminDatabaseTableTrait::addPermissionRoute()
Add permission route.
public integer addPermissionRoute ( $id, $route, $moduleName = '@app' ) | ||
$id | ||
$route | string | |
$moduleName | ||
return | integer |
Returns the fake id for the auth entry. |
---|
public function addPermissionRoute($id, $route, $moduleName = '@app')
{
$this->insertRow('admin_auth', [
'id' => $id,
'module_name' => $moduleName,
'alias_name' => $route,
'route' => $route,
]);
return $id;
}
public void afterSetup ( ) |
public function afterSetup()
{
parent::afterSetup();
if (!$this->modelClass) {
throw new InvalidConfigException("The 'modelClass' property can not be empty.");
}
$this->modelFixture = new ActiveRecordFixture([
'modelClass' => $this->modelClass,
'fixtureData' => $this->modelFixtureData,
'schema' => $this->modelSchema,
]);
$this->mockBasicAdminModels();
if ($this->apiClass) {
$class = $this->apiClass;
$modelClass = $this->modelClass;
$this->api = new $class($modelClass::ngRestApiEndpoint(), $this->app);
}
if ($this->controllerClass) {
$class = $this->controllerClass;
$className = preg_replace('/^.*\\\/', '', $class::className());
$this->controllerId = strtolower(str_replace('Controller', '', $className));
$this->controller = new $class($this->controllerId, $this->app);
}
}
Gives the test user create api permission or removes it
protected void apiCanCreate ( $value = true ) | ||
$value |
protected function apiCanCreate($value = true)
{
$this->app->db->createCommand()->upsert('admin_group_auth', [
'id' => self::ID_GROUP_AUTH_API,
'group_id' => self::ID_GROUP_TESTER,
'auth_id' => self::ID_AUTH_API,
'crud_create' => (int)$value,
'crud_update' => 0,
'crud_delete' => 0,
], [
'crud_create' => (int)$value,
])->execute();
return $this;
}
Gives the test user delete api permission or removes it
protected void apiCanDelete ( $value = true ) | ||
$value |
protected function apiCanDelete($value = true)
{
$this->app->db->createCommand()->upsert('admin_group_auth', [
'id' => self::ID_GROUP_AUTH_API,
'group_id' => self::ID_GROUP_TESTER,
'auth_id' => self::ID_AUTH_API,
'crud_create' => 0,
'crud_update' => 0,
'crud_delete' => (int)$value,
], [
'crud_delete' => (int)$value,
])->execute();
return $this;
}
Gives the test user list api permission or removes access
protected void apiCanList ( $value = true ) | ||
$value |
protected function apiCanList($value = true)
{
if (!$value) {
return $this->removeApiPermissions();
}
try {
$this->app->db->createCommand()->insert('admin_group_auth', [
'id' => self::ID_GROUP_AUTH_API,
'group_id' => self::ID_GROUP_TESTER,
'auth_id' => self::ID_AUTH_API,
'crud_create' => 0,
'crud_update' => 0,
'crud_delete' => 0,
])->execute();
} catch (DbException $e) {
// permission is initialized, so having list access already
}
return $this;
}
Gives the test user update api permission or removes it
protected void apiCanUpdate ( $value = true ) | ||
$value |
protected function apiCanUpdate($value = true)
{
$this->app->db->createCommand()->upsert('admin_group_auth', [
'id' => self::ID_GROUP_AUTH_API,
'group_id' => self::ID_GROUP_TESTER,
'auth_id' => self::ID_AUTH_API,
'crud_create' => 0,
'crud_update' => (int)$value,
'crud_delete' => 0,
], [
'crud_update' => (int)$value,
])->execute();
return $this;
}
Defined in: luya\testsuite\cases\BaseTestSuite::assertContainsNoSpace()
Assert Contains without spaces but with newlines.
public boolean assertContainsNoSpace ( $needle, $haystack ) | ||
$needle | string | |
$haystack | string |
public function assertContainsNoSpace($needle, $haystack)
{
return $this->assertStringContainsString($this->trimSpaces($needle), $this->trimSpaces($haystack));
}
Defined in: luya\testsuite\cases\BaseTestSuite::assertContainsTrimmed()
Same as assertContains but trims the needle and haystack content in order to compare.
This will also remove newlines.
public boolean assertContainsTrimmed ( $needle, $haystack ) | ||
$needle | string | |
$haystack | string |
public function assertContainsTrimmed($needle, $haystack)
{
return self::assertStringContainsString($this->trimContent($needle), $this->trimContent($haystack));
}
Defined in: luya\testsuite\cases\BaseTestSuite::assertSameNoSpace()
This assert Same option allows you to compare two strings but removing spaces and tabes, so its more easy to work with readable contents but better comparing.
This wont remove new lines.
public boolean assertSameNoSpace ( $needle, $haystack ) | ||
$needle | string | |
$haystack | string |
public function assertSameNoSpace($needle, $haystack)
{
return $this->assertSame($this->trimSpaces($needle), $this->trimSpaces($haystack));
}
Defined in: luya\testsuite\cases\BaseTestSuite::assertSameTrimmed()
Assert Same but trim content (remove, double spaces, tabs and newlines.
public boolean assertSameTrimmed ( $needle, $haystack ) | ||
$needle | string | |
$haystack | string |
public function assertSameTrimmed($needle, $haystack)
{
return $this->assertSame($this->trimContent($needle), $this->trimContent($haystack));
}
Defined in: luya\testsuite\traits\AdminDatabaseTableTrait::assignGroupAuth()
Assigne a group to an auth entry.
public integer assignGroupAuth ( $groupId, $authId, $canCreate = true, $canUpdate = true, $canDelete = true ) | ||
$groupId | integer | |
$authId | integer | |
$canCreate | boolean | |
$canUpdate | boolean | |
$canDelete | boolean | |
return | integer |
The number of affected rows. |
---|
public function assignGroupAuth($groupId, $authId, $canCreate = true, $canUpdate = true, $canDelete = true)
{
return $this->insertRow('admin_group_auth', [
'group_id' => $groupId,
'auth_id' => $authId,
'crud_create' => (int) $canCreate,
'crud_update' => (int) $canUpdate,
'crud_delete' => (int) $canDelete,
]);
}
Defined in: luya\testsuite\cases\WebApplicationTestCase::beforeSetup()
Method which is executed before the setUp() method in order to inject data on before Setup.
Make sure to call the parent beforeSetup() method when overwriting this method.
public void beforeSetup ( ) |
public function beforeSetup()
{
parent::beforeSetup();
$_SERVER['SCRIPT_FILENAME'] = 'index.php';
$_SERVER['SCRIPT_NAME'] = '/index.php';
$_SERVER['REQUEST_URI'] = '/';
}
This method is triggered before the application test case tearDown() method is running.
public void beforeTearDown ( ) |
public function beforeTearDown()
{
parent::beforeTearDown();
$this->modelFixture->cleanup();
$this->userFixture->cleanup();
$this->userGroupFixture->cleanup();
$this->userOnlineFixture->cleanup();
$this->ngrestLogFixture->cleanup();
$this->langFixture->cleanup();
$this->dropAdminAuthTable();
$this->dropAdminGroupAuthTable();
$this->dropAdminUserGroupTable();
$this->dropAdminUserAuthNotificationTable();
}
public void bootApplication ( luya\base\Boot $boot ) | ||
$boot | luya\base\Boot |
public function bootApplication(Boot $boot)
{
// ensure the admin module is registered, if not do so.
$config = ArrayHelper::merge([
'modules' => [
'admin' => ['class' => 'luya\admin\Module']
],
'components' => [
'session' => ['class' => 'luya\testsuite\components\DummySession'],
'cache' => ['class' => 'yii\caching\DummyCache'],
'adminuser' => ['class' => 'luya\admin\components\AdminUser', 'enableSession' => false],
'db' => ['class' => 'yii\db\Connection', 'dsn' => 'sqlite::memory:'],
]
], $boot->getConfigArray());
// set the new config.
$boot->setConfigArray($config);
// boot the application
$boot->applicationWeb();
}
Gives the test user access to the controller action
protected void controllerCanAccess ( $actionId, $value = true ) | ||
$actionId | ||
$value |
protected function controllerCanAccess($actionId, $value = true)
{
$this->app->db->createCommand()->update('admin_auth', [
'alias_name' => $actionId,
'route' => implode('/', [$this->app->id, $this->controllerId, $actionId]),
], [
'id' => self::ID_AUTH_CONTROLLER
])->execute();
if ($value) {
$this->app->db->createCommand()->insert('admin_group_auth', [
'id' => self::ID_GROUP_AUTH_CONTROLLER,
'group_id' => self::ID_GROUP_TESTER,
'auth_id' => self::ID_AUTH_CONTROLLER,
])->execute();
} else {
$this->app->db->createCommand()->delete('admin_group_auth', [
'id' => self::ID_GROUP_AUTH_CONTROLLER,
])->execute();
}
}
Defined in: luya\testsuite\traits\AdminDatabaseTableTrait::createAdminAuthTable()
Create the admin auth table.
public void createAdminAuthTable ( ) |
public function createAdminAuthTable()
{
$this->createTableIfNotExists('admin_auth', [
'id' => 'INT(11) PRIMARY KEY',
'alias_name' => 'text',
'module_name' => 'text',
'is_crud' => 'int(11)',
'route' => 'text',
'api' => 'text',
'pool' => 'text',
]);
}
Defined in: luya\testsuite\traits\AdminDatabaseTableTrait::createAdminGroupAuthTable()
Create the admin group auth table.
public void createAdminGroupAuthTable ( ) |
public function createAdminGroupAuthTable()
{
$this->createTableIfNotExists('admin_group_auth', [
'id' => 'INT(11) PRIMARY KEY',
'group_id' => 'int(11)',
'auth_id' => 'int(11)',
'crud_create' => 'int(11)',
'crud_update' => 'int(11)',
'crud_delete' => 'int(11)',
]);
}
Defined in: luya\testsuite\traits\AdminDatabaseTableTrait::createAdminGroupFixture()
Create the Group Fixture with given ID.
public luya\testsuite\fixtures\NgRestModelFixture createAdminGroupFixture ( $id ) | ||
$id | integer |
public function createAdminGroupFixture($id)
{
return new NgRestModelFixture([
'modelClass' => Group::class,
'fixtureData' => [
'tester' => [
'id' => $id,
'name' => 'Test Group',
],
],
]);
}
Defined in: luya\testsuite\traits\AdminDatabaseTableTrait::createAdminLangFixture()
Create admin language fixture
public luya\testsuite\fixtures\NgRestModelFixture createAdminLangFixture ( array $fixtureData = [] ) | ||
$fixtureData | array |
public function createAdminLangFixture(array $fixtureData = [])
{
return new NgRestModelFixture([
'modelClass' => Lang::class,
'fixtureData' => $fixtureData,
]);
}
Defined in: luya\testsuite\traits\AdminDatabaseTableTrait::createAdminNgRestLogFixture()
Create the NgRest Log Fixture.
public luya\testsuite\fixtures\ActiveRecordFixture createAdminNgRestLogFixture ( array $fixtureData = [] ) | ||
$fixtureData |
public function createAdminNgRestLogFixture(array $fixtureData = [])
{
return new ActiveRecordFixture([
'modelClass' => NgrestLog::class,
'fixtureData' => $fixtureData,
]);
}
Defined in: luya\testsuite\traits\AdminDatabaseTableTrait::createAdminQueueLogErrorFixture()
Queue Log Error
public luya\testsuite\fixtures\ActiveRecordFixture createAdminQueueLogErrorFixture ( array $fixtureData = [] ) | ||
$fixtureData | array |
public function createAdminQueueLogErrorFixture(array $fixtureData = [])
{
return new ActiveRecordFixture([
'modelClass' => QueueLogError::class,
'fixtureData' => $fixtureData,
]);
}
Defined in: luya\testsuite\traits\AdminDatabaseTableTrait::createAdminQueueLogFixture()
Queue Log
public luya\testsuite\fixtures\ActiveRecordFixture createAdminQueueLogFixture ( array $fixtureData = [] ) | ||
$fixtureData | array |
public function createAdminQueueLogFixture(array $fixtureData = [])
{
return new ActiveRecordFixture([
'modelClass' => QueueLog::class,
'fixtureData' => $fixtureData,
]);
}
Defined in: luya\testsuite\traits\AdminDatabaseTableTrait::createAdminQueueTable()
Create the Admin Queue Table
public void createAdminQueueTable ( ) |
public function createAdminQueueTable()
{
return $this->createTableIfNotExists('admin_queue', [
'id' => 'INT(11) PRIMARY KEY',
'channel' => 'text',
'job' => 'blog',
'pushed_at' => 'int(11)',
'ttr' => 'int(11)',
'delay' => 'int(11)',
'priority' => 'int(11)',
'reserved_at' => 'int(11)',
'attempt' => 'int(11)',
'done_at' => 'int(11)',
]);
}
Defined in: luya\testsuite\traits\AdminDatabaseTableTrait::createAdminStorageFileFixture()
Storage File
public luya\testsuite\fixtures\ActiveRecordFixture createAdminStorageFileFixture ( array $fixtureData = [] ) | ||
$fixtureData | array |
public function createAdminStorageFileFixture(array $fixtureData = [])
{
return new ActiveRecordFixture([
'modelClass' => StorageFile::class,
'fixtureData' => $fixtureData,
]);
}
Defined in: luya\testsuite\traits\AdminDatabaseTableTrait::createAdminStorageFilterChainFixture()
Storage Filter Chain
public luya\testsuite\fixtures\ActiveRecordFixture createAdminStorageFilterChainFixture ( array $fixtureData = [] ) | ||
$fixtureData | array |
public function createAdminStorageFilterChainFixture(array $fixtureData = [])
{
return new ActiveRecordFixture([
'modelClass' => StorageFilterChain::class,
'fixtureData' => $fixtureData,
]);
}
Defined in: luya\testsuite\traits\AdminDatabaseTableTrait::createAdminStorageFilterFixture()
Storage Filter
public luya\testsuite\fixtures\ActiveRecordFixture createAdminStorageFilterFixture ( array $fixtureData = [] ) | ||
$fixtureData | array |
public function createAdminStorageFilterFixture(array $fixtureData = [])
{
return new ActiveRecordFixture([
'modelClass' => StorageFilter::class,
'fixtureData' => $fixtureData,
]);
}
Defined in: luya\testsuite\traits\AdminDatabaseTableTrait::createAdminStorageImageFixture()
Storage Image
public luya\testsuite\fixtures\ActiveRecordFixture createAdminStorageImageFixture ( array $fixtureData = [] ) | ||
$fixtureData | array |
public function createAdminStorageImageFixture(array $fixtureData = [])
{
return new ActiveRecordFixture([
'modelClass' => StorageImage::class,
'fixtureData' => $fixtureData,
]);
}
Defined in: luya\testsuite\traits\AdminDatabaseTableTrait::createAdminTagFixture()
Create admin tag fixture
public luya\testsuite\fixtures\NgRestModelFixture createAdminTagFixture ( array $fixtureData = [] ) | ||
$fixtureData | array |
public function createAdminTagFixture(array $fixtureData = [])
{
return new NgRestModelFixture([
'modelClass' => Tag::class,
'fixtureData' => $fixtureData,
]);
}
Defined in: luya\testsuite\traits\AdminDatabaseTableTrait::createAdminTagRelationFixture()
Create admin tag relation fixture
public luya\testsuite\fixtures\ActiveRecordFixture createAdminTagRelationFixture ( array $fixtureData = [] ) | ||
$fixtureData | array |
public function createAdminTagRelationFixture(array $fixtureData = [])
{
return new ActiveRecordFixture([
'modelClass' => TagRelation::class,
'fixtureData' => $fixtureData,
]);
}
Defined in: luya\testsuite\traits\AdminDatabaseTableTrait::createAdminUserAuthNotificationTable()
Create the admin user auth notification table.
public void createAdminUserAuthNotificationTable ( ) |
public function createAdminUserAuthNotificationTable()
{
$this->createTableIfNotExists('admin_user_auth_notification', [
'id' => 'INT(11) PRIMARY KEY',
'user_id' => 'int(11)',
'auth_id' => 'int(11)',
'is_muted' => 'int(11)',
'model_latest_pk_value' => 'text',
'model_class' => 'text',
'created_at' => 'int(11)',
'updated_at' => 'int(11)',
]);
}
Defined in: luya\testsuite\traits\AdminDatabaseTableTrait::createAdminUserDeviceFixture()
User Device
public luya\testsuite\fixtures\ActiveRecordFixture createAdminUserDeviceFixture ( array $fixtureData = [] ) | ||
$fixtureData | array |
public function createAdminUserDeviceFixture(array $fixtureData = [])
{
return new ActiveRecordFixture([
'modelClass' => UserDevice::class,
'fixtureData' => $fixtureData,
]);
}
Defined in: luya\testsuite\traits\AdminDatabaseTableTrait::createAdminUserFixture()
Create the User Fixture with given fixture Data.
public luya\testsuite\fixtures\NgRestModelFixture createAdminUserFixture ( array $fixtureData = [], $defaultSchema = true ) | ||
$fixtureData | array | |
$defaultSchema |
public function createAdminUserFixture(array $fixtureData = [], $defaultSchema = true)
{
return new NgRestModelFixture([
'modelClass' => User::class,
'schema' => $defaultSchema ? [
'title' => 'int(11)',
'firstname' => 'text',
'lastname' => 'text',
'email' => 'text',
'is_deleted' => 'int(11)',
'is_api_user' => 'boolean',
'api_last_activity' => 'int(11)',
'auth_token' => 'text',
'is_request_logger_enabled' => 'boolean',
'email_verification_token_timestamp' => 'int(11)',
'login_attempt_lock_expiration' => 'int(11)',
'login_attempt' => 'int(11)',
'email_verification_token' => 'text',
'api_allowed_ips' => 'text',
'api_rate_limit' => 'int(11)',
'cookie_token' => 'text',
'settings' => 'text',
'force_reload' => 'int(11)',
'secure_token_timestamp' => 'int(11)',
'secure_token' => 'text',
'password' => 'text',
'password_salt' => 'text',
'login_2fa_enabled' => 'int(11)',
'login_2fa_secret' => 'text',
'login_2fa_backup_key' => 'text',
'password_verification_token' => 'text',
'password_verification_token_timestamp' => 'int(11)',
] : null,
'fixtureData' => $fixtureData,
]);
}
Defined in: luya\testsuite\traits\AdminDatabaseTableTrait::createAdminUserGroupTable()
Create the admin user group table.
public void createAdminUserGroupTable ( ) |
public function createAdminUserGroupTable()
{
$this->createTableIfNotExists('admin_user_group', [
'id' => 'INT(11) PRIMARY KEY',
'user_id' => 'int(11)',
'group_id' => 'int(11)',
]);
}
Defined in: luya\testsuite\traits\AdminDatabaseTableTrait::createAdminUserLoginFixture()
Admin User Fixture
public luya\testsuite\fixtures\NgRestModelFixture createAdminUserLoginFixture ( array $fixtureData = [] ) | ||
$fixtureData | array |
public function createAdminUserLoginFixture(array $fixtureData = [])
{
return new NgRestModelFixture([
'modelClass' => UserLogin::class,
'fixtureData' => $fixtureData,
]);
}
Defined in: luya\testsuite\traits\AdminDatabaseTableTrait::createAdminUserLoginLockoutFixture()
User Login Lockout
public luya\testsuite\fixtures\ActiveRecordFixture createAdminUserLoginLockoutFixture ( array $fixtureData = [] ) | ||
$fixtureData | array |
public function createAdminUserLoginLockoutFixture(array $fixtureData = [])
{
return new ActiveRecordFixture([
'modelClass' => UserLoginLockout::class,
'fixtureData' => $fixtureData,
]);
}
Defined in: luya\testsuite\traits\AdminDatabaseTableTrait::createAdminUserOnlineFixture()
Create the User Online Fixture.
public luya\testsuite\fixtures\ActiveRecordFixture createAdminUserOnlineFixture ( array $fixtureData = [] ) | ||
$fixtureData |
public function createAdminUserOnlineFixture(array $fixtureData = [])
{
return new ActiveRecordFixture([
'modelClass' => UserOnline::class,
'fixtureData' => $fixtureData,
]);
}
Defined in: luya\testsuite\traits\AdminDatabaseTableTrait::createGroupFixture()
Create the Group Fixture with given ID.
public luya\testsuite\fixtures\NgRestModelFixture createGroupFixture ( $id ) | ||
$id | integer |
public function createGroupFixture($id)
{
trigger_error('use createAdminGroupFixture() instead', E_USER_DEPRECATED);
return $this->createAdminGroupFixture($id);
}
Defined in: luya\testsuite\traits\AdminDatabaseTableTrait::createNgRestLogFixture()
Create the NgRest Log Fixture.
public luya\testsuite\fixtures\ActiveRecordFixture createNgRestLogFixture ( array $fixtureData = [] ) | ||
$fixtureData |
public function createNgRestLogFixture(array $fixtureData = [])
{
trigger_error('use createAdminNgRestLogFixture() instead', E_USER_DEPRECATED);
return $this->createAdminNgRestLogFixture($fixtureData);
}
Defined in: luya\testsuite\traits\AdminDatabaseTableTrait::createUserFixture()
Create the User Fixture with given fixture Data.
public luya\testsuite\fixtures\NgRestModelFixture createUserFixture ( array $fixtureData = [] ) | ||
$fixtureData | array |
public function createUserFixture(array $fixtureData = [])
{
trigger_error('use createAdminUserFixture() instead', E_USER_DEPRECATED);
return $this->createAdminUserFixture($fixtureData);
}
Defined in: luya\testsuite\traits\AdminDatabaseTableTrait::createUserOnlineFixture()
Create the User Online Fixture.
public luya\testsuite\fixtures\ActiveRecordFixture createUserOnlineFixture ( array $fixtureData = [] ) | ||
$fixtureData |
public function createUserOnlineFixture(array $fixtureData = [])
{
trigger_error('use createAdminUserOnlineFixture() instead', E_USER_DEPRECATED);
return $this->createAdminUserOnlineFixture($fixtureData);
}
Defined in: luya\testsuite\traits\AdminDatabaseTableTrait::dropAdminAuthTable()
Drop the admin auth table.
public void dropAdminAuthTable ( ) |
public function dropAdminAuthTable()
{
$this->dropTableIfExists('admin_auth');
}
Defined in: luya\testsuite\traits\AdminDatabaseTableTrait::dropAdminGroupAuthTable()
Drop the admin group auth table.
public void dropAdminGroupAuthTable ( ) |
public function dropAdminGroupAuthTable()
{
$this->dropTableIfExists('admin_group_auth');
}
Defined in: luya\testsuite\traits\AdminDatabaseTableTrait::dropAdminUserAuthNotificationTable()
Drop the admin user auth notification table.
public void dropAdminUserAuthNotificationTable ( ) |
public function dropAdminUserAuthNotificationTable()
{
$this->dropTableIfExists('admin_user_auth_notification');
}
Defined in: luya\testsuite\traits\AdminDatabaseTableTrait::dropAdminUserGroupTable()
Drop the admin user group table.
public void dropAdminUserGroupTable ( ) |
public function dropAdminUserGroupTable()
{
$this->dropTableIfExists('admin_user_group');
}
Defined in: luya\testsuite\cases\BaseTestSuite::fixture()
Get Fixture Object
public luya\testsuite\fixtures\ActiveRecordFixture fixture ( $fixtureClass ) | ||
$fixtureClass | string |
public function fixture($fixtureClass)
{
if (is_array($this->_fixtures)) {
return array_key_exists($fixtureClass, $this->_fixtures) ? $this->_fixtures[$fixtureClass] : false;
}
return false;
}
Defined in: luya\testsuite\cases\BaseTestSuite::fixtures()
Defines a list of fixtures classes which can be loaded.
Example fixtures list:
public function fixtures()
{
return [
'app\fixtures\MyTestFixture',
MySuperFixture::class,
];
}
public array fixtures ( ) |
public function fixtures()
{
return [];
}
Defined in: luya\testsuite\cases\BaseTestSuite::getConfigArray()
Provide Configurtion Array.
public abstract void getConfigArray ( ) |
abstract public function getConfigArray();
Defined in: luya\testsuite\cases\BaseTestSuite::invokeMethod()
Call a private or protected method from an object and return the value.
public function testProtectedMethod()
{
// assuming MyObject has a protected method like:
// protected function hello($title)
// {
// return $title;
// }
$object = new MyObject();
$this->assertSame('Hello World', $this->invokeMethod($object, 'hello', ['Hello World']));
}
public mixed invokeMethod ( &$object, $methodName, array $parameters = [] ) | ||
$object | object |
The object the method exists from. |
$methodName | string |
The name of the method which should be called. |
$parameters | array |
An array of paremters which should be passed to the method. |
public function invokeMethod(&$object, $methodName, array $parameters = [])
{
$reflection = new \ReflectionClass(get_class($object));
$method = $reflection->getMethod($methodName);
$method->setAccessible(true);
return $method->invokeArgs($object, $parameters);
}
Basic admin module env model mocking.
protected void mockBasicAdminModels ( ) |
protected function mockBasicAdminModels()
{
$this->createAdminUserGroupTable();
$this->createAdminGroupAuthTable();
$this->createAdminAuthTable();
$this->createAdminUserAuthNotificationTable();
$this->createAdminUserLoginLockoutFixture();
$this->langFixture = $this->createAdminLangFixture([
1 => [
'id' => 1,
'short_code' => 'en',
'name' => 'English',
'is_default' => 1,
'is_deleted' => 0,
]
]);
// user
$this->userFixture = $this->createAdminUserFixture([
'user1' => [
'id' => self::ID_USER_TESTER,
'firstname' => 'John',
'lastname' => 'Doe',
'email' => '[email protected]',
'is_deleted' => 0,
'is_api_user' => true,
'api_last_activity' => time(),
'auth_token' => 'TestAuthToken',
'is_request_logger_enabled' => false,
]
]);
// user group
$this->userGroupFixture = $this->createAdminGroupFixture(self::ID_GROUP_TESTER);
// login the user
$this->app->adminuser->login($this->userFixture->getModel('user1'));
// user online table
$this->userOnlineFixture = $this->createAdminUserOnlineFixture();
// ngrest logger
$this->ngrestLogFixture = $this->createAdminNgRestLogFixture();
$this->insertRow('admin_user_group', [
'user_id' => self::ID_USER_TESTER,
'group_id' => self::ID_GROUP_TESTER,
]);
$apiEndpoint = $this->modelClass::ngRestApiEndpoint();
list(, , $alias) = explode('-', $apiEndpoint);
$this->insertRow('admin_auth', [
'id' => self::ID_AUTH_API,
'alias_name' => $alias,
'module_name' => $this->app->id,
'is_crud' => 1,
'api' => $apiEndpoint,
]);
$this->insertRow('admin_auth', [
'id' => self::ID_AUTH_CONTROLLER,
'module_name' => $this->app->id,
'is_crud' => 0,
]);
}
Disables api access for test user
protected void removeApiPermissions ( ) |
protected function removeApiPermissions()
{
$this->app->db->createCommand()->delete('admin_group_auth', ['id' => self::ID_GROUP_AUTH_API])->execute();
}
Defined in: luya\testsuite\traits\AdminDatabaseTableTrait::removePermissionApi()
Remove permission api entry.
public integer removePermissionApi ( $api ) | ||
$api | string | |
return | integer |
The number of affected rows. |
---|
public function removePermissionApi($api)
{
return $this->deleteRow('admin_auth', [
'api' => $api,
]);
}
Defined in: luya\testsuite\traits\AdminDatabaseTableTrait::removePermissionRoute()
Remove permission route
public integer removePermissionRoute ( $route ) | ||
$route | string | |
return | integer |
The number of affected rows. |
---|
public function removePermissionRoute($route)
{
return $this->deleteRow('admin_auth', [
'route' => $route,
]);
}
Helps to initialize api access permissions
protected void resetApiPermissions ( $create = false, $update = false, $delete = false ) | ||
$create | ||
$update | ||
$delete |
protected function resetApiPermissions($create = false, $update = false, $delete = false)
{
$state = [
'crud_create' => (int)$create,
'crud_update' => (int)$update,
'crud_delete' => (int)$delete,
];
$this->app->db->createCommand()->upsert(
'admin_group_auth',
ArrayHelper::merge([
'id' => self::ID_GROUP_AUTH_API,
'group_id' => self::ID_GROUP_TESTER,
'auth_id' => self::ID_AUTH_API,
], $state),
$state
)->execute();
return $this;
}
Run a certain action insdie a controller, whether its an api or "controller" context.
$this->runControllerAction($this->api, 'index');
Would run the index action of the API Controller.
protected mixed runControllerAction ( yii\base\Controller $controller, $action, array $params = [], $permission = true ) | ||
$controller | yii\base\Controller | |
$action | string | |
$params | array | |
$permission | boolean |
protected function runControllerAction(Controller $controller, $action, array $params = [], $permission = true)
{
$this->app->controller = $controller;
if ($permission) {
$this->setQueryAuthToken();
}
return $controller->runAction($action, $params);
}
Set the query parameter as auth token.
protected void setQueryAuthToken ( $value = true, $token = null ) | ||
$value | boolean | |
$token | string |
The token to set as query param, if not defined the token from user model user1 will be taken. |
protected function setQueryAuthToken($value = true, $token = null)
{
if ($value) {
$accessToken = $token ? $token : $this->userFixture->getModel('user1')->auth_token;
$this->app->request->setQueryParams(['access-token' => $accessToken]);
} else {
$this->app->request->setQueryParams(['access-token' => null]);
}
}
Defined in: luya\testsuite\cases\BaseTestSuite::set_up()
See also \PHPUnit\Framework\TestCase::setUp().
protected void set_up ( ) |
protected function set_up() {
parent::set_up();
$this->beforeSetup();
$boot = new Boot();
$boot->setConfigArray($this->getConfigArray());
$boot->mockOnly = true;
$boot->setBaseYiiFile('vendor/yiisoft/yii2/Yii.php');
$this->bootApplication($boot);
$this->boot = $boot;
$this->app = $boot->app;
$this->afterSetup();
$this->setupFixtures();
}
Defined in: luya\testsuite\cases\BaseTestSuite::setupFixtures()
Create all fixtures from fixtures() list.
public void setupFixtures ( ) |
public function setupFixtures()
{
if ($this->_fixtures === null) {
$loadedFixtures = [];
foreach ($this->fixtures() as $fixtureClass) {
$loadedFixtures[$fixtureClass] = Yii::createObject($fixtureClass);
}
$this->_fixtures = $loadedFixtures;
}
}
Defined in: luya\testsuite\cases\BaseTestSuite::tearDownFixtures()
Run cleanup() on all loaded fixtures.
public void tearDownFixtures ( ) |
public function tearDownFixtures()
{
if (is_array($this->_fixtures)) {
/** @var ActiveRecordFixture $object */
foreach ($this->_fixtures as $object) {
$object->cleanup();
}
}
}
Defined in: luya\testsuite\cases\BaseTestSuite::tear_down()
See also \PHPUnit\Framework\TestCase::tearDown().
protected void tear_down ( ) |
protected function tear_down() {
// Any clean up needed related to `set_up()`.
parent::tear_down();
$this->beforeTearDown();
$this->tearDownFixtures();
unset($this->app, $this->boot);
}
Basic Tests
public void testBasicNgRestMethods ( ) |
public function testBasicNgRestMethods()
{
$class = $this->modelClass;
$this->assertNotNull($class::ngRestApiEndpoint());
$this->assertNotNull($class::tableName());
$this->assertTrue(is_array($this->modelFixture->newModel->ngRestAttributeTypes()));
$this->assertNotNull($this->modelFixture->newModel->attributeLabels());
$this->assertTrue(is_array($this->modelFixture->newModel->attributeHints()));
$this->assertTrue(is_array($this->modelFixture->newModel->ngRestScopes()));
$this->assertTrue(is_array($this->modelFixture->newModel->ngRestActiveButtons()));
$this->assertTrue(is_array($this->modelFixture->newModel->ngRestActiveWindows()));
$this->assertTrue(is_array($this->modelFixture->newModel->ngRestRelations()));
if ($this->api) {
$this->assertInstanceOf('luya\admin\ngrest\base\NgRestModel', $this->api->model);
$this->expectException('yii\web\ForbiddenHttpException');
$this->runControllerAction($this->api, 'unlock');
//$this->assertNull($this->api->actionUnlock());
$this->expectException('yii\web\ForbiddenHttpException');
$this->runControllerAction($this->api, 'services');
//$this->api->actionServices();
$this->expectException('yii\web\ForbiddenHttpException');
$this->runControllerAction($this->api, 'search', ['foo']);
//$this->api->actionSearch('foo');
$this->expectException('yii\web\ForbiddenHttpException');
$this->runControllerAction($this->api, 'relation-call', [1, 'foo', 'none']);
//$this->api->actionRelationCall(1, 'foo', 'none');
$this->expectException('yii\web\ForbiddenHttpException');
$this->runControllerAction($this->api, 'filter', ['none']);
//$this->api->actionFilter('none');
$this->expectException('yii\web\ForbiddenHttpException');
$this->runControllerAction($this->api, 'active-window-callback');
//$this->api->actionActiveWindowCallback();
$this->expectException('yii\web\ForbiddenHttpException');
$this->runControllerAction($this->api, 'active-window-render');
//$this->api->actionActiveWindowRender();
$this->expectException('yii\web\ForbiddenHttpException');
$this->runControllerAction($this->api, 'export');
//$this->api->actionExport();
}
if ($this->controller) {
$this->assertInstanceOf('luya\admin\ngrest\base\NgRestModel', $this->controller->getModel());
$this->assertContains('<script>zaa.bootstrap.register', $this->controller->actionIndex());
}
}
Defined in: luya\testsuite\cases\BaseTestSuite::trimContent()
No Spaces and No Newline Trims the given text. Remove whitespaces, tabs and other chars in order to compare readable formated texts.
protected string trimContent ( $text ) | ||
$text | string | |
return | string |
The trimmed text. |
---|
protected function trimContent($text)
{
return str_replace(['> ', ' <'], ['>', '<'], trim(preg_replace('/\s+/', ' ', $text)));
}
Defined in: luya\testsuite\cases\BaseTestSuite::trimSpaces()
No Spaces with Newline
Removes tabs and spaces from a string. But keeps newlines.
protected string trimSpaces ( $text ) | ||
$text | string |
protected function trimSpaces($text)
{
$lines = null;
foreach (preg_split("/((\r?\n)|(\r\n?))/", $text) as $line) {
if (!empty($line)) {
$lines .= $this->trimContent($line) . PHP_EOL;
}
}
return $lines;
}
Defined in: luya\testsuite\traits\AdminDatabaseTableTrait::unAssignGroupAuth()
Unassigne a group from auth
public integer unAssignGroupAuth ( $groupId, $authId ) | ||
$groupId | integer | |
$authId | integer | |
return | integer |
The number of affected rows. |
---|
public function unAssignGroupAuth($groupId, $authId)
{
return $this->deleteRow('admin_group_auth', [
'group_id' => $groupId,
'auth_id' => $authId,
]);
}