public function testGetCreatedModels()
 {
     ReadPermissionsSubscriptionUtil::recreateTable('account_read_subscription');
     ModelCreationApiSyncUtil::buildTable();
     $messageLogger = new DebuggingMessageLogger();
     $timestamp = time();
     sleep(1);
     $models = ModelStateChangesSubscriptionUtil::getCreatedModels('apiTest', 'Account', 5, 0, $timestamp);
     $this->assertEquals(false, $models);
     // Now create new account model
     AccountTestHelper::createAccountByNameForOwner('First Account', Yii::app()->user->userModel);
     $models = ModelStateChangesSubscriptionUtil::getCreatedModels('apiTest', 'Account', 5, 0, $timestamp);
     $this->assertEquals(false, $models);
     // Now run ReadPermissionSubscriptionUtil
     $this->assertEquals(ReadPermissionsSubscriptionUtil::STATUS_STARTED, ReadPermissionsSubscriptionUtil::getReadPermissionUpdateStatus());
     ReadPermissionsSubscriptionUtil::updateAllReadSubscriptionTables($messageLogger);
     $this->assertEquals(ReadPermissionsSubscriptionUtil::STATUS_COMPLETED, ReadPermissionsSubscriptionUtil::getReadPermissionUpdateStatus());
     $models = ModelStateChangesSubscriptionUtil::getCreatedModels('apiTest', 'Account', 5, 0, $timestamp);
     $this->assertTrue(is_array($models));
     $this->assertEquals(1, count($models));
     $this->assertEquals('First Account', $models[0]->name);
     sleep(1);
     $timestamp = time();
     sleep(1);
     $models = ModelStateChangesSubscriptionUtil::getCreatedModels('apiTest', 'Account', 5, 0, $timestamp);
     $this->assertEquals(false, $models);
     AccountTestHelper::createAccountByNameForOwner('First Test Account', Yii::app()->user->userModel);
     AccountTestHelper::createAccountByNameForOwner('Second Test Account', Yii::app()->user->userModel);
     AccountTestHelper::createAccountByNameForOwner('Third Test Account', Yii::app()->user->userModel);
     $this->assertEquals(ReadPermissionsSubscriptionUtil::STATUS_COMPLETED, ReadPermissionsSubscriptionUtil::getReadPermissionUpdateStatus());
     ReadPermissionsSubscriptionUtil::updateAllReadSubscriptionTables($messageLogger);
     $this->assertEquals(ReadPermissionsSubscriptionUtil::STATUS_COMPLETED, ReadPermissionsSubscriptionUtil::getReadPermissionUpdateStatus());
     $models = ModelStateChangesSubscriptionUtil::getCreatedModels('apiTest', 'Account', 2, 0, $timestamp);
     $this->assertTrue(is_array($models));
     $this->assertEquals(2, count($models));
     $this->assertEquals('First Test Account', $models[0]->name);
     $this->assertEquals('Second Test Account', $models[1]->name);
     $models = ModelStateChangesSubscriptionUtil::getCreatedModels('apiTest', 'Account', 2, 2, $timestamp);
     $this->assertTrue(is_array($models));
     $this->assertEquals(1, count($models));
     $this->assertEquals('Third Test Account', $models[0]->name);
 }
 /**
  * Get array of newly created items since beginning or since datetime in past
  * @param $params
  * @return ApiResult
  * @throws ApiException
  */
 protected function processGetCreatedItems($params)
 {
     $modelClassName = $this->getModelName();
     $stateMetadataAdapterClassName = $this->resolveStateMetadataAdapterClassName();
     if (!isset($params['sinceTimestamp'])) {
         $sinceTimestamp = 0;
     } else {
         $sinceTimestamp = (int) $params['sinceTimestamp'];
     }
     $pageSize = Yii::app()->pagination->getGlobalValueByType('apiListPageSize');
     if (isset($params['pagination']['pageSize'])) {
         $pageSize = (int) $params['pagination']['pageSize'];
     }
     // Get offset. Please note that API client provide page number, and we need to convert it into offset,
     // which is parameter of RedBeanModel::getSubset function
     if (isset($params['pagination']['page']) && (int) $params['pagination']['page'] > 0) {
         $currentPage = (int) $params['pagination']['page'];
     } else {
         $currentPage = 1;
     }
     $offset = $this->getOffsetFromCurrentPageAndPageSize($currentPage, $pageSize);
     $models = ModelStateChangesSubscriptionUtil::getCreatedModels('API', $modelClassName, $pageSize, $offset, $sinceTimestamp, $stateMetadataAdapterClassName, Yii::app()->user->userModel);
     $totalItems = ModelStateChangesSubscriptionUtil::getCreatedModelsCount('API', $modelClassName, $sinceTimestamp, $stateMetadataAdapterClassName, Yii::app()->user->userModel);
     $data = array('totalCount' => $totalItems, 'pageSize' => $pageSize, 'currentPage' => $currentPage);
     if (is_array($models) && !empty($models)) {
         foreach ($models as $model) {
             $data['items'][] = static::getModelToApiDataUtilData($model);
         }
         $result = new ApiResult(ApiResponse::STATUS_SUCCESS, $data, null, null);
     } else {
         $result = new ApiResult(ApiResponse::STATUS_SUCCESS, $data, null, null);
     }
     return $result;
 }