/** * Method to test the loadFromObject method. * @since 1.0.0 * @test */ public function testLoadFromObject() { //create an Account Entity. $account = new Account(); //the object without prefix to load the Account Entity. $object = new \stdClass(); $object->id = 1; $object->name = 'Name'; $object->value = 'Value'; //load the object without prefix to the Account Entity. $account->loadFromObject($object); //check whether the values are valid. $this->assertEquals(1, $account->id); $this->assertEquals('Name', $account->name); $this->assertEquals('Value', $account->value); //the object with prefix to load the Account Entity. $object_prefix = new \stdClass(); $object_prefix->test_id = 2; $object_prefix->test_name = 'TestName'; $object_prefix->test_value = 'TestValue'; //load the object with prefix to the Account Entity. $account->loadFromObject($object_prefix, 'test_'); //check whether the values are valid. $this->assertEquals(2, $account->id); $this->assertEquals('TestName', $account->name); $this->assertEquals('TestValue', $account->value); }
/** * 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; } } }