Beispiel #1
0
 /**
  * Method to test the update method.
  * @since 1.0.0
  * @test
  */
 public function testUpdate()
 {
     //the Account Entity which will be updated on database.
     $account = new Account();
     $account->id = 2;
     $account->name = 'BATTLELOG_USERNAME';
     $account->value = 'ExampleUser';
     //the AccountMapper to update the Account Entity on database.
     $accountMapper = new AccountMapper($this->getConnection()->getConnection());
     $accountMapper->update($account);
     //get the actual and expected table.
     $actualTable = $this->getConnection()->createQueryTable('account', 'SELECT * FROM account');
     $expectedDataset = __DIR__ . '/DataSets/Account/account-update.xml';
     $expectedTable = $this->createXMLDataSet($expectedDataset)->getTable('account');
     //check whether the tables are equal.
     $this->assertTablesEqual($expectedTable, $actualTable);
     //another Entity than Account Entity is not valid on the AccountMapper.
     $this->assertFalse($accountMapper->update(new Team()));
 }
Beispiel #2
0
 /**
  * Method to save the Account.
  * @return boolean The state whether the Account could be saved.
  * @since 1.0.0
  */
 public function accountSave()
 {
     //this method need a Session.
     $this->needSession();
     //get the ID of the Account.
     $account_id = filter_input(INPUT_POST, 'account_id', FILTER_VALIDATE_INT, FILTER_NULL_ON_FAILURE);
     //get the ID of the User.
     $user_id = filter_input(INPUT_POST, 'user_id', FILTER_VALIDATE_INT, FILTER_NULL_ON_FAILURE);
     //check whether a Account should be updated.
     if ($account_id > 0) {
         //create the AccountRepository to load the Account from database.
         $accountRepository = AccountRepository::build();
         $accounts = $accountRepository->findByID($account_id);
         //check whether the Account could be loaded with the AccountRepository.
         if (count($accounts) !== 1) {
             $jsonOutput = [];
             $jsonOutput['state'] = LogLevel::ERROR;
             $jsonOutput['message'] = 'The Account could not be found.';
             $jsonOutput['redirect'] = URL . 'user/edit/' . $user_id;
             $jsonOutput['tab_selected'] = 'tab-accounts';
             echo json_encode($jsonOutput);
             return false;
         }
         //create a new Account and load the Account from database.
         $account = new Account();
         $account->loadFromObject($accounts[0]);
         $account->loadFromPOST('account_');
         //create a AccountMapper to save the Account on database.
         $accountMapper = AccountMapper::build();
         //check whether the Account could be saved.
         if ($accountMapper->save($account)) {
             $jsonOutput = [];
             $jsonOutput['state'] = LogLevel::INFO;
             $jsonOutput['message'] = 'The Account was successfully saved.';
             $jsonOutput['redirect'] = URL . 'user/edit/' . $user_id;
             $jsonOutput['tab_selected'] = 'tab-accounts';
             echo json_encode($jsonOutput);
             return true;
         } else {
             $jsonOutput = [];
             $jsonOutput['state'] = LogLevel::ERROR;
             $jsonOutput['message'] = 'The Account could not be saved.';
             $jsonOutput['redirect'] = URL . 'user/edit/' . $user_id;
             $jsonOutput['tab_selected'] = 'tab-accounts';
             echo json_encode($jsonOutput);
             return false;
         }
     } else {
         //create the UserRepository to load the User from database.
         $userRepository = UserRepository::build();
         $users = $userRepository->findByID($user_id);
         //check whether the User could be loaded with the UserRepository.
         if (count($users) !== 1) {
             $jsonOutput = [];
             $jsonOutput['state'] = LogLevel::ERROR;
             $jsonOutput['message'] = 'The User of the Account could not be found.';
             $jsonOutput['redirect'] = URL . 'user/edit/' . $user_id;
             $jsonOutput['tab_selected'] = 'tab-accounts';
             echo json_encode($jsonOutput);
             return false;
         }
         //create a new Account and load the Account.
         $account = new Account();
         $account->loadFromPOST('account_');
         //create a AccountDataMapper to save the Account on database.
         $accountMapper = AccountMapper::build();
         //check whether the new Account could be saved.
         if ($accountMapper->save($account) === false) {
             $jsonOutput = [];
             $jsonOutput['state'] = LogLevel::ERROR;
             $jsonOutput['message'] = 'The Account could not be saved!';
             $jsonOutput['redirect'] = URL . 'user/edit/' . $user_id;
             $jsonOutput['tab_selected'] = 'tab-accounts';
             echo json_encode($jsonOutput);
             return false;
         }
         //get the ID of the new Account on database.
         $account->id = Database::getInstance()->getConnection()->lastInsertId();
         //create a new Account User TableMapper.
         $accountUserTableMapper = AccountUserTableMapper::build();
         //check whether the association between Account and User could be created.
         if ($accountUserTableMapper->create($account, $users[0])) {
             $jsonOutput = [];
             $jsonOutput['state'] = LogLevel::INFO;
             $jsonOutput['message'] = 'The Account was successfully created.';
             $jsonOutput['redirect'] = URL . 'user/edit/' . $user_id;
             $jsonOutput['tab_selected'] = 'tab-accounts';
             echo json_encode($jsonOutput);
             return true;
         } else {
             $jsonOutput = [];
             $jsonOutput['state'] = LogLevel::ERROR;
             $jsonOutput['message'] = 'The Account could not be saved!';
             $jsonOutput['redirect'] = URL . 'user/edit/' . $user_id;
             $jsonOutput['tab_selected'] = 'tab-accounts';
             echo json_encode($jsonOutput);
             return false;
         }
     }
 }
Beispiel #3
0
 /**
  * Method to build a new object of AccountRepository.
  * @return AccountRepository The created object of AccountRepository.
  * @since 1.0.0
  * @uses AccountMapper::build() to get the Account Entities from database.
  */
 public static function build()
 {
     return new self(AccountMapper::build());
 }